/**
*This function handles the toggle view on the category page. The desired view
* is passed a an argument (either grid or list). The arrangement of the dom
* elements are changed as well as css tags.
*/
 function toggleView(view) {
	if(view == "grid") {
		var divElements = document.getElementsByTagName('div');
		var k = 0; 						
		for (var i=0; i<divElements.length; i++){
			className = divElements[i].className;
			if(className.indexOf('shop_row') >= 0){
				k = k+1;
				divElements[i].className='shop_block';
				if(k <= 3){
					divElements[i].className=divElements[i].className + ' t_border';
				}
				
				if (!(k %3 == 0)) {
					divElements[i].className=divElements[i].className + ' r_border';
				}					
				var children = divElements[i].childNodes;
				var txtIndex = 0;
				for (var j=0; j< children.length; j++){
					if (children[j].className == "txt")
					{
						txtIndex = j;
						break;
					}
				}
				
				var txtChildren = children[txtIndex].childNodes;
				var pChild;
				var singChild;
				var infoChild;
				var bottomChild;					
				for (var j=0; j< txtChildren.length; j++){
					if (txtChildren[j].className == "logo")
					{
						logoIndex = j;
					}
					if (txtChildren[j].tagName == "p" || txtChildren[j].tagName == "P")
					{
						pChild = txtChildren[j];
					}
					if (txtChildren[j].className == "sing cluetip")
					{
						singChild = txtChildren[j];
					}
					if (txtChildren[j].className == "info")
					{
						infoChild = txtChildren[j];
					}
					if (txtChildren[j].tagName == "div" || txtChildren[j].tagName == "DIV" )
					{
						bottomChild = txtChildren[j];
					}			
					
				}
				
				divElements[i].removeChild(children[txtIndex]);
				divElements[i].appendChild(singChild);
				divElements[i].appendChild(pChild);
				divElements[i].appendChild(infoChild);
				bottomChild.className="bottom";
				divElements[i].appendChild(bottomChild);			
			}				
			
		}
		
		var cookie = readCookie('viewPreference');
		if (cookie == null || cookie == 'list'){
			createCookie('viewPreference','grid', 10*365);
		}
	} else {
		var divElements = document.getElementsByTagName('div');
		var className="";
		for (var i=0; i<divElements.length; i++){
			className = divElements[i].className;
			if(className.indexOf('shop_block') >=0){
				divElements[i].className='shop_row';
				var children = divElements[i].childNodes;
				var txtDiv = document.createElement('div');
				txtDiv.className = "txt"; 
				var logoIndex = 0;
				var pChild;
				var singChild;
				var infoChild;
				var bottomChild;
				for (var j=0; j< children.length; j++){
					if (children[j].className == "logo")
					{
						logoIndex = j;
					}
					if (children[j].tagName == "p" || children[j].tagName == "P")
					{
						pChild = children[j];
					}
					if (children[j].className == "sing cluetip")
					{
						singChild = children[j];
					}
					if (children[j].className == "info")
					{
						infoChild = children[j];
					}
					if (children[j].className == "bottom")
					{
						bottomChild = children[j];
					}
				}
				txtDiv.appendChild(pChild);
				txtDiv.appendChild(singChild);
				txtDiv.appendChild(infoChild);
				bottomChild.className= "";
				txtDiv.appendChild(bottomChild);					
				divElements[i].appendChild(txtDiv);
			}
		}
		var cookie = readCookie('viewPreference');
		if (cookie == null || cookie == 'grid'){
			createCookie('viewPreference','list', 10*365);
		}
		
	}
  }
 /**
 *This function can be used to create a cookie with a given name
 * value and expiry date.
 *
 */ 
	  
 function createCookie(name,value,days) {
       	if (days) {
       		var date = new Date();
       		date.setTime(date.getTime()+(days*24*60*60*1000));
       		var expires = "; expires="+date.toGMTString();
       	}
       	else var expires = "";
       	document.cookie = name+"="+value+expires+"; path=/";
}	

/**
*This function can be used to get the cookie value given the name
*/
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/**
*This function can be used to change the view in case 'list' view is set
* in client's browser. 
*/
function changeView() {
	var cookie = readCookie('viewPreference');
	if(cookie != null && cookie == 'list') {
		toggleView('list');
	}
}