//********************
// Popup Constructor *
//********************
function Popup(loc, method, params)
{
	// get id's
	this.popup_handler				= document.getElementById('cs_popup_handler');
	this.popup_handler_anchor		= this.popup_handler.getElementsByTagName('a').item(0);
	this.popup_listener				= document.getElementById('cs_popup_listener');
	this.popup_listener_wrapper		= document.getElementById('cs_popup_listener_wrapper');
	this.popup_listener_shadow		= document.getElementById('cs_popup_listener_shadow');
	this.popup_listener_content		= document.getElementById('cs_popup_listener_content');
	this.popup_filter				= document.getElementById('cs_popup_filter');
	this.popup_product_enlarge		= document.getElementById('cs_popup_product_enlarge');
	this.cs_popup_order_button		= document.getElementById('cs_popup_order_button');
	
	// set vars
	this.time_out				= 2000; // 2 seconds
	this.hide_class				= 'cs_hide';
	this.show_class				= 'cs_show';
	this.active_class			= 'cs_active';
	this.product_amount_class	= 'cs_product_amount_';
	this.checkbox_class			= 'cs_checkbox';
	
	this.http_location	= loc;
	this.http_method	= method;
	this.http_params	= params;
	
	// initialize popup
	this.init();
}

//****************************
// Function initialize popup *
//****************************
Popup.prototype.init = function()
{
	// set handler events
	this.handlerEvents();
	
	// set listener events
	this.listenerEvents();
	
	// set document events
	this.documentEvents();
}

//**************************
// Function handler events *
//**************************
Popup.prototype.handlerEvents = function()
{
	// set vars
	var _this = this;
	
	this.popup_handler.onclick = function(e)
	{
		cancelBubbling(e);
		
		// show popup
		if (_this.popup_listener_wrapper.className == _this.hide_class)
		{
			_this.show();
		}
		
		// hide popup
		else
		{
			_this.hide();
		}
	}
	
//	this.popup_handler.onmouseout = function()
//	{
//		if (_this.popup_listener_wrapper.className == _this.show_class)
//		{
//			window.popup_timer_handler = setTimeout(
//				function ()
//				{
//					_this.hide();
//				}
//				, _this.time_out
//			);
//		}
//	}
//	
//	this.popup_handler.onmouseover = function()
//	{
//		// clear listener timeout
//		if (window.popup_timer_listener)
//		{
//			clearTimeout(window.popup_timer_listener);
//		}
//		
//		if (_this.popup_listener_wrapper.className == _this.show_class)
//		{
//			clearTimeout(window.popup_timer_handler);
//		}
//	}
}

//**************************
// Function listener events *
//**************************
Popup.prototype.listenerEvents = function()
{
	// set vars
	var _this = this;
	
	this.popup_listener_content.onclick = function(e)
	{
		cancelBubbling(e);
	}
	
//	this.popup_listener_content.onmouseout = function()
//	{
//		if (_this.popup_listener_wrapper.className == _this.show_class)
//		{
//			window.popup_timer_listener = setTimeout(
//				function ()
//				{
//					_this.hide();
//				}
//				, _this.time_out
//			);
//		}
//	}
//	
//	this.popup_listener_content.onmouseover = function()
//	{
//		// clear handler timeout
//		if (window.popup_timer_handler)
//		{
//			clearTimeout(window.popup_timer_handler);
//		}
//		
//		if (_this.popup_listener_wrapper.className == _this.show_class)
//		{
//			clearTimeout(window.popup_timer_listener);
//		}
//	}
}

//***************************
// Function document events *
//***************************
Popup.prototype.documentEvents = function()
{
	// set vars
	var _this = this;
	
	document.onclick = function()
	{
		// hide popup
		if (_this.popup_listener_wrapper.className == _this.show_class)
		{
			// clear handler timeout
			if (window.popup_timer_handler)
			{
				clearTimeout(window.popup_timer_handler);
			}
			
			// clear listener timeout
			if (window.popup_timer_listener)
			{
				clearTimeout(window.popup_timer_listener);
			}
			
			_this.hide();
		}
	}
	
	window.onresize = function()
	{
		_this.position();
	}
}

//************************
// Function popup events *
//************************
Popup.prototype.productEnlargeEvents = function()
{
	// set vars
	var _this = this;
	
	this.popup_filter.onclick = function(e)
	{
		cancelBubbling(e);
	}
	
	this.popup_product_enlarge.onclick = function(e)
	{
		cancelBubbling(e);
	}
}

//***********************
// Function get content *
//***********************
Popup.prototype.getContent = function(obj, loc, method, params)
{
	// set object to var
	var _this = this;
	
	var my_conn = new XHConn();
	
	if (!my_conn) alert("XMLHTTP not available. Please try a newer/better browser.");
	
	// return result when done
	var fnWhenDone = function (oXML)
	{
		// put response in the wrapper
		obj.innerHTML = oXML.responseText;
	}
	
	// make the connection
	my_conn.connect(loc, method, params, fnWhenDone);
}

//**********************
// Function hide popup *
//**********************
Popup.prototype.hide = function()
{
	// set normal state of anchor
	this.popup_handler_anchor.className = '';
	
	// hide popup
	this.popup_listener_wrapper.className = this.hide_class;
}

//**********************
// Function show popup *
//**********************
Popup.prototype.show = function()
{
	// set active state of anchor
	this.popup_handler_anchor.className = this.active_class;
	
	// request content if no content is present
	if (!this.popup_listener_content.innerHTML)
	{
		this.getContent(this.popup_listener_content, this.http_location, this.http_method, this.http_params);
	}
	
	// show popup
	this.popup_listener_wrapper.className = this.show_class;
	
	// set position
	this.position();
}

//**************************
// Function position popup *
//**************************
Popup.prototype.position = function()
{
	// get position of handler
	var pos_x = (findPosX(this.popup_handler_anchor) - this.popup_listener_content.offsetWidth) + (this.popup_handler_anchor.offsetWidth - 10);
	var pos_y = findPosY(this.popup_handler_anchor) - this.popup_listener_content.offsetHeight - 10;
	
	// set position
	this.popup_listener_wrapper.style.left	= pos_x + 'px';
	this.popup_listener_wrapper.style.top	= pos_y + 'px';
}

//********************************
// Function hide product enlarge *
//********************************
Popup.prototype.hideProductEnlarge = function()
{
	// empty content
	this.popup_product_enlarge.innerHTML = '';
	
	// hide filter
	this.popup_filter.className = this.hide_class;
	
	// hide product enlarge
	this.popup_product_enlarge.className = this.hide_class;
}

//********************************
// Function show product enlarge *
//********************************
Popup.prototype.showProductEnlarge = function(loc, method, params)
{
	// set events
	this.productEnlargeEvents();
	
	// show filter
	this.popup_filter.className = this.show_class;
	
	this.getContent(this.popup_product_enlarge, loc, method, params);
	
	// show popup
	this.popup_product_enlarge.className = this.show_class;
}

//*******************************
// Function toggle input amount *
//*******************************
Popup.prototype.toggleInputAmount = function(obj)
{
	var _this			= this;
	var arr_handler		= obj.id.split('_');
	var handler_id		= arr_handler[2];
	var input_amount	= document.getElementById(this.product_amount_class + handler_id);
	
	// enable input
	if (input_amount.disabled)
	{
		input_amount.disabled = false;
		
		input_amount.value	= 1;
		
		input_amount.focus();
		input_amount.select();
		
		input_amount.onblur = function()
		{
			if (!_this.checkValidNumber(this.value) || this.value == 0)
			{
				this.value = 1;
			}
		}
	}
	
	// disable input
	else
	{
		input_amount.blur();
		input_amount.value		= 0;
		input_amount.disabled	= true;
	}
	
	// toggle order button
	this.toggleOrderButton();
}

//*******************************
// Function toggle order button *
//*******************************
Popup.prototype.toggleOrderButton = function()
{
	// set vars
	this.form_order_popup		= document.getElementById('form_order_popup');
	this.cs_popup_order_button	= document.getElementById('cs_popup_order_button');
	var input_fields			= this.form_order_popup.getElementsByTagName('input');
	
	// hide button
	this.cs_popup_order_button.className = this.hide_class;
	
	for (var a=0; a<input_fields.length; a++)
	{
		var input_field = input_fields[a];
		
		if (input_field.className == this.checkbox_class)
		{
			if (input_field.checked)
			{
				this.cs_popup_order_button.className = this.show_class;
				break;
			}
		}
	}
}

//**************************************
// Function check if input is a number *
//**************************************
Popup.prototype.checkValidNumber = function(val)
{
	var result = null;
	
	// For only positive whole numbers, including zero, uncomment the following:  
	result = val.match(/^\d+$/);
	
	// For only positive decimal numbers, including zero, uncomment the following:  
	// result = val.match(/^\d+(\.\d+)?$/);
	
	// For positive and negative decimal numbers, including zero, uncomment the following:  
	// result = val.match(/^-?\d+(\.\d+)?$/);
	
	// For positive and negative numbers, including zero, uncomment the following:  
	// result = val.match(/^-?\d+$/);
	
	return (result != null);
}
