/**
	Original author : Paul Sowden
    Website: http://www.alistapart.com/articles/alternate 
*/

//create namespace
Venda.namespace('Widget.gridlistSwitcher');

/**
 * Stub function is used to support JSDoc.
 * @class Venda.Widget.gridlistSwitcher
 * @constructor
 */
Venda.Widget.gridlistSwitcher = function (){};

/**
 * Disable all link tags that have title attribute and enable only the selected one
 * @param {string} title a title that is a selected style
 */
Venda.Widget.gridlistSwitcher.setActiveStyleSheet = function(title) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
    if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
      a.disabled = true;
      if(a.getAttribute('title') == title) a.disabled = false;
    }
  }
  Venda.Widget.gridlistSwitcher.createCookie(cookieName, title);
  Venda.Widget.gridlistSwitcher.toggleView(title);
};


/**
 * Create the cookie
 * @param {string} name the cookie name 
 * @param {string} value the cookie value
 */
Venda.Widget.gridlistSwitcher.createCookie = function(name,value) {
	var cj = new CookieJar({expires: 3600 * 24 * 7, path: '/'});
	cj.put(name,value);
};


/**
 * Read the cookie
 * @param {string} name the cookie name 
 */
Venda.Widget.gridlistSwitcher.readCookie = function(name) {
	var cj = new CookieJar({expires: 3600 * 24 * 7, path: '/'});
	return cj.get(name);
};

/**
 * Set title of alternate stylesheet link tag 
 * @param {string} defaultview is value that is selected what view to display in productlist or search result page
*/
Venda.Widget.gridlistSwitcher.setTitleLink =  function(defaultview) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
	    if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('rel').indexOf('alt') != -1 && a.getAttribute('title')) {		
			a.setAttribute('title', 'gridview');
		}
  }
};

/**
 * Switch between grid and list view
 * @param {string} id is the element id of text link for click to switch view
*/
Venda.Widget.gridlistSwitcher.toggleView =  function(id) {
		if(id=="gridview"){
			document.getElementById("gridview").style.display = "none";
			document.getElementById("listview").style.display="inline";
		} else {
			document.getElementById("gridview").style.display="inline";
			document.getElementById("listview").style.display="none";
		}
};

