Event.observe(window, 'load', function() {
    getCart();
});

var WEB_APP = "cart/";
var notifyNumber = 0;
var lastCartOpenTime = new Date();

function getCart()
{
	startLoading();
	new Ajax.Request( WEB_APP, { method: "GET", onSuccess: showCart } );
}

function lockAndSave()
{
	startLoading();
	new Ajax.Request( WEB_APP, { method: "POST", parameters: { action: "lockandsave" },  onSuccess: showCart } );
}

function makeRecurringPayment()
{
	startLoading();
	new Ajax.Request( WEB_APP, { method: "POST", parameters: { action: "makeRecurringPayment" },  onSuccess: showCart } );
}

function makeManualPayment()
{
	startLoading();
	new Ajax.Request( WEB_APP, { method: "POST", parameters: { action: "makeManualPayment" },  onSuccess: showCart } );
}

function showCart( ajax )
{
	var newCart = ajax.responseText;
	$("cartContainer").innerHTML = newCart;
	$("cartContainer").removeClassName("cartContainer");
	jQuery('div#cartContainer').unblock(); 
}

function showCartNotify( ajax )
{
	showCart( ajax )
	if ( $("cart").style.display == "none" )
	{
		notifyNumber++;
		//$("displaycartButton").innerHTML = "Show cart (" + notifyNumber + ")";
	}
	displayCart();
}

function delItem( item_num )
{
	startLoading();
	new Ajax.Request( WEB_APP, { method: "POST", parameters: { "action": "remove", "key": item_num }, onSuccess: showCart } );
}

function hideCart()
{
	if ( $("cartContainer").style.display != "none" )
	{
		jQuery('#cart').slideUp('fast');
		$("shoppingcartholder").removeClassName("top_menu_selected");
		return true;
	}
	return false;
}

function displayCart()
{
	if ( $("cart").style.display == "none" )
	{
		lastCartOpenTime = new Date();
		jQuery('#cart').slideDown('fast');
		notifyNumber = 0;
		$("shoppingcartholder").addClassName("top_menu_selected");
		return true;
	}
	return false;
}

function displayAndHideCart()
{
	if ( !displayCart() )
		hideCart();
}

function startNewCart()
{
	startLoading();
	new Ajax.Request( WEB_APP, { method: "POST", parameters: { action: "startnewcart" },  onSuccess: showCart } );
}

function startLoading()
{
	$("cartContainer").addClassName("cartContainer");
	jQuery('div#cartContainer').block({ message: null });
}
/*
	* gameservernew:
		- GID
		- slots
		- months
	* gameserverrenewal:
		- ID (van de game server)
		- slots
		- months
	* gameserverdonation:
		- ID (van de game server)
		- months
	* markmodcoinsdonation:
		- coinsAmount
*/
function addToCartgameservernew( GID, slots, months )
{
	startLoading();
	new Ajax.Request( WEB_APP, { method: "POST", parameters: { action: "add", itemkind: "gameservernew", GID: GID, slots: slots, months: months }, onSuccess: showCartNotify } );
	playAddToCartAni();
}

function addToCartgameserverrenewal( gameserverID, slots, months )
{
	startLoading();
	new Ajax.Request( WEB_APP, { method: "POST", parameters: { action: "add", itemkind: "gameserverrenewal", ID: gameserverID, slots: slots, months: months }, onSuccess: showCartNotify } );
	playAddToCartAni();
}




// ===== Animation functions go below =====

var clickfromX;
var clickfromY;

function getClickFromPos()
{
	var ScrollTop = document.body.scrollTop;
	
	if (ScrollTop == 0)
	{
		if (window.pageYOffset)
			ScrollTop = window.pageYOffset;
		else
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	var documentHoogte 	= document.body.clientHeight;
	var scrollLocatie	= ScrollTop;
	var schermHoogte	= window.innerHeight;
	var elementLocatie;
	
	$("toCartAni").style.width = "32px";
	$("toCartAni").style.height = "32px";
	$("toCartAni").style.top = clickfromY - scrollLocatie - 30 + "px";
	$("toCartAni").style.right = window.innerWidth - clickfromX - 40 + "px";
	$("toCartAni").style.display = "block";
	jQuery("#toCartAni").animate({"top": (190 - scrollLocatie ) + "px", "right": "130px"}, 1200, function () { $("toCartAni").style.display = "none"; });
}

function getY( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) 
	{
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function playAddToCartAni()
{
	lastCartOpenTime = new Date();
	getClickFromPos();
}

function getcords(e){
	clickfromX = Event.pointerX(e);
	clickfromY = Event.pointerY(e);
}

Event.observe(document, 'mousemove', getcords);
