$(document).ready(function(){
  $("a.add-to-cart").click(function(event){
    //$("#shopping-cart").css({"display":"none"});
    //$("#shopping-cart-content").html("");
    var itemID = $(this).attr("href");
	var qty = $("#qty"+itemID).val();
    var price = $("#price"+itemID).text();
	var q = "item_id="+itemID+"&qty="+qty+"&price="+price;
	$.post('add_to_cart.php', q, 
      function(data){
        if ($(data).find('error_msg').text()){
          alert($(data).find('error_msg').text());
        }
        else {
          if ($(data).find('total_of_this_item').text()){
            $("#in-cart-"+itemID).text($(data).find('total_of_this_item').text());
          }
          if ($(data).find('cart_summary').text()){
            $("#cart-summary").text($(data).find('cart_summary').text());
            $("#cart-icon").attr('class', "expandable");
            show_cart("");
          }  
        }
      }, 
    'xml');
    event.preventDefault();
  });
  
  $(".expandable").click(function(event){
    show_cart("");
    event.preventDefault();
  });

  function show_cart(q){
    $.post('tpl_cart.php', q, function(data){
      if ($(data).find('show_cart').text()=="yes"){
        
        $("#shopping-cart-content").html($(data).find('content').text());
        
        if ($(data).find('error_msg').text())
          alert($(data).find('error_msg').text());

        $("#shopping-cart-content a.update").click(function(event){
          var itemID = $(this).attr("href");
          show_cart("mode=update_qty&item_id="+itemID+"&qty="+$("#cart_qty"+itemID).val());
          event.preventDefault();
        });
        
        $("#shopping-cart-content a.submit-code").click(function(event){
          show_cart("mode=promo_code&promo_code="+$("#promo_code").val());
          event.preventDefault();
        });  

        $("#shopping-cart-content a.next").click(function(event){
          go_to_shipping("");
          event.preventDefault();
        });

        $("#shopping-cart").fadeIn();
      }  
      else { 
        $("#cart-icon").attr("class", "static");
        $("#shopping-cart-content").html("");
        $("#shopping-cart").fadeOut();
      } 
      $("#cart-summary").text($(data).find('cart_summary').text());
      if ($("a.add-to-cart").find() && $(data).find('changed_qty')){
        the_id = $(data).find('changed_qty').children().first();
        the_qty = $(data).find('changed_qty').children().last();
        if ($("#in-cart-"+$(the_id).text()).find())
          $("#in-cart-"+$(the_id).text()).text($(the_qty).text());
      }  
    }, "xml");  
  };

  function go_to_shipping(q){
    $.post('tpl_shipping.php', q, function(data){    

      $("#shopping-cart-content").html($(data).find('content').text());

      if ($(data).find('error_msg').text())
        alert($(data).find('error_msg').text());

      $("#shopping-cart-content a.calculate-shipping").click(function(event){
        var the_q = $("#shipping-form").serialize();
        go_to_shipping(the_q);
        event.preventDefault();
      });  

      $("#shopping-cart-content a.back").click(function(event){
        show_cart("");
        event.preventDefault();
      });

      $("#shopping-cart-content a.recalculate").click(function(event){
        go_to_shipping("recalculate=1");
        event.preventDefault();
      });

      $("#shopping-cart-content a.checkout").click(function(event){
        go_to_checkout("");
        event.preventDefault();
      });

      $(".text-to-clean").click(function(event){
        $(this).val("");
        $(this).attr('class', "text-box");
      });

      $("#shopping-cart").fadeIn();
    }, "xml");
  };

  function go_to_checkout(q){
    $.post('tpl_checkout.php', q, function(data){

      $("#shopping-cart-content").html($(data).find('content').text());

      if ($(data).find('error_msg').text()){
        alert($(data).find('error_msg').text());
        $(data).find('error_fields').children().each(function(key, val){
          red_id = $(val).text();
          $("#"+red_id+"_label")
          .css({ color: 'red' });
        });  
      }

      $("#shopping-cart-content a.go-to-paypal").click(function(event){
        var the_q = $("#shipping-address-form").serialize();
        go_to_checkout(the_q);
        event.preventDefault();
      });

      $("#shopping-cart-content a.back").click(function(event){
        show_cart("");
        event.preventDefault();
      });

      $("#shopping-cart-content a.recalculate").click(function(event){
        go_to_shipping("");
        event.preventDefault();
      });

      $("#shopping-cart").fadeIn();
      
      if ($(data).find('go_to_paypal').text()){
        $("#pay_form").submit();
      }

    }, "xml");
  };

  $(".hide-cart").click(function(event){
    $("#shopping-cart").fadeOut();
    $("#shopping-cart-content").html();
    event.preventDefault();
  });

  
  $("#shipping-form").submit(function(event){
    event.preventDefault();
  });  

  // add to cart button rollover
  $(".add-to-cart-img").hover(
    function(event){
      $(this).attr('src', '/images/buy-now.gif');
    },
    function(event){
      $(this).attr('src', '/images/add-to-cart.gif');
    }
  );  
  
  // shopping cart button rollover
  $("#cart-icon").hover(
    function(event){
      $(this).css({'background-image':'url(/images/shopping-cart-icon-over.gif)'});
    },
    function(event){
      $(this).css('background-image', 'url(/images/shopping-cart_icon.gif)');
    }
  );

});


