
jQuery(document).ready(function($){


	// HOVER
	$("#siteNav a").hover( 
		// over
		function(){
			var theImg = $(this).find('img:first');
			// store the original image src
			theImg.data( "originalsrc" , theImg.attr("src") );
			
			// add -over to the image unless it is already -over
			if (!theImg.attr("src").match(/-over/) ) {
				theImg.attr( "src", theImg.attr("src").replace(/\./, "-over.") );
			}
			
		},
		
		// out
		function(){
			var theImg = $(this).find('img:first');
			// replace the original src
			theImg.attr("src", theImg.data("originalsrc")).data("originalsrc", null);
		}
	);
	
	// add white rounded borders
	borders();
	
	// must be after the .border function
	var options = {
            zoomWidth: 400,
            zoomHeight: 265,
            showEffect: 'fadein',
            hideEffect: 'fadeout',
			title: false
        }
    $(".jqzoom").jqzoom(options);
	
	
	// open up home page teasers
	$('.teaser').hoverIntent(
		function(){
			$(this).children('.teaserOpen').stop(true, true).slideDown('slow');
		}, function(){
			$(this).children('.teaserOpen').stop(true, true).slideUp('slow');
		}	
	);
	
	// rounded corners on content
	cornerz();
	
	$('.products-grid a.product-image').cornerz({
		radius : 10
	});
	
	$('.mini-products-list .product-image').cornerz({
		radius : 10,
		background : '#B1C481'
	});
	
	$('#shopping-cart-table .odd .product-image').cornerz({
		radius : 10,
		background : '#ffffff'
	});
	
	$('#shopping-cart-table .even .product-image').cornerz({
		radius : 10,
		background : '#F6F6F6'
	});
	
	 
	// change the main photo on the product page by thumbnail click
	$(".more-views a").click(function(e){
		e.preventDefault();
		swapImage(
			$(this).attr('href'),
			 $(this).attr('rel')
			);
	
	});
	
	// change the main photo on the product page by attribute dropdown
	$("#attribute506").change(function(){
		var dropdown = $(this)[0];
		var color = dropdown.options[dropdown.selectedIndex].text;
		
		swapImage(
			$(".more-views a[alt=" + color + "]").attr('href'),
			$(".more-views a[alt=" + color + "]").attr('rel')
		);
		
		
	});
	
	// utility to swap the image
	function swapImage(smallImg, largeImg) {
		$("#image").attr('src', smallImg);
		$("a.jqzoom").attr('href', largeImg);
	}
		
});

window.onload = function() {
		// preload
		jQuery("#siteNav a img").each(
			function() {
				if (!jQuery(this).attr("src").match(/-over/) && !jQuery(this).attr("src").match(/-active/) ) {
					jQuery("<img>").attr( "src", jQuery(this).attr("src").replace(/\./, "-over.") );
				}
			}
		);
		

}

// functions to call after Prototype has injected content
function borders() {
	jQuery('.border').not('.hasBorders').wrapInner('<table border="0" cellpadding="0" cellspacing="0" width="100%" class="borderTable"><tr class="theRow"><td style="background : #fff;"></td></tr></table>');
	jQuery('.theRow').not('.hasBorders .theRow').before('<tr><td class="c_tl"></td><td class="horzBorder"></td><td class="c_tr"></td></tr>').after('<tr><td class="c_bl"></td><td class="horzBorder"></td><td class="c_br"></td></tr>');
	jQuery('.theRow').not('.hasBorders .theRow').prepend('<td class="vertBorder"></td>').append('<td class="vertBorder"></td>');
	jQuery('.border').addClass('hasBorders');
}

function cornerz() {
	jQuery('.cornerz').not('.hasCornerz').cornerz({
		radius : 10
	}).addClass('hasCornerz');
}
	