	$(document).ready(function() {
	
		if(window._orig_windowSetTimeout !== undefined)
			window.setTimeout = _orig_windowSetTimeout;
		if(window._orig_windowSetInterval !== undefined)
			window.setInterval = _orig_windowSetInterval;
		
		$("li.widgetBox").dropShadow();
		
		$('ul#mainnav li').not('.current').hover(function(){
			$(this).css('border-left-color','#8CC63F').css('border-right-color','#8CC63F').css('background','url(/lfs/images/header/header_mainnav_highlight.gif) repeat-x top left');
		},function(){
			$(this).css('border-left-color','#00A164').css('border-right-color','#00A164').css('background','none');			
		});
		
		$('ul#utilnav li').hover(function(){
			var thisID = $(this).attr('id');
			$(this).css('background-image','url(/lfs/images/header/header_icon_'+thisID+'mo.gif)');
		},function(){
			var thisID = $(this).attr('id');
			$(this).css('background-image','url(/lfs/images/header/header_icon_'+thisID+'.gif)');
		});
		
		//$("li.widgetBox").dropShadow();
		$(".numeric").numeric();
		
		$(".widgetClick").click(function(){
			$('#utilnav li').each(function(){$(this).find('ul').hide()});
			$(this).next().css('position','absolute').fadeIn().parent().css('z-index','100000');
			$("li.widgetBox").dropShadow();
			return false;
		});
		$(".closeWidget").click(function(){
			$('#utilnav li').each(function(){$(this).find('ul').hide()});
			return false;
		});
		
		$('#getCardBalanceInput').attr('value','gift card number...').css('color','#999999');
		$('#getCardBalanceInput').focus(function(){
			var thisValue = $(this).attr('value');
			if(thisValue=='gift card number...'){
				$(this).attr('value','').css('color','black');
			}
			return false;
		});
		$('#getCardBalanceGo').click(function(){
			var thisValue = $('#getCardBalanceInput').attr('value');
			$.getJSON("/lfs/components/remoteProxy.cfc?method=returnGiftCardBalance&giftCardNumber="+thisValue,function(json){
			  	var message = json.MESSAGE;
			  	var error = json.ERROR;
			  	if(error==1)$('#getCardBalanceSpan').css('color','red');
			  	else $('#getCardBalanceSpan').css('color','green');
			  	$('#getCardBalanceSpan').html(message);
			  });
			return false;
		});
		
		

		////////////////////////////////////////////////////////////////
		//////////////Store Locator/////////////////////////////////////
		////////////////////////////////////////////////////////////////
		var storeWidgetBox = $('#storeWidgetBox');
		//submit the locator form to the locator page if the user clicks More Results...
		$(storeWidgetBox).find('#locatorResultsDiv div.locatorMoreResults').click(function(){
			$(storeWidgetBox).find('#selectedStoreNo').val('');
			$(storeWidgetBox).find('#locatorForm').attr('action','/index.cfm/aboutus/stores/').submit();
		});
		
		//when user clicks Search Again we need to reset the display back to the default form
		$(storeWidgetBox).find('#locatorResultsDiv div.locatorSearchAgain').click(function(){
			$("li.widgetBox").removeShadow();
			$(storeWidgetBox).animate({height:'200px',width:'300px'},500,function(){
				$("li.widgetBox").redrawShadow();
			});
			$(storeWidgetBox).find('#locatorResultsDiv').hide();
			$(storeWidgetBox).find('#locatorSearchDiv').show();
			$(storeWidgetBox).find('#locatorNoResultsDiv').hide();
			$(storeWidgetBox).find('#locatorStartLocation').html('');
			return false;
		});
		
		//this begins the event when the user submits the search form
		$(storeWidgetBox).find('#locatorSubmit').click(function(){
			//first we get all fot he form values and create the data string to send to the locator component
			var zipcode = $(storeWidgetBox).find('#locatorZipCode').val();
			var city = $(storeWidgetBox).find('#locatorCity').val();
			var state = $(storeWidgetBox).find('#locatorState').val();
			var radius = $(storeWidgetBox).find('#locatorRadius').val();
			var limit = $(storeWidgetBox).find('#locatorLimit').val();
			var dataString = '&zipcode='+zipcode+'&city='+city+'&state='+state+'&radius='+radius+'&limit='+limit;
			
			//here we clean up the display so that any previous search results are cleared
			$(storeWidgetBox).find('#locatorStartLocation').html('');
			$(storeWidgetBox).find('.storeRow').remove();
			$(storeWidgetBox).find('#locatorNoResultsDiv').hide();

			
			
			//here we make the call to the remote proxy with the data search to get back the search results in JSON format
			$.getJSON("/lfs/components/remoteProxy.cfc?method=returnStoresByLocation"+dataString,function(json){
				
				//set up our initial variables with the returned JSON data
				//storeData is the actual search results query serialized into JSON format
			  	var storeRowcount = json.stores.ROWCOUNT;
			  	var storeData = json.stores.DATA;
				var startLat = json.startLatitude;
				var startLon = json.startLongitude;
				var startPlacename = json.startPlacename;
				var startZipCode = json.startZipCode;
				
				//if there were no results, show the no results message and quit
			  	if(storeRowcount==0){
					$(storeWidgetBox).find('#locatorNoResultsDiv').show();
					return false;
				}
			  	else{
					//if we get search results, start by setting up the display...hide the form, show the map, etc.
					$("#storeWidgetBox").animate({height:"250px",width:"400px"},500,function(){
						$("li.widgetBox").redrawShadow();
					});
					$(storeWidgetBox).find('#locatorSearchDiv').hide();
					$(storeWidgetBox).find('#locatorResultsDiv').show();
					$(storeWidgetBox).find('span#locatorStartLocation').html(' near '+startPlacename);
					widgetMap = new lowesmap('smallLocatorMap',false,false,'small');
					
					//now we loop through the results query and add the results rows
					for(i=0;i<storeRowcount;i++){
						//set up this iterations local vars
						//shorten the store display name if it is going to wrap
						var thisStoreName = storeData.STORE_DISPLAY_NAME[i];
						if(thisStoreName.length>45){
							var thisStoreName = thisStoreName.substr(0,45)+'...';
						}
						var thisLat = storeData.LATITUDE[i];
						var thisLon = storeData.LONGITUDE[i];
						var thisDist = storeData.DISTANCE[i]+' mi';
						var thisStoreNo = storeData.STORE_NUMBER[i];
						//this creates the row div that is appended within the results body div
						var thisHTML = "<div style='float:left;width:50px'>"+thisDist+"</div><div style='float:left;margin-left:5px;'><a href='#'>"+thisStoreName+"</a></div>";
						//this clones the hidden copy row, removed the copyrow class, adds the storeRow class, appends it within the body, includes the html, adds the store number as the id, and binds a click event
						$(storeWidgetBox).find('.copyrow').clone().removeClass('copyrow').addClass('storeRow').appendTo($(storeWidgetBox).find('#locatorResultsDiv div.locatorResultsBody')).html(thisHTML).show().attr('id',thisStoreNo).click(function(){
							//when the row is clicked, we submit the form - tagging on the store id so we know which to highlight on the locator page
							
							$(storeWidgetBox).find('#selectedStoreNo').val($(this).attr('id'));
							$(storeWidgetBox).find('#locatorForm').submit();
						});
						//we add a marker to the marker array for this result
						widgetMap.addMarker(thisLat,thisLon,'red'+i+'.png',0,false);
					}
					//once we are finished adding the rows we stripe the even rows
					$(storeWidgetBox).find('.locatorResultDiv').removeClass('even');
					$(storeWidgetBox).find('.locatorResultDiv:even').addClass('even');
					
					//finally, we refresh the map's markers and zoom the map to fit the marker boundaries
					setTimeout("degauseMap("+startLat+","+startLon+")",300);
				};
			  });
		});
		////////////////////////////////////////////////////////////////
		//////////////END Store Locator/////////////////////////////////
		////////////////////////////////////////////////////////////////
		
	});
	
function degauseMap(lat,lon){
	widgetMap.refreshMarkers();
	widgetMap.centerMap(lat,lon);
	widgetMap.refreshZoom(true);
}
