$(function(){

if ($("#main.drink").length > 0) {
    $('.slide-out-div').hide();
}
else {
    $('.slide-out-div').tabSlideOut({
        tabHandle: '.handle',                     //class of the element that will become your tab
        pathToTabImage: 'images/bg-find.gif',  //path to the image for the tab //Optionally can be set using css
        imageHeight: '211px',                     //height of tab image           //Optionally can be set using css
        imageWidth: '30px',                       //width of tab image            //Optionally can be set using css
        tabLocation: 'left',                      //side of screen where tab lives, top, right, bottom, or left
        speed: 300,                               //speed of animation
        action: 'click',                          //options: 'click' or 'hover', action to trigger animation
        topPos: '225px',                          //position from the top/ use if tabLocation is left or right
        leftPos: '0px',                          //position from left/ use if tabLocation is bottom or top
        fixedPosition: false                      //options: true makes it stick(fixed position) on scroll
    });
}


// locate zevia code
LSTAT = {
	OK: 0,
	ERROR_BADZIP: 1
}
$("input.locate-zevia-input").each(function(){
	var input = this;
	// this function fires when either the locate zevia button is pressed, or enter is pressed when the keycode input is focused
	// this function shows the locator element, and gathers the data for display in it.
	var submitCallback = function(){
		$(".locate-zevia-modal table td").parent().remove();
		$(".map").empty();
		if($(input).val() == "") {
			alert("Please enter your zip code to locate stores near you.");
			return;
		}
		var zip = $(input).val();
		
		$(".locate-zevia-modal").show().find(".table").hide().parent().find(".map").hide().parent().find(".loading").show();
		var url = "";
		if($(".locate-zevia-modal").hasClass("canada")) url="functionality.php?do=get_location_ca&zip=";
		else url="functionality.php?do=get_location&zip=";
		$.get(url+zip,function(data){
			var response = eval('(' + data + ')');
			if(typeof(response.status) == "undefined") return alert("Either not a valid zip code, or server error");
			if(response.status == LSTAT.ERROR_BADZIP) return alert("Bad Zip Code Entered, Please enter a valid zip code.");
			
			var zipcodecenter = new google.maps.LatLng(response.zipcode.latitude,response.zipcode.longitude);
            var map = new google.maps.Map($("#map").get(0),{
                center:zipcodecenter,
                zoom:9,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });

            
            var entry, markers = [], position, marker;
            for(var i = 0; i < response.result.length; i++){
                entry = response.result[i];
                var tr = $("<tr>").appendTo(".locate-zevia-modal table").get(0);
                $("<td>").html(entry.name).addClass("first").appendTo(tr);
                $("<td>").html(entry.address1).appendTo(tr);
                $("<td>").html(entry.city).appendTo(tr);
                $("<td>").html(entry.telephone).appendTo(tr);
                if($(".locate-zevia-modal").hasClass("canada")) $("<td>").html(entry.distance + " km").addClass("last").appendTo(tr);
                else $("<td>").html(entry.distance + " mi").addClass("last").appendTo(tr);
                position = new google.maps.LatLng(entry.lat,entry.lon);
                marker = new google.maps.Marker({position:position,map:map,title:entry.name});
                marker.setIcon("images/zevia_googlemaps_icon.png");    
                markers.push(marker);
            }
			$(".locate-zevia-modal table tr:even").addClass("even");
			$(".locate-zevia-modal .table").show().parent().find(".map").show().parent().find(".loading").hide();
			
		});
	}

	// preload the loading animated gif
	var loadingGif = new Image();
	loadingGif.src = "images/loading.gif";
	loadingGif.src = "../images/loading.gif";
	
	// set the bindings for the submit callback
	$(".locate-zevia-button").css({cursor:"pointer"}).click(submitCallback);
	$(this).bind("keypress",function(e){ if( (e.keyCode ? e.keyCode : e.which) == 13 ) submitCallback(); });
});

if($(".drink.drinkelement").length > 0){
	
	$(".drink .flavors a").click(function(){
		setTimeout(function(){
			$(".drink.drinkelement").hide().removeAttr("id");
			if(window.location.hash.length > 0) $(window.location.hash.replace("#",".")+".drink").show().attr("id","main");
			else $(".drink.drinkelement").eq(0).show().attr("id","main");
		},100);
	});
	$(".drink .flavors a:first").trigger("click");
	setInterval(function(){
		$(".drink .flavors a:first").trigger("click");
	},1000);
}

$(".rounded").corner("5px");

$("#newsletter-button").click(function() { $("#header #signup").fadeToggle(); });

});

