var smallCartVisible = 0;
var strTimeOutId;

$(document).ready(function()
{		
	$("#store_small_cart_toggle_btn").click(function()
	{
		if(smallCartVisible==0)
		{
			open_small_cart();	
			return false;
		}
		else
		{
			close_small_cart();
			return false;
		}
			
	});	
});

function position_small_cart()
{
	var pos = $("#store_small_cart_toggle_btn").offset();    
	var eWidth = $("#store_small_cart_toggle_btn").outerWidth();
	var mWidth = $("#store_small_cart_container").outerWidth();
	var left = (pos.left+90 + eWidth - mWidth) + "px";
	var top = 24+pos.top + "px";

	$("#store_small_cart_container").css(
	{ 	
		zIndex: 5000
	});
}

function open_small_cart()
{
	clearTimeout(strTimeOutId);
	position_small_cart();
	$("#store_small_cart_container").hide().slideDown("slow");
	smallCartVisible = 1;
	
	//setTimeout(close_small_cart, 5000);
}

function close_small_cart()
{
	clearTimeout(strTimeOutId);
	$("#store_small_cart_container").fadeOut('slow');
	smallCartVisible = 0;
}

function update_small_cart(objAjaxRequest)
{
//	arrRequest = objAjaxRequest.responseText.split("<script>");
	arrRequest = objAjaxRequest.responseText.split('<script type="text/javascript">');
	strJS = arrRequest[1].replace("</script>","");

	eval(strJS);
	toggle_loading_div(false);
	//eval(objAjaxRequest.responseText);
	document.getElementById('store_small_cart_container').innerHTML = arrRequest[0];
	clearTimeout(strTimeOutId);
	strTimeOutId = setTimeout(close_small_cart, 7000);

	$("#header-bar-cart-total").html(fltTotalPrice);
	$("#header-bar-cart-items").html(intTotalQty+' Items:');

	if( typeof(check_auto_reorder_frequency) != 'undefined') {
		check_auto_reorder_frequency();
	}
}

function toggle_loading_div(boolTurnLoadingDivOn)
{
	if(boolTurnLoadingDivOn)
	{
		document.getElementById('store_small_cart_main').style.display = "none";
		document.getElementById('store_small_cart_main_loading').style.display = "block";
	}
	else
	{
		document.getElementById('store_small_cart_main').style.display = "block";
		document.getElementById('store_small_cart_main_loading').style.display = "none";
	}
}

function prepare_cart_display(strPageURI, boolScrollCartToBt)
{

	toggle_loading_div(true);
		
	if(boolScrollCartToBt == true)
	{
		document.location.href=strPageURI+'#header-shopping-cart-no-scroll';
		$("#store_small_cart_container").addClass('mini_cart_no_scroll');
		document.getElementById('store_small_cart_container').style.position='fixed';
		document.getElementById('store_small_cart_container').style.top='20px';
		document.getElementById('store_small_cart_container').style.left='50%';
		document.getElementById('store_small_cart_container').style.margin='0 0 0 -230px';
		document.getElementById('store_small_cart_container').style.display='block';
		smallCartVisible=1;
	}
	else
	{
		document.location.href=strPageURI+'#header-shopping-cart';
		document.getElementById('store_small_cart_container').style.display='block';
		smallCartVisible=1;
	}	
}


function remove_from_cart_ajax(strURL)
{
	toggle_loading_div(true); 
	send_ajax_request('update_small_cart', strURL);
	if( typeof(check_auto_reorder_frequency) != 'undefined') {
		check_auto_reorder_frequency();
	}
}
function clear_cart_ajax(strURL)//TO DO
{
//	toggle_loading_div(true); 
	send_ajax_request('update_small_cart', 'cart-ajax;cart,clear_cart.html');
//	if( typeof(check_auto_reorder_frequency) != 'undefined') {
//		check_auto_reorder_frequency();
//	}
}
function update_cart_ajax(strURL)
{
	i=0;
	strQuantities = '';

	do
	{
		intQty = $('#cart_item_qty_'+i).val();

		if(typeof(intQty) != 'undefined')
		{
			strQuantities = strQuantities + '&cart_item_qty_' + i + '=' + intQty;
		}
		i++;			
	}
	while (intQty);

	toggle_loading_div(true); 

	send_ajax_request('update_small_cart', strURL+'?'+strQuantities);
}

function add_to_cart_ajax(strProdSKU, strProdId, boolScrollCartToBt)
{
	prepare_cart_display(arrGlobals['page_uri'],boolScrollCartToBt); 

	
	send_ajax_request('update_small_cart', arrGlobals['site_url'] + arrGlobals['link_pre'] + 'cart-ajax;cart,add_to_cart,' + strProdSKU + ',' + (document.getElementById('qty_'+strProdId).value) + arrGlobals['link_post']);	
	return false;
}

function toggle_reorder_frequency()
{
	if(intReorderCount < 1)
	{
		if(document.getElementById('auto_reorder_frequency'))
		{
			document.getElementById('auto_reorder_frequency').disabled = true;
			document.getElementById('autoreorderfrequency_div').style.display='none';
		}
	}
	else
	{
		document.getElementById('auto_reorder_frequency').disabled = false;		
		document.getElementById('autoreorderfrequency_div').style.display='block';
	}
}
