/**
* author: Brett Reid
* email: brett@activeice.co.za
* url: http://activeice.co.za
* updated: 23 Oct 2006
*
**/

var timer = null;

/**
*
* @params none
*
* @constructor
*/
function cart() {
	
	this.AJAX_DIR = SITE_URL + "/seed/public/javascript/";
	this.products = new Array();
	this.cart_items = 0;
	this.debug = false;
	
}

/**
* Sets the cart quantity, gets contents of
* cart from session/db loads as html
*
* @param none
* @return void
*/
cart.prototype.init = function() {

	//exists('items');
	//exists('cart_contents');
	this.update();

}

/**
* Add a product to the cart by quantity
*
* @param id : Product ID
* @return void
*/
cart.prototype.add = function(id) {
	
	var found = false;
	var qty = ( arguments[1] ) ? arguments[1] : '';
	
	if ( qty == '' )
		return;
	
	for (var i = 0; i < this.products.length; i++ ) {
		if ( this.products[i].id == id ) {
			found = true;
			break;
		}
	}
	
	if ( found ) {
		// if we found the product, set new quantity
		if ( qty == '' ) {
			this.products[i].quantity++;
			qty = this.products[i].quantity;
		} else {
			this.products[i].quantity = qty;
		}
	} else { // otherwise create new product in cart
		if ( qty == '' ) { qty = 1; }
		this.products[ this.products.length ] = new Product( id, qty );
	}
	
	ajax_response = doAJAX( this.AJAX_DIR + "ai_cart.php?a=add&id=" + id + "|" + this.products[i].quantity );
	
	if ( this.debug ) alert(ajax_response);
	
	this.update();
	
	this.show_notice();

}

/**
* Remove a product from the cart
*
* @param id : Product ID
* @return void
*/
cart.prototype.remove = function(id) {
	
	var found = false;
	
	for (var i = 0; i < this.products.length; i++ ) {
		if ( this.products[i].id == id ) {
			found = true;
			break;
		}
	}

	if ( !found ) return;
	
	if ( this.products[i].quantity < 2 )
		this._delete(i,id);
	else
		this.reduce(i,id);

	this.update();

}

/**
* Delete an item from cart
*
* @param id : Product Array Index
* @param id : Product ID
* @return void
*/
cart.prototype.reduce = function(index,id) {
	this.products[index].quantity--;
	ajax_response = doAJAX( this.AJAX_DIR + "ai_cart.php?a=reduce&id=" + id);
	if ( this.debug ) alert(ajax_response);
}

/**
* Delete an item from cart
*
* @param id : Product Array Index
* @param id : Product ID
* @return void
*/
cart.prototype._delete = function(id) {
	
	//alert(this.AJAX_DIR + "ai_cart.php?a=delete&id=" + id);
	//return;
	ajax_response = doAJAX( this.AJAX_DIR + "ai_cart.php?a=delete&id=" + id);
	
	if ( this.debug ) alert(ajax_response);
	
	window.location.href = SITE_URL + '/our-wines/order';
}

/**
* Updates the cart to display new values
*
* @params none
* @return void
*/
cart.prototype.update = function() {
	
	ajax_response = doAJAX( this.AJAX_DIR + "ai_cart.php?a=item_count" );
	if ( this.debug ) alert(ajax_response);
	
	html = ( ajax_response != '' ) ? '('+ajax_response+')' : '';
	//load_html("items",html);
	
	ajax_response = doAJAX( this.AJAX_DIR + "ai_cart.php?a=cart_contents" );
	if ( this.debug ) alert(ajax_response);
	
	html = ( ajax_response != '' ) ? ajax_response : '';
	//load_html("cart_contents",html);
	
}


/**
* Updates the cart to display new values
*
* @params none
* @return void
*/
cart.prototype.show_notice = function() {
	clearTimeout(timer);
	document.getElementById("ajaxnotice_icon").style.display = 'block';
	document.getElementById("ajaxnotice_message").style.display = 'block';
	timer = setTimeout("hide_ajaxnotice()", 3000);
}

function hide_ajaxnotice() {
	document.getElementById("ajaxnotice_icon").style.display = 'none';
	document.getElementById("ajaxnotice_message").style.display = 'none';
}

/**
* Creates products with parameters
*
* @params id : Product ID
* @params id : Quantity
* @return void
*/
function Product(id, qty) {
	this.id = id;
	this.quantity = qty;
}

/*
* Create cart
*/
if ( typeof( cart ) != "object" )
	cart = new cart();


