Array.prototype.inArray = function(value) { var i; for ( i = 0; i < this.length; i++ ) { if ( this[i] == value ) { return true; } } return false; };
Array.prototype.removeItems = function(itemsToRemove) { if (!/Array/.test(itemsToRemove.constructor)) { itemsToRemove = [ itemsToRemove ]; } var j; for ( var i = 0; i < itemsToRemove.length; i++ ) { j = 0; while ( j < this.length) { if (this[j] == itemsToRemove[i]) { this.splice(j, 1); } else { j++; } } } }

// PPK Cookie Functions - http://www.quirksmode.org/js/cookies.html
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+"="+value+expires+"; path=/"; }
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 c.substring(nameEQ.length,c.length); } return null; }
function deleteCookie(name) { createCookie(name,"",-1); }


function addRecentlyViewedProduct(id) {
	
	// Name the cookie!
	var cookie_name = 'its-recently-viewed-products';
	
	// How many product should we keep in history?
	var limit = 3;
	
	// Read the cookie and reverse the order of the contents...
	var recent = readCookie(cookie_name) ? readCookie(cookie_name).split('-').reverse() : [];
	
	// Remove the id from the array if it exists...
	if( recent.inArray(id) ) { recent.removeItems(id); }
	
	// Add the id to the array...
	recent.push(id);
	
	// Reverse it...
	recent.reverse();
	
	// Check the limits...
	recent = recent.length > limit ? recent.splice(0, limit) : recent;
	
	// Write the cookie...
	createCookie( cookie_name, recent.join('-'), 365 );
	
		
}



