var winWidth	= $(window).width();
var winHeight	= $(window).height();
var xMargin	= winWidth /2;
var yMargin	= winHeight /2;
var idArray	= new Array();
var slugArray	= new Array();
var timer;
var scrolltimer;

// -------------------------------------------------------------------
function getVendorSelect(){
	clearTimeout(timer);
	$("#vendors-loader").css("visibility","visible");
	tmpStateVal	= $("#state-country").val();
	$("#state-country").find("option:selected").each(function(){
		stateCountry	= $(this).parent().data("value");
	});
	tmpName	= $("#vendors-text-search").val();
	tmpDefault	= $("#vendors-text-search")[0].defaultValue;
	tmpTrim	= $.trim(tmpName);
	var JSONObj	= new Object;

	if (typeof(stateCountry) != "undefined"){
		if (stateCountry == "state"){JSONObj.state = tmpStateVal;}
		if (stateCountry == "country"){JSONObj.country = tmpStateVal;}
	}
	if (tmpTrim == tmpDefault){tmpTrim = '';}
	if (tmpTrim.length > 0){JSONObj.name = tmpName;}
	JSONStr	= JSON.stringify(JSONObj);
	$.post("/wp-content/themes/vanwinkle/get-vendors-results-only.php", {data : JSONStr}, function(data){
		$("#vendors-results").html(data);
		$("#vendors-loader").css("visibility","hidden");
	});
}
// -------------------------------------------------------------------
function showMap(tmpDiv,tmpAddress,mapsAddress){
	$(tmpDiv).css('display','block');
	$("#vendors-map-error").css('display','none');
	$("#vendors-map-info").append('<a href="http://maps.google.com/?daddr='+mapsAddress+'">Get Directions</a>');
	$(tmpDiv).gmap3({
		action: 'addMarker',
		address: tmpAddress,
		unit: 'mi',
		map:{center: true, zoom: 14},
		marker:{options:{draggable: false}},
	});
}
// -------------------------------------------------------------------
function noMap(tmpDiv, tmpAddress){
	$(tmpDiv).css('display','none');
	outAddress = tmpAddress.split(' ').join('+');
	$(".gmaps-link").attr("href","http://maps.google.com/?q=" + outAddress);
	$("#vendors-map-error").css('display','block');
}
// -------------------------------------------------------------------
function scrollToPage(tmpPage, animTime){


	$("#vendors-map-page").fadeOut(50);
	offset		= $(tmpPage).position();
	tmpPageWidth	= $(tmpPage).width();
	relMargin	= tmpPageWidth / 2;
	xPos		= 0 - offset.left - relMargin;

	bgAnim		= (xPos / 3.5) + "px 50%";
	$("#article-wrapper").animate({left:xPos}, animTime);
	$('body').animate({backgroundPosition: bgAnim}, animTime);

	tmpDiv		= "#" + $(tmpPage).attr('id');
	$("article").removeClass("active");
	$(tmpDiv).addClass("active");
}
// -------------------------------------------------------------------

$(document).ready(function(){
	//Redirect "enter" link.
	$(".legal-entry").attr("href","/enter-js.php");

	//Space articles.
	$("article").css('marginLeft', xMargin);
	$("article").css('marginRight', xMargin);

	//Grab IDs.
	$(".main-nav").each(function(){
		tmpID		= $(this).data("id");
		tmpSlug	= $(this).data("slug");
		idArray.push(tmpID);
		slugArray.push(tmpSlug);
	});

	//Load pages into articles.
	i = 1;
	for (tmpID in idArray){
		tmpDiv		= "#page-" + i;
		tmpClass	= slugArray[tmpID] + "-page";
		i++;
	//Hard-coded for now.
		tmpPath = "/wp-content/themes/vanwinkle/get-page-contents.php?id=";
		if ($(tmpDiv).is(':empty')) {	//Only add to empty divs. Remove this 'if' in case of errors.
			$(tmpDiv).load(tmpPath + idArray[tmpID]);
			$(tmpDiv).addClass(tmpClass);
		}
	}
	

	//Reset the position.
	scrollToPage(".active", 1);

	//Enable time microformat.
	jQuery("abbr.timeago").timeago();

});


// -------------------------------------------------------------------
// Window resizing
$(window).resize(function() {
	clearTimeout(scrolltimer);
	scrolltimer	= setTimeout("resizeWindow()",15);
});

function resizeWindow(){
	winWidth	= $(window).width();
	if (winWidth < 920){$(window).width('920'); winWidth = 920;}
	winHeight	= $(window).height();
	xMargin	= winWidth /2;
	yMargin	= winHeight /2;
	$("article").css('marginLeft', xMargin);
	$("article").css('marginRight', xMargin);
	scrollToPage(".active", 0);
	clearTimeout(scrolltimer);
}
// -------------------------------------------------------------------
// History pushstates
$(".logo-button").live("click",function(e){
	tmpURL		= $(this).attr("href");
	if (typeof(window.history.pushState) == 'function') {window.history.pushState(null, document.title, tmpURL);} 
	else {window.location.hash = '#!' + tmpURL;}
	_gaq.push(['_trackPageview', tmpURL]);
});

$(".main-nav").live("click",function(e){
	tmpURL		= $(this).attr("href");
	if (typeof(window.history.pushState) == 'function') {window.history.pushState(null, document.title, tmpURL);} 
	else {window.location.hash = '#!' + tmpURL;}
	_gaq.push(['_trackPageview', tmpURL]);
});

$(".product").live("click",function(e){
	tmpURL		= $(this).attr("href");
	if (typeof(window.history.pushState) == 'function') {window.history.pushState(null, document.title, tmpURL);} 
	else {window.location.hash = '#!' + tmpURL;}
	_gaq.push(['_trackPageview', tmpURL]);
});
// -------------------------------------------------------------------
// Legal "ENTER" click
$(".legal-entry").live("click",function(e){
	thisLink = $(this).attr("href");
	$("#age-screen").fadeOut(600, function(){
		$(this).load(thisLink);
	});
	e.preventDefault();
});
// -------------------------------------------------------------------
// Main nav click
$(".logo-button").live("click",function(e){
	tmpPage		= $(this).data("page");
	scrollToPage(tmpPage, 1000);
	e.preventDefault();
});

$(".main-nav").live("click",function(e){
	tmpPage	= $(this).data("page");
	scrollToPage(tmpPage, 1000);
	e.preventDefault();
});
// -------------------------------------------------------------------
// Products page
$(".product").live("mouseover",function(){
	$(this).find("img").animate({opacity:1.0}, 200);
	tmpTitle	= $(this).data('product');
	$("#product-name").text(tmpTitle);
});

$(".product").live("mouseout",function(){
	$(this).find("img").animate({opacity:0.0}, 200);
	$("#product-name").empty();
});

$(".product").live("click",function(e){
	tmpID	= $(this).data("id");
	tmpDiv	= "#product-"+tmpID;
	$(tmpDiv).fadeIn(300);
	e.preventDefault();
});

$(".close-button").live("click",function(e){
	$(this).parent().fadeOut(300, function(){
//			$("#vendors-map-error").css('display','none'); //Take care of the vendors stuff
//			$("#vendors-map").css('display','none');
	});
	e.preventDefault();
});
// -------------------------------------------------------------------
// Vendors page
$('input[type="text"]').live('focus',function(){
	if (this.value == this.defaultValue){this.value = '';}  
	// if (this.value != this.defaultValue){this.select();}
});



$('input[type="text"]').live('blur',function(){
	if ($.trim(this.value) == ''){this.value = this.defaultValue;}
});

$("#vendors-text-search").live('keydown',function(e){ 
	if (e.which == 13) e.preventDefault();
});
 
$("#vendors-text-search").live('keyup',function(){
	clearTimeout(timer);
	timer	= setTimeout("getVendorSelect()",175);
});

$("#state-country").live('change',function(){
	clearTimeout(timer);
	getVendorSelect();
});


$(".display-map").live('click',function(e){
	$tmpInfo	= $(this).parent();
	$("#vendors-map-info").html($tmpInfo.html());
	$("#vendors-map-info").find('.display-map').remove();
	tmpAddress	= $(this).data('address');
	tmpMapAddress	= $(this).data('mapsaddress');
	tmpLatLong	= 0;
	$("#vendors-map").gmap3({
		action:'getAddress',
		address: tmpAddress,
		callback:function(results){
			if(results){
				tmpPrecision	= results[0].geometry.location_type;
				finalAddress	= results[0].formatted_address;
				if (tmpPrecision != 'APPROXIMATE'){showMap("#vendors-map",finalAddress,tmpMapAddress);} else {noMap("#vendors-map", tmpAddress);}
			} 
			else {noMap("#vendors-map", tmpAddress);}
		}
	});
	$("#vendors-map-page").fadeIn(1);
	e.preventDefault();
});
// -------------------------------------------------------------------
// Single page overlay
$(".show-page-overlay").live('click',function(e){
	tmpID = $(this).data('id');
	tmpPath = "/wp-content/themes/vanwinkle/get-post-contents.php?id=";
	$("#page-div-text").load(tmpPath + tmpID,function(){
		$("#page-screen").fadeIn(300);
	});
	e.preventDefault();
});

$("#page-screen").live('click',function(){
	$(this).fadeOut(300);
});

