// var overlay = null;
// $(function() {
//   overlay = $("#pop").overlay();
// });


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() {
  
  // slides closed sidebar after x amount of time
  setTimeout(function() {
  $("#sidebar").animate({width: "29px"}, afterSidebarAnimate)
  } , 8000);

  $.tools.overlay.addEffect("slideUppp", 
      function(done) {}, 
      function(done) {// close function 
          this.getOverlay().animate({top: "-500px"}, null, null, done); 
          console.log("custom close");
      } 
  );
  
  // 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");
  
  $("#subscribe_list").bind("click", function() {
      var mail_list_id = $("#mail_list_id").val();
      var name = $("#uname").val();
      var email = $("#email").val();
      $.post("/admin/mailer", {'do': 'createsubscriber', 'email': email, 'name': name}, function(data) {
        text = data.substring(0,data.indexOf("|"));
        $('#response').show();  
        $('#response').html(text);  
        setTimeout(function(){subscribeReset();}, 2000);
      }); 
      return false;   
  });
});

$.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();
    } 
);

function subscribeReset(){
  $('#response').hide('slow');
  $("#uname").val('');
  $("#email").val('');
}
function openVideo(videoId) {
  $("#pop").overlay({expose: "#000" , target: '#pop'});
  $(".video-holder").load("/loadlightbox.php", {'id': videoId}, function(){
    $("#pop").overlay({expose: "#000" , target: '#pop'}).load();
    $("#pop").css("display","block");
  });
}

function LoadNotificationForm(id) {
  //console.log("test");
  $(".notification-form").overlay({expose: "#000" , target: '.notification-form'});
  $(".form-holder").load("/load-notification-form.php", {'id': id}, function(){
  $(".notification-form").overlay({expose: "#000" , target: '.notification-form'}).load();
  $(".notification-form").css("display","block");
});
}

function openGallery(galleryId) {
  $("#triggers").load("/loadgallery.php", {'id': galleryId}, function() {
    var triggers = $("#triggers a").overlay({expose: "#000" , target: '#gallery'}).gallery({preload: true, speed: "slow", autohide: false});
    $(triggers[0]).click();
  });
}

function buyProduct(productId) {
  $.post('/loadproduct.php', {'id': productId}, function(data) {
    window.open(data);
  });
}

function openLink(url) {
  console.log(url);
  window.open(url);
}