//**********************
// Product Constructor *
//**********************
function Product(output_wrapper,path_prefix)
{	
	// get id's
	this.output_wrapper = document.getElementById(output_wrapper);
	
	// set strings
	this.path_prefix 	= !path_prefix ? '' : path_prefix;
	this.path			= this.path_prefix + 'components/multishop/http/';
	this.active_class	= 'active';
	this.disabled_class	= 'disabled';
	this.fade_amount	= 20;
	this.fade_timer		= 1000;
	this.time_out		= 5000;
	
	// set var for setTimeout function
	this.globalFunctionActivated	= false;
}

//**************************************
// Function get products from category *
//**************************************
Product.prototype.getProductsFromCategory = function(obj, template_id, loading_img)
{
	// set object to var
	var _this = this;
	
	// create the loading screen
	createLoader(this.output_wrapper, loading_img);
	
	var myConn = new XHConn();
	
	if (!myConn) alert("XMLHTTP not available. Please try a newer/better browser.");
	
	// return result when done
	var fnWhenDone = function (oXML)
	{
		// put response in the wrapper
		_this.output_wrapper.innerHTML = oXML.responseText;
		
		// remove the loading screen
		removeLoader();
	}
	
	// make the connection
	myConn.connect(this.path + 'getProducts.php', 'POST', 'cid=' + obj.value + '&tid=' + template_id, fnWhenDone);
}

//**********************************
// Function get details of product *
//**********************************
Product.prototype.getDetails = function(obj, product_id, detail_id, loading_img, request_params)
{
	// set object to var
	var _this = this;
	
	// create the loading screen
	createLoader(this.output_wrapper, this.path_prefix + loading_img);
	
	var myConn = new XHConn();
	
	if (!myConn) alert("XMLHTTP not available. Please try a newer/better browser.");
	
	// set state of all tabs to non-active
	var details_tabs	= obj.parentNode.parentNode.getElementsByTagName('li');
	
	for (var a=0; a<details_tabs.length; a++)
	{
		var details_tab			= details_tabs[a];
		details_tab.className	= '';
	}
	
	// set current tab to active
	obj.parentNode.className	= this.active_class;
	
	// return result when done
	var fnWhenDone = function (oXML)
	{
		// put response in the wrapper
		_this.output_wrapper.innerHTML = oXML.responseText;
		
		// remove the loading screen
		removeLoader();
	}
	
	var send_request_params = request_params ? '&'+request_params : '';
	
	// make the connection
	myConn.connect(this.path + 'getProductDetails.php', 'POST', 'pid=' + product_id + '&did=' + detail_id + send_request_params, fnWhenDone);
}

//********************************
// Function get image of product *
//********************************
Product.prototype.getImage = function(imageObj, shop_id, product_id, image_id, loading_img)
{
	// set object to var
	var _this = this;
	
	// get image tag
	var imageTag	= document.getElementById(imageObj);
	
	// create the loading screen
	createLoader(this.output_wrapper, loading_img);
	
	var myConn = new XHConn();
	
	if (!myConn) alert("XMLHTTP not available. Please try a newer/better browser.");
	
	// return result when done
	var fnWhenDone = function (oXML)
	{
		// get image information
		var responseArray	= oXML.responseText.split('_QQQ_');
		var image_src		= responseArray[0];
		var image_previous	= responseArray[1];
		var image_next		= responseArray[2];
		var previous_button	= document.getElementById('previous_button');
		var next_button		= document.getElementById('next_button');
		
		// set source of image
		imageTag.src = image_src;
		
		// set states of previous and next button
		previous_button.className	= image_previous == 0 ? _this.disabled_class : '';
		next_button.className		= image_next == 0 ? _this.disabled_class : '';
		
		// set events of previous and next button
		previous_button.onclick = function()
		{
			if (image_previous > 0)
			{
				product_gallery.getImage(imageObj, shop_id, product_id, image_previous, loading_img);
			}
			return false;
		}
		
		next_button.onclick = function()
		{
			if (image_next > 0)
			{
				product_gallery.getImage(imageObj, shop_id, product_id, image_next, loading_img);
			}
			return false;
		}
	}
	
	// remove the loading screen
	imageTag.onload = function()
	{
		removeLoader();
	}
	
	// make the connection
	myConn.connect(this.path + 'getProductImage.php', 'POST', 'sid=' + shop_id + '&pid=' + product_id + '&iid=' + image_id + '&path_prefix=' + this.path_prefix, fnWhenDone);
}

//*******************************************
// Function update product in shopping cart *
//*******************************************
Product.prototype.updateInCart = function(input_id, order_product_id, loading_img, state, coupon_input_id)
{
	// get input field
	var inputObj		= document.getElementById(input_id);
	var couponInputObj	= document.getElementById(coupon_input_id);
	
	if (inputObj) var input_obj_value = inputObj.value;
	else var input_obj_value = 0;
	
	if (couponInputObj) var coupon_input_obj_value = couponInputObj.value;
	else var coupon_input_obj_value = 0;	
	
	// set object to var
	var _this = this;
	
	if (!state)
	{
		if (this.globalFunctionActivated == false)
		{
			// execute the function after a brief moment
			var newTimer = setTimeout(
				function ()
				{
					_this.updateInCart(input_id, order_product_id, loading_img, 'true', coupon_input_id);
					_this.globalFunctionActivated = false;
				}
				, _this.time_out
			);
				
			this.globalFunctionActivated = true;
		}
	}
	
	else
	{
		// check if value is an integer or if first character is '0'
		if (!isInteger(inputObj.value) || inputObj.value.charAt(0) == 0)
		{
			// reset value
			inputObj.value = 1;
			
			// select value
			inputObj.select();
		}
		
		// create the loading screen
		createLoader(this.output_wrapper, loading_img);
		
		var myConn = new XHConn();
		
		if (!myConn) alert("XMLHTTP not available. Please try a newer/better browser.");
		
		// return result when done
		var fnWhenDone = function (oXML)
		{
			// get image information
			var responseArray			= oXML.responseText.split('_QQQ_');
			var product_amount			= responseArray[0];
			var product_price_inc		= responseArray[1];
			var product_price_total_inc	= responseArray[2];
			var logistics_inc			= responseArray[3];
			var total_inc				= responseArray[4];
			var total_exc				= responseArray[5];
			var total_tax				= responseArray[6];
			var coupon_discount			= responseArray[7];
			var feedback				= responseArray[8];
			var max_amount				= responseArray[9];
			
			product_amount = Math.round(product_amount);
			var amount		= document.getElementById('amount_' + order_product_id);
			var sub_price	= document.getElementById('sub_price_' + order_product_id);
			var order_price	= document.getElementById('price_' + order_product_id);
			var logistics	= document.getElementById('logistics');
			var total		= document.getElementById('total');
			var sub_total	= document.getElementById('sub_total');
			var tax			= document.getElementById('tax');
			var coupon_value = document.getElementById('coupon_value');

			if (feedback)
			{
				var feedback_node  = document.getElementById('shopping_cart_feedback');
				if (feedback_node) feedback_node.style.display = 'block';
				
				if (max_amount)
				{
					var max_order_amount	= max_amount.split('=')[1];
					
					if (max_order_amount)
					{
						var converted_max_order_amount = max_order_amount.split('.')[0];
						
						if (converted_max_order_amount)
						{
							var feedback_txt		= feedback_node.innerHTML;
							var new_feedback_txt	= feedback_txt.replace('{maxorderamount}', parseInt(converted_max_order_amount));
							
							// set new text
							feedback_node.innerHTML = new_feedback_txt;
						}
					}
				}
			}
			else
			{
				var feedback_node  = document.getElementById('shopping_cart_feedback');
				if (feedback_node) feedback_node.style.display = 'none';
			}
			
			// put response in the right nodes
			amount.value			= product_amount;
			sub_price.innerHTML		= product_price_inc;
			order_price.innerHTML	= product_price_total_inc;
			logistics.innerHTML		= logistics_inc;
			total.innerHTML			= total_inc;
			sub_total.innerHTML		= total_exc;
			tax.innerHTML			= total_tax;
			coupon_value.innerHTML	= coupon_discount;

			// fade element with the beautiful FAT
			Fat.fade_element('sub_price_' + order_product_id, _this.fade_amount, _this.fade_timer, '#FFF');
			Fat.fade_element('price_' + order_product_id, _this.fade_amount, _this.fade_timer, '#FFF');
	
			// remove the loading screen
			removeLoader();
		}
		// make the connection
		myConn.connect(this.path + 'updateProductInCart.php', 'POST', 'opid=' + order_product_id + '&amount=' + input_obj_value + '&type=1&code=' + coupon_input_obj_value, fnWhenDone);
	}
}
//*******************************************
// Function get coupon in shopping cart *
// type = 2 (remove coupon)
//*******************************************
Product.prototype.getCoupon = function(input_id, order_product_id, loading_img, state, type)
{
	// get input field
	var inputObj = document.getElementById(input_id);
	
	if (inputObj)
	{
		// set object to var
		var _this = this;
		if (!state)
		{
			if (this.globalFunctionActivated == false)
			{
				if (type == 1)
				{
					_this.getCoupon(input_id, order_product_id, loading_img, 'true', 1);
				} else if (type == 2)
				{
					_this.getCoupon(input_id, order_product_id, loading_img, 'true', 2);
				}
				_this.globalFunctionActivated = false;
			}
		}
		else
		{
			// create the loading screen
			createLoader(this.output_wrapper, loading_img);
			var myCouponConn = new XHConn();
			if (!myCouponConn) alert("XMLHTTP not available. Please try a newer/better browser.");
			// return result when done
			var fnWhenDone = function (oXML)
			{
				// get image information
				var responseArray			= oXML.responseText.split('_QQQ_');
				var coupon_discount			= responseArray[0];
				var total_inc				= responseArray[1];
				var tax						= responseArray[2];
				var previous_total			= document.getElementById('total').innerHTML;
				
				document.getElementById('coupon_value').innerHTML	= coupon_discount;
				document.getElementById('tax').innerHTML			= tax;
				document.getElementById('total').innerHTML			= total_inc < 0 ? 0 : total_inc;
				
				if (type == 2)
				{
					document.getElementById('coupon_code').value = '';
				}
				// remove the loading screen
				removeLoader();
			}
			// make the connection
			if (type == 1)
			{
				myCouponConn.connect(this.path + 'updateProductInCart.php', 'POST', 'opid=' + order_product_id + '&code=' + inputObj.value + '&type=2', fnWhenDone);
			} else if (type == 2)
			{
				myCouponConn.connect(this.path + 'updateProductInCart.php', 'POST', 'opid=' + order_product_id + '&code=-1&type=2', fnWhenDone);
			}
		}
	}
}

//**************************************
// Function calculate price of product *
//**************************************
Product.prototype.calculatePrice2 = function(input_id, product_id, loading_img, state)
{
	// get input field
	var inputObj = document.getElementById(input_id);

	// set object to var
	var _this = this;
	
	if (!state)
	{
		if (this.globalFunctionActivated == false)
		{
			// execute the function after a brief moment
			var newTimer = setTimeout(
				function ()
				{
					_this.calculatePrice(input_id, product_id, loading_img, 'true');
					_this.globalFunctionActivated = false;
				}
				, _this.time_out
				);
				
			this.globalFunctionActivated = true;
		}
	}
	
	else
	{
		// check if value is an integer or if first character is '0'
		if (!isInteger(inputObj.value) || inputObj.value.charAt(0) == 0)
		{
			// reset value
			inputObj.value = 1;
			
			// select value
			inputObj.select();
		}
		
		// create the loading screen
		createLoader(this.output_wrapper, loading_img);
		
		var myConn = new XHConn();
		
		if (!myConn) alert("XMLHTTP not available. Please try a newer/better browser.");
		
		// return result when done
		var fnWhenDone = function (oXML)
		{
			// get image information
			var responseArray			= oXML.responseText.split('_QQQ_');
			var product_price_inc		= responseArray[0];
			var product_price_total_inc	= responseArray[1];
			
			var sub_price	= document.getElementById('sub_price_' + product_id);
			var order_price	= document.getElementById('price_' + product_id);
			
			// put response in the right nodes
			sub_price.innerHTML		= product_price_inc;
			order_price.innerHTML	= product_price_total_inc;
			
			// fade element with the beautiful FAT
			Fat.fade_element('sub_price_' + product_id, _this.fade_amount, _this.fade_timer, '#FFF');
			Fat.fade_element('price_' + product_id, _this.fade_amount, _this.fade_timer, '#FFF');
	
			// remove the loading screen
			removeLoader();
		}
			
		// make the connection
		myConn.connect(this.path + 'calculatePrice.php', 'POST', 'pid=' + product_id + '&amount=' + inputObj.value, fnWhenDone);
	}
}

//*******************************
// Function get shopping basket *
//*******************************
Product.prototype.getShoppingBasket = function(obj, type_id, basket_id, loading_img, tab_event)
{
	// set object to var
	var _this = this;
	
	// create the loading screen
	createLoader(this.output_wrapper, loading_img);
	
	var myConn = new XHConn();
	
	if (!myConn) alert("XMLHTTP not available. Please try a newer/better browser.");
	
	// set state of all tabs to non-active
	var basket_tabs_wrapper	= document.getElementById('shopping_basket_tabs');
	var basket_tabs			= basket_tabs_wrapper.getElementsByTagName('li');
	
	if (!tab_event)
	{
		for (var a=0; a<basket_tabs.length; a++)
		{
			var basket_tab			= basket_tabs[a];
			basket_tab.className	= '';
		}
		
		// set current tab to active
		obj.parentNode.className	= this.active_class;
	}
	
	// return result when done
	var fnWhenDone = function (oXML)
	{
		// put response in the wrapper
		_this.output_wrapper.innerHTML = oXML.responseText;
		
		// remove the loading screen
		removeLoader();
	}
	
	// set extra parameter if basket_id is present
	bid_param = '';
	
	if (basket_id)
	{
		bid_param = '&bid=' + basket_id;
	}
	
	// make the connection
	myConn.connect(this.path + 'getShoppingBasket.php', 'POST', 'tid=' + type_id + bid_param , fnWhenDone);
}