var Ajax;
if (Ajax && (Ajax != null)) {
	Ajax.Responders.register({
	  onCreate: function() {
        if($('spinner') && Ajax.activeRequestCount>0)
          Effect.Appear('spinner',{duration:0.5,queue:'end'});
	  },
	  onComplete: function() {
        if($('spinner') && Ajax.activeRequestCount==0)
          Effect.Fade('spinner',{duration:0.5,queue:'end'});
	  }
	});
}
$(document).ready(function () {
	/* Store locator enter key validation */
	$(".storeLocator").keypress(function(e){
		return validateStoreLocator($(this),e);
	});
	
	/* Modal Closer Calls */
	$("#fadeContainer").live('click', hideModal);
	$("#modalClose").live('click', hideModal)
	
	$(".searchInputButton").live('click',function(){
		return validateSearch();
	});
	
	/* Show Availability links */
	$(".availability-link").click(showAvailability);
	
	/* Popup windows */
	$(".newWinPop").click(newWinPop);
});


function newWinPop(){window.open($(this).attr('href'),'terms','scrollbars=yes,width=700,height=600');return false;}

function showAvailability() {
    $.get($(this).attr('href'), showImagePopup);
    return false;
}

/* Storelocator validation */
function validateStoreLocator(slForm,e){
	if (e.which == 13 || e == 'goButton'){
		var thisZip = slForm.find('input.zipcode').val();
		var thisCity = slForm.find('input.city').val();
		var thisState = slForm.find('select.state').val();
		var thisStore = slForm.find('input.storeId').val();
		var errTxt = '';
		if(thisStore != undefined && thisStore!='' && thisStore!='Store Id'){
			$('.zipcode').val("");
			$('.state').val("");
			$('.city').val("");
			slForm.submit();
			return true;
		}
		if ((thisCity != 'City' && thisCity != '') && thisState!=''){
			$('.zipcode').val("");
			slForm.submit();
			return true;
		}
		if (thisZip != 'Zip' && thisZip != ''){
			$('.state').val("");
			$('.city').val("");
			slForm.submit();
			return true;
		}
		if((thisZip=='Zip' || thisZip=='') && (thisCity=='City' || thisCity=='') && thisState=='' && (thisStore=='' || thisStore=='Store Id' || thisStore == undefined)){
			errTxt = 'Please fill in the fields';
		}else if((thisCity=='City' || thisCity=='') && thisState!=''){
			errTxt = 'Please select a City';
		}else if(thisState=='' && (thisZip=='Zip' || thisZip=='')){
			errTxt = 'Please select a State';
		}
		slForm.find('.errors').html(errTxt);
		return false;
	}
}

/* Search Box Validation */
function validateSearch() {
	var content = $('#search-control .inputbox').val();
	if(content == "Search Site" || content == ""){
		return false;
	}
}

/* Absolute Centering Code */
$.clientCoords=function(){var a={width:0,height:0};if(document.documentElement){a.width=document.documentElement.scrollWidth;a.height=document.documentElement.scrollHeight}else if(window.innerWidth&&window.innerHeight){a.width=window.innerWidth;a.height=window.innerHeight}return a};var win=$(window),getViewport=function(){return{top:win.scrollTop(),left:win.scrollLeft(),width:win.width(),height:win.height()}};

/* Zip Code Validator */
function validateZipcode(zip){
	postalCodeRegex = /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/;
	return postalCodeRegex.test(zip);
}

/* Modal Code */
function showModal(divName) {
	$(".modalHide").hide();
	$("#fadeUpContent").removeClass();
	$("#fadeUpContent").addClass("fadeUp");		
	var ctntDiv = "#"+divName;
	$(".fadeUp").hide();
	var fadeCont = $("#fadeContainer"); 
	fadeCont.hide();
	fadeCont.addClass("transparent");
	fadeCont.css("width", "100%");
	fadeCont.css("height", $(document).height());
	fadeCont.show();
	$(ctntDiv).css("left", ($.clientCoords().width - $(ctntDiv).width())/2);
	$(ctntDiv).fadeIn("slow");
	if($(ctntDiv).css('height')!='auto'){
		var mH = $(ctntDiv).css('height').substring(0,$(ctntDiv).css('height').length-2);	
	}else{
		var mH = 120;
	}
	var cTop = getViewport().height - mH;
	cTop=cTop/2;
	cTop=cTop-30;
	var tmp = $(ctntDiv).css('top').substring(0,$(ctntDiv).css('top').length-2);
	$(ctntDiv).css("top",getViewport().top+cTop);	
	return false;
}
hideModal = function(callBack){
	$(".modalHide").show();
	$("#fadeContainer").hide();
	$("#fadeContainer").removeClass();
	$("#fadeCtn").fadeOut("slow");
	$(".fadeUp").each(function(i, el){ 
			if(!$(el).is(":hidden")){
				if(typeof callBack == 'function'){ $(el).fadeOut("slow", callBack); }
				else{ $(el).fadeOut("slow"); }
			}
		}
	);
	return false;
}

function showHomePageModal() {
	if($.cookie('PreRegModal')!=1){
		modal = '<div id="fadeContainer"></div>';
		modal+= '<div id="signupModal" class="fadeUp"><a href="/rewards/join"><div id="modalSignup"><span class="acsTxt">Pre-Register Now</span></div></a><div id="modalClose"></div></div>';
		modal+= ''
		$("body").prepend(modal);
		showModal('signupModal');
		$.cookie('PreRegModal', '1',{expires: 700, domain: 'biglots.com'});
	}	
}

function startHomePageSlideshow() {
	$("#slideshow").webwidget_slideshow_dot({
        slideshow_time_interval: '5000',
        slideshow_window_width: '771',
        slideshow_window_height: '348',
        slideshow_title_color: '#17CCCC',
        slideshow_foreColor: '#000'
    });
}
