var $j = jQuery.noConflict();

var zip_text = "zip code";
$j("#zip").livequery("blur", function() { if ($j(this).val() == "") { $j(this).val(zip_text); }; });
$j("#zip").livequery("focus", function() { if ($j(this).val() == zip_text) { $j(this).val(""); }; });
	

$j(document).ready( function() {
	
	$j(".bubbles").pngFix();
	
	// $j(".content").equalHeights();
	
	$j("#searchsubmit,#zipsubmit").val(""); 
	
	var zip_text = "zip code";
	$j("#zip").livequery("blur", function() { if ($j(this).val() == "") { $j(this).val(zip_text); }; });
	$j("#zip").livequery("focus", function() { if ($j(this).val() == zip_text) { $j(this).val(""); }; });
	
	$j("#zip").live("keydown",function() { $j(this).numeric(); });
	$j("#zip").live("keyup",function() {
		var len = this.value.length;
		if (len >= 5) { this.value = this.value.substring(0, 5); }
    });
	
    // pre-submit callback 
	function showRequest(formData, jqForm, options) { 
	    var queryString = $j.param(formData); 
	 	return true; 
	} 
	 
	// post-submit callback 
	function showResponse(responseText, statusText)  { 
		// handled by target in options	
	} 
    
    // bind form using 'ajaxForm' 
	$j('#zip_form').livequery("submit", function() {
		$j(this).ajaxSubmit({
	        target:        '#zip_check',   // target element(s) to be updated with server response 
	        beforeSubmit:  showRequest,  // pre-submit callback 
	        success:       showResponse  // post-submit callback
		});
		return false;
	}); 
	
	$j('#get_started_form').livequery("submit", function() {
		$j(this).ajaxSubmit({
	        target:        '#get_started_output',   // target element(s) to be updated with server response 
	        beforeSubmit:  showRequest,  // pre-submit callback 
	        success:       showResponse  // post-submit callback
		});
		return false;
	}); 
	
	$j("#productWrapper a[title=Natural]").append('<br /><img src="/wp-content/themes/earth2o/images/prod_bottles.png" alt="" />');
	$j("#productWrapper a[title=Nursery]").append('<br /><img src="/wp-content/themes/earth2o/images/prod_1gal.png" alt="" />');
	$j("#productWrapper a[title=Distilled]").append('<br /><img src="/wp-content/themes/earth2o/images/prod_1gal.png" alt="" />');
	$j("#productWrapper a[title=Delivery]").append('<br /><img src="/wp-content/themes/earth2o/images/prod_5gal-1.png" alt="" />');
	
	
	
	$j("a[rel*=zip_start_over]").live("click", function() {
		$j("#zip_check").load("/zip_check/zip_form.php");
		return false;
	})
	
	function show_video(href) {
		$j("#videoPlayer").fadeOut( function() {
			$j(this).load(href, function() {
				$j(this).fadeIn();
			});
		});
		
	}
	
	show_video("http://earth2o.com/vimeovid.php?vid=8529585");
	// $j("#videoPlaylist a:first").parent().addClass("active_vid");
	
	$j("#videoPlaylist a").live("click",function() {
		show_video($j(this).attr("href"));
		$j(".active_vid").removeClass("active_vid");
		if ($j(this).children().is(".thumb")) {
			$j(this).parent().addClass("active_vid");
		}
		else {
			$j(this).parent().parent().addClass("active_vid");
		}
		return false;
	})
	
	$j('a[rel*=facebox]').live("mousedown", function() {
	 	$j("#videoPlayer object").hide();
	 	$j(this).facebox();
	 	return false;
	});
	
		$j("#facebox .close").live("mousedown", function() {
			$j("#videoPlayer object").show();
		})
	
	
	/* use ajax to switch between tabs (not in use)
	$j(".subNav a").live("click", function() {
		$j(".subNav .current_page_item").removeClass("current_page_item");
		$j(this).parent("li").addClass("current_page_item");
		$j("#content").fadeOut( function() {
			$j(this).html('<img src="/wp-content/themes/earth2o/images/ajax-loader.gif" alt="" />Loading...').fadeIn();
		});
		$j("#fillCol").load($j(this).attr("href")+" #content", function() {
			$j(this).fadeIn();
		});
		return false;
	});
	*/
	
	function size_product(product,size) {
		switch (size) {
			case "grow":
				var set_top_margin = "0";
				var set_width = "85%";
				var set_height = "85%";
				var set_font_color = "#003d79";
				var set_font_size = "100%";
				break;
			case "shrink":
				var set_top_margin = "37px";
				var set_width = "135px";
				var set_height = "185px";
				var set_font_color = "#555";
				var set_font_size = "80%";
				break;
		}
				
		var product = product.replace(" ", ".");	
		var product = product.replace(" ", "."); // doing this twice because apparently js only replaces one at a time(?)	
		var product = "#productWrapper ."+product+" a";
		$j(product).animate({ 
			marginTop: set_top_margin,
		    width: set_width,
		    height: set_height,
		    color: set_font_color,
		    fontSize: set_font_size
		}, 300 );
		
	}
	
	size_product($j("#productWrapper .current_page_item a").parent().attr("class"),"grow");	
	
	$j("#productWrapper li a img").mouseover(function() {
		if (!$j(this).parent().parent().is(".current_page_item")) size_product( $j(this).parent().parent().attr("class"),"grow");
	});
	
	$j(".subNav li a").mouseover(function() {
		if (!$j(this).parent().is(".current_page_item")) size_product( $j(this).parent("li").attr("class"),"grow");
	});

	$j("#productWrapper li a img").mouseout(function() {
		if (!$j(this).parent().parent().is(".current_page_item")) size_product( $j(this).parent().parent().attr("class"),"shrink");
	});
	
	$j(".subNav li a").mouseout(function() {
		if (!$j(this).parent().is(".current_page_item")) size_product( $j(this).parent("li").attr("class"),"shrink");
	});
	
	$j("#productWrapper li a,.subNav li a").click(function() {
		size_product( $j(this).parent("li").attr("class"),"shrink");
		size_product("current_page_item","shrink");
	});

});
