/* ------------------------ Cookie actions ----------------------*/
function QuickList() {
	// Init
	this.days = 365;
	
	// get all existing id's from cookie
	this.getAll=function () {
		return readCookie('vidList');
	}

	this.countAll=function() {
		var counter = this.getAll();
		if (!counter)
			counter = 0;
		else 
			counter = counter.split(" ").length;
		return counter;
	};
	
	this.escapeChars=function(str) {
		str = str.replace(/\*/g, "&#42;");
		return str;
	}
	
	this.cookieMaxSize=function() {
		var MAX=4000; // making this 4005 will mean it will not be stored off... (too big)
		if (document.cookie.length > MAX) {
			alert('רשימת צפייה מהירה הינה מלאה יש לרוקן פריטים ולנסות שוב');
			return false;
		}
		return true;
	}
	
	
	// add item
	this.addItem=function(id,Ttl,img,dur,url) {
		if (this.cookieMaxSize()) {
			Ttl = this.escapeChars(Ttl);
			dur = this.escapeChars(dur);
			
			if(checkCookieEnabled()) {
				if(!this.isItemExist(id)) {
					this.changeItem(id,Ttl,img,dur,url,this.days);
					var vidList=(readCookie('vidList') == null)? vidList = id : vidList = readCookie('vidList') + ' ' + id;
					setCookie('vidList',vidList,this.days,'/');
				} 
			} else {
				alert('Error: Please Enable Your Cookies');
			}
		}
	};

	// get specific item (id,title,imgPath,duration) by id
    this.getItem=function(id) {
		return unescape(readCookie(id));
	};
	
	
	// remove item  by id
	this.removeItem=function (id) {
		this.changeItem(id,'null','null','null','null','');
		
		// remove from id List
		var items = this.getAll().split(" ");
		var updatedList = '';
		for (i=0;i<items.length;i++) {
			if(items[i] != id)
				updatedList += " " + items[i];
		}
		setCookie('vidList',updatedList,this.days,'/');
		if(typeof window.myHotRmv == 'function') {
			myHotRmv(id,true);
		}
	}
	
	// check if exist
	this.isItemExist=function(id) {
		var items = this.getAll();
		var regsub = "\\b"+id+"\\b";
		if (items == null) {
			return false;
		} else {
			var flag=(items.match(regsub) == null) ? false : true;
			return flag;
		}
	};
	
	this.saveToServer=function(userid) {
		// ajax
	};

	//add_remove (val days) actions
	this.changeItem=function(vidId,Ttl,img,dur,url,days) {
		var cookieVals = vidId + "*" + Ttl + "*" + img + "*" + dur + "*";
		setCookie(vidId,escape(cookieVals),days,'');
	}
};


