var Minicart = Class.create();
Minicart.prototype = {
    initialize: function(form, minicartUrl){
        this.form = form;
        this.minicartUrl = minicartUrl;
        this.onUpdate = this.fillForm.bindAsEventListener(this);
		this.onGetRecentProducts = this.fillRecentProducts.bindAsEventListener(this);
		this.onGetRelatedProducts = this.fillRelatedProducts.bindAsEventListener(this);
        this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
    },
    update: function(){
		var request = new Ajax.Request(
			this.minicartUrl,
			{
				method: 'post',
				onComplete: this.onComplete,
				onSuccess: this.onUpdate,
				onFailure: this.onComplete,
				parameters: Form.serialize(this.form)
			}
		);
    },
    fillForm: function(transport){
        var elementValues = {};
        if (transport && transport.responseText){
            try{
                elementValues = eval('(' + transport.responseText + ')');
            }
            catch (e) {
                elementValues = {};
            }
        }
        else{
        }
		if(elementValues.cart.items == 1) {
			items = ' item';
		} else {
			items = ' items';
		}
		$('minicart').innerHTML = 'My Cart: <strong>'+ elementValues.cart.price +' ('+ elementValues.cart.items + items + ')</strong>';
		var freeshippingprice = FREE_SHIPPING;
		if(freeshippingprice == '') {
			freeshippingprice = 399.00;
		}
		if(elementValues.cart.subTotal == null) {
			elementValues.cart.subTotal = 0.00;
		}
		untillprice = parseFloat(freeshippingprice) - parseFloat(elementValues.cart.subTotal);	
		if(untillprice < 0) {
			if(freeshippingprice == 149) {
				$('freeshipping').innerHTML = '<a href="javascript:void(0);" title="free shipping" onclick="popWin(\''+URL+'freeship\', \'\' , \'width=600,height=225,left=100,top=100,location=no,status=yes,scrollbars=yes,resizable=yes\'); return false;"><strong class="a-left">FREE SHIPPING</strong></a>';  
			} else {
				$('freeshipping').innerHTML = '<a href="javascript:void(0);" title="free shipping" onclick="popWin(\''+URL+'freeshipping\', \'\' , \'width=600,height=225,left=100,top=100,location=no,status=yes,scrollbars=yes,resizable=yes\'); return false;"><strong class="a-left">FREE SHIPPING</strong></a>';  
			}
		} else {
			if(freeshippingprice == 149) {
				$('freeshipping').innerHTML = '$'+parseFloat(Math.round(untillprice*Math.pow(10,2))/Math.pow(10,2)) + ' until <a href="javascript:void(0);" title="free shipping" onclick="popWin(\''+URL+'freeship\', \'\' , \'width=600,height=225,left=100,top=100,location=no,status=yes,scrollbars=yes,resizable=yes\'); return false;"><strong>free shipping</strong></a>';
			} else {
				$('freeshipping').innerHTML = '$'+parseFloat(Math.round(untillprice*Math.pow(10,2))/Math.pow(10,2)) + ' until <a href="javascript:void(0);" title="free shipping" onclick="popWin(\''+URL+'freeshipping\', \'\' , \'width=600,height=225,left=100,top=100,location=no,status=yes,scrollbars=yes,resizable=yes\'); return false;"><strong>free shipping</strong></a>';
			}
		}
    },
    getRecentProducts: function(){
		var request = new Ajax.Request(
			this.minicartUrl,
			{
				method: 'post',
				onComplete: this.onComplete,
				onSuccess: this.onGetRecentProducts,
				onFailure: this.onComplete,
				parameters: Form.serialize(this.form)
			}
		);
    },
    fillRecentProducts: function(transport){
        var elementValues = {};
        if (transport && transport.responseText){
			$$('div.col-right').innerHTML = transport.responseText;
        }
		else {
			alert(transport.responseText);
		}
    },
    getRelatedProducts: function(){
		var request = new Ajax.Request(
			this.minicartUrl,
			{
				method: 'post',
				onComplete: this.onComplete,
				onSuccess: this.onGetRelatedProducts,
				onFailure: this.onComplete,
				parameters: Form.serialize(this.form)
			}
		);
    },
    fillRelatedProducts: function(transport){
        var elementValues = {};
        if (transport && transport.responseText){
       		$$('div.col-right').innerHTML = transport.responseText;
        }
		else {
			alert(transport.responseText);
		}
    },
	startLoadWaiting: function(transport) {
	},
	resetLoadWaiting: function(transport) {
	}
}