<!--

var CacheCart = Class.create();
CacheCart.prototype = {
	
	/**
	 *	Adds a product to the cart
	 */
	initialize: function() {
		// set vars
		this.isOpen = false;
		this.curCartHtml = '';
		
		// create cart div
		var cartDiv = document.createElement("div");
		cartDiv.setAttribute('id','cacheCart');
		cartDiv.style.display = 'none';
		
		// insert new html element at top of body
		var objBody = document.getElementsByTagName("body").item(0);
		$(objBody).insert( {'top' : cartDiv } );

		// make initial call to load the cart - for page refreshes
		var url    = '/cart/cart.php';
		var rand   = Math.random(9999);
		var myAjax = new Ajax.Request( url, {method: 'get', onComplete: function(response){ cart.showResponseInit(response); } } );
	},
	
	/**
	 *	Adds a product to the cart
	 */
	addToCart: function( productId, intl ) {
		this.showLoad();
		var url    = '/cart/cart.php';
		var rand   = Math.random(9999);
		var pars   = 'action=add&product_id=' + productId + '&intl=' + intl + '&rand=' + rand;
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(response){ cart.showResponse(response); } } );
	},	
	
	/**
	 *	Empties the cart
	 */
	clearCart: function() {
		this.showLoad();
		var url    = '/cart/cart.php';
		var rand   = Math.random(9999);
		var pars   = 'action=clear&rand=' + rand;
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(response){ cart.showResponse(response); } } );
	},
	
	/**
	 *	Removes a product based on broduct id string
	 */
	clearProduct: function( productId ) {
		this.showLoad();
		var url    = '/cart/cart.php';
		var rand   = Math.random(99999);
		var pars   = 'action=clear_product&product_id=' + productId + '&rand=' + rand;
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(response){ cart.showResponse(response); } } );	//onLoading: showLoad, 
	},	
	
	/**
	 *	Sets the entire cart text when it comes back
	 */
	showResponse: function( originalRequest ) {
		// hide loading bar
		this.hideLoad();
		
		// set cart <div> contents
		$('cacheCart').innerHTML = originalRequest.responseText;
		
		// hide/show cart
		if( $('cacheCart').innerHTML == '' )
			this.hideCart();
		else
		{
			new Effect.ScrollTo('cacheCart');
			this.showCart();
			this.curCartHtml = originalRequest.responseText;
		}
	},
	
	/**
	 *	Sets the entire cart text when it comes back
	 */
	showResponseInit: function( originalRequest ) {
		
		// set cart <div> contents
		$('cacheCart').innerHTML = originalRequest.responseText;
		this.curCartHtml = originalRequest.responseText;
		
		// hide/show cart
		if( $('cacheCart').innerHTML != '' )
			this.showCartInit();
		
	},
	
	/**
	 *	Shows the loading message when an ajax call is made
	 */
	showLoad: function() {
		if( this.isOpen )
		{
			//alert($('cartLoading').innerHTML);
			$('cartLoading').show();
		}
	},
	
	/**
	 *	Hides the loading message
	 */
	hideLoad: function() {
		if( this.isOpen )
		{
			//alert('HIDE IT');
			$('cartLoading').hide();
		}
	},
	
	/**
	 *	Opens the cart when a product is added
	 */
	showCart: function()
	{
		if( this.isOpen == false )
		{
			this.isOpen = true;
			Effect.BlindDown( $('cacheCart'), { duration:.2 } );
		}
	},
	
	/**
	 *	Opens the cart without animation
	 */
	showCartInit: function()
	{
		this.isOpen = true;
		$('cacheCart').show();
	},
	
	/**
	 *	Closes the cart when it's empty
	 */
	hideCart: function()
	{
		if( this.isOpen == true )
		{
			this.isOpen = false;
			// replace the html that got cleared out by the ajax call so we can fade out
			$('cacheCart').innerHTML = this.curCartHtml;
			Effect.BlindUp( $('cacheCart'), { duration:.2 } );
		}
	},
	
	/**
	 *	Navigates to the PayPal url that was generated by our php script
	 */
	goCheckOut: function(originalRequest) {
		// hide loading bar
		this.hideLoad();
		
		// open the url
		window.open( originalRequest.responseText );
	},
	
	end: function() {
		
	}
	
}

// fire it up on page load
function initCart() { cart = new CacheCart(); }
Event.observe( window, 'load', initCart, false );
//-->