var _intBrandPosition = 0;
var _intBrandIndex = 0;
var _intLastPosition = 0;
var _intAnimateSpeed = 175;

var HeaderControl = function(strSearchURL, strGender, strMrktURL){
	var _arrTimeouts = {};
	var blnMSIE = ($.browser.msie);
	var blnMSIE6 = (blnMSIE && $.browser.version.substring(0,1) == "6");
	
	var init = function(){
		var _strDept = ""
		var strNavHTML = "";
		var strLoaded = "<div style='display: none;'>loaded_" + strGender + "</div>";
		
		strNavHTML = window.name;
				
		if(strNavHTML.length == 0 || window.name.indexOf(strLoaded) == -1){
			var arrDept = [];
			var strDept = "";
		
			strDept = arrDept.join('');
					
			$.get(strSearchURL,function(strHTML){
				strHTML += strLoaded;
				window.name = strHTML;
				BindHTML(strHTML, strMrktURL);
			},'html')
			
		}else{
			BindHTML(strNavHTML, strMrktURL);
		}
		
		$('#hMenu > li').hover(onmouseover, onmouseout);
	}
	
	var onmouseover = function(){
		var $obj = $(this);
		var id = $obj.attr("id");
		$obj.attr("on", "true");
		
		_arrTimeouts[id] = setTimeout(function(){
			var strClass = $obj.attr("class");
			var strClassCurrent = [$obj.attr("class"), "-hover"].join('');
			var blnOn = $obj.attr("on");
			
			if(blnOn == "true" && strClass.indexOf("-hover") < 0){
				$obj.removeClass(strClass).addClass(strClassCurrent).find("div:first").show();
			}
		}, 150);
	}
	
	var onmouseout = function(){
		var $obj = $(this);
		var id = $obj.attr("id");
		var strClass = $obj.attr("class")
		var strClassCurrent;
		
		strCssClassCurrent = strClass.replace("-hover", "");
		$obj.removeClass(strClass).addClass(strCssClassCurrent).attr("on","false").find("div:first").hide();
		
		clearTimeout(_arrTimeouts[id]);
	}
	
	/*Issue prior that IE was having issues of downloading imagery while constructing the DOM needed for the shelf navigation
	so it was decided that $(window).load was suitable for this instance*/
	if(!blnMSIE6 && blnMSIE){
		$(window).load(function(){init();});
	}else{
		$(document).ready(function(){
			if(blnMSIE6){
				//IE 6 has its own menus but it still needs the shelf child menu applied to each list element
				$("ul.level1 li").addClass("Shelf-Child-Menu");
			}else{
				init();
			}
		});
	}
}

function BindHTML(strHTML, strMrktURL){
	var arrList = [];
	$("body").append(strHTML);
					
	window.name = strHTML;
	
	/* Append each of the unordered list elements to the parent of the anchor tag */
	$('a[dept]').each(function(){
		var $obj = $(this);
		var objUL = $(['#ul_',$obj.attr('dept')].join(''));
		var objClone = $obj.parent();
		
		$obj.parent().append(objUL);
		$obj.find('span').text(["(", objUL.attr("stylecount"),")"].join(''));
		$obj.find('a').css({display : 'block'});
	});
	
	/* Need to bind the HTML to the specific keyword departments */
	$('#ul_cat_001 > li, #ul_cat_002 > li').each(function(){
		var $obj = $(this);
		var $objKeyword = $obj.attr("keywordid") ? $($obj.attr("keywordid")) : null;
		
		if($objKeyword != null){
			$objKeyword.clone().appendTo($obj);
			$obj.addClass("Shelf-Child-Menu");
		}
	});

	/* Append dlcontainer which is the Manufacturers for the site */
	$('#scroller').append($('#dlcontainer'));
	
	
	$('#dlcontainer dt[position]').each(function(intIndex){
		var $obj = $(this);
		/* Here we are building a string for the manufactures first letters */
		arrList.push("<li id='ltr_", $obj.text().toUpperCase(), "' ");
		arrList.push("firstletter='", $obj.text().toUpperCase(), "' ");
		arrList.push("position='", $obj.attr('position'), "' brandindex='", intIndex ,"'>");
		arrList.push("<span>", $obj.text(), "</span></li>");
	});
	
	/* Append the list items to the unordered list and then set a click function for each list item */
	$('#ul_atoz').append(arrList.join('')).find('li').click(function(){
		var $obj = $(this);
		var intCurrentPosition = $obj.attr('position');
		var strFirstLetter = $obj.attr("firstletter");
		var intPosition = $("#dl_" + intCurrentPosition).position().left * -1;
		
		/* This will appear to have changed the selected background image */
		$("li[brandindex='" + _intBrandIndex + "']").removeClass("selected-nav-letter");
		$obj.addClass("selected-nav-letter"); 
		$("#dlcontainer").animate({left : intPosition + "px"},_intAnimateSpeed);
		
		$("#dlcontainer dt").removeClass("selected-letter");
		$(["#dt_",strFirstLetter].join('')).addClass("selected-letter");
		
		_intBrandPosition = parseInt(intCurrentPosition);
		_intBrandIndex = parseInt($obj.attr("brandindex"));
	});
	
	/* Making sure the first letter is highlighted */
	$("#dlcontainer dt:first").each(function(){
		var strFirstLetter = $(this).attr("firstletter");
		$(["#dt_",strFirstLetter].join('')).addClass("selected-letter");
		$(["#ltr_",strFirstLetter.toUpperCase()].join("")).addClass("selected-nav-letter");
	});
	
	/* Moves to the next position for the list of MFGs */
	$("#next,#previous").click(function(){
		var intMove = parseInt($(this).attr("move"));
		var intIndex = parseInt(_intBrandPosition)+intMove;
		var objDL = $("#dl_" + intIndex);
		
		if(objDL.length > 0){
			var intPosition = objDL.position().left*-1;
			var strFirstLetter = objDL.attr("firstletter");
			var objLi = $(["#ltr_",strFirstLetter].join(''));
			
			/* Shift the current brandindex list item to make it appear that it's no longer selected */
			$(["li[brandindex='",_intBrandIndex,"']"].join('')).removeClass("selected-nav-letter");
			
			/* Set the current list item to appear that it's selected */
			objLi.addClass("selected-nav-letter");
			
			/* Shift the container to the particular DL relative position */
			$("#dlcontainer").animate({left:[intPosition,"px"].join('')},_intAnimateSpeed);
			
			/* Set the First Letter Background Color */
			$("#dlcontainer dt").removeClass("selected-letter"); //css({backgroundColor:"transparent"});
			objDL.find("dt:first").addClass("selected-letter");
			
			/* Set the Brand DL Position and the brand index */
			_intBrandPosition = intIndex;
			_intBrandIndex = objLi.attr("brandindex");
		}
	});
	
	$("ul.level1 li").addClass("Shelf-Child-Menu").find("ul").parent().addClass("Shelf-Parent-Menu").removeClass("Shelf-Child-Menu");
}

