//  helper functions
function pageVertOffset() {
	var ypos = 0;
	// -= Vertical Position =-
	if (window.innerHeight) { ypos = window.pageYOffset; } 
	else if (document.documentElement && document.documentElement.scrollTop) { ypos = document.documentElement.scrollTop; }
	else if (document.body) { ypos = document.body.scrollTop; }
	return ypos;
}
function pageHorzOffset() {
	var xpos = 0;
	// -= Horizontal Position =-
	if (window.innerWidth) { xpos = window.pageXOffset; }
	else if (document.documentElement && document.documentElement.scrollLeft) { xpos = document.documentElement.scrollLeft; }
	else if (document.body) { xpos = document.body.scrollLeft; }
	// -==-
	return xpos;
}
function callBackAjax(html){
	// put HTML into the results container
	$("#wheretobuy #results").html(html);
	// apply jQuery to links to perform AJAX on more location links on click
	$("#wheretobuy #morelocations a").click(function () {
		// display loading screen
		$("#wheretobuy #results").html('<p class="loading"><img src="/images/test/ajax-loader-sky.gif" alt="loading..." /> Loading...</p>');
		// apply AJAX
		var URL = this.href.replace(/\?/,"?AJAX=t&")
		$.get(URL, function(html){ callBackAjax(html); });
		return false;
	});	
	return false;	
}

// jQuery code
$(document).ready(function() {
	// Reposition the header right away incase the page loads to a 'deeper' section
	if($("#header").css('position') != 'fixed') { 
		$("#header").css('top', pageVertOffset() + 'px');
	}
	/*
	 * Reposition the header if the page scrolls and the browser doesn't support 'fixed'
	 */
	$(window).scroll(function () { 		
		// only scroll vertical if position isn't fixed
		if($("#header").css('position') != 'fixed') { 
			$("#header").css('top', pageVertOffset() + 'px');
		}
		if(typeof(IE6BROWSER) == "undefined" && pageHorzOffset() != 0) { 
			$("#header").css('left','-' + pageHorzOffset + 'px');
		}
	});

	/*
	 * Need to add JavaScript effects for window resize due to scroll manipulation.
	 */
	$(window).resize(function () {
		var hxpos = parseInt($("#header").css('left')); // position of the header
		var windowWidth = $(window).width();
		if(hxpos < 0 && windowWidth >= 720 ) { 
			$("#header").css('left','0px');
		}
	});
	
	/*
	 * Deactive "active" links by simply giving it the CSS style "deactive"
	 */
	$("#menu a").click(function () { 
		// activate all other classes
		$("#menu a").removeClass('deactive');
		$(this).addClass('deactive');
	});
	
	/*
	 * Use a remote request to load data for store locator
	 */
	$("#wheretobuy form").submit(function () {
		//
		$("#wheretobuy #results").html('<p class="loading"><img src="/images/test/ajax-loader-sky.gif" alt="loading..." /> Loading...</p>');
		// load form variables and 
		var postalcode = $("#wheretobuy #postalcode").val();
		//alert('3:postalcode received:'+postalcode);
		//$("#wheretobuy #results").load("locations.php", {'AJAX': true, 'submit': true, 'postalcode': postalcode});
		/**/
		// AJAX to get locations data
		$.get("locations.php", { 'AJAX': true, 'submit': true, 'postalcode': postalcode }, function(html){ callBackAjax(html); });
		/**/
		return false;
	 });
	
	/**/
	
});
