$j = jQuery.noConflict();

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

	$j('div#homepage-rotation ul').innerfade({
		speed: 1000,
		timeout: 2000,
		type: 'sequence',
		containerheight: '152px'
	});

	//add 1 px to the width of the first nav item so that it lines up with the search box.
	$j('div#navigation ul.right-options li a').each(function(i) {
		if (i == 0) $j(this).css({width: $j(this).width()+1});
	});

	//navigation animation
	$j('div#navigation ul.left-options li a')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$j(this).stop().animate({
				backgroundPosition:"(-120px 0)"
			}, {
				duration:500
			})
		})
		.mouseout(function(){
			$j(this).stop().animate({
				backgroundPosition:"(-240px 0)"
			}, {
				duration:400,
				complete:function(){
					$j(this).css({backgroundPosition: "0 0"
				})
			}
		})
	})

	//navigation animation - right
	$j('div#navigation ul.right-options li a')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$j(this).stop().animate({
				backgroundPosition:"(-190px 0)"
			}, {
				duration:500
			})
		})
		.mouseout(function(){
			$j(this).stop().animate({
				backgroundPosition:"(-320px 0)"
			}, {
				duration:400,
				complete:function(){
					$j(this).css({backgroundPosition: "0 0"
				})
			}
		})
	})

	//search form
	$j('form#search_mini_form div#submit-div a').click(function() {
		$j('form#search_mini_form').submit();
	});


	featured_wines_items_per_row = 3;
	featured_wines_total_rows = 1;

	//featured wines - update the margin-top on the image to make each item the same height.
	featured_wines_content_height = 0;
	$j('div.listing-type-grid div.record div.content').each(function(index) {
		if ($j(this).height() > featured_wines_content_height) featured_wines_content_height = $j(this).height();
	});
	$j('div.listing-type-grid div.record div.content').each(function(index) {
		if ($j(this).height() < featured_wines_content_height) {
			$j(this).find('img').css({'margin-top': featured_wines_content_height - parseInt($j(this).height())});
		}
	});

	//featured wines - update height of grey panel with a min-height of 66px
	featured_wines_desc_height = 66;
	$j('div.listing-type-grid div.record div.desc').each(function(index) {
		if ($j(this).height() > featured_wines_desc_height) featured_wines_desc_height = $j(this).height();
	});
	$j('div.listing-type-grid div.record div.desc').css({'height': featured_wines_desc_height});


	/*************************************************/
	/* START : How to buy page                       */
	/*************************************************/
	$j('div.how-to-buy-panel:eq(2)').css({'margin-right': -10});

	how_to_buy_content_height = 0;
	$j('div.how-to-buy-panel div.content').each(function() {
		if ($j(this).height() > how_to_buy_content_height) how_to_buy_content_height = $j(this).height();
	});
	$j('div.how-to-buy-panel div.content').css({'height': how_to_buy_content_height});
	/*************************************************/
	/* END : How to buy page                         */
	/*************************************************/


	/*************************************************/
	/* START : wine category navigation              */
	/*************************************************/
	/*
	$j('div#category-navigation>ul>li>a').click(function() {
		$j(this).parent().find('ul').slideToggle('slow');
		if ($j(this).parent().hasClass('active')) {
			$j(this).parent().removeClass('active');
		} else {
			$j(this).parent().addClass('active');
		}
		return false;
	});

	$j('div#category-navigation>ul>li.active').find('ul').slideToggle('slow');
	*/
	/*************************************************/
	/* END : wine category navigation                */
	/*************************************************/

	//fix layout - buy-wine-product-large
	if ($j('div#buy-wine-product-large')) {
		var image_h = parseInt($j('div#buy-wine-product-large div.record div.image').height());
		var content_h = parseInt($j('div#buy-wine-product-large div.record div.content').height());

		if (image_h > content_h) {
			$j('div#buy-wine-product-large div.record div.content').css({'padding-top': (image_h - 5) - content_h});
			$j('div#category-navigation').height(image_h-20);
		} else {
			$j('div#buy-wine-product-large div.record div.image').css({'padding-top': content_h - image_h});
			$j('div#category-navigation').height(content_h-20);
		}

		//fix layout - 2nd small wine record
		$j('div#buy-wine-product-small div.record:eq(1)').css({'border-bottom': 'none', 'margin-bottom': 0, 'padding-bottom': 0});

		//fix layout - small wine bottles - align to bottom.
		$j('div#buy-wine-product-small div.record').each(function() {
			var image_h = parseInt($j(this).find('div.image').height());
			var content_h = parseInt($j(this).find('div.content').height());

			if (image_h > content_h) {
				$j(this).find('div.content').css({'padding-top': (image_h - content_h)});
			} else {
				$j(this).find('div.image').css({'padding-top': content_h - image_h});
			}
		});

		//fix layout - align both large and smalls to the bottom.
		var large_h = parseInt($j('div#buy-wine-product-large').height());
		var smalls_h = parseInt($j('div#buy-wine-product-small').height());
		if (large_h > smalls_h) {
			$j('div#buy-wine-product-small').css({'padding-top': large_h - smalls_h});
		} else {
			$j('div#buy-wine-product-large').css({'padding-top': (smalls_h - large_h) + 20});
		}
	}

});