var smallCartVisible = 0;
var strTimeOutId;

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

function position_small_cart()
{
	var pos = $("#showSmallCartButton").offset();    
	var eWidth = $("#showSmallCartButton").outerWidth();
	var mWidth = $("#smallCart").outerWidth();
	var left = (pos.left+90 + eWidth - mWidth) + "px";
	var top = 24+pos.top + "px";
	
	$("#smallCart").css(
	{ 
		position: 'absolute',
		zIndex: 5000,
		left: left, 
		top: top
	});

}

function open_small_cart()
{
	clearTimeout(strTimeOutId);
	position_small_cart();
	$("#smallCart").hide().slideDown("slow");
	smallCartVisible = 1;

	//setTimeout(close_small_cart, 5000);
}

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

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

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

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

function prepare_cart_display(strPageURI)
{
	toggle_loading_div(true);
	
	document.location.href=strPageURI+'#smallCartTopAnchor';
	document.getElementById('smallCart').style.display='block';
	smallCartVisible=1;
}

function remove_from_cart_ajax(strURL)
{
	toggle_loading_div(true); 
	send_ajax_request('update_small_cart', strURL);
}

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

	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';
	}
}