/*
--------------------------------------------------
www.palliser.co.nz

Copyright (c) 2010 Palliser Estate Wines
Design by Flightless - www.flightless.co.nz
--------------------------------------------------
*/
$(document).ready(function() {
  // main menu
	$('#slideMenuHolder').mouseover(function() {
		$('#slideMenu').stop().show(0);
	});
	$('#slideMenuHolder').mouseout(function() {
		$('#slideMenu').delay(750).hide(0);
	});
	
  // main background image show/hide
  $('a#hide-image').click(function() {
    $('#caption').fadeOut(200);
    $('#show-button').show();
    $('#hide-button').hide();
    $('#gradientWrapper').show();
		$('#shadowWrapper').slideDown(200);

    return false;
  });
  
  $('a#show-image').click(function() {
    $('#shadowWrapper').slideUp(200);
    $('#caption').delay(200).fadeIn(200);
    $('#show-button').delay(200).hide(0);
    $('#hide-button').delay(200).show(0);
    $('#gradientWrapper').delay(200).hide(0);

    return false;
  });

  // wines previous vintages show/hide  
  $('a#show-wines').click(function() {
    $('#previousWines').slideDown(400);
    $('#winetabShow').hide();
    $('#winetabHide').show();

    return false;
  });
  
  $('a#hide-wines').click(function() {
    $('#previousWines').slideUp(400);
    $('#winetabShow').show();
    $('#winetabHide').hide();

    return false;
  });

	// vintage cellaring guide show/hide
	$('a#show-cellaringguide').click(function() {
    $('#cellarGuide').show();

    return false;
  });

	$('a#hide-cellaringguide').click(function() {
    $('#cellarGuide').hide();

    return false;
  });

  // awards show/hide
	$('.awardItem .show-awards a').click(function() {
		var awards = $(this).closest('.awardItem');
		awards.children('.wine-awards').slideDown(400);
    awards.children('.wine').children('.show-awards').hide();
    awards.children('.wine').children('.hide-awards').show();

		return false;
	});

	$('.awardItem .hide-awards a').click(function() {
		var awards = $(this).closest('.awardItem');
		awards.children('.wine-awards').slideUp(400);
    awards.children('.wine').children('.hide-awards').hide();
    awards.children('.wine').children('.show-awards').show();

		return false;
	});
	
	//var quantity = parseInt(String(wine.children('.quantity').attr('value')).replace(/\$/,''));
	
	// order quantities
	$('#shop .quantity').change(function() {
		// update subtotal
		var wine = $(this).closest('.wine');
		var quantity = parseInt(wine.find('.quantity').val());
		if (isNaN(quantity)) quantity = 0;
		var price = parseFloat(wine.children('.price').attr('value'));
		if (isNaN(price)) price = 0.00;
		var subtotal = (quantity * price).toFixed(2);
		wine.children('.subtotal').text('$'+subtotal);
		
		// update total
		var total = 0.00;
		$('#shop .wine').each(function(i) {
			wine = $(this);
			quantity = parseInt(wine.find('.quantity').val());
			if (isNaN(quantity)) quantity = 0;
			price = parseFloat(wine.children('.price').attr('value'));
			if (isNaN(price)) price = 0.00;
			total += (quantity * price);
			$('#ordertotal').text('$'+total.toFixed(2));
		});
		
	});
	
	// order use billing details
	$('#delivery_use_billing').click(function() {
		var ub = this.checked;
		var bgcol = (ub?'#eee':'#fff');
		$('#delivery_name, #delivery_address, #delivery_city, #delivery_country, #delivery_post_code').attr('disabled', ub).css('backgroundColor',bgcol);
	});
});

