<!--

var CacheCart = Class.create();
CacheCart.prototype = {
	
	/**
	 *	Adds a product to the cart
	 */
	initialize: function() {
		// set vars
		this.isOpen = false;
		this.curCartHtml = '';
		
		// add the cart div to the end of the html
		/*
		var objBody = document.getElementsByTagName("body").item(0);
		var cartDiv = document.createElement("div");
		cartDiv.setAttribute('id','cart');
		cartDiv.style.display = 'none';
		objBody.appendChild( 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){ mycart.showResponseInit(response); } } );
	},
	
	/**
	 *	Adds a product to the cart
	 */
	addToCart: function( productId ) {
		this.showLoad();
		var url    = 'cart/cart.php';
		var rand   = Math.random(9999);
		var pars   = 'action=add&product_id=' + productId + '&rand=' + rand;
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(response){ mycart.showResponse(response); } } );	//evalJS:'force', 
	},	
	
	/**
	 *	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){ mycart.showResponse(response); } } );
	},
	
	/**
	 *	Removes a product based on broduct id string
	 */
	clearProduct: function( productId ) {
		this.showLoad();
		var url    = 'cart/cart.php';
		var rand   = Math.random(9999);
		var pars   = 'action=clear_product&product_id=' + productId + '&rand=' + rand;
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(response){ mycart.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
		$('cart').innerHTML = originalRequest.responseText;
		
		// hide/show cart
		if( $('cart').innerHTML == '' )
			this.hideCart();
		else
		{
			this.showCart();
			this.curCartHtml = originalRequest.responseText;
		}
	},
	
	/**
	 *	Sets the entire cart text when it comes back
	 */
	showResponseInit: function( originalRequest ) {
		
		// set cart <div> contents
		$('cart').innerHTML = originalRequest.responseText;
		this.curCartHtml = originalRequest.responseText;
		
		// hide/show cart
		if( $('cart').innerHTML != '' )
			this.showCartInit();
		
	},
	
	/**
	 *	Shows the loading message when an ajax call is made
	 */
	showLoad: function() {
		if( this.isOpen )
		{
			$('loading').show();
		}
	},
	
	/**
	 *	Hides the loading message
	 */
	hideLoad: function() {
		if( this.isOpen )
		{
			$('loading').hide();
		}
	},
	
	/**
	 *	Opens the cart when a product is added
	 */
	showCart: function()
	{
		if( this.isOpen == false )
		{
			this.isOpen = true;
			Effect.BlindDown( $('cart'), { duration:.2 } );
		}
	},
	
	/**
	 *	Opens the cart without animation
	 */
	showCartInit: function()
	{
		this.isOpen = true;
		$('cart').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
			$('cart').innerHTML = this.curCartHtml;
			Effect.BlindUp( $('cart'), { duration:.2 } );
		}
	},
	
	/**
	 *	Makes a call to the php script that assembles the PayPal url based on cart contents
	 */
	checkOut: function() {
		this.showLoad();
		var url    = 'cart/get-checkout-link.php';
		var rand   = Math.random(9999);
		var pars   = 'rand=' + rand;
		var myAjax = new Ajax.Request( url, {method: 'post', parameters: pars, onComplete: function(response){ mycart.goCheckOut(response); }} );
	},
	
	/**
	 *	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() { mycart = new CacheCart(); }
Event.observe( window, 'load', initCart, false );
//-->