var cCart = jQuery.Class.create({
	
	sVariable 	: '',
	aProducts 	: {},
	

	count		: 0,
	total		: 0,
	infoDiv		: null,
	
	init : function( sVariable )
	{
		this.sVariable = sVariable;
	},
	
	registerProduct : function( sSupplier, sFullProductId, iQuantity, fPrice, fShipping )
	{
		if( !this.aProducts[sSupplier] ) this.aProducts[sSupplier] = {};
		
		this.aProducts[sSupplier][sFullProductId] = { 'iQuantity' : iQuantity, 'fPrice' : fPrice, 'fShipping' : fShipping };
	},
	
	getProductsCnt : function( iSupplier )
	{
		if( !iSupplier || !this.aProducts[sSupplier] )return 0;
		var iCnt = 0;
		for( i in this.aProducts[sSupplier] ) iCnt++;
		return iCnt;
	},
	
	ajaxRequest : function( oParameters, sRespondMethid )
	{
		oParameters['actionType'] = 'cart';
		eval( 'oJSSite.ajaxRequest( oParameters, ' + this.sVariable + '.' + sRespondMethid + ' );' );
	},
	
	
	// -- Add to cart START -- //
	addToCart : function( iProductId, iQuantity, iParam0, iParam1, iParam2, iParam3, sPersonalisation )
	{	
		if( !iParam0 ) iParam0 = '';
		if( !iParam1 ) iParam1 = '';
		if( !iParam2 ) iParam2 = '';
		if( !iParam3 ) iParam3 = '';
		if( !sPersonalisation ) sPersonalisation = '';
		
		var oParameters = { 
			'action'		: 'addToCart', 
			'iProductId'	: iProductId, 
			'sPersonalisation'	: sPersonalisation, 
			'iParam0'		: iParam0, 
			'iParam1'		: iParam1, 
			'iParam2'		: iParam2, 
			'iParam3'		: iParam3, 
			'iQuantity'		: iQuantity 
			};
		this.ajaxRequest( oParameters, 'addToCartResponce' );
	},
	
	addToCartResponce : function( oRes )
	{
		if( oRes.success == 'true' )
		{
			oCart.registerProduct( oRes.sSupplier, oRes.sFullProductId, oRes.iQuantity, oRes.fPrice, oRes.fShipping );
			
			if(oRes.sAttention && "" != oRes.sAttention)
				$("#product_added_attention").html(oRes.sAttention);
			else
				$("#product_added_attention").html("");
//			oCart.calculate();
			tAlert.open( 'product_added_message' );
//			jQuery( '#basketMsg' ).html( 'Product was added.' );
		}else{
			jQuery( '#basketMsg' ).html( oRes.msg );
		}
	},
	// -- Add to cart END -- //

	changeShippingCountry : function( sSupplier, iCountry )
	{
		var oParameters = { 
			'action'		: 'changeShipping',
			'iCountry'		: iCountry,
			'sSupplier'		: sSupplier
			};
		this.ajaxRequest( oParameters, 'changeShippingCountryResponce' );
	},
	
	changeShippingCountryResponce : function( oRes )
	{
		if( oRes.success == 'true' )
		{
			for( i in oRes.aProducts )
			{
				if( oRes.aProducts[i] == 'false' )
				{
					jQuery( '#shipping_' + i ).html( "Can be delivered only to UK" );
				}else{
					jQuery( '#shipping_' + i ).html( this.number( oRes.aProducts[i] ) )
				}
			}
		}
	},
	
	// -- Edit to cart START -- //
	editCartList : function()
	{
		var oParameters = { 'action' : 'editCart', 'product' : {} };
		
		for( i in this.elements )
		{
			oParameters['product'][i] = { 
				'sFullProductId' : i, 
				'iQuantity' : $( '#qty_' + i ).val(), 
				'iParam0' : 0, 
				'iParam1' : 0, 
				'iParam2' : 0, 
				'iParam3' : 0 
			};
		}
		this.ajaxRequest( oParameters, 'editCartListResponce' );
	},
	
	editCartListResponce : function( oRes )
	{
		if( oRes.success == 'true' )
		{
			for( i in oRes.products )
			{
				var oProduct = oRes.products[i];
				oCart.elements[oProduct.sFullProductId] = { 'iId' : oProduct.iId, 'iQuantity' : oProduct.iQuantity, 'fPrice' : oProduct.fPrice };
				if( jQuery( '#total_' + oProduct.sFullProductId ) )jQuery( '#total_' + oProduct.sFullProductId ).attr( 'innerHTML', this.number( oProduct.fPrice * oProduct.iQuantity + new Number( oProduct.fShipping ) ) );
			}
//			oCart.calculate();
		}else{
			alert( oRes.msg );
		}
	},

	updateCart : function( sSupplier, bRedirect )
	{
		jQuery( '#message_' + sSupplier ).html( '' );
		var oParams = {
			'action' : 'updateCart',
			'sSupplier' : sSupplier,
			'iCountry' : jQuery( '#country_' + sSupplier ).val(),
			'bRedirect' : ( !bRedirect ? 'false' : 'true' )
		};
		
		for( i in this.aProducts[sSupplier] )
		{
			oParams['iQuantity[' + i + ']'] = jQuery( '#qty_' + i ).val();
		}
		
		this.ajaxRequest( oParams, 'updateCartResponse' );
	},
	
	updateCartResponse : function( oRes )
	{
		jQuery( '#message_' + oRes.sSupplier ).html( oRes.msg );

		if( oRes.success = 'true' )
		{
			for( i in oRes.aProducts )
			{
				var aProduct = oRes.aProducts[i];
				if( aProduct.fShipping == '' )
				{
					jQuery( '#shipping_' + i ).html( 'Can be delivered only to UK' );
					jQuery( '#shipping_td_' + i ).css( { 'border' : '2px solid #aa0000' } );
				}else{
					jQuery( '#shipping_' + i ).html( '&pound;&nbsp;' + oCart.number( aProduct.fShipping ) );
					jQuery( '#shipping_td_' + i ).css( { 'border' : '0px' } );
					jQuery( '#shipping_td_' + i ).css( { 'border-right' : '1px solid #DCE2E3' } );
					jQuery( '#shipping_td_' + i ).css( { 'border-bottom' : '1px solid #DCE2E3' } );
				}
				
				jQuery( '#qty_' + i ).val( aProduct.iQuantity );
				jQuery( '#total_' + i ).html( '&pound;&nbsp;' + oCart.number( aProduct.iQuantity * aProduct.fPrice ) );
			}
			
			if( typeof( oRes.aDeleted ) == 'object' ) for( i in oRes.aDeleted )oCart.deleteCartElement( oRes.sSupplier, oRes.aDeleted[i] );
			
			jQuery( '#subtotal_' + oRes.sSupplier ).html( '&pound;&nbsp;' + oCart.number( oRes.aTotal.products ) );
			jQuery( '#shipping_' + oRes.sSupplier ).html( '&pound;&nbsp;' + oCart.number( oRes.aTotal.shipping ) );
			jQuery( '#total_' + oRes.sSupplier ).html( '&pound;&nbsp;' + oCart.number( ( new Number( oRes.aTotal.products ) + new Number( oRes.aTotal.shipping ) ) ) );
			
			if( oRes.bAvailable == 'true' && oRes.bRedirect == 'true' )
			{
				document.location = '/basket/confirmation/id' + oRes.sSupplier + '/'
			}
		}
	},
	

	number : function( fNumber )
	{
		return new Number( fNumber ).toFixed(2);
	},
/*	editCart : function( sFullProductId, iQuantity, iParam0, iParam1, iParam2, iParam3 )
	{	
		var oParameters = { 
			'action'		: 'editCart', 
			'sFullProductId': sFullProductId, 
			'iParam0'		: iParam0, 
			'iParam1'		: iParam1, 
			'iParam2'		: iParam2, 
			'iParam3'		: iParam3, 
			'iQuantity'		: iQuantity 
			};
		this.ajaxRequest( oParameters, 'editCartResponce' );
	},
	
	editCartResponce : function( oRes )
	{
		if( oRes.success == 'true' )
		{
			if( oRes.sDeleteElement ) oCart.deleteCartElement( oRes.sDeleteElement );
			
			oCart.elements[oRes.sFullProductId] = { 'iQuantity' : oRes.iQuantity, 'fPrice' : oRes.fPrice };
			oCart.calculate();
		}else{
			alert( oRes.msg );
		}
	},*/
	// -- Edit to cart END -- //
	
	// -- Delete from cart START -- //
	deleteFromCart : function( sSupplier, sFullProductId )
	{	
		var oParameters = { 
			'action'		: 'deleteFromCart', 
			'sSupplier'		: sSupplier,
			'sFullProductId': sFullProductId 
			};
		this.ajaxRequest( oParameters, 'deleteFromCartResponce' );
	},
	
	deleteFromCartResponce : function( oRes )
	{
		if( oRes.success == 'true' )
		{
			oCart.deleteCartElement( oRes.sSupplier, oRes.sDeleteElement );
//			oCart.calculate();
		}else{
			alert( oRes.msg );
		}
	},
	// -- Delete from cart END -- //
	
	deleteCartElement : function( sSupplier, sFullProductId )
	{
		for( i in this.aProducts ) 
		{
			delete( this.aProducts[i][sFullProductId] );
			
			var iCnt = 0;
			for( p in this.aProducts[i] ) iCnt ++;
			if( iCnt == 0 )
			{
				jQuery( '#basket_' + i ).fadeOut( 'hide' );
				delete( this.aProducts[i] );
			}
		}
		
		jQuery( '#product_' + sFullProductId ).fadeOut( 'hide' );
	}
	
/*	,	
	calculate : function()
	{
		this.count = 0;
		this.total = 0;
		var shipping_price = 0;
		for( i in this.elements )
		{
			this.count ++;
			var total = ( this.elements[i]['fPrice'] * this.elements[i]['iQuantity'] );
			this.total += total;
		}
		
		var subtotal = this.total;
	}
*/	
	
});
