$(document).ready(function() {
	/* jQuery column stuff */
	var currentTallest = 0,
		 currentRowStart = 0,
		 rowDivs = new Array(),
		 $el,
		 topPosition = 0;

	 $('.blocks').each(function() {

	   $el = $(this);
	   topPostion = $el.position().top;

	   if (currentRowStart != topPostion) {

		 // we just came to a new row.  Set all the heights on the completed row
		 for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
		   rowDivs[currentDiv].height(currentTallest);
		 }

		 // set the variables for the new row
		 rowDivs.length = 0; // empty the array
		 currentRowStart = topPostion;
		 currentTallest = $el.height();
		 rowDivs.push($el);

	   } else {

		 // another div on the current row.  Add it to the list and check if it's taller
		 rowDivs.push($el);
		 currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);

	  }

	  // do the last row
	   for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
		 rowDivs[currentDiv].height(currentTallest);
	   }

	 });
	 
	 //Find the parent to highlight for indented items
	$('#side-nav ol li.indent').each(function(i, item) {
		if ($(this).hasClass('active')) {
			$('#side-nav ol li.indent').first().prev().addClass('active');
		}
	 });
	 
	 $('.back-link').click(function() {
		window.history.back();
		return false;
	});
	
	/* Image rollovers */
	$('img[data-hover]').hover(function() {
        $(this).attr('tmp', $(this).attr('src')).attr('src', $(this).attr('data-hover')).attr('data-hover', $(this).attr('tmp')).removeAttr('tmp');
    }).each(function() {
        $('<img />').attr('src', $(this).attr('data-hover'));
    });

    /* Strip tables */
    $("table.stripe tbody tr:odd").addClass("odd");

    $.each($('.tabs ul li a'), function() {
    	if ($('#' + $(this).attr('rel')).is(':empty')) {
    		$(this).parent().hide();
    	}
    });

    /* Tabs control */
    $('.tabs ul li a').click(function() {
    	$('.tab-content').hide();
    	$.each($('.tabs ul li'), function() {
    		$(this).removeClass('on');
    	});
	    $(this).parent().addClass('on');
		$('#' + $(this).attr('rel')).fadeIn();
    	return false;
    });

    /* News accordian effect */
	$('article a.show-more').click(function() {
		ele = $(this);
		ele.parent().parent('div').slideUp(400, function() {
			ele.parent().parent().next('div').slideDown();
		});
		
		return false;
	});

	$('article a.show-less').click(function() {
		ele = $(this);
		ele.parent().parent('div').slideUp(400, function() {
			ele.parent().parent().prev('div').slideDown();	
		});
		return false;
	});

	/* Newsletter signup */
	$('#newsletter-signup a').click(function() {
		$('#newsletter-signup').submit();
		return false;
	});

	/* Update profile button */
	$(".update_profile").click(function () {
        window.location = '/shop_myprofile.asp';
    });

    /* Reload wine tips */
    $('.wine-tips-link').live('click', function(){
    	$.get('/includes/inc_wine_tips.asp', function(data){
    		$('.wine-tip-container').html(data);
    	});
    	return false;
    });

    /* Submit wine poll */
	$('#wine-poll-form').submit(function() {
		$.post("/includes/inc_poll.asp", $('#wine-poll-form').serialize(), function(data) {
			$('.wine-poll-container').html(data);
		});
		return false;
	});

	/* mp3 player */
	

    $('.play-pronounication').click(function() {

    	$("#jquery_jplayer_1").jPlayer("destroy");
    	srcFile = $(this).attr('href');
		$("#jquery_jplayer_1").jPlayer({
			ready: function (event) {
				$(this).jPlayer("setMedia", {
					mp3:srcFile
				}).jPlayer("play");
			},
			swfPath: "/swf",
			supplied: "mp3",
			wmode: "window"
		});
        return false;
    });

    $('.play-wine-show').click(function() {
    	$("#jquery_jplayer_1").jPlayer("destroy");
    	srcFile = $(this).attr('href');
		$("#jquery_jplayer_1").jPlayer({
			ready: function (event) {
				$(this).jPlayer("setMedia", {
					mp3:srcFile
				}).jPlayer("play");
			},
			swfPath: "/swf/Jplayer.swf",
			supplied: "mp3",
			wmode: "window"
		});

        $('.thirtyfifty-player').fadeIn();
        return false;
    });
 });
