// JavaScript Document
google.load("jquery", "1.3.2");
if("https:" == document.location.protocol){
} else {
google.load("maps", "2.x");
	}
google.setOnLoadCallback(function() {

	jQuery.fn.extend({scrollTo: function( speed ){if(speed === null){speed = 1000;}$('html,body').animate({scrollTop: $(this).offset().top},speed);}});
	jQuery.fn.extend(
		{
			isEmpty: function(  ){
				if($(this).val().length == 0){
					return true;
				} else {
					return false;
				}
			},
			isFormElement: function(  ){
				domE = this.get(0);
				tagName = domE.tagName.toLowerCase();
				if(tagName == 'input' || tagName == 'select' || tagName == 'textarea'){
					return true;
				} else {
					return false;
				}
			}
		}			 
	);
	$("a.external").each(function(){
		$(this).attr("target","_blank");
	});
	if($(".google_map").size() && GBrowserIsCompatible() ){
		$(".google_map").each(function(){
			latitude  = $(this).find(".latitude").html();
			longitude = $(this).find(".longitude").html();
			// Create Map
			map = new GMap2(this);
			latLng = new GLatLng(latitude,longitude);
			map.setCenter(latLng,12);
			map.addOverlay(new GMarker(latLng));
		});
	}
	$(".enlarge").fancybox({
		'hideOnContentClick' : false,
		'frameWidth' : 450,
		'frameHeight': 150
	});
	
	$(".fancybox").fancybox({
		//'hideOnContentClick' : false,
		'frameWidth' : 425,
		'frameHeight': 350
	});
	
	$(".shipping-address").blur(updateShippingEstimate);
	$(".billing-address").blur(updateShippingEstimate);
	function updateShippingEstimate(){
		// JS Enabled for this step - Notify the form to skip JS
		if($("#js-enabled").size() == 0){
			$("<input />").attr("id","js-enabled").attr("name","js_enabled").attr("type","hidden").val("true").appendTo($(this).parents("form"));
		}
		if($("#same_as_billing:checked").val() == null){
			address  = $(".shipping-address");
		} else {
			address  = $(".billing-address");
		}
		
		address1 = address.get(0).value;
		address2 = address.get(1).value;
		city     = address.get(2).value;
		state    = address.get(3).value.toUpperCase();
		zip      = address.get(4).value;
		
		if(address1 =="" || city =="" || state =="" || zip == ""){
			return;
		}
		
		var url  = '/ups_shipping_rates.sstg';
		var data = { address_1: address1, address_2:address2, city:city, state:state, zip:zip};
		$.getJSON(url,data,callback);
		function callback(json){
			// Do nothing just save the estimate into the cart
		}
	}
	if($(".update-total").size()){ // Run this code if this is the confirmation page
		var value = $(".update-total").get(0).value.split(" - ");
		var total = value[1]*1 + $("#cart-subtotal").html()*1 + $("#cart-tax").html()*1;
		$("#cart-total").html(Math.round(total*100)/100);
	}
	$(".update-total").change(function(){
		var value = this.value.split(" - ");
		var total = value[1]*1 + $("#cart-subtotal").html()*1 + $("#cart-tax").html()*1;
		$("#cart-total").html(Math.round(total*100)/100);
	});
	
	$(".inner-label").each(replaceInnerLabel).focus(emptyInnerLabel).blur(replaceInnerLabel);
	function emptyInnerLabel(){
		if($(this).attr("title") == $(this).val()){
			$(this).val("");
		}
	}
	function replaceInnerLabel(){
		if($(this).val() == ""){
			$(this).val($(this).attr("title"));
		}
	}
	/* Printer Friendly */
	$("#printer-friendly").click(function(){
		window.print();
		return false;
	});
	
	/* Make all shipping address items required */
	$("#same_as_billing").click(function(){
		$(".shipping").each(function(){
			$(this).toggleClass("required");
			if($(this).hasClass("highlight")){
				$(this).removeClass("highlight");
			}
		});
	});
	
	$(".required").blur(function(){
		if($(this).isEmpty()){
			if(!$(this).hasClass("highlight")){
				$(this).addClass("highlight");
			}
		} else {
			if($(this).hasClass("highlight")){
				$(this).removeClass("highlight");
			}
		}	
	});
	$("form").submit(function(){
		var err = false;
		// Check if this form has any required fields
		children = $(this).find(".required");
		if(children.size()){ 
			$(children).each(function(){
				if($(this).isFormElement()){
					if($(this).isEmpty()){
						if(!$(this).hasClass("highlight")){
							$(this).addClass("highlight");	
						}
						err = true;
					}
				}
			});
		}
		if(err){
			alert("Please complete all required fields.");
			return false;
		} else {
			return true;
		}
	});
	
});

