$(function(){
	Sears.topNav(); //GLOBAL NAVIGATION
	searchAutoSuggest(); //SEARCH AUTOSUGGEST
	getSIVfromURL(); //GET SIV AND TERM FROM URL
	espLinkShowHide(); //INTERNATIONAL SHOW/HIDE
	globalNavDZcalls(); //MISC DOM-READY CALLS
});

//MISC DOM-READY CALLS
function globalNavDZcalls(){	
	//CLEARFIX ON SUBCAT
	$('#subcategory div.row div.util').add('#keyword div.row div.util').css('clear', 'both');
	
	//CART HEIGHT FIXES
	var shopCart_col_1_height = $('body#shoppingcart div.col_1').height();
	var shopCart_col_2_height = $('body#shoppingcart div.col_2').height();
	if($('body').is('#shoppingcart')){  
	    if($.browser.msie && $.browser.version.split('.')[0] == 6){    
	        if ( shopCart_col_1_height > shopCart_col_2_height ){
	            $('body#shoppingcart div#content').css('height', shopCart_col_1_height );
	        } else {
	            $('body#shoppingcart div#content').css('height', shopCart_col_2_height );
	        }
	    }
	};
	
	//SCROLLFIX FOR FIREFOX
	if($.browser.mozilla){
		$(window).bind('resize', scrollFixFF);
		if($('body').attr('id') != 'compare'){ 
			scrollFixFF();   
		}
	};
	
	//ANCHOR BG IMAGE FLICKER FIX
	if($.browser.msie){
		document.execCommand("BackgroundImageCache", false, true);
	};
}

//AUTOSUGGEST DOM-READY FUNCTION
var acBaseURL = "/a/af/";
function searchAutoSuggest() {
	$("#ul_af").remove();
	$("#keyword").removeClass("ac_input");
	$('#af').remove();
	var w = $('#keyword').parent().width();
	$("#keyword").autocomplete("genFiles",{type: "GET", delay:10, minChars:2, matchSubset:1, matchContains:1, cacheLength:0, maxItemsToShow:10, onItemSelect:selectItem, autoFill:false, highlight: af_highlight });
};

//AUTOSUGGEST HIGHLIGHT FUNCTION
function af_highlight(value, term) { 
	return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
};

//SEARCH WITHIN A VERTICAL SUBMIT FUNCTION
function searchfunction(){
	var keywordentered = $('div.searchBox input#keyword').val();
	var ulcode = '<ul name="searchBy"  id="searchBy" title="Search Product Options"><li><a href="javascript:;" id="" title="All Products">All Products<\/a><\/li><li><a href="javascript:;" id="app_n" title="Appliances">Appliances<\/a><\/li><li><a href="javascript:;" id="aut_n" title="Automotive &amp; Tires">Automotive &amp; Tires<\/a><\/li><li><a href="javascript:;" id="bab_n" title="Baby">Baby<\/a><\/li><li><a href="javascript:;" id="bea_n" title="Beauty">Beauty<\/a><\/li><li><a href="javascript:;" id="bks_n" title="Books">Books<\/a><\/li><li><a href="javascript:;" id="art_n" title="Custom Art">Custom Art<\/a><\/li><\/li><li><a href="javascript:;" id="clo_n" title="Clothing">Clothing</a></li><li><a href="javascript:;" id="ele_n" title="Electronics &amp; Computers">Electronics &amp; Computers<\/a><\/li><li><a href="javascript:;" id="ent_n" title="Video Games">Video Games<\/a><\/li><li><a href="javascript:;" id="mus_n" title="Movies &amp; TV Shows">Movies &amp; TV Shows<\/a><\/li><li><a href="javascript:;" id="mus_n" title="Music">Music<\/a><\/li><li><a href="javascript:;" id="fit_n" title="Fitness &amp; Sports">Fitness &amp; Sports<\/a><\/li><li><a href="javascript:;" id="fth_n" title="Bed, Bath &amp; Home">Bed, Bath &amp; Home<\/a><\/li><li><a href="javascript:;" id="gift_n" title="Gifts">Gifts<\/a><\/li><li><a href="javascript:;" id="hel_n" title="Health &amp; Wellness">Health &amp; Wellness<\/a><\/li><li><a href="javascript:;" id="jew_n" title="Jewelry &amp; Watches">Jewelry &amp; Watches<\/a><\/li><li><a href="javascript:;" id="lag_n" title="Lawn &amp; Garden">Lawn &amp; Garden<\/a><\/li><li><a href="javascript:;" id="ofc_n" title="Office Products">Office Products<\/a><\/li><li><a href="javascript:;" id="out_n" title="Christmas">Christmas<\/a><\/li><li><a href="javascript:;" id="sho_n" title="Shoes">Shoes<\/a><\/li><li><a href="javascript:;" id="tls_n" title="Tools">Tools<\/a><\/li><li><a href="javascript:;" id="toy_n" title="Toys &amp; Games">Toys &amp; Games<\/a><\/li><li><a href="javascript:;" id="clear_n" title="Clearance">Clearance<\/a><\/li><\/ul>';
	if(document.getElementById('searchBy') == null) {
		$('body').append(ulcode);
		var pos = $("#selectsearch").offset();
		$('#searchBy').css({position:'absolute',top:pos.top + 'px',left:pos.left + 'px',zIndex: '1000001',display: 'block',listStyle:'none', background:'#FFFFFF scroll', border:'1px solid #92B3D4', margin:'0', padding:'2px 5px 1px 5px', width:'133px'});
	}
		
	$('#searchBy').bind("mouseleave", function(){
		var childR = document.getElementById('searchBy');
		var parentR = childR.parentNode;
		parentR.removeChild(childR);
	});

	$('ul#searchBy a').css({color:'#999999',fontSize:'11px', textAlign:'left', textDecoration:'none'});
	$('ul#searchBy li').css({width:'100%'}).hover( 
		function(){
			$(this).css('background-color', '#BDDAFE');
			$(this.firstChild).css('background-color', '#BDDAFE');
		},	
		function(){
			$(this).css('background-color', '#ffffff');
			$(this.firstChild).css('background-color', '#ffffff');
		}
	).click(function(){
		var clickedTitle = this.firstChild.getAttribute('title'); 
		var clickedId = this.firstChild.getAttribute('id');
		var selectedSearchOption = '';
		selectedSearchOption += '<a class="selectsearch" id="selectsearch" style="float: left;" href="JavaScript:;" onclick="javascript:searchfunction()" id="' + clickedId + '" title="' + clickedTitle + '">' + clickedTitle + '</a>';				document.getElementById("searchDropDown").innerHTML = selectedSearchOption;
		var child = document.getElementById('searchBy');
	    var parent = child.parentNode;
	    parent.removeChild(child);	
		if (clickedTitle == 'Books' || clickedTitle == 'Custom Art' || clickedTitle == 'Movies & TV Shows' || clickedTitle == 'Music'){
			$('#gobtn').css({display:'none'});
			$('#go_btn').css({display:'block'}); 	
		}				
	}); 
};

//SEARCH IN VERTICAL DOM READY DROPDOWN POPULATION
function getSIVfromURL(){
	 var vNameReturn = "";
	 var strVHref = window.location.href;
	 if(strVHref.indexOf("vName") > -1 ){			 	
		var strVQueryString = strVHref.substr(strVHref.indexOf("vName"));			 
		var aVQueryString = strVQueryString.split("=");			 
		vNameReturn = aVQueryString[1];
		if(typeof jsonProperties != 'undefined' && typeof jsonProperties.csi != 'undefined' && jsonProperties.csi != null && !jsonProperties.csi){
			if(vNameReturn.indexOf("&") > -1 ) {					
				var strVSubQueryString = vNameReturn.substr(vNameReturn.indexOf(vNameReturn));
				var aVSubQueryString = strVSubQueryString.split("&");		
				vNameReturn = aVSubQueryString[0];
				if(vNameReturn == ''){
					document.getElementById("selectsearch").innerHTML = 'All Products';
				}else{					
					vNameReturn = replaceAll(vNameReturn,"%20"," ");
					vNameReturn = replaceAll(vNameReturn,"+"," ");
					vNameReturn = undoCleanFilterParam(vNameReturn);
					if(vNameReturn == 'Automotive'){
						vNameReturn = 'Automotive & Tires';					
					}else if(vNameReturn == 'Computers & Electronics'){
						vNameReturn = 'Electronics & Computers';
					}else if(vNameReturn == 'Jewelry'){
						vNameReturn = 'Jewelry & Watches';
					}else if(vNameReturn == 'For the Home'){		
						vNameReturn = 'Bed, Bath & Home';							
					}else if(vNameReturn == 'Computers & Electronics_Office Products'){
						vNameReturn = 'Electronics & Computers';					  
					}else if(vNameReturn == 'Outdoor Living'){
						vNameReturn = 'Outdoor Living & Seasonal';
					}else if(vNameReturn == 'Entertainment'){
						vNameReturn = 'Video Games';			
					}
					document.getElementById("selectsearch").innerHTML = vNameReturn;
				 }
			}else{
				document.getElementById("selectsearch").innerHTML = 'All Products';
			}
		}else{
			document.getElementById("selectsearch").innerHTML = 'All Products';
		}
	}else{
		document.getElementById("selectsearch").innerHTML = 'All Products';
	}
};

//SEARCH IN A VERTICAL LINK SCRUBBING AND C2C REDIRECT
function checkLink(){
	var title = document.getElementById('selectsearch').title;
	var keywordEntered = document.getElementById('keyword').value;
	if(document.getElementById('selectsearch').title=="Books"){
		window.location = 'http://books.sears.com/TRP/actions/searchHandler.do?parentNum=11532&nextPage=search&key=' + keywordEntered;
	}else if(document.getElementById('selectsearch').title=="Custom Art"){
		window.location = 'http://art.sears.com/ShowSearch.aspx?x=0&y=0&s=' + keywordEntered;
	}else if(document.getElementById('selectsearch').title=="Music") {
		window.location = 'http://entertainment.sears.com/GlobalSearch.aspx?si=sears&search=' + keywordEntered;
	}else if (document.getElementById('selectsearch').title=="Movies & TV Shows") {
		window.location = 'http://entertainment.sears.com/GlobalSearch.aspx?si=sears&search=' + keywordEntered;
	}else{
		document.getElementById('srchFrm').submit();
	}
};

//SCROLLFIX FOR FIREFOX
function scrollFixFF(){
    var overflow; 
    overflow = (document.documentElement.offsetWidth < 1009) ? "auto" : "hidden";
    $('html').css('overflow-x', overflow);
};

//Absolute Positioned Col1 Col2 Height Fix
function readjustColHeight(){
	var col1 = $('#content div.col_1');
	if(col1.css('position') == 'absolute'){
		var content = $('.col_2');
		var col1H = col1.height();
		var contentH = content.height();
		if(col1H > contentH){
			content.css('height', col1H);
		}
	}
};

//INTERNATIONAL SHOW/HIDE
function espLinkShowHide(){
	if(typeof d_site != 'undefined' && typeof espLinkNew != 'undefined'){
		$('espanolNew').hide();
	}
};

//PRESEARCH VALIDATION
function preSearchValidation(){
	if($('#keyword').val() == ""){
		return false;
	}else{
		fn_kw_checkKeyword();
	}
};

//I HAVE NO IDEA WHAT THIS IS OR WHY IT WOULD BE NEEDED
/*$(window).unload(function() { 
	$('*').unbind(); 
});*/
