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{
            // do nothing.
        }
		//alert(elementValues.cart.products +' '+ elementValues.cart.price);
		// Write the code to modify the contents of the mini cart
		// javascript:alert($$('div.shop-access'));
//.each(function(a) {alert(a.innerHTML();});
// javascript:($$('div.shop-access').each(function(a) {alert(a.innerHTML());}));
		$('cartContent').innerHTML = '('+ elementValues.cart.products +')';

    },

    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){
            //alert(transport.responseText);
			// TODO: Fill the received response in the box where related products are to be shown.
			$$('div.availability').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){
            //alert(transport.responseText);
			// TODO: Fill the received response in the box where related products are to be shown.
			$$('div.availability').innerHTML = transport.responseText;
        }
    },
	
	startLoadWaiting: function(transport) {
		// If we want to show the Loading image set it here.
	},
	
	resetLoadWaiting: function(transport) {
		// If Loading image is set, unset it here.
	}
}