var XSCart = {
	products: [],
    numberOfProducts: 0,

    writeToCookie: function() {
        //console.log("Writing to cookie : \n" + this.toText());
        $.cookie("XSCart", this.toText(), { path: '/' });    
    },

    reset: function() {
        this.products.splice(0,this.numberOfProducts);
        this.numberOfProducts = 0;
        this.writeToCookie();
    },

    indexForProductWithIdAndLabel: function(id,label) {
        id = parseInt(id);
        var product;
        for(i=0;i<this.numberOfProducts;i++)
        {   product = this.fetchProduct(i);
            if(product[0] == id && product[1] == label)
                return i;
        }
        return -1;
    },

	addProduct: function (id,label,price,amount) {

        //label = escape(label);
        //alert("Trying to add product with label:\n " + label);
        id = parseInt(id);
        price = parseFloat(price);
        amount = parseInt(amount);
        var existingIndex = this.indexForProductWithIdAndLabel(id,label);
        if(existingIndex == -1)
        {   // Add new product
            //console.log("Adding product with id " + id + " and label " + label + " and amount " + amount + " at price " + price + " (" + label +")");
            var product = new Array(id,label,price,amount);
            this.products[this.numberOfProducts] = product;
            this.numberOfProducts++;
            //this.numberOfItems += amount;
            //this.totalPrice += parseFloat((price * parseFloat(amount)));
        }
        else
        {
            // Update existing product
            //console.log("Updating product with id " + id + " and label " + label + " and amount " + amount + " at price " + price + " (" + label +")");
            this.products[existingIndex][1] = label;
            this.products[existingIndex][2] = price;
            this.products[existingIndex][3] = amount;
        }
        this.writeToCookie();
        parent.location.reload(true);
        parent.Shadowbox.close();
        
    },

    removeProductAtIndex: function (index) {
        index = parseInt(index);
        var product = this.fetchProduct(index);
        this.numberOfProducts--;
        //this.numberOfItems -= parseInt(product[3]);
        //this.totalPrice -= (parseFloat(product[2])*parseInt(product[3]));
        //console.log("Removed item, subtracted EUR" + (parseFloat(product[2])*parseInt(product[3])) + " from total");
        this.products.splice(index,1);
        this.writeToCookie();
        location.reload(true);
    },

    fetchProduct: function (index) {
        index = parseInt(index);
        if(index > -1 && index < this.numberOfProducts)
            return this.products[index];
        else
            alert("Error; product array index invalid");
    },  


	restoreProduct: function (id,label,price,amount) {
        id = parseInt(id);
        price = parseFloat(price);
        amount = parseInt(amount);
        var product = new Array(id,label,price,amount);
        this.products[this.numberOfProducts] = product;
        this.numberOfProducts++;
    },


    toText: function () {
        //console.log("TO TEXT");
        var output = "";
        var product;
        for(i=0;i<this.numberOfProducts;i++)
        {   product = this.fetchProduct(i);
            if(i>0)
                output = output+"&-%-&";
            if(parseInt(product[3]) < 1)
                continue;
            output = output + product[0].toString() + "#-%-#" + escape(product[1]) + "#-%-#" + product[2].toString() + "#-%-#" + product[3].toString();
            //console.log("writing to text ... " + product[0].toString() + ": " + unescape(product[1]));
        }
        //console.log("END TO TEXT");
        return output;
    },
    
    loadFromText: function(input) {
        this.products = new Array();
        this.numberOfProducts = 0;
        var inputProducts = input.split("&-%-&");
        for(i=0;i<inputProducts.length;i++)
        {   product = inputProducts[i].split("#-%-#");
            if(product[0] != "")
            {   //console.log("loading from text ... " + product[0] + ": " + unescape(product[1]));
                this.restoreProduct(parseInt(product[0]),unescape(product[1]),parseFloat(product[2]),parseInt(product[3]));
            }
        }
    }
    
}
