// Set up document
$(document).ready(function(){
	var div = document.getElementsByTagName('div');
	var divDesc = document.createElement('div');
	var bImgs = document.getElementById('bImgHolder').getElementsByTagName('img'); /* Wrapper that Holders the Big Catalog Images. */
	var strUrl = window.location.toString().toLowerCase(); /* GET URL */
	
	$("#helpWrapper").hide();
	$("#changeButton").click( function(){
		if(this.alt=="grid"){this.alt="row";this.src="/img/icons/switch_row.gif";this.title="Switch to row view";$("#catWrap").attr("class","grid");}
		else if(this.alt=="row"){this.alt="grid";this.src="/img/icons/switch_grid.gif";this.title="Switch to grid view";$("#catWrap").attr("class","row");}
	});
	
	$("a.quick").click( function(){
		if (document.getElementById('quick')) {
			createQuickOrderDiv(bImgs, this.rel);
			/* OMNITURE TRACKING */
			var h3 = this.parentNode.parentNode.parentNode.getElementsByTagName('h3')[0].childNodes[0].data;
			var s = s_gi('searscom');
			s.linkTrackVars = 'eVar9';
			s.linkTrackEvents = 'None';
			s.eVar9 = h3;
			s.tl(this,'o',h3);
		}
	});
	
	/* Gridview, create dynamic popup with the corresponding catalog desc in it. */
	$(".grid .group > div > img:first-child").hover( function(){
		if (this.parentNode.parentNode.parentNode.className == 'grid') {
			var desc = this.parentNode.getElementsByTagName('p')[0].childNodes[0].data;
			this.parentNode.appendChild(divDesc);
			divDesc.id  = 'divDesc';
			divDesc.innerHTML = desc;
			if (divDesc.parentNode.parentNode.getElementsByTagName('div')[3]) {
				if (divDesc.parentNode.style.zIndex=='1') {
					divDesc.className = 'divDesc';
					divDesc.parentNode.style.zIndex='5'
				} else {
					divDesc.className = '';
				}
			} else {
				divDesc.className = '';
			}
		}
	},function() {
		if (this.parentNode.parentNode.parentNode.className == 'grid') {
			if (divDesc.parentNode.style.zIndex=='5') {
				divDesc.parentNode.style.zIndex='1'
			}
			this.parentNode.removeChild(divDesc);
		}
	});
	
	/* Set the zIndex on each wrappper 1 less then the other
	   so IE will display the desc over top of the img */
	for (var i=0; i<div.length; i++) {
		if (div[i].className == 'group') {
			var wrap = div[i].getElementsByTagName('div');
			for (var j=wrap.length, t=0; j>0; j--) {
				wrap[t].style.zIndex = j;
				t++;
			}
		}		
	}
	/*-----------------------------------------
	   CREATE CATALOG ARRAY and GET PARAMETER
	   AND SHOW CORRESPONDING QUICK ORDER DIV
	-------------------------------------------*/
	if (document.getElementById('quick')) {
		createCatArrayAndShowQuickOrderDiv(strUrl, bImgs);
	}
	
});

function createQuickOrderDiv(pBImgs, pImg) {
	$("#quick").css('z-index','1001').show();
	for (var i=0; i<pBImgs.length; i++) {
		pBImgs[i].style.display='none';
	}			
	document.getElementById(pImg).style.display='block';			
	var title = $(".group a[@rel="+pImg+"]").parent().parent().parent().find("h3:eq(0)").html();
	$("#quick .floatRight h1:eq(0)").empty();
	$("#quick .floatRight h1:eq(0)").append(title);
	setPosCenter('quick');
	$("img.help").click( function(){
		var id = $(this).attr("rel");
		$(".info div").hide();
		$("#"+id).show();
	});
}

function createCatArrayAndShowQuickOrderDiv(pStrUrl, pBImgs) {	
	/* CREATE CATALOG ARRAY */
	var catArr = new Array();
	for (var i=0; i<pBImgs.length; i++) {
		catArr.push(pBImgs[i].id);
	}
	
	/* GET PARAMETER AND SHOW CORRESPONDING QUICK ORDER DIV */
	function getParameter(queryString, parameterName) {
		/* Add "=" to the parameter name (i.e. parameterName=value) */
		var parameterName = parameterName + "=";
		if (queryString.length > 0) {
			/* Find the beginning of the string */
			var begin = queryString.indexOf(parameterName);
			/* If the parameter name is not found, skip it, otherwise return the value */
			if (begin != -1) {
				/* Add the length (integer) to the beginning */
				begin += parameterName.length;
				/* Multiple parameters are separated by the "&" sign */
				var end = queryString.indexOf( "&" , begin);
				if (end == -1) {
					end = queryString.length
				}
				/*-----------------------------------------------------------------
				   loop through catalog array and see if it mathes req parameter.
				   if it does show the corresponding quick order popup.
				-------------------------------------------------------------------*/
				var rParam = queryString.substring(begin, end);
				for (var i=0; i<catArr.length; i++) {
					if (rParam == catArr[i]) {
						createQuickOrderDiv(pBImgs, rParam);
						/* OMNITURE TRACKING FOR EXTERNAL CQO LINKS */
						var h3two = $(".group a[@rel="+rParam+"]").parent().parent().parent().find("h3:eq(0)").html();
						var s = s_gi('searscom');
						s.linkTrackVars = 'eVar9';
						s.linkTrackEvents = 'None';
						s.eVar9 = h3two;
						s.tl(this,'o',h3two);
					}
				}
			}
			/* Return "null" if no parameter has been found */
			return "null";
		}		
	}
	getParameter(pStrUrl, 'cat');	
}

// Remove pre-populated text
function clearHelpText(field, text) {
    if (field.value == text) {
	    field.value='';
    }
}
function setPosCenter(id){
		// Set position of this div //
		var thisDIV = document.getElementById(id);
		if (this.innerHeight) // all except Explorer
		{
			xPos = (this.innerWidth/2) - 10;
			yPos = (this.innerHeight/2) + window.pageYOffset;
		}
		else if (document.body) // other Explorers
		{
			xPos = (document.body.clientWidth/2);
			yPos = (document.body.clientHeight/2) + document.body.scrollTop;
		}
		thisDIV.style.top = (yPos - (thisDIV.offsetHeight/2)) + "px";
		thisDIV.style.left = (xPos - (thisDIV.offsetWidth/2)) + "px";
}

// JS for catalog order //
function callSearch(frm)
{
  	var qoItemValue = document.getElementById('qoItem').value;
  	var itemNum=qoItemValue;
  	var keyCode = document.getElementById('keyCode');
  	var customerNum = document.getElementById('customerNum');
	itemNum = trim(itemNum);
	createCookie("keycode"+itemNum,keyCode,0);
	createCookie("customerNumber"+itemNum,customerNum,0);
	if(itemNum.length>0&&itemNum!="Enter item number"){
		frm.action='KeywordSearch?storeId='+frm.storeId.value + '&catalogId=' + frm.catalogId.value + '&keyword='+itemNum;
		frm.submit();
	}
	else {
		alert("Please enter an item number.");
		return false;
	}
}
function trim(value){
	// right trim
	while(value.charAt((value.length -1))==" "){
		value = value.substring(0,value.length-1);
	}
	// left trim
	while(value.charAt(0)==" "){
		value = value.replace(value.charAt(0),"");
	}
	return value;
}

function readCatalogCookie(cookieName,itemNum) {
	cookieName = cookieName + itemNum;
	return readCookie(cookieName);
}

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=/";
}

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;
}
