var myArray = [];
var RVItem = null;

function buildArray(name, url){
    
    var cookie = $.cookie('vwd-breadcrumb-cookie');
	// set the cookie information
	if (cookie){
		RVItem = {	'itemName': name.toString(), 
					'itemURL': url.toString()
				 };
		//store the newly gathered product name and product url into an JSON object.		 
		var tempArray = JSON.parse(cookie);
		//parse the string array into an Object array to work
		var tempItem;
		// check the array if the same product name has already been seen, 
		// take it out of the array and put it into the back of the line
		for(var i = 0; i < tempArray.recentlyViewed.length; i++){
			if(tempArray.recentlyViewed[i].itemName == RVItem['itemName']){
				tempItem = tempArray.recentlyViewed.splice(i,1);
			}
		}
		//add the new item into the array, make into a string and save it to the cookie
		tempArray.recentlyViewed.push(RVItem);
		builtStringArray = JSON.stringify(tempArray);
		$.cookie('vwd-breadcrumb-cookie', builtStringArray);
	}else{
	  RVItem = 	{ 'recentlyViewed': [
					{'itemName': name.toString(), 
					'itemURL': url.toString()}
				  ]
				};
		//myArray[0] = RVItem;
		builtStringArray = JSON.stringify(RVItem);
		$.cookie('vwd-breadcrumb-cookie', builtStringArray);
	}
}
function clearCookie(info){
	$.cookie('vwd-clear-cookie', info);
}
$(document).ready(function() {
	var myClearCookie = $.cookie('vwd-clear-cookie');
  var myCookie = $.cookie('vwd-breadcrumb-cookie');
	var tempArray = null;
	if(myCookie){
		tempArray = JSON.parse(myCookie);
	}
	var newArray = [];
	var numToShow = 4;
	var display = "";
    if(myCookie && tempArray.recentlyViewed.length >= 1){
		// limit the amount of links being shown onto the page. 
			if(tempArray.recentlyViewed.length > numToShow){
				display = "<a href='index.html'>Home</a>";
				newArray = tempArray.recentlyViewed.slice((tempArray.recentlyViewed.length - numToShow), tempArray.recentlyViewed.length);
				for(var i = 0; i < numToShow; i++){
						if(i == (newArray.length - 1)){
							display += "<span>" + newArray[i].itemName + "</span>";
						}else{
							display += "<a href='" + newArray[i].itemURL +"'>" + newArray[i].itemName + "</a>";
						}
				}
			}else{
				display = "<a href='index.html'>Home</a> ";
				for(var i = 0; i < tempArray.recentlyViewed.length; i++){
						if(i == (tempArray.recentlyViewed.length - 1)){
							display += "<span>" + tempArray.recentlyViewed[i].itemName + "</span>";
						}else{
							display += "<a href='" + tempArray.recentlyViewed[i].itemURL +"'>" + tempArray.recentlyViewed[i].itemName + "</a>";
						}
				}
			}
      $('.vwd-breadcrumbs').html(display);

    }
		if(myClearCookie){
			$.cookie('vwd-breadcrumb-cookie', null);
			$.cookie('vwd-clear-cookie', null);
		}
});

function debug(){
    if(window.console && console.log)
      for(var i = 0; i < arguments.length; i++){
        //console.dir(arguments[i]);
        //console.log(typeof(arguments[i]));
        console.log("[jquery.cookie]: " + arguments[i]);
      }
  }

