$(document).ready(function() {
		
	if ( !Modernizr.backgroundsize ) {
	
	 $(function(){
		  // this will need to get replaced with the rotating images on load !
		  //$('body').append('<img src="/images/fw/bg_image.jpg" id="bg" >');
		});
		
		$(window).load(function() {
			var theWindow = $(window),
			$bg = $("#bg"),
			aspectRatio = $bg.width() / $bg.height();
			
			function resizeBg() {
				if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
				$bg
				.removeClass()
				.addClass('bgheight');
				} else {
				$bg
				.removeClass()
				.addClass('bgwidth');
				}
			}
		
			theWindow.resize(function() {
				resizeBg();
			}).trigger("resize");
		});
	}
	
	$('.date').datepicker();

	// Fancybox .overlay links
	$('.overlay').fancybox();
	
	// hover fixes for left side of styled links
	$(".sublink span").hover(
		function () {
			$(this).children('a').css("background", "url(/assets/images/navArrowRightAccent.png) right");
	  	},
	  	function () {
			$(this).children('a').css("background", "url(/assets/images/navArrowRight.png) right");
	  	}
	);
	
	$(".mainNav li").hover(
		function () {
			$(this).children('a').css("background", "url(/assets/images/navArrowRightOn.png) right");
	  	},
	  	function () {
			$(this).children('a').css("background", "url(/assets/images/navArrowRight.png) right");
	  	}
	);	
	
	$(".mainNav li.selected").hover(
		function () {
			$(this).children('a').css("background", "url(/assets/images/navArrowRightOn.png) right");
	  	},
	  	function () {
			$(this).children('a').css("background", "url(/assets/images/navArrowRightOn.png) right");
	  	}
	);	
	
	$(".invertedLink span").hover(
		function () {
			$(this).children('a').css("background", "url(/assets/images/navArrowRight.png) right").css("color", "#fff");
	  	},
	  	function () {
			$(this).children('a').css("background", "url(/assets/images/navArrowRightOn.png) right").css("color", "#000");
	  	}
	);
	
	$('nav').css({'visibility':'visible'});
	
	// Gift Card Order Form
	
	$("#proceed-to-payment").click(function(e) {
			e.preventDefault();
			$("#gift-card-form").submit();
		});
		
	$(".item-amount, .item-quantity").live('change',function() {
		
		// the tr that contains the row
		$tr = $(this).parent().parent();
		
		var amt = $tr.children().children('.item-amount').val();
		var quantity = parseInt($tr.children().children('.item-quantity').val());			
		var subtotal = amt * quantity;
			
		$tr.children().children(".item-subtotal").val(subtotal);		
	});
	
	/**
	 * loop through all items in cart and get totals, and description
	 */
	function generateTotalsAndDescription() {
		var instr = $('#specialInstructions').val();
		var shipping = parseFloat($('#shipping').val());						
		
		var grand = 0;
		var description = '';
		var total = 0;
		
		$.each($('.item-row'), function(index, value) {								
			description = description + $(this).children().children('.item-quantity').val() + ' @ $' + $(this).children().children('.item-amount').val() + ', ';
			grand = grand + parseFloat($(this).children().children('.item-subtotal').val());
		});	
		total = grand + shipping;
		
		$("#description").val('Anthonys Gift Card Purchase: ' + description + ' SUBTOTAL: = $' + grand + ' SHIPPING $' + shipping + ' TOTAL: $' + total + ' ** ' + instr + ' **');
		$("#grand").html('$ ' + total);
		$("#amount").val(total);
	}
	
	$("#specialInstructions, #shipping, .item-amount, .item-quantity").live('change', function() {		
		generateTotalsAndDescription();
	});
	
	$("#feedback, #gift-card-form, #banquet-request, #email-signup").validationEngine('attach');
	
	// add another row to the table
	$("#add-item").click(function(){
		$(".item-row:first").clone().appendTo("#item-table");
		$(".item-row:last").children().children('.item-subtotal').val(0);
		$(".item-row:last").children().children('.item-quantity').val(1);	
		
	});
	
	// remove item from cart, and recalculate totals
	$(".remove-item").live('click', function(){
			
		// if there is more than one item row
		if ($('.item-row').length > 1) {
			// the tr that contains the row
			$(this).parent().parent().remove();
			generateTotalsAndDescription();
		}
	});
	
	// submit location search on change of drop down
	$('#restaurantArea').change(function() {
		$(this).parents('form').submit();
	})

});
