var cCart = jQuery.Class.create({
	variable 	: '',
	elements 	: {},
	count		: 0,
	total		: 0,
	
	fCostDiscount	: 0,
	fCostInsurance	: 0,
	bIsInsurance	: false,
	
	loader		: null,
	
	init : function( variable, elements, fDiscount, fInsurance, bIsInsurance )
	{
		this.variable = variable;
		this.elements = elements;
		this.fCostDiscount = fDiscount;
		this.fCostInsurance = fInsurance;
		this.bIsInsurance = bIsInsurance;
		
		for( i in this.elements ) this.count ++;
		
		this.loader = document.createElement( 'img' );
		this.loader.setAttribute( 'src', '/images/loading.gif' );
		this.loader.setAttribute( 'width', '25px' );
	},
	
	ajax : function( oParams, fResp )
	{
		oParams['action'] = 'cart';
		oParams['variable'] = this.variable;
		oMain.ajaxRequest( oParams, fResp );
	},

	showMessage : function( msg, sType )
	{
		oJ( '#' + sType + '_msg' ).html( msg );
	},
	
	changeInsurance : function( iValue )
	{
		this.ajax( { 'todo': 'insurance', 'iValue' : iValue }, function( oRes ){ 
			eval( 'var oObj = ' + oRes.variable + ';' ); 
			
			if( oRes.success == 'false' ) return;
			
//			oObj.bIsInsurance = oRes.insurance;
			oObj.fCostInsurance = oRes.fCostInsurance;

			oObj.calculate();
			
		} );		
	},
	
	add : function( iProductId, iQuantity, sType, oA )
	{
		this.showMessage( '', sType );
		oA.parentNode.appendChild( this.loader );
		oJ( this.loader ).show();
		oJ( oA ).hide();
		this.ajax( { 'todo': 'add', 'iProductId' : iProductId, 'iQuantity' : iQuantity }, function( oRes ){ 
			eval( 'var oObj = ' + oRes.variable + ';' ); 
			
			oObj.showMessage( oRes.msg, sType );
			oJ( oObj.loader ).hide();
			
			if( oRes.success == 'false' )
			{
				oJ( oA ).show();
				return;
			}

			if( !oObj.elements[iProductId] )
			{
				oObj.count ++;
				oObj.elements[iProductId] = { 'quantity' : iQuantity };
			}else{
				oObj.elements[iProductId]['quantity'] += iQuantity;
			}			
			oObj.calculate();
			
		} );
	},

	remove : function( iProductId )
	{
		this.ajax( { 'todo': 'remove', 'iProductId' : iProductId }, function( oRes ){ 
			eval( 'var oObj = ' + oRes.variable + ';' ); 
			
			if( oRes == 'false' )return;

			oObj.fCostDiscount = oRes.fCostDiscount;
			
			oObj.bIsInsurance = oRes.bIsInsurance;
			if( oRes.bIsInsurance == 'false' )
			{
				jQuery( '#insurance_tr' ).hide();
			}
			
			oObj.count --;
			delete( oObj.elements[iProductId] );
			
			oJ( '#cart_element_' + iProductId ).hide();
			
			oObj.calculate();
			
		} );
	},
	
	edit : function( iProductId, iQuantity )
	{
		this.ajax( { 'todo': 'edit', 'iProductId' : iProductId, 'iQuantity' : iQuantity }, function( oRes ){ 
			eval( 'var oObj = ' + oRes.variable + ';' ); 
			
			if( oRes == 'false' )return;
			oObj.fCostDiscount = oRes.fCostDiscount;
			
			oObj.elements[iProductId]['quantity'] = iQuantity;
			oObj.calculate();
		} );
	},
	
	calculate : function()
	{
		this.total = 0;
		var shipping_price = 0;
		for( i in this.elements )
		{
			var total = ( this.elements[i]['price'] * this.elements[i]['quantity'] );
			oJ( '#cost_' + i ).html( new Number( total ).toFixed(2) );
			this.total += total;
			
//			if( new Number( shipping_price ) < new Number( this.elements[i]['shipping_price'] ) )shipping_price = this.elements[i]['shipping_price'];
		}
		
		
		var subtotal = new Number( this.total );
		this.total += new Number( this.fCostDiscount );
				
		oJ( '#subtotal_price' ).html( new Number( subtotal ).toFixed(2) );
		oJ( '#discount_price' ).html( new Number( this.fCostDiscount ).toFixed(2) );

		if( this.bIsInsurance == 'true' || this.bIsInsurance == '1' )
		{
			this.total += new Number( this.fCostInsurance );
			oJ( '#insurance_price' ).html( new Number( this.fCostInsurance ).toFixed(2) );
		}
		
		oJ( '#total_price' ).html( new Number( this.total ).toFixed(2) );
	}
	
	
} );
