/**
*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 == "list") {
		var productsElement = document.getElementById('products');		
		if (productsElement != null) {
			productsElement.className='list';
			var cookie = readCookie('viewPreference');
			if (cookie == null || cookie == 'grid'){
				createCookie('viewPreference','list', 10*365);
			}	
		}
	} else {
		var productsElement = document.getElementById('products');		
		productsElement.className='grid';
		if (productsElement != null) {
			var cookie = readCookie('viewPreference');
			if (cookie == null || cookie == 'list'){
				createCookie('viewPreference','grid', 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');
	}
}

function expandCategories() {
	document.getElementById('categories').style.display='inline';
	document.getElementById('change_category').style.display='none';
}

function collapseCategories(event) {
	document.getElementById('categories').style.display='none';
	document.getElementById('change_category').style.display='inline';
}
		
		/*********************************************************************
		 * No onMouseOut event if the mouse pointer hovers a child element 
		 * *** Please do not remove this header. ***
		 * This code is working on my IE7, IE6, FireFox, Opera and Safari
		 * 
		 * Usage: 
		 * <div onMouseOut="fixOnMouseOut(this, event, 'JavaScript Code');"> 
		 *		So many childs 
		 *	</div>
		 *
		 * @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com		
		**/
		function is_child_of(parent, child) {
		
			if( child != null ) {	
			while(child.parentNode) {
					if( (child = child.parentNode) == parent ) {
					return true;
					}
				}
			}
			return false;
		}
		function fixOnMouseOut(element, event) {
			
			
			if (!event) var event = window.event;
			var current_mouse_target = event.relatedTarget || event.toElement;
			if(!is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
								
				collapseCategories();		
			}
			
		}
		/*********************************************************************/


/**
 * Determines whether the browser is IE6 or not 
 */
function isIE6() {
	var isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1
	return isIE6;
}
/**
 * Function aligns grid columns height. 
 */
function alignGridHeight() {
	var productsDiv = document.getElementById("products");
	if (!productsDiv) {
		return;
	}
	if(productsDiv.className != "grid") {
		return;
	}
	for (var i = 0; i < productsDiv.childNodes.length; i++) {
		var currentChild = productsDiv.childNodes[i];
		if (currentChild.tagName != "DIV" || (currentChild.className != "row" && currentChild.className != "first row") ) {
			continue;
		}
		var divRow = currentChild;
		if (!divRow) {
			continue;
		}
		alignGridRow(divRow);
	}
}

function alignGridRow(divRow) {
		var descriptionHeight = 0;
		var minRowHeight = 0;
		for (var j = 0; j < divRow.childNodes.length; j++) {
			var rowChild = divRow.childNodes[j];
			if (rowChild.tagName != "DIV" || rowChild.className != "product") {
				continue;
			}	
			var divProduct = rowChild;
			if (!divProduct) {
				continue;
			}
			descriptionHeight = getDescriptionHeight(divProduct, descriptionHeight);
			if (descriptionHeight > minRowHeight) {
				minRowHeight = descriptionHeight;
			}
		}
		
		for (var j = 0; j < divRow.childNodes.length; j++) {
			var rowChild = divRow.childNodes[j];
			if (rowChild.tagName != "DIV" || rowChild.className != "product") {
				continue;
			}	
			var divProduct = rowChild;
			if (!divProduct) {
				continue;
			}
			updateDescriptionHeight(divProduct, minRowHeight);
		}
		
}

function getDescriptionHeight(divProduct, descriptionHeight) {
			for (var k = 0; k < divProduct.childNodes.length; k++) {
				var productChild = divProduct.childNodes[k];
				if (productChild.className != "description") {
					continue;
				}
				var description = productChild;
				if (description.offsetHeight > descriptionHeight) {
					descriptionHeight = description.offsetHeight;
				} 
			}
			return descriptionHeight;
}

function updateDescriptionHeight(divProduct, minRowHeight) {
			for (var k = 0; k < divProduct.childNodes.length; k++) {
				var productChild = divProduct.childNodes[k];
				if (productChild.className != "description") {
					continue;
				}
				var description = productChild;
				if (description.offsetHeight < minRowHeight) {
					description.style.height = minRowHeight + "px";
				}
			}
}