// Products & Shopping Cart stuff
function addToCart (productID) {
  $.getJSON(("/cart/add_product/"+productID), function(data) {
    totalPrice = 0; product = data.product.product; cart_item = data.cart_item.cart_item

    id = product.identity;
    $("table#items").append("<tr class=\"product\" id=\""+id+"\" rel=\""+cart_item.id+"\"><td class=\"name\">"+product.name+"</td><td class=\"price\">"+product.price+"</td><td class=\"close\">X</td></tr>");
    refreshCart();

    toggleSidebar({data:{dontClose:true}});
    openSidebarPanel("#cart.panel");
  })
}

function removeCartItem(event) {
  cartItemId = $(this).parent().attr("rel");
  $.getJSON("/cart/remove_product/"+cartItemId, function(data) {
    $("[rel='"+cartItemId+"']").remove();
    refreshCart();
  });
}

function refreshCart() {
  $("table#items .close").bind("click", $(this), removeCartItem);
  if ($("table#items tr.product").size() < 1) {
    $("#cart #checkout").hide();
  } else {
    $("#cart #checkout").show();
  }
}

function subscribeToList (listID, campaignDescription) {
  $("#maildescription").html(campaignDescription);
  $("#mail_list_id").attr("value", listID);
  toggleSidebar({data:{dontClose:true}});
  openSidebarPanel("#mail.panel");
  $("#mail").effect("highlight", {color: "#FFFF00"}, 3000);
}

// Sidebar animation & stuff
function toggleSidebar(event) {
  dontClose = event.data.dontClose;
  // Toggle sidebar dipsplay. if dontClose==true, sidebar will only open when closed, but won't close while open.
  var toggleWidth = $("#sidebar #toggle").width();
  var width = $("#sidebar #panels").width() + toggleWidth;
  if($("#sidebar").hasClass("collapsed")) {
    showSidebar(width);
  } else if (!$("#sidebar").hasClass("collapsed") && !dontClose) {
    hideSidebar(toggleWidth);
  }
}

function showSidebar(width, callback) {
  $("#sidebar").animate({width: width}, afterSidebarAnimate);
}
function hideSidebar(toggleWidth) {
  $("#sidebar").animate({width: toggleWidth}, afterSidebarAnimate);
}
function afterSidebarAnimate() {
  $("#sidebar").toggleClass("collapsed");
}
function openSidebarPanel (panelSelector) {
  $(panelSelector).children("h2").removeClass("collapsed");
  $(panelSelector).children("h2").next().show("fast");
}

$(document).ready(function() {
  
  $.tools.overlay.addEffect("slideUppp", 
      function(done) {}, 
      function(done) {// close function 
          this.getOverlay().animate({top: "-500px"}, null, null, done); 
          console.log("custom close");
      } 
  );
  
  if($("body.splash").length > 0) {
    swfobject.embedSWF("/flash/splash.swf", "splash", "413", "584", "9.0.0","expressInstall.swf", null, {bgColor: "#000"}, null, function() {
      splashOverlay = $("#splash").overlay({effect: 'slideUp', expose: {color: '#000', loadSpeed: 0, opacity: 1}, closeOnClick: true, api: true});
      splashOverlay.onClose(function(){
        // $("div:last").remove();
        // $("#splash").remove();
        $("body").removeClass("splash");
      });
      splashOverlay.load();
      setTimeout("splashOverlay.close()", 4000);
    });
  }
  
  // Expand/collapse panel when title is clicked
  $("body.magazine .panel h2").bind("click", function() {
    $(this).next().toggle("fast", function() {
      state = ""; if($(this).parent(".panel").toggleClass("collapsed").hasClass("collapsed")) {state = "collapsed"}
      $.post("/json/set_cookie", {authenticity_token: auth_token, c_name: $(this).parent().attr("id"), c_value: state});
    });
  });
  $("body.magazine .panel h2").bind("mouseover mouseout", function(){$(this).toggleClass("hover")});
  $("input.autoclear").one("focus", function() {
    $(this).attr("value","");
    $(this).siblings(".submit").removeAttr("disabled");
    $(this).siblings(".submit").removeClass("disabled");
  });
  $("#toggle").bind("click", {dontClose: false}, toggleSidebar);
  $("input[type='text'],input[type='password'],textarea").addClass("textinput");
  refreshCart();
});

$.tools.overlay.addEffect("slideUp", 
    function(){}, 
    function(done) { 
        var height = "-"+$("body").innerHeight()+"px";
        var duration = 500;
        this.getOverlay().animate({top: height}, duration); 
        $("div:last").animate({top: height}, duration);
        done.call();
    } 
);