/* ############ recently viewed items ########### */
var name = "recentItems"; //cookieName
var days = 7; // days before expiration
var newString = "";
var num = 5; // number of items to display
var unique ="yes"; //

// create cookie assigning name, value and expiration days
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+escape(value)+expires+"; path=/";
}

// read cookie by name and list value
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
	}
	return null;
}

// delete cookie by name
function eraseCookie(name) {
	createCookie(name,"",-1);
}

var preCookie = readCookie(name);

// append new data to cookie.
function setCookie(curString){
	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;
		  }
	} 
	// newString = escape(newString);
	createCookie(name,newString,days)
}

//splitting and writing cookie data
function writeRecent(){
	if (preCookie && preCookie != ""){
	   preCookie = unescape(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"></div>');
  	var rec = $('recentlyviewed');
  	rec.appendChild(new Element('div', {
  	  'id':'recentItemHeader',
  	  'html':'&nbsp;'+'Recently Viewed Items'
  	})).appendChild(new Element('img', {
      'src':'/lib/yhst-16674707432794/recent-bar.gif'
    }));		
		
		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){
				  var thisItem = rec.appendChild(new Element('div', {
				    'id':'recentItem'+i2,
				    'class':'recentItem'
				  }));
  					var thisDiv = thisItem.appendChild(new Element('div', {'class':'recentImg'}));
  					  var thisA = thisDiv.appendChild(new Element('a', {'href':eachElement[0]+'.html'}));
    					  thisA.appendChild(new Element('img', {
    					    'src':eachElement[1],
    					    'border':'0'
    					  }));
            var thisDiv = thisItem.appendChild(new Element('div', {'class':'recentName'}));
              thisDiv.appendChild(new Element('a', {
                'href':eachElement[0]+'.html',
                'html':eachElement[2]
              }));  				
            					
  					if (eachElement[3] && eachElement[3] != ""){
  						thisItem.appendChild(new Element('div', {
  						  'class':'recentPrice',
  						  'html':'Retail Price: '+eachElement[3]
  						}));
  					}
  					if (eachElement[4] && eachElement[4] != ""){
  						thisItem.appendChild(new Element('div', {
  						  'class':'recentSprice',
  						  'html':'Your Price: '+eachElement[4]
  						}));
  					}
					i2 = i2 + 1; 
				}
			} else { i2 = i; } // don't increment counter is the current page is already displayed
		}
		
	}	 
}
/*######## end recently viewed*/

