jQuery(document).ready(function($) {


  /********************************/
  /* AutoMagical SignUp and LogIn */
  /********************************/
  
  
  //consoloidate into one function -  catch values and pass to it
  $(".tskn-signup-and-login-panel .tab").click(function(e){
    tskn__e_stop(e);
    var className = jQuery.grep($(this).attr("class").split(" "), function(v) { return v != "tab" });

    $(".body .pane").hide();
    $(".body ." + className + ".pane").show();
    if($(".close-signup-and-login") && $(".body ." + className + ".pane .close-signup-and-login").length == 0){
      $(".body ." + className + ".pane").prepend('<div class="close-signup-and-login">[close]</div>');
      $(".close-signup-and-login").click(function(e){
        $(".close-signup-and-login").remove();
        $(".body .pane").hide();
      });
    }
  });
  
  $(".tskn-signup-and-login-panel a[href^=javascript]").click(function(e){
    tskn__e_stop(e);
    // narrow down the variables but keep it dynamic  
    var className = $(this).attr("class").split(" ");
    var parentClass = removeSame($(this).parents(".pane").siblings().map(function () {return $(this).attr("class") } ).get().join(" ").split(" "), "pane");
    var classN;
    // compare
    for (var i=0;i<className.length;i++){
      for (var a=0;a<parentClass.length;a++){
        if(className[i]==parentClass[a]){
          classN = className[i];
        } 
      }
    }
    
    // ignore when this variable
    // is not set to something
    if (!classN) return;
    
    //this pressently only works if the DOM in reference ends with the requie
    // revisese for to catch regardless, and ignore classes that aren't being used.
    $(".body .pane").hide();
    $(".body ." + classN + ".pane").show();
    if($(".close-signup-and-login") && $(".body ." + classN + ".pane .close-signup-and-login").length == 0){
      $(".body ." + classN + ".pane").prepend('<div class="close-signup-and-login">[close]</div>');
      $(".close-signup-and-login").click(function(e){
        $(".close-signup-and-login").remove();
        $(".body .pane").hide();
      });
    }
  });
  

  /**************/
  /* Lightboxes */
  /**************/
  // enable the links in the footer to open proper lightboxes
  $('.tskn-terms-of-service-lightbox-opener').click(function(e) {
    tskn__e_stop(e);
    plb__lightbox.open($('#tskn-footer .tskn-lightbox.terms-of-service').clone().removeClass('tskn-hidden'));
  });
  $('.tskn-privacy-lightbox-opener').click(function(e) {
    tskn__e_stop(e);
    plb__lightbox.open($('#tskn-footer .tskn-lightbox.privacy-policy').clone().removeClass('tskn-hidden'));
  });  
  $('.tskn-upgrade-lightbox-opener').click(function(e) {
    tskn__e_stop(e);    
    plb__lightbox.open($('#tskn-footer .tskn-lightbox.upgrade-to-premium').clone().removeClass('tskn-hidden'));
  });  
  // try lightbox on the video
  
  $('.vid-thumb').click(function(e) {
    tskn__e_stop(e);
    plb__lightbox.open($('.featured-vid .video.hide').clone().removeClass('hide'));
  });
});

/********************/
/* Helper Functions */
/********************/

function removeSame(array, item) {
  var i = 0;
  while (i < array.length) {
    if (array[i] == item) {
      array.splice(i, 1);
    } else {
      i++;
    }
  }
  return array;
}
function tskn__e_stop(e) 
{
  // shortcut for 
  // jquery events
  e.preventDefault();
  e.stopPropagation();
}
