﻿(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
  return this.each(function(i){
  var ah = $(this).height();
  var ph = $(this).parent().height();
  var mh = Math.ceil((ph-ah) / 2);
  $(this).css('margin-top', mh);
  });
};
})(jQuery);

$(document).ready(function(){
// Executed once all the page elements are loaded

  $(".pic-meta").vAlign();
  
  var preventClick=false;
  
  $("a.fancybox-textlink").click(function (e) {
      e.preventDefault();
      var $li = $(this).parent().parent().parent();
      var $a = $("div.pic a", $li );
      $a.click();
  });
  
  $("a.weeks").each(function () {
    var $this = $(this);
    if ( $this.attr("data-week") == currentWeek ) {
      setTimeout(function () {
        $this.click();
      }, 200);
    }
  });
  
  $("a.weeks").hover(
    function () {
      if ( ! $(this ).hasClass("selected")) {
        activateButton( $(this ) );
      }
    }, 
    function () {
      if ( ! $(this ).hasClass("selected")) {
       deactivateButton( $(this ) );
      }
    }
  );
  
  $("a.weeks.selected").each(function () {
    selectButton( $(this ) );
  });
  
  $("a.weeks").click(function (e) {
    e.preventDefault();
    deselectButton( $("a.weeks.selected") );
    selectButton( $(this) );
    filterPhotos( $(this).attr("data-week") );
  });
  
  function filterPhotos( week ) {
    var counter = 0;
    if ( week == "all" ) {
      $("div.pic").each( function () {
        $(this).stop(true, true).hide().css("top", "").css("left", "").delay( counter * 25 ).fadeIn(100);
        counter++;
      });
    } else {
      
      $("div.pic").each(function () {
        if ( $(this).attr("data-week") == week ) {
          $(this).stop(true, true).hide().css("top", "").css("left", "").delay( counter * 25 ).fadeIn(100);
          counter++;
        } else {
          $(this).stop(true, true).hide();
        }
      });
    }
  }
  
  function selectButton( obj ) {
     setBGYPosition( obj, -44);
  }
    
  function deselectButton( obj ) {
       setBGYPosition( obj, 0);
  }
    
  function activateButton( obj ) {
     setBGYPosition( obj, -22);
  }
  
  function deactivateButton( obj ) {
    setBGYPosition( obj, 0);
  }
  
  function setBGYPosition( obj, ypos ) {
    var x = 0;
    if ( obj.css("background-position-y") != "" ) {
      obj.css("background-position-y", ypos + "px");
    } else {
        x = obj.css("backgroundPosition").split(" ")[0];
       obj.css("backgroundPosition", x + " " + ypos + "px");
    }
    
  }
  
  $("#photogallery .pic a").bind("click",function(e){
    /* This function stops the drag from firing a click event and showing the lightbox */
    if(preventClick)
    {
      e.stopImmediatePropagation();
      e.preventDefault();
    }
  });

  $("#photogallery .pic").draggable({
    /* Converting the images into draggable objects */
    containment: 'parent',
    start: function(e,ui){
      /* This will stop clicks from occuring while dragging */
      preventClick=true;
    },
    
    stop: function(e, ui) {
      /* Wait for 250 milliseconds before re-enabling the clicks */
      setTimeout(function(){ preventClick=false; }, 250);
    }
  });


  $('#photogallery .pic').mousedown(function(e){
                 
    /* Executed on image click */
    
    var maxZ = 0;
    
    /* Find the max z-index property: */
    
    $('.pic').each(function(){
      var thisZ = parseInt($(this).css('zIndex'))
      if(thisZ>maxZ) maxZ=thisZ;
    });
    
    /* Clicks can occur in the picture container (with class pic) and in the link inside it */
    if($(e.target).hasClass("pic"))
    {
      /* Show the clicked image on top of all the others: */
      $(e.target).css({zIndex:maxZ+1});
    }
    else $(e.target).closest('.pic').css({zIndex:maxZ+1});
  });
  
  /* Converting all the links to a fancybox gallery */
  $("a.fancybox").fancybox({
    zoomSpeedIn: 300,
    zoomSpeedOut: 300,
    overlayShow:false,
    titlePosition:"inside",
    titleFormat: function(title, currentArray, currentIndex, currentOpts) {
         var rndID = "id" + Math.round( Math.random() * 1000000 );
          setTimeout(function () {
            addthis.toolbox( "#" + rndID );
           }, 200);
          $("div.addthis_toolbox", $($("a.fancybox")[currentIndex])).attr("id", rndID).html("<a href=\"http://www.addthis.com/bookmark.php?v=250&amp;username=fazer\" class=\"addthis_button_compact\">Dela med dig</a> <span class=\"addthis_separator\">|</span> <a class=\"addthis_button_facebook\"></a><a class=\"addthis_button_pusha\"></a><a class=\"addthis_button_twitter\"></a>");
          var html =  $($("a.fancybox")[currentIndex]).html();
          $("div.addthis_toolbox", $($("a.fancybox")[currentIndex])).attr("id", "").html("");
          return html;
        }
  });
  
  $("a.tack").fancybox({
    zoomSpeedIn: 300,
    zoomSpeedOut: 300,
    overlayShow:false,
    titleShow:false});
  
  /* Converting the share box into a droppable: */
  $('.drop-box').droppable({
    hoverClass: 'active',
    drop:function(event,ui){
      $('#url').val(location.href.replace(location.hash,'')+'#'+ui.draggable.attr('id'));
      $('#modal').dialog('open');
    }
  });

  /* Converts the div with id="modal" into a modal window  */
  $("#modal").dialog({
    bgiframe: true,
    modal: true,
    autoOpen:false,
    buttons: {
        Ok: function() {
          $(this).dialog('close');
        }
      }
  });
  
  if(location.hash.indexOf('#pic-')!=-1)
  {
    /* Checks whether a hash is present in the URL */
    /* and shows the respective image */
    $(location.hash+' a.fancybox').click();
  }
  
});

// JavaScript Document
