var currentArticle;

function Cart(fromJSON) {
	this.articles=[];
	
	this.getArticle=function(index) {
		return this.articles[index];
	};
	
	this.addArticle=function(id, title, size, sizeTitle, price, ingredientGroup, fromJSON) {
		var a=new Article(id, title, size, sizeTitle, price, ingredientGroup, fromJSON);
		this.articles.push(a);
		if (!fromJSON) {
			this.processChanges();
		}
		return a;
	};
	
	this.removeArticle=function(index) {
		this.articles.splice(index, 1);
		this.processChanges();
	};	
	
	this.incArticleCount=function(index) {
		if (this.articles[index]) {
			this.articles[index].count++;
		}
		this.processChanges();
	};	

	this.decArticleCount=function(index) {
		if (this.articles[index]) {
			if (this.articles[index].count<=1) {
				this.removeArticle(index);
			} else {
				this.articles[index].count--;
			}
		}
		this.processChanges();
	};	

	this.processChanges=function() {
		$.cookie("singletonCart", JSON.stringify(singletonCart));
		jQuery(this).trigger("change");
	};
	
	this.getArticleCount=function() {
		var count=0;
		for(var i in this.articles) {
			var a=this.articles[i];
			if (a) {
				count+=a.count;
			}
		}
		return count;
	};
	
	this.clean=function() {
		this.articles=[];
	};

	if (fromJSON) {
		for(var i in fromJSON.articles) {
			var a=fromJSON.articles[i];
			if (a) {
				this.addArticle(a.id, a.title, a.size, a.sizeTitle, a.price, a.ingredientGroup, a);
			}
		}
	}
}

var cCart=$.cookie("singletonCart");
var singletonCart;
if (cCart) {
	singletonCart=new Cart(JSON.parse(cCart));
} else {
	singletonCart=new Cart();
}
jQuery(function() {singletonCart.processChanges();});

function Article(id, title, size, sizeTitle, price, ingredientGroup, fromJSON) {
	this.id=id;
	this.title=title;
	this.size=size;
	this.sizeTitle=sizeTitle;
	this.price=price;
	this.count=1;
	this.ingredientGroup=ingredientGroup;
	
	this.ingredients=[];
	
	this.addIngredient=function(ingId, ingTitle, ingPrice, fromJSON) {
		if (this.ingredients[ingId]) {
			this.ingredients[ingId].count++;
		} else {
			this.ingredients[ingId]=new Ingredient(ingId, ingTitle, ingPrice);
		}
		if (!fromJSON) {
			singletonCart.processChanges();
		}
	};

	this.removeIngredient=function(ingId) {
		if (this.ingredients[ingId] && this.ingredients[ingId].count>1) {
			this.ingredients[ingId].count--;
		} else {
			delete this.ingredients[ingId];
		}
		singletonCart.processChanges();
	};

	this.getIngredientCount=function() {
		var count=0;
		for(var i in this.ingredients) {
			var a=this.ingredients[i];
			if (a) {
				count+=a.count;
			}
		}
		return count;
	};

	if (fromJSON) {
		this.count=fromJSON.count;
		for(var i in fromJSON.ingredients) {
			var a=fromJSON.ingredients[i];
			if (a) {
				this.addIngredient(a.id, a.title, a.price, a.count, a);
			}
		}
	}	
}

function Ingredient(id, title, price, count) {
	this.id=id;
	this.title=title;
	this.price=price;
	this.count=count?count:1;
}

function formatPrice(price) {
    var f="";
    var d="00";
    price=(Math.round(price*100)/100)+"";
    if (price.indexOf(".")>=0) {
      d=price.substr(price.indexOf(".")+1);
      if (d.length==1) { d+="0"; }
      price=price.substr(0,price.indexOf("."));
    }
    while(price.length>0) {
      var s=Math.min(3,price.length);
      if (f.length>0) {
        f="."+f;
      }
      f=price.substr(price.length-s,3)+f;
      price=price.substr(0, price.length-s);
    }
    return f+","+d;
}
