$(document).ready(function() {
 	
 	var $body = $('body');
 	$body.removeClass('nojs');
 	
 	$('#nav').subnav({pause: "800"});
	
	// Home
	if ($body.hasClass('home'))
	{
		$("#featured-items").scrollable({ 
									
									size: 1, 
									vertical: true, 
									loop: true
									
								   })
								  .navigator()
							      .autoscroll();
	
		$('#featured-products').scrollable({ size: 4 }).navigator();
		
		$("#homeads li").equalizeHeights();
	}
	// Shopping
	else if ($body.hasClass('shop'))
	{
		/* lightbox for products */
		var $detailimage = $('.detail-image'),
		    $thumbs = $('ul.detail-image-thumbs li'),
		    $thumblinks = $thumbs.find('a');
		
		$(".lightbox").lightbox({fitToScreen:'true'});
		
		$detailimage.not(':eq(0)').hide();
		//$detailimage.eq(0).fadeIn("normal");
		
		
		if ($thumbs.length <= 1) { $thumbs.hide(); }
		
		$thumblinks.click(function() {
		    $detailimage.hide();
		    $detailimage.eq($thumblinks.index(this)).fadeIn("slow");
		    
		    return false;
		});

		if ($thumbs.length <= 1) { $thumbs.hide(); }
		
		$thumblinks.click(function() {
		    $detailimage.hide();
		    $detailimage.eq($thumblinks.index(this)).fadeIn("slow");
		    
		    return false;
		});

	}

	/* shopping cart */
	var $cart = $('table#mycart');
	if ($cart.length > 0)
	{
		var $cartnav = $('#accountnav a[href=/checkout/cart/]'),
		    cartitems = $cart.find('td.cartItems').length;
		    items = parseInt($cartnav.text().split(' ')[0]);
		
		if (items !== cartitems)
		{
			$cartnav.fadeOut('fast', function() { $cartnav.text('Cart (' + cartitems + ' items)'); });
			$cartnav.fadeIn('fast');
		}
	}

	// default values
	$('.cssform').defaultvalues({focusaction:"clear"});
	
	// external links
	$("a[href*='http://']:not([href*='"+location.hostname+"']), a[href*='.pdf']").click(function(e) 
	{ 
		e.preventDefault();
				
		window.open($(this).attr('href'));

	});
	// contact form
	var $cpform = $('.cpform');
	if ($cpform.length > 0) { $cpform.cpform({selectAutoWidth: false, ajaxSubmit: false, speed: 400}); }
	
	// form values
	$('.newslettersignup, .searchvendors, .searchzips').defaultvalues({focusaction:"clear"});

	// IE <= 6 fixes
	if ($.browser.msie && $.browser.version <= 6)
	{
		$('input[type="text"]').addClass('text');
		$('input[type="radio"]').addClass('radio');
		$('input[type="submit"]').addClass('submit');
		$('input[type="checkbox"]').addClass('checkbox');
	}


});

;(function($) {
			
	$.fn.equalizeHeights = function() {

		var equalheight = 0;

		this.each(function() { 
			   	var thisHeight = $(this).height();
		        if(thisHeight > equalheight) { equalheight = thisHeight; }
		});
		    
	    if (equalheight > 0) 
	    {
			if ($.browser.msie && $.browser.version == 6.0) { this.css({'height': equalheight}); }
			else { this.css({'min-height': equalheight}); }
		}	    		

	};
	
})(jQuery);