/* ############ recently viewed items ########### */
var name = "recentItems"; //cookieName
var days = 7; // days before expiration
var newString = "";
var num = 3; // number of items to display
var unique ="yes"; //
var recentStyles='<style>#recentItemHeader {padding:3px 5px; margin:10px 0 1px 0; background:#F7D993; color:#5A2672; font-weight:bold;} .recentItem {float:left; width:187px; max-width:175px; height:219px; border:1px #999 solid; padding:5px; margin:0 5px 5px 0; text-align:center;} .recentImg img {border:0;} .recentName {font-weight:bold; margin:5px 0;} .recentPrice {} .recentSprice {} #recentItem2 {margin:0 0 5px 0;}</style>';

var preCookie = readCookie(name);

// append new data to cookie.
function setCookie(){
	if (!curString) return;
	var s = curString.replace("^","");
	if (!preCookie || preCookie == ""){
	// if cookie does not exist, create it with the current item's data
		newString = curString;
	} else { 
	// If cookie exists, split and compare its content to current item 
		var eachItem = preCookie.split("^");
		for (i = 0; i < num; i++){
			if (eachItem[i] == s ) { 
			unique = "no";
			} 	
		} if (unique == "yes"){
		// if current data is unique, add it to cookie
		  var x = curString + preCookie;
		  var a = x.split("^");	
		  for (i=0;i<num + 1;i++){
		  	newString+=a[i] + "^";
		  }
		  } else {
		// if current data is not unique, do not add it to cookie
		  newString = preCookie;
		  }
	}  
	createCookie(name,newString,days)
}

//splitting and writing cookie data
function writeRecent(){
	if (preCookie && preCookie != ""){
		var eachItem = preCookie.split("^");
		if (!eachItem || eachItem.length == 0) return;
		var firstElement = eachItem[0].split("|");	
		if (firstElement[0] == prodID && eachItem.length == 2) return;
		var i2 = 0;
    	document.write('<div class="contents-title" id="recentlyviewed">');
    	document.write('<div id="recentItemHeader">Recently Viewed Items</div>');
		for (i = 0; i < num || i2 < num; i++){
			//only write item if it exists, is not empty and is unique
			if (eachItem[i] && eachItem[i] != "" && eachItem[i] != "undefined"){
				var eachElement = eachItem[i].split("|");	
				if (eachElement[0] != prodID){
					document.write('<div id="recentItem' + i2 + '"' + '  class="recentItem"> ');
					document.write('<div class="recentImg"><a href="' + eachElement[0] + '.html"><img src="' + eachElement[1] + '" border="0" /></a></div>');
					document.write('<div class="recentName"><a href="' + eachElement[0] + '.html">' + eachElement[2] + '</a></div>');
					if (eachElement[3] && eachElement[3] != ""){
						document.write('<div class="recentPrice">Retail Price: ' + eachElement[3] + '</div>');
						}
					if (eachElement[4] && eachElement[4] != ""){
						document.write('<div class="recentSprice">Your Price: ' + eachElement[4] + '</div>');
						}
					document.write('</div>');
					i2 = i2 + 1; 
				}
			} else { i2 = i; } // don't increment counter is the current page is already displayed
		}
		
    	document.write('</div><br clear="all">');
		document.write(recentStyles);
	}	 
}
/*######## end recently viewed*/
