/**
 * @fileoverview Venda.Widget.RecentlyViewedItems - cookie based recently viewed items.
 *
 * This widget enables us to set a cookie each time a user lands on the product detail.  
 * The script also uses the ajax function to write items to the page.
 * @author Dan Brook <dbrook@venda.com>
 */

//create namespace
var rviNs = Venda.namespace("Widget.RecentlyViewedItems");

//declare constants here
rviNs.RVI_SEPARATOR   = ',';
rviNs.RVI_COOKIE_NAME = 'RVI';

//declare object property
rviNs.check = 1;

/**
 * calls the cookie and then splits it to be displayed in the saveRecentlyViewedItems
 * @params {string} set rviCookie name and separate it using the RVI_SEPARATOR
 */
rviNs.getRecentlyViewedItems = function (rviCookie) {
  return rviCookie.split(rviNs.RVI_SEPARATOR);
}
/**
 * @constructor
 * @class get the item and set the cookie with name and value.
 * @requires Ajax									/venda-support/js/Ajax.js
 * @requires CookieJar						/venda-support/js/external/cookiejar.js
 * @requires YAHOO.utils.yahoo		/venda-support/js/external/yui/build/yahoo/yahoo.js
 * @requires YAHOO.util.Event			/venda-support/js/external/yui/build/event/event.js
 * @requires YAHOO.util.DOM				/venda-support/js/external/yui/build/dom/dom.js
 * @requires YAHOO.util.Animation	/venda-support/js/external/yui/build/animation/animation.js
 * @requires connection-min				/venda-support/js/external/yui/build/connection/connection-min.js
 * @param {string} get the item name (product sku)
 * @param {string} cj is the instance cookieJar to set cookie
 * @param {string} numberofrvi is the amount of rvi to be displayed in the cookie
 */
rviNs.saveRecentlyViewedItems = function (item, cj, numberofrvi) {
  var cookieVal = '';
  var rviCookie = cj.get(rviNs.RVI_COOKIE_NAME);
		var cj = new CookieJar({expires: 3600 * 24 * 7, path: '/'});
  if(rviCookie) {
    var rvi = rviNs.getRecentlyViewedItems(rviCookie);
    // Don't need to set the cookie if this item is already in the list.
    for(var i = 0; i < rvi.length; i++)
      if(rvi[i] == item)
        return;
    rvi.unshift(item);
    cookieVal += rvi.slice(0,numberofrvi).join(rviNs.RVI_SEPARATOR);
  } else
    cookieVal += item;
		/**
		 * update the cookie
		 */
  return cj.put(rviNs.RVI_COOKIE_NAME, cookieVal);
};

/**
 * the following function is called on the product detail
 * template to set the item into the cookie
 * @param {string} invtref is the product sku reference
 * @param {string} numberofrvi are the amount of RVI's being set
 */
rviNs.setRecentlyViewedItems = function (invtref,numberofrvi) {
	var cj = new CookieJar({expires: 3600 * 24 * 7, path: '/'});
	var rviCookie = cj.get(rviNs.RVI_COOKIE_NAME);
		if(rviCookie) {
			document.getElementById('showRVI').style.display="block";
			var rviList = document.getElementById('rvilist');
			var rvi     = rviNs.getRecentlyViewedItems(rviCookie);
			/**
			 * to show the amount of RVI's in the cookie, uncomment the following:
			 	//var p = document.createElement('p');
				//set the class name for the items
				//p.className = 'RVIamnt';
				// show the amount items in the cookie.
				//p.innerHTML = rvi.length;
				//document.getElementById('showRVI').appendChild(p);
			 */
			
			// Setup the recently viewed items list.
			for(var i = 0; i < rvi.length; i++) {
				var item = rvi[i];
				var li = document.createElement('li');
				li.id = 'rviItem-'+i;
				rviList.appendChild(li);
				if (item){
				/** 
				 * write the items to the list using the ajax function
				 * @param {string} '/invt/' - static link appendment
				 * @param {string} item - the cookie value (sku)
				 * @param {string} 'temp=textrvi&layout=blank' - invt template and layout to be used
				 * @param {string} li.id - give the list item the id of 'rviItem-i' 
				 */
				 ajaxFunction('/invt/' + item + '&temp=rviproductdetail&layout=blank', li.id);
			 }
		 }
	 }
	rviNs.saveRecentlyViewedItems(invtref, cj,numberofrvi);
	// only show the right and left arrows when there are more that 3 items.
	if(document.getElementById('rvilist').childNodes.length <= 3) {
		document.getElementById('slideLeft').style.visibility  = 'hidden'; 
		document.getElementById('slideRight').style.visibility = 'hidden';
	}
};
/**
 * the following function is called on the left navigation 
 * to display the text version of the RVI.  
 */
rviNs.setRVISiteWide = function () {
	var cj = new CookieJar({expires: 3600 * 24 * 7, path: '/'});
	var rviCookie = cj.get(rviNs.RVI_COOKIE_NAME);
	if(rviCookie) {
		document.getElementById('showRVISiteWide').style.display="block";
		var rviList = document.getElementById('rvisitewidelist');
		var rvi     = rviNs.getRecentlyViewedItems(rviCookie);
		// Setup the recently viewed items list.
		for(var i = 0; i < rvi.length; i++) {
			var item = rvi[i];
			var li = document.createElement('li');
			li.id = 'rviItem-'+i;
			rviList.appendChild(li);			
			if (item){
				/** 
				 * write the items to the list using the ajax function
				 * @param {object} '/invt/' - static link appendment
				 * @param {array} item the cookie value (sku)
				 * @param {object} static setting to use invt template and layout 'temp=textrvi&layout=blank' 
				 * @param {array} li.id - give the list item the id of 'rviItem-i' 
				 */
				ajaxFunction('/invt/' + item + '&temp=rvisitewide&layout=blank', li.id);
			}
		}
	}
};
