var varId = "";

var recentlyViewedGadget = new fluid.retail.merchandiser.util.RecentlyViewedGadget();	
recentlyViewedGadget.cookieId = "recently_viewed_gadget"; // any unique identifier for this setup
recentlyViewedGadget.sessionLength = 60 * 24 * 30; 

var itemsXmlGadget = recentlyViewedGadget.getGadgetItemsXml();
var configGadget = { dataXml: itemsXmlGadget };

fluid.defineConfiguration("merchandiser:59:Gadget_Template", configGadget);

// Sets varId to keep track of currnet variation to be passed when a product is shared w/ iGoogle
function setVariation(variationId)
{
	varId = variationId;
}
// Create the handlers for the events
function handleDisplayLoaded(event)
{

}
         
function handleDisplayChanged(event)
{
    var display = event.invoker;
	
	//only display items that you've viewed product displays pages for
	if(display.displayId == "main")
	{
		var uniqueId = event.productId + "_" + event.variationId;
	    var itemId = uniqueId + ":2:" + event.productId + ":" + event.variationId;
	    // first thing we want to do is remove any item with the same product id
	 	var itemsGadget = recentlyViewedGadget.getGadgetItems();
	    var item;
	    var productId;
	    var variationId;
		
		//check for duplicates before adding another product 
	    for(var i = 0; i < itemsGadget.length; i++)
	    {
	        item = itemsGadget[i];
	        productId = item.id.split(":")[2];
			
			//removes duplicate cookie values for productIds
	        if(productId == event.productId && itemId != item.id)
	        {
				recentlyViewedGadget.removeItem(item.id);
	        }//if
	    }//for

		var baseUrl = "http://www.jansport.com/catalog/conceptretail/customers/c";
		//retrieves the name from the server
		var productName = getName();
		
		//format the customSwf URL 
		var customSWFContainerURL = "http://www.jansport.com/gadget/containers/MerchandiserImageTextContainer.swf";
		// Add the custom SWF
		var itemUrl = customSWFContainerURL + "?";
		// Add the Product ID
		itemUrl += "productId=" + event.productId;
		// Add the Variation ID
		itemUrl += "&amp;variationId=" + event.variationId;
		// Add the Product Name
		itemUrl += "&amp;productName=" + productName;
		 // Add the Text Color
		itemUrl += "&amp;txtColor=" + '0x221d3d';
		// Add the baseURL to the Image
		itemUrl += "&amp;imageURL=" + baseUrl + display.customerId + "/" + event.productId + "/generated/";
		//add the imageURL
		itemUrl +=  event.productId + "_" + event.variationId + "_" + event.viewId + "_255x265.jpg";

		//add the new item to the cookie
		recentlyViewedGadget.addGadgetItem(itemId, itemUrl);
		
		//set variationId to be passed for iGoogle share button to use
		setVariation(event.variationId);
	}//if
}//handleDisplayChanged()
            
// Must wait for the load event to ensure the PMR is ready 
// to go
function handleWindowLoaded(event)
{
    // Get the PMR
    var pmr = fluid.application;
    // Add the event listeners. Note the fluid.retail.display.events.DisplayEvent 
    // class must be included with the Fluid Core in order to directly reference 
    // it here. You can skip the include and simply use the string value of the
    // constant if desired.
                
    var changeType = fluid.retail.display.events.DisplayEvent.CHANGE;
    pmr.addEventListener(changeType, this, this.handleDisplayChanged);
                
    // Because the onload has been overridden the PMR's onload handler
    // must be explicitely called
    pmr.handleWindowLoaded(event);
}
            
function clearItems()
{
    recentlyViewed.clearItems();
}
