// JavaScript Document

//////////////////////
// Cookie Functions //
//////////////////////

function getCompareCookieArray(c_name){ // if blank, returnes empty array, else returnes Array split by "|"
	if (document.cookie.length>0)	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)	{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return (document.cookie.substring(c_start,c_end)=="" || document.cookie.substring(c_start,c_end)=="%3D") ? Array() : unescape(document.cookie.substring(c_start,c_end)).split("|");// ignore "=" if that's all there is in the cookie
		} 
	}
	return Array();
}

function setCompareCookieArray(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";
}

//////////////////////////
// End Cookie Functions //
//////////////////////////



var aCompareList = getCompareCookieArray("compare");

function updateCompareSpan() {// Update the cookie and the span where the number of watches in the compare list is displayed
	var o = document.getElementById("compare_link");
	setCompareCookieArray("compare",aCompareList.join("|"),365);
	aCompareList = getCompareCookieArray("compare");
	if (o) {
		if (aCompareList.length >= 2) {
			o.style.display = "inline";
		} else {
			o.style.display = "none";
		}
	}
}

function updateDisplay() {// internal function run whenever changes are made
	var cItem; //cookieItem
	for (i=0;i<aCompareList.length;i++) {
		cItem = aCompareList[i].split("=");
		displayCompared(cItem[0],cItem[1],false,aCompareList.length);
	}
}

function checkCookie() {// initialize compare list. use in body onLoad()
	updateCompareSpan();
	updateDisplay();
}

function Compare(sID,spanText) {// Toggle watch in compare list
	aCompareList = getCompareCookieArray("compare");
	for(var i = 0, l = aCompareList.length; i < l; i++) {
		if(aCompareList[i] == sID + "=" + spanText) {
			aCompareList.splice(i,1);
			displayCompared(sID,spanText,true,aCompareList.length);
			updateDisplay();
			updateCompareSpan();
			return false;
		}
	}
	if (aCompareList.length < conMaxCompareItems) {
		aCompareList.push(sID + "=" + spanText);
		updateDisplay();
		updateCompareSpan();
	} else {
		alert("You may compare a maximum of "+conMaxCompareItems+" items");
	}
}

function displayCompared(sID, spanText, bRemove, nLength) {// Change text and link when a watch is toggled
	var o=document.getElementById("compare_"+sID)
	if (!o) {
	} else if (bRemove) {// state 1: unchecked. This state should match one in watch_box.asp that is displayed by default
		o.className = "compare_link"; // JJ, use for styling. please rename as needed.
		o.innerHTML = "<a href='javascript:void(null);' onclick=\"Compare('"+ sID +"','"+ spanText +"');return false;\"><span>Add to compare</span></a>";
	} else if (nLength < 2) {// state 2: checked(single)
		o.className = "compare_link_single"; // JJ, use for styling. please rename as needed.
		o.innerHTML = "<a href='javascript:void(null);' onclick=\"Compare('"+ sID +"','"+ spanText +"');return false;\"><span style='width:50px;'>Add another item to compare</span></a>";
	} else if (nLength > 1) {// state 3: checked(multiple)
		o.className = "compare_link_added"; // JJ, use for styling. please rename as needed.
		o.innerHTML = "<a href='javascript:void(null);' onclick=\"Compare('"+ sID +"','"+ spanText +"');return false;\" style=\"width:5px\" >&nbsp;</a><a href='javascript:void(null);' onClick='compareLink();' class=\"noBg\" style=\"background-image:none;\"><span>View Side by Side</span></a>";
	}
}

function compareLink(bRedirect) {// for linking to the compare page
	if (typeof bRedirect == "undefined") bRedirect = true;
	var sQs = "";
	var cItem; //cookieItem

	aCompareList = getCompareCookieArray("compare");
	for(var i = 0, l = aCompareList.length; i < l; i++) {
		cItem = aCompareList[i].split("=");
		sQs += "&w=" + cItem[0];
	}
	if (bRedirect) location.href = sBase + "/comparison.asp?a=compare" + sQs;
}

function removeCompare(sID,bRedirect) {// Remove watch from compare page
	if (typeof bRedirect == "undefined") bRedirect = true;
	aCompareList = getCompareCookieArray("compare");
	for(var i = 0, l = aCompareList.length; i < l; i++) {
		if(aCompareList[i].indexOf(sID + "=")>-1) {
			aCompareList.splice(i,1);
			setCompareCookieArray("compare",aCompareList.join("|"),365);
			aCompareList = getCompareCookieArray("compare");
			compareLink(bRedirect);
			return;
		}
	}
}