var timerFinish; 
var loadFinish; 
var product_names = [];
var products_in_basket_before_submit;
var timeDelay = 2000;
var scrolltop;

/**
 * Add product to basket without ajax
 * @param product_id	:	product id of the main product
 * @param category_id	:	category id of the main product
 */
function addToCart_noAjax(product_id, category_id) {
	$("#product_add_to_cart_form input[name='args[product_id]']").val( product_id );
	$("#product_add_to_cart_form input[name='args[category_id]']").val( category_id );
	$("#product_add_to_cart_form input[name='product_id']").val( product_id );
	$("#product_add_to_cart_form input[name='category_id']").val( category_id );
	$("#product_add_to_cart_form")[0].submit();
}

/**
 * Fill the values of the form, then doing the ajax request to add the product to the cart
 * @param product_id		:	product id of the main product
 * @param _product_names	:	String or Array of the product names to be added
 * @param category_id		:	category id of the main product
 */
function addToCart(product_id, _product_names, category_id){
	//scroll to top
	scrolltop = $(window).scrollTop();
	$('html, body').animate({scrollTop:0}, 'slow', 'easeOutQuart');
	
	product_names = _product_names
	$("#product_add_to_cart_form input[name='args[product_id]']").val( product_id );
	$("#product_add_to_cart_form input[name='args[category_id]']").val(category_id);		
	$("#product_add_to_cart_form input[name='product_id']").val(product_id);
	$("#product_add_to_cart_form input[name='category_id']").val(category_id);
	
	//refill products loading
	$('#minibasket_loadInfo_products').html("");
	if(product_names.constructor == Array){
		for(var i=0; i<product_names.length; i++)
			$('#minibasket_loadInfo_products').html($('#minibasket_loadInfo_products').html() + "<span class='arrow'>" + product_names[i] + "<\/span><br/>");
	}else{
		$('#minibasket_loadInfo_products').html("<span class='arrow'>" + product_names + "<\/span>");
	}
	
	//fill container with load info
	$('#basket_popup .box div.container').html( $('#basket_load_info').html() );
	toggleOverlay('show', $('#basket_popup .box'), {fadeDuration:600});
	
	//set timer
	timerFinish = false;
	loadFinish = false;
	var timer = setTimeout(function(){
		clearTimeout(timer);
		timerFinish = true;
		if(loadFinish) showAddedToCart();
	}, timeDelay);
	
	products_in_basket_before_submit = parseInt($('#minibasket_placeholder span.hidden_var_products_count').text());
	process_submit(document.getElementById('product_add_to_cart_form'), 'minibasket_placeholder','includes/include_minibasket', 'minibasket_loader', callBackProcesSubmit);	
}

/**
 * Callback function from ajax submit on succes
 */
function callBackProcesSubmit(){
	loadFinish = true;
	if(timerFinish)	showAddedToCart();
}

/**
 * show confirm info popup in animation type
 */
function showAddedToCart() {
	var error = (products_in_basket_before_submit == parseInt($('#minibasket_placeholder span.hidden_var_products_count').text())) ? true : false;
	var container = $('#basket_popup .box .container');

	container.fadeTo(fadeDuration, 0, function(){
		if (error){
			//onerror
			var new_width = (jQuery.browser.msie) ? $('#basket_confirm_error').width()+14 : $('#basket_confirm_error').width();
			container.animate({
				'width'			: new_width,
				'height'		: $('#basket_confirm_error').height() + 15
			}, 500, "easeOutQuart", function(){
				container.html($('#basket_confirm_error').html());
				container.fadeTo(fadeDuration, 1);
			});
		}else{
			//onsuccess
			var product_text = "";
			if (product_names.constructor == Array){
				$.each(product_names, function(index, product_name){
					product_text += "<span class='arrow'>" + product_name + "<\/span><br />";
				});
			}else{
				product_text += "<span class='arrow'>" + product_names + "<\/span><br />";
			}
			$('#basket_confirm_info .product_text').html(product_text);
			
			var new_width = (jQuery.browser.msie) ? $('#basket_confirm_info').width()+14 : $('#basket_confirm_info').width();
			container.animate({
				'width'			: new_width,
				'height'		: $('#basket_confirm_info').height() + 15
			}, 500, "easeOutQuart", function(){
				container.html($('#basket_confirm_info').html());
				container.fadeTo(fadeDuration, 1);
			});
		}
	});
}

/**
 * hide the addedToCart popup (deletes all content and styles)
 * Event triggered when click on 'Close' button of confirm popup
 */
function hideAddedToCart() {
	toggleOverlay('hide', $('#basket_popup .box'), {fadeDuration:200, onComplete:function(){
		$('#basket_popup .box').removeAttr('style');
		$('#basket_popup .box div.container').html("");
		$('#basket_popup .box div.container').removeAttr('style');
		$('#basket_popup .box').attr('style', 'display:none;');
		
		//scrollback to where you clicked the add to basket
		$('html, body').animate({scrollTop:scrolltop}, 'slow', 'easeOutQuart');
	}});
}
