/* Author: Jim Webb */

jQuery(document).ready(function($){
	
	// ----------------- WATERMARK --------------------
	
	// look for an image with a watermark, and crop the watermark out
	// watermarks are 30px from the bottom
	
	$("#cropper img.watermark").load(function() {
		var height = $(this).height();
		$("#cropper").height(height-30);
	});
	
	// fire this also on document load
	var height = $("#cropper img.watermark").height();
	if (height) {
		$("#cropper").height(height-30);
	}
	
	// ----------------- HOME PAGE SLIDESHOW --------------------
	
	if ($("body").hasClass("home")) {
	
		// unhide the hidden images
		$("#slideshow #images li.visuallyhidden").removeClass("visuallyhidden");
		
		// build the slideshow
		$("#slideshow #images").cycle({ 
			fx:     'fade', 
			speed:  'fast',
			timeout: 4000, 
			pager:  '#pager',
			height: height,
			 
			// callback fn that creates a thumbnail to use as pager anchor 
			pagerAnchorBuilder: function(idx, slide) { 
				return '<li><a href="#">Image</a></li>'; 
			} 
		});
	};

	// ----------------- STOCK SEARCH RESULT HOVER --------------------
	
	if ($("body").hasClass("stock-search")) {
	
		$('body.stock-search #thumbnails .thumb').bt({
		  contentSelector: "i = $(this).parent().index(); $('li.preview:eq('+i+')').html(); ",
		  width: 460,
		  killTitle: false,
		  fill: 'rgba(255,255,255,0.9)',
		  strokeWidth: 0, /*no stroke*/
		  spikeLength: 15,
		  spikeGirth: 20,
		  padding: 8,
		  cornerRadius: 3,
		  cssClass: "preview",
		  shadow:           true,                 // use drop shadow? (only displays in Safari and FF 3.1) - experimental
		  shadowOffsetX:    2,                     // shadow offset x (px)
		  shadowOffsetY:    2,                     // shadow offset y (px)
		  shadowBlur:       5,                     // shadow blur (px)
		  shadowColor:      "#000",                // shadow color/alpha
		  showTip: function(box){ $(box).fadeIn(100); }
		});
		
	};


	// ----------------- STOCK INQUIRY SHOW FORM --------------------

	$("body.stock-single #stock-inquiry a").click(function(e) { 
		console.log("hi");
		$(this).hide().next().find("form").fadeIn(300);
		e.preventDefault();
	});



	// ----------------- PRINT ORDER FORM --------------------

	$("form#printorder").submit(function() {
		
		// remove name from the select menu, so it won't get sent to the server. We'll fill the hidden inputs with the price and shipping instead.
		$("#printsize").removeAttr("name");
		
		var selected = $("form#printorder #printsize option:selected");
		$("input#price").val($(selected).attr("data-price"));
		$("input#units").val($(selected).attr("data-shipping"));
		
		var convertedsize = $("input#product").val();
		convertedsize = convertedsize.replace("&times;","x");
		
		$("input#product").val($(selected).attr("data-size") + " - " + convertedsize);

		// okay, submit the form now.
		return true;
	
	});
	
	$("form#printorder #printsize").change(function() {
	
		// update the total when we change the size option
		var selected = $("form#printorder #printsize option:selected");
		var price = new Number($(selected).attr("data-price"));
		
		$("#total").html("$" + price.toFixed(2) );
	
	});


});
























