// JavaScript Document
$(document).ready(function() {
	$.history.init(callback);
	$("#fooditem input").live('blur',function() {
		daprice="$"+roundDollar(Math.round($("input#foodprice").val() * $("input#foodqty").val()*100)/100);
//		$("input#totalprice").val(daprice);
		$("#totalprice").html("$"+roundDollar(Math.round($("input#foodprice").val() * $("input#foodqty").val()*100)/100));
	});
	$(".scrollable").scrollable();
	$(".items img").click(function() {
		if ($(this).hasClass("active")) { return; }
		var url = $(this).attr("src").replace("_t", "");
		var wrap = $("#image_wrap").fadeTo("medium", 0.5);
		var img = new Image();
		img.onload = function() {
			wrap.fadeTo("fast", 1);
			wrap.find("img").attr("src", url);
		};
		img.src = url;
		$(".items img").removeClass("active");
		$(this).addClass("active");
		autoplay:true;
	}).filter(":first").click();
	$("#fooditem .formbuttons").live('click',function() {
		$('#items-in-cart').load('elements/add-item.php?id='+$("#foodid").val()+'&qty='+$("#foodqty").val()+($('#options').val()?'&options='+$('#options').val():""));
		$("#items-in-cart").show();
		$('#right_content').load('/ajax_load.php?page=cart');
//		$("title").load('/ajax_title.php?page=cart');
		$('body').attr('id','bodycart');
		location.hash='cart';
		if($('#edit_tool_edit_page').length==0) return(false);
	});
	$("#left_menu a, #top_menu a").click(function () {
		$(this).blur();
		var myregexp= /[a-z\-_]+\.html$/i;
		mymatch = myregexp.exec(this.href);
		var bodyFilter=/[^a-zA-Z_]/;
		$('#right_content').load('/ajax_load.php?page='+mymatch[0].substring(0,mymatch[0].length-5), setTimeout('reloadstuff();',700));
//		$("title").load('/ajax_title.php?page='+mymatch[0]);
		newbody=mymatch[0].substring(0,mymatch[0].length-5);
		location.hash=newbody;
		newbody=newbody.replace(bodyFilter,'');
		newbody=newbody.replace(bodyFilter,'');
		$('body').attr('id','body'+newbody);
		if($('#edit_tool_edit_page').length==0) return(false);
	});
	$(".productlist .delete").live('click',function() {
		var thisid=this.id.substring(6,this.id.length-2);
		var thisopt=this.id.substring(this.id.length-1,this.id.length);
		if(thisopt<1) thisopt='0';
		$("#productlist"+thisid+'-'+thisopt).hide('slow', function () {
			$("#productlist"+thisid+'-'+thisopt).detach();
		});
		$('#items-in-cart').load('elements/add-item.php?id='+thisid+'&delete=true&options='+thisopt, function() {
			if($("#items-in-cart").text().length<1) $("#items-in-cart").hide();
			$('#right_content').load('/ajax_load.php?page=cart', setTimeout('reloadstuff();',700));
		});
		return(false);
	});
	$(".productlist .update").live('click',function() {
		var thisid=this.id.substring(6,this.id.length-2);
		var thisopt=this.id.substring(this.id.length-1,this.id.length);
		var thisqty=$("#productlist"+thisid+(thisopt?"-"+thisopt:"")+" input[name=ItemQty]").val();
		if(thisopt<1) thisopt='0';
		$('#items-in-cart').load('elements/add-item.php?id='+thisid+'&options='+thisopt+'&update=true&qty='+thisqty, function() {
			if($("#items-in-cart").text().length<1) $("#items-in-cart").hide();
			$('#right_content').load('/ajax_load.php?page=cart', setTimeout('reloadstuff();',700));

		});
		return(false);
	});
	$("#zipcode .update").live('click',function() {
		$("#submitme").html("");
		$("#submitme").removeClass('formbuttons');
		$("#freight").html('Freight: <img src="/images/loading.gif" alt="Loading" />');
		loadthefreight();
	});
	if($("#items-in-cart").text()>0) {
			$("#items-in-cart").show();
	}
	$("select[name=freight]").live('change', function() {
		$.get("/ajax_freight.php?amount="+$("select[name=freight]").val(), function () {
			recalculatestuff();
		});
	});
	$("#submitme").live('click',function () { 
		submitdaform();
	});
	$("form#cartform").live('submit',function() {
		submitdaform();
	});
	$("#popper .closebox").live('click',function() {
		$("#overlay").fadeOut('1000');
	});
	reloadstuff();
});
function callback(hash) {
	if(location.hash.length>0) {
		var bodyFilter=/[^a-zA-Z_]/;
		newbody=location.hash.replace(bodyFilter,'');
		newbody=newbody.replace(bodyFilter,'');
		newbody=newbody.replace(bodyFilter,'');
		$('body').attr('id','body'+newbody);
		$('#right_content').load('/ajax_load.php?page='+location.hash.substring(1,location.hash.length), setTimeout('reloadstuff();',700));
//		$("title").load('/ajax_title.php?page='+location.hash.substring(1,location.hash.length));
	}
}
function roundDollar(tempvar) {
	mynewvar=''+tempvar+'';
	retvar='';
	checker=mynewvar.length-mynewvar.indexOf('.');
	if(mynewvar.indexOf('.')== -1) retvar=(mynewvar+".00");
	else if(checker=="1") retvar=(mynewvar+"0");
	else if(checker=="2") retvar=(mynewvar+"0");
	else retvar=(mynewvar);
	return(retvar);
}
	function initialize_gmap() {
		var latlng = new google.maps.LatLng(30.30,-92.09);
		var myOptions = {
			zoom: 12,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		var myLatlng = new google.maps.LatLng(30.2610750,-92.1085790);
		var contentString = "<b>The Best Stop Supermarket in Scott, LA</b><br /><img src='/images/client_images/thestoret.jpg' alt='Best Stop' style='float:left; margin:0 5px 5px 0;' />615 Hwy 93 N.<br />Scott, LA 70583<br /><a href='http://maps.google.com/maps?oe=utf-8&client=firefox-a&ie=UTF8&q=The+Best+Stop+Supermarket&fb=1&gl=us&hq=best+stop+supermarket&hnear=Lafayette,+LA&cid=5712922055313020451'>Get Directions</a><br />(337) 233-5805<br /><a href='beststopinscott.com'>beststopinscott.com</a>";
		var marker = new google.maps.Marker({
			position: myLatlng,
			map: map,
			title: contentString
		});
		var infowindow = new google.maps.InfoWindow({
			content: contentString
		});
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map,marker);
		});
		infowindow.open(map,marker);
	}
function reloadstuff() {
	try {
		$("#formbuttons").html('<a href="#" onclick="document.forms[\'contactform\'].reset();">Reset Form</a> <a href="#" onclick="document.forms[\'contactform\'].submit();">Send Message</a>');
	}catch(err) { }
	try { initialize_gmap(); } catch(err) { }
	try {
		loadthefreight();
	} catch(err) { }
}
function recalculatestuff() {
	if($("#freight").text()!="Freight: Please enter a valid ZIP Code.") {
		var mymath= /[0-9\.]+/;
		mysubt = parseFloat(mymath.exec($("#subtotal").text()));
		myfreight = parseFloat($("select[name=freight]").val());
		mypackaging = parseFloat(mymath.exec($("#packaging").text()));
		$("input[name=Shipping]").val(myfreight+mypackaging+";"+$("select[name=freight] option[value='"+$("select[name=freight]").val()+"']").text());	
		$("#grandtotal").text("Grand Total: $"+Math.round((mysubt+myfreight+mypackaging)*100)/100);
	}
}
function loadthefreight() {
	var zipcode=$("#zipcode input").val();
	var length=$("#length").text();
	var width=$("#width").text();
	var height=$("#height").text();
	var totalqty=$("#totalqty").text();
	if(zipcode!='')$("#freight").html('Freight: <img src="/images/loading.gif" alt="Loading" />');
	$("#freight").load('/ajax_timeintransit.php?totalqty='+totalqty+'&zip='+zipcode+"&length="+length+"&width="+width+"&height="+height, function() {
		recalculatestuff();
		if($("#freight").text()!="Freight: Please enter a valid ZIP Code.") {
			$("#submitme").html("Submit and Ship");
			$("#submitme").addClass('formbuttons');
			$("#needzipcode").hide('slow');
		} else {
			$("#needzipcode").show('slow');
		}
	});
	$("#salestax").load('/ajax_checktax.php', function () {
		$("input[name=ApplyTax]").val($("#salestax").text());
	});
}
function submitdaform() {
//	alert("Online Submission not available, please call Dana or Damon at (337) 233-5805 to place your order.");
//	return(false);
	if($("input[name=Shipping]").val()!='') { document.forms[0].submit(); }
	else { 
		loadthefreight();
		if($("input[name=Shipping]").val()=='') {
			alert('There was an error with freight. Please try again or call Dana or Damon at (337) 233-5805 to place your order.');
			return(false);
		} 
		return(false);
	}
}
