// JavaScript Document

SitePrefix = "http://www.hatcraze.com/";  // Can switch to a / when on my site
ProdImagesPrefix = "/proimages/";
CookiePrefix = 'hc';  // The cookie name, followed by a number 0 to StackSize -1
StackSize = 6;  // Number of cookies to hold. 


// Given a current page item number and category, push onto the 'cookie list'
//   and display the most recently viewed items.
function displayRecentlyViewed()
{
	//document.write('Hey list is here:<BR>');
	displayRecentlyViewed_Vertical();
	//displayRecentlyViewed_Horizontal();
}

// Dumps a vertical view in HTML of the most recently viewed from the cookies
function displayRecentlyViewed_Vertical()
{
	//document.write("display vertical here:<BR>");
	// dumpCookieStack();
	
	// Get all the cookies into variables....
	var hcpArray = new Array(StackSize);
	var i;
	
	// Pull all the cookies into an array (don't need the #0 element)
	for (i=1; i<StackSize; i++) {
		hcpArray[i]=getCookie(CookiePrefix + i);
	}
	
	document.write('<TABLE border=0 width="100%">');
	var nextrow = "";
	
	for (i=0; i<StackSize; i++) {
		// Get the cookie.....
		var thisValue = getCookie(CookiePrefix + i);
		
		// If null or undefined, break out.
		if (thisValue=="undefined" || thisValue==null)
			break;
		
		var tmpArray = new Array();
		tmpArray = thisValue.split('|');
		var itemNumber=tmpArray[0];
		var prodCat =tmpArray[1];
		var prodName =tmpArray[2];
		
		if (itemNumber=='undefined' || itemNumber=='' || prodCat=='undefined' || prodCat=='')
			continue;
			
		var linkStart = '<a href="' +SitePrefix + tmpArray[1] + '/' + itemNumber+ '.html" class="recentlyViewed" >';
		var incelldata = linkStart + '<img src="' +SitePrefix + ProdImagesPrefix  + '/' + itemNumber + '-icon.jpg" width=60 border=0>';
		
		// Write the current cell
		document.write('<TR><TD valign="bottom" align="center" style="padding-bottom:15px">' + incelldata + '<BR>');
		
		// and save of the next row's matching cell
		document.write(prodName + '</a></TD></TR>');
	}
	document.write('</TABLE>');
}


// Given a current page item number and category, push onto the 'cookie list'
//   and display the most recently viewed items.
function addToRecentlyViewed( itemnumber, category, productName )
{
	fixUpCookieList( itemnumber, category, productName);  // Push onto list
}

// Given a current product/category/name, fix up our virtual cookie list
function fixUpCookieList( itemnumber, category, productName )
{
	//document.write("TRACE: fixUpCookieList: itemnumber is " +itemnumber + "<BR>");
	
	// Get all the cookies into variables....
	var hcpArray = new Array(StackSize);
	var i;
	
	// Pull all the cookies into an array
	for (i=0; i<StackSize; i++) {
		hcpArray[i]=getCookie(CookiePrefix + i);
	}
	
	// If the first item in the array matches our current item number....
	var fullCookie = itemnumber + "|" + category + "|" + productName;
	if (hcpArray[0] == fullCookie) {
		// document.write("TRACE: Cookie 0 (" + CookiePrefix + "0) " +hcpArray[0]+ " is the SAME as itemnumber " +itemnumber + ", RETURNING<BR>");
		// we're done.  Leave the cookie list as-is.
		return;
	}
	//document.write("TRACE: Cookie 0 '" + hcpArray[0] + "' is definitely NOT the same as itemnumber '" +itemnumber + "'<BR>");
	
	// Not the same page, so set it at the head of the virtual list.
	setCookie(CookiePrefix + "0", fullCookie);  // set 0th cookie with the new info
	
	// Set the rest of the cookies - shifting down a position
	for (i=0; i<StackSize; i++) {
		var nextnum=i+1;
		if (hcpArray[i] == fullCookie) 
			break;   // If we get to one matching item number, don't shift down any more values
		setCookie(CookiePrefix + nextnum, hcpArray[i]);
	}
}

// Dumps a horizontal view in HTML of the most recently viewed from the cookies
function displayRecentlyViewed_Horizontal()
{
	document.write("<b>displayRecentlyViewed_Horizontal: This will be our recently viewed HTML</b><BR>");
	
	// Dump our cookie stack as a debug....
	dumpCookieStack();
	
	// Get all the cookies into variables....
	var hcpArray = new Array(StackSize);
	var i;
	
	// Pull all the cookies into an array (don't need the #0 element)
	for (i=1; i<StackSize; i++) {
		hcpArray[i]=getCookie(CookiePrefix + i);
	}
	
	document.write('<TABLE border=0 width="100%"><TR>');
	var nextrow = "";
	
	for (i=1; i<StackSize; i++) {
		// Get the cookie.....
		var thisValue = getCookie(CookiePrefix + i);
		
		// If null or undefined, break out.
		if (thisValue=="undefined" || thisValue==null)
			break;
		
		var tmpArray = new Array();
		tmpArray = thisValue.split('|');
		
		var incelldata = '<a href="' +SitePrefix + tmpArray[1] + '/' + tmpArray[0]+ '.html"><img src="' +SitePrefix + ProdImagesPrefix  +  tmpArray[0] + '-icon.jpg" width=100></a>';
		
		// Write the current cell
		document.write('<TD valign="bottom" align="center">' + incelldata + '</TD>');
		
		// and save of the next row's matching cell
		nextrow+= '<TD valign="top" align="center"> item#: ' + tmpArray[0] + '</TD>';
	}
	document.write("</TR> <TR>" + nextrow + "</TR></TABLE>");
}



function dumpCookieStack()
{
	var myArray = new Array(StackSize);
	
	document.write("<BR>TRACE: dumpCookieStack:<BR>=================<BR> <UL>");
	for (i=0; i<StackSize; i++) {
		myArray[i] = getCookie(CookiePrefix + i);
		document.write("<LI>TRACE: Cookie " + i + " is " + myArray[i] +"</LI>");
	}
	document.write("</UL>");
}


// ////////////////////////////////////////////
//  Basic setCookie and getCookie functions
// ////////////////////////////////////////////
function setCookie(name, value)
{
	var today = new Date()
	var expires = new Date()
	
	//document.write("<BR>TRACE: setCookie( name='"+name +"', value='" +value+ "')<BR>");
	
	expires.setTime(today.getTime() + 1000*60*60*24*30) // expires in a month
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +";path=/"
}

function getCookie(Name) {
	var search = Name + "="
	if(document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		if(offset != -1) {
			offset += search.length
			end = document.cookie.indexOf(";", offset)
			if(end == -1) 
				end = document.cookie.length
			return unescape(document.cookie.substring(offset, end))
		}
		else return ""
	}
}