var Sears = {
	topNav: function(){
		var global = this;
		var depts = $('#deptNav')
		var shopAll = $('#MNshopAll');
		var timer = 0;
		var timer2 = 0;
		var timer3 = 0;
		var timer4 = 0;
		var timer5 = 0;
		var idx = 0;
		var buffer = 0;
		if($('body').is('#home')){
			depts.show();
		}else{
			depts.appendTo('body').css({
				top:  shopAll.offset().top + shopAll.height(),
				left: shopAll.offset().left
			});	
		}
		function loadFly(){			
			$('#tempNav').remove();
			depts.find('li.active').removeClass('active');
			depts.find('li#'+idx).addClass('active');
			var div = $('<div/>').attr({id:'tempNav'}).css({
					position:'absolute',
					display:'none',
					float:'left',
					top:(depts.offset().top-22),
					left:(depts.offset().left+depts.width())-buffer,
					zIndex:1000
				}).appendTo('body').hover(function(){
					clearTimeout(timer2);
					clearTimeout(timer4);
					clearTimeout(timer5);
				},function(){
					timer2 = setTimeout(function(){
						$('#tempNav').fadeOut(300,function(){$(this).remove()});
						if($('body').is(':not(#home)')){
							depts.fadeOut(300).find('li.active').removeClass('active');
						}else{
							depts.find('li.active').removeClass('active');
						}
					},800);	
				});
			div.append($('#'+idx.split('_n')[0]+'_fly').clone()).show(10,function(){
				//alert($('#tempNav').attr('id'));
				var _this = $('#tempNav');
				_this.bgIframe();
				var tallest = _this.find('.col:eq(0)').height();
				_this.find('.col').each(function(i){
					if($(this).height() > tallest){
						tallest = $(this).height();
					}
				});
				_this.find('.col').height(tallest);
			});							   
		}
		shopAll.hover(function(){
			timer3 = setTimeout(function(){
				depts.fadeIn(300).bgiframe();
				
			},500)				   
		},function(){
			clearTimeout(timer3);
			timer4 = setTimeout(function(){
				if($('body').is('#home')){
					$('#tempNav').fadeOut(300).find('li.active').removeClass('active');
				}else{
					depts.add('#tempNav').fadeOut(300).find('li.active').removeClass('active');
				}
			},500);
		}).click(function(){return false;});
		depts.children().hover(function(){
			clearTimeout(timer4)
			idx = $(this).attr('id');
			$(this).addClass('hover');
			var tempNav = $('#tempNav');
			timer = setTimeout(loadFly,400);	
		},function(){
			$(this).removeClass('hover');
			clearTimeout(timer);
		});
		depts.hover(function(){
			clearTimeout(timer2);
		},function(){
			$('#tempNav').mouseout();
			timer5 = setTimeout(function(){
				$('#tempNav').fadeOut(300,function(){$(this).remove()});
				if($('body').is(':not(#home)')){
					depts.fadeOut(300).find('li.active').removeClass('active');
				}else{
					depts.find('li.active').removeClass('active');
				}
			},500);
		});
		$('input.srch').attr({disabled:'disabled'});
		$('#keyword').css({color:'#666666'}).val('What are you shopping for?').focus(function(){
			$('input.srch').removeAttr('disabled');
			$(this).css({color:'#000000'});
			if(this.value == 'What are you shopping for?'){
				this.value = '';
			}else{
				$(this).select();
				
			}	 
		}).blur(function(){
			if(this.value == ''){
				$(this).css({color:'#666666'}).val('What are you shopping for?');
				$('input.srch').attr({disabled:'disabled'});
			}else{
				$('input.srch').removeAttr('disabled');
			}	
		});
		
	},
	getWindowSize : function(){
		var xScroll, yScroll, windowWidth, windowHeight;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		if (self.innerHeight) {	
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { 
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { 
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		return {'pageWidth':pageWidth, 'pageHeight':pageHeight, 'windowWidth':windowWidth, 'windowHeight':windowHeight};
	},
	/**
	 * 
	 */
	showHideRecentlyView: function(e){
		if(e)
			$("#recentlyView").fadeIn("slow");
		else 
			$("#recentlyView").fadeOut("slow");
	},
	preloadImages : function(){
		if(document.images){
			document.myImages = new Array();
			for (var i = 0; i < preloadImages.arguments.length; i++){
				document.myImages[i] = new Image;
				document.myImages[i].src = preloadImages.arguments[i];
			}
		}
	}
};
/*
$(window).resize(function(){
	var windowWidth = Sears.getWindowSize().windowWidth;
	if (!window.XMLHttpRequest){
		if(windowWidth < 1000){
			$("#content").css("width", "980px");
			$("#footer").css("width", "1000px");
			$("#header").css("width", "1000px");
		} else { 
			$("#content").css("width", (windowWidth-50)+"px");
			$("#footer").css("width", (windowWidth-30)+"px");
			$("#header").css("width", (windowWidth-30)+"px");
		}
			
	}
});*/

function showHideContainer(element){
	if (document.all) {
		if (element.style.display == "none")
			$("#" + element.id).fadeIn("slow");
		else
			$("#" + element.id).fadeOut("slow");
	}
}

//Ajax call
var httpRequestSupported = true;

function ajaxCall(url,callbackFunction,returnData,linkId) 
{ 
    var httpRequest = false; 

    // Exit if this function is not supported
    if (!httpRequestSupported) {
        return;  
    }
    
    // check if supported
    httpRequest = isHttpRequestSupported();
    
    if (!httpRequest) { 
        httpRequestSupported = false;
        return false; 
    } 
   
    // Map the response to the callback function
    httpRequest.onreadystatechange = function() { 
        if (httpRequest.readyState == 4) {  
            if (httpRequest.status == 200) {
                if (returnData) { 
                    eval(callbackFunction + '(httpRequest.responseXML,linkId)'); 
                } else { 
                    eval(callbackFunction + '(httpRequest.responseText,linkId)'); 
                }  
            } else { 
                // TODO:  Keep this line commented in production.  The user will have no idea what this means.
                //('The AJAX request was made to our backend server and the following error occurred: ' + httpRequest.status); 
                eval(callbackFunction + '("")'); 
            } 
        }
        else{
            if(callbackFunction == 'displayTabContent'){
                
                htmlValue = "<img src= '"+ imagePath + "img/backgrounds/loading.gif' />";
                document.getElementById(linkId).innerHTML = htmlValue;
            }
            if(callbackFunction == 'browseSpecialOffer'){   
                htmlValue = "<img src= '"+ imagePath + "img/backgrounds/loading.gif' />";
                linkId.innerHTML = htmlValue;           
            }
        } 
    } 
    
    httpRequest.open('GET', url, true); 
    httpRequest.send(null); 
}

function isHttpRequestSupported() {

    if (window.XMLHttpRequest) {   
        // Test if the Gecko engine is running.  Gecko supports AJAX
        httpRequest = new XMLHttpRequest(); 
        if (httpRequest.overrideMimeType) { 
            httpRequest.overrideMimeType('text/xml'); 
        }  
    } else if (window.ActiveXObject) { 
        // Test if an IE engine is running
        try {      
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
        } catch (e) { 
            try {  
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
            } catch (e) {} 
        } 
    } 
     
    if (!httpRequest) { 
        httpRequestSupported = false;
    } 
    return httpRequest;
}
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

function trimStr( value ) {
	
	return LTrim(RTrim(value));
	
}
function setStoreId(storeIdValue){
    storeId = storeIdValue;
}
(function($){
$.fn.bgIframe = $.fn.bgiframe = function(s) {
	// This is only for IE6
	if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
		s = $.extend({
			top     : 'auto', // auto == .currentStyle.borderTopWidth
			left    : 'auto', // auto == .currentStyle.borderLeftWidth
			width   : 'auto', // auto == offsetWidth
			height  : 'auto', // auto == offsetHeight
			opacity : true,
			src     : 'javascript:false;'
		}, s || {});
		var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
		    html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
		               'style="display:block;position:absolute;z-index:-1;'+
			               (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
					       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
					       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
					       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
					       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
					'"/>';
		return this.each(function() {
			if ( $('> iframe.bgiframe', this).length == 0 )
				this.insertBefore( document.createElement(html), this.firstChild );
		});
	}
	return this;
};
})(jQuery);
if (document.all)
{
	if (window.XMLHttpRequest) //IE7
	{
		document.write("<link href=\"/customers/sears/css/ie7fixes.css\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\">");
	}
	else //IE6
	{
		document.write("<link href=\"/customers/sears/css/ie6fixes.css\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\">");
	}
}
