var secra;
if(!secra) {secra = {};}
else if(typeof secra !== "object") {throw new Error("secra ist kein Objekt!");}

secra.Cart = function() {

  this.browserName = navigator.appName;
  this.isIE = this.browserName.indexOf("Microsoft Internet Explorer")==-1 ? false : true;

  this.cookiename = 'imgdb_warenkorb';

  this.callback = null;

  this.init();
};

secra.Cart.prototype.onChange = function(){
	if(typeof this.callback === "function"){
		this.callback();
	}
};

secra.Cart.prototype.init = function(){
	this.collection = new Array();
	this.load();
};

secra.Cart.prototype.addToCart = function(bildnr, size){
	if(!this.isInList(bildnr)){
		var dataset = new Object();
		dataset.bildnr = bildnr;
		dataset.size = size;
		this.collection.push(dataset);
	}
	this.onChange();
};

secra.Cart.prototype.isInList = function(bildnr){
	for(var i = 0; i < this.collection.length; i++){
		if(this.collection[i].bildnr == bildnr){
			return true;
		}
	}
	return false;
};

secra.Cart.prototype.setSize = function(bildnr, size){
	for(var i = 0; i < this.collection.length; i++){
		if(this.collection[i].bildnr == bildnr){
			this.collection[i].size = size;
			this.save();
			return true;
		}
	}
	return false;
};

secra.Cart.prototype.trim = function(txt) {
 	return txt.replace(/^\s+/, '').replace(/\s+$/, '');
};

secra.Cart.prototype.removeFromCart = function(bildnr){
	var copy = new Array();
	for(var i = 0; i < this.collection.length; i++){
		if(this.collection[i].bildnr != bildnr){
			copy.push(this.collection[i]);
		}
	}
	this.collection = copy;
	this.onChange();
};

secra.Cart.prototype.save = function(){
	var encoded = this.collection.toJSONString();
	this.writeCookie(encoded);
};

secra.Cart.prototype.load = function(){
    var encoded = this.getCookie();
    if(encoded!=''){
    	try {
		    this.collection = encoded.parseJSON();
    	} catch(ex) {
    		this.collection = new Array();		
    	}
    }
    this.onChange();
};

secra.Cart.prototype.getCookie = function(){
	 var cookieval = document.cookie;
	 var ret, cname, cval, i;
	 
	 while(cookieval != ''){

	  cname = cookieval.substring(0,cookieval.search('='));
	  cval = cookieval.substring(cookieval.search('=')+1,cookieval.search(';'));

	  if(cookieval.search(';') == -1){
	  	  cval = cookieval.substring(cookieval.search('=')+1,cookieval.length);
	  }

	  if(cname==this.cookiename){
	  	return unescape(cval);
	  }
	
	  i = cookieval.search(';')+1;
	  if(i==0){
	  	i = cookieval.length;
	  }
	  cookieval = this.trim(cookieval.substring(i,cookieval.length));
	 }
	 return '';		
};

secra.Cart.prototype.getCount = function(){
	return this.collection.length;
};

secra.Cart.prototype.writeCookie = function(cookiestr){
	 var datum = new Date();
	 datum = new Date(datum.getTime() +1000*60*60*24*365); // 1 Jahr
	 document.cookie = this.cookiename + '=' + escape(cookiestr) + '; expires='+datum.toGMTString()+'; path=/;';//domain='+document.domain; 	
};

secra.Cart.prototype.clear = function(){
	this.collection = new Array();
	this.save();
	this.onChange();
};
