﻿$(function(){
   /* prevent the page from scrolling to the top if a left side nav category with subcategories is clicked */
   $('#nav .hasSubcategories').click(function(){
        return false;
   });
   
   /* copy billing information to shipping information if input is checked, remove if unchecked */
   $('#copy_billing_data_to_shipping').click(function(){
        
        if($(this).is(":checked")){
            var billName = $('#ShopperorderOrderShopperBillName').val();
            var billAddress = $('#ShopperorderOrderShopperBillAddress').val();
            var billCity = $('#ShopperorderOrderShopperBillCity').val();
            var billState = $('#ShopperorderOrderShopperBillState').val();
            var billZip = $('#ShopperorderOrderShopperBillZip').val();
            var billCountry = $('#ShopperorderOrderShopperBillCountry').val();
            
            $('#ShopperorderOrderShopperShippingName').val(billName);
            $('#ShopperorderOrderShopperShippingAddress').val(billAddress);
            $('#ShopperorderOrderShopperShippingCity').val(billCity);
            $('#ShopperorderOrderShopperShippingState').val(billState);
            $('#ShopperorderOrderShopperShippingZip').val(billZip);
            $('#ShopperorderOrderShopperShippingCountry').val(billCountry);    
        } else {
            $('#ShopperorderOrderShopperShippingName, #ShopperorderOrderShopperShippingAddress, #ShopperorderOrderShopperShippingCity, #ShopperorderOrderShopperShippingState, #ShopperorderOrderShopperShippingZip, #ShopperorderOrderShopperShippingCountry').val('');    
        }
       
    });
   
   /* when a shipping method is picked, add $totalIncludingTax and the shipping price, display in totalWithShipping */
   $('#shoppingcart input[name=shippingOption]').click(function(){
      var totalIncludingTax = $('#totalIncludingTax').text();
      var formattedTotalWithTax = parseFloat(totalIncludingTax.substring(1, totalIncludingTax.length));
      
      var shipMethodCost = parseFloat($(this).parent().parent().children().last().text());
      
      var total = formattedTotalWithTax + shipMethodCost;
      
      $('#totalWithShipping').text('$' + total);
      
   });
   
   $('#closeLink').click(function(){
        $('#flashMessage').slideUp('fast');
    });
   
   $('#gallery .productimage').hover(
        function(){
            var src = $(this).attr('src');
            // change the 40 to 240 in the file name
            var newSrc = src.replace('/40/','/240/');
            $('#gallery #pic').attr('src',newSrc);
        },
        function(){
            var a = false;
        }
    );
   
   $('#showTax').click(function(e){
        e.preventDefault();
        $('#sales-tax').slideToggle('fast');
    });
   $('#hideTax').click(function(e){
        e.preventDefault();
        $('#sales-tax').slideToggle('fast')
    });
});
