/**
 * @fileoverview Venda.Widget.RecentSearchItems - cookie based recent search items.
 *
 * This widget enables us to set a cookie each time a user searches from the keyword search field.  
 * Adapted from RecentlyViewedItems
 * @author Dan Brook <dbrook@venda.com> and Nabi Arshad <narshad@venda.com>
 */

//create namespace
Venda.namespace("Widget.RecentSearchItems"); 

/**
 * Stub function is used to support JSDoc.
 * @class Venda.Widget.RecentSearchItems
 * @constructor
 */
Venda.Widget.RecentSearchItems = function (){};

//declare constants
RSI_SEPARATOR   = ',';
RSI_COOKIE_NAME = 'RSI';

//instantiate cookiejar, set the expiry time of the cookie
var cj = new CookieJar({expires: 3600 * 24, path: '/'});

//declare object property
Venda.Widget.RecentSearchItems.check = 1;

/**
 * read the cookie and split using the "RSI_SEPARATOR" constant 
 * @param {String} rsiCookie the string being passed
 */
Venda.Widget.RecentSearchItems.getRSItems = function (rsiCookie) {
  return rsiCookie.split(RSI_SEPARATOR);
};

/**
 * @requires CookieJar			/venda-support/js/external/cookiejar.js
 * 
 * @param {String} item get the item name (keyword search term)
 * @param {String} cj to instantiate cookieJar to set cookie
 * @param {String} numberofrsi the amount of rsi to be displayed in the cookie
 * @param {String} searchurl the long searchurl generated by <venda_searchurl> 
 */

Venda.Widget.RecentSearchItems.saveRSItems = function(item, cj, numberofrsi,searchurl) {
  var cookieVal = '';
  var rsiCookie = cj.get(RSI_COOKIE_NAME);
  if(rsiCookie) {
   var rsi = Venda.Widget.RecentSearchItems.getRSItems(rsiCookie);
    // Don't need to set the cookie if this item is already in the list.
    for(var i = 0; i < rsi.length; i++)
     if(rsi[i] == item)
      return;
    rsi.unshift(item);
    cookieVal += rsi.slice(0,numberofrsi).join(RSI_SEPARATOR);
  } else
    cookieVal += item;
  return cj.put(RSI_COOKIE_NAME, cookieVal);
};

/**
 * only take the keyword search term from the search url so 
 * we are able to use this as the link text.
 * @param {String} url the value of the item (cookie value)
 */

Venda.Widget.RecentSearchItems.extractKeywordFromURL = function(url) {
  var match = url.match(/termtextkeywordsearch=([^&]+)&\w+=/);
		if(!match)
		return '';
		return unescape(match[1]);
};

/**
 * set the cookie when a user lands on the search results page.
 * this funtion will be called on the search results template to check:
 *
 * a) if the cookie is set.
 * b) if its set then show the items by writing an ordered list to the page
 * c) if the cookie is not set then call Venda.Widget.RecentSearchItems.saveRSItems 
 * to set  or update the cookie.
 
 * @param {String} searchurl  search url value being passed from the element template
 * @param {String} numberofrsi the amount of rsi to be displayed (element extended field for RecentSearch Element)
 */

Venda.Widget.RecentSearchItems.setRSItems = function (searchurl, numberofrsi) {
  var rsiCookie = cj.get(RSI_COOKIE_NAME);
   if(rsiCookie) {
    document.getElementById('showRSI').style.display="block";
    var rsiList = document.getElementById('rsilist');
    var rsi     = Venda.Widget.RecentSearchItems.getRSItems(rsiCookie);
    // Setup the recently searched items list.
	  for(var i = 0; i < rsi.length; i++) {
	   var item = rsi[i];
		 if (Venda.Widget.RecentSearchItems.extractKeywordFromURL(item) !== "") {
	   var li = document.createElement('li');
	   	li.id = 'rsiItem-'+i;
	   	rsiList.appendChild(li);
		 	li.innerHTML = '<a href="'+ item +'">' + Venda.Widget.RecentSearchItems.extractKeywordFromURL(item) +'</a>';
	   }
	  }
	 }
/**
 * call this to set / update the cookie 
 */
  Venda.Widget.RecentSearchItems.saveRSItems(searchurl, cj, numberofrsi);
};

/**
 * show the items side wide, without setting a value in the cookie.
 * it will only read the cookie and show the values in an ordered list.

 * @param {String} cj to instantiate cookiejar
 */
Venda.Widget.RecentSearchItems.ShowRSItems = function (cj) {
 var rsiCookie = cj.get(RSI_COOKIE_NAME);
  if(rsiCookie) {
    document.getElementById('showRSI').style.display="block";
    var rsiList = document.getElementById('rsilist');
    var rsi     = Venda.Widget.RecentSearchItems.getRSItems(rsiCookie);
		// Setup the recently searched items list.
    for(var i = 0; i < rsi.length; i++) {
    var item = rsi[i];
		if (Venda.Widget.RecentSearchItems.extractKeywordFromURL(item) !== "") {
    var li = document.createElement('li');
    li.id = 'rsiItem-'+i;
    rsiList.appendChild(li);
			li.innerHTML = '<a href="'+ item +'">' + Venda.Widget.RecentSearchItems.extractKeywordFromURL(item) +'</a>';
	   }
		}
  }
};

/**
 * remove the cookie entirely and hide div containing ordered list. 
 
 * @param {String} id the css id of the div containing the ordered list
 * @param {String} cookiename the cookie to be deleted
 */
 
Venda.Widget.RecentSearchItems.RemoveAll = function (id,cookiename){
 cj.remove(cookiename);
 document.getElementById(id).style.display="none";
};
