/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);

/*
 * myLightbox 
 * http://mylittlehomepage.net/de/sonstiges/mylightbox
 * 
 * Copyright (c) 2011 Mark Alexander Hoschek
 */
myLightboxSettings = new Object();
myLightboxSettings['vertical_align'] =      30; // 'center' or number of pixels from top
myLightboxSettings['next_link'] =           '[&raquo;]';
myLightboxSettings['next_link_title'] =     'next';
myLightboxSettings['previous_link']       = '[&laquo;]';
myLightboxSettings['previous_link_title'] = 'previous';
myLightboxSettings['close_link'] =          '[x]';
myLightboxSettings['close_link_title'] =    'close';
myLightboxSettings['hash'] =                'photo'; // example.com/#photo=1
myLightboxSettings['html_box'] = '<div id="mylightbox">\
<div id="mylightbox-header">\
<div id="mylightbox-title"></div>\
<div id="mylightbox-nav"></div>\
<div id="mylightbox-controls"><a href="#" id="mylightbox-close" title="'+myLightboxSettings['close_link_title']+'" onclick="return false"><span>'+myLightboxSettings['close_link']+'</span></a></div>\
</div>\
<div id="mylightbox-photo"></div>\
<p id="mylightbox-description"></p>\
</div>';
myLightboxSettings['html_background'] = '<div id="mylightbox-background"></div>';

// 0 means disabled; 1 means enabled;
var popupStatus = 0;

// loading popup with jQuery magic!
function loadPopup()
 {
  //loads popup only if it is disabled
  if(popupStatus==0)
   {
    //window.location.hash = 'mylightbox';
    
    $("body").append(myLightboxSettings['html_background']);
    $("body").append(myLightboxSettings['html_box']);
    
    $("#mylightbox-background").css({
	   "opacity": "0.7"
    });

    $("#mylightbox-background").fadeIn(150);
    $("#mylightbox").fadeIn(150);
		
    // center popup on window resize:
    $(window).bind('resize', function() {
     centerPopup($("#mylightbox").width(),$("#mylightbox").height());
    });
    
    // close on click on close button:
    $("#mylightbox-close").click(function(){
     //disablePopup();
     //$(this).attr('href', '#mylightbox='+i);
     window.location.hash = "#"+myLightboxSettings['hash']+"=0";
    });

    // close on click on background:
    $("#mylightbox-background").click(function(){
     window.location.hash = "#"+myLightboxSettings['hash']+"=0";
    });

    // close on ESC key
    $(document).keypress(function(e){
     if(e.keyCode==27 && popupStatus==1){
      window.location.hash = myLightboxSettings['hash']+"=0";
     }
    });    

    popupStatus = 1;
   }
 }

function disablePopup()
 {
  //window.location.hash = '';
  //if(popupStatus==1){
  $("#mylightbox").fadeOut(150, remove);
  $("#mylightbox-background").fadeOut(150, remove);
  popupStatus = 0;
  delete myLightboxCurrentWidth;
  //}
}

// callback function to remove box and background:
function remove()
 {
  $(this).remove();
 }

function centerPopup(width, height)
 {
 
  if(myLightboxSettings['vertical_align']=='center')
   {
    var top = $(window).scrollTop()+$(window).height()/2-height/2;
   }
  else
   {
    var top = $(window).scrollTop()+myLightboxSettings['vertical_align'];
   }
  var left = $(window).width()/2-width/2;

  if(top<0) top=0;
  if(left<0) left=0;

  $("#mylightbox").css({
   "position": "absolute",
   "top": top+'px',
   "left": left+'px'
  });
  
  //only need force for IE6
  $("#mylightbox-background").css({
   "height": $(window).height()
  }); 
 }

$(function()
 {
  // get mylightbox links, image sources, titles (alt) and descriptions (title):
  var myLightboxLinks = new Array();
  var srcs = new Array();
  var titles = new Array();
  var descriptions = new Array();

  $("a[rel='lightbox']").each(function(i) // get all links with rel="lightbox"
   {
    $(this).click(function(){
     nr = i + 1;
     location.hash = "#"+myLightboxSettings['hash']+"="+nr;
     return false;
    });
    myLightboxLinks.push($(this));
    srcs.push($(this).attr('href'));
    titles.push($(this).find('img').attr('alt'));
    descriptions.push($(this).find('img').attr('title'));
   });

 var numberOfImages = myLightboxLinks.length;

 $(window).hashchange( function(){
  var hash = location.hash;
  if(hash=='')
   {
    disablePopup();
   }
  else
   {
    var hashparts = hash.split('=');
    if(hashparts[0]=="#"+myLightboxSettings['hash'])
     {
      if(hashparts[1]=='0')
       {
        disablePopup();
       }
      else
       {
        var image = parseInt(hashparts[1])-1;
        if(numberOfImages>1)
         {
          if(image < numberOfImages-1) var next = image+2;
          else next = 1;
          if(image > 0) var prev = image;
          else prev = numberOfImages;
         }
        if(image<numberOfImages)
         {
          loadPopup();
          if(typeof(myLightboxCurrentWidth)!='undefined') $("#mylightbox").css({"width":myLightboxCurrentWidth+'px'});
          // previous and next buttons:
          if(typeof(prev)!='undefined' && typeof(next)!='undefined')
           {
            $("#mylightbox #mylightbox-nav").html('<a id="mylightbox-prev" href="'+srcs[prev-1]+'" title="'+myLightboxSettings['previous_link_title']+'"><span>'+myLightboxSettings['previous_link']+'</span></a>&nbsp;&nbsp;<span id="nr">'+(image+1)+'/'+numberOfImages+'</span>&nbsp;&nbsp;<a id="mylightbox-next" href="'+srcs[next-1]+'" title="'+myLightboxSettings['next_link_title']+'"><span>'+myLightboxSettings['next_link']+'</span></a>');
            $("#mylightbox-prev").click(function(e){ $(this).attr('href', '#'+myLightboxSettings['hash']+'='+prev); });
            $("#mylightbox-next").click(function(e){ $(this).attr('href', '#'+myLightboxSettings['hash']+'='+next); });
           }
          else
           {
            $("#mylightbox #mylightbox-nav").html('&nbsp;');
           }
          $("#mylightbox #mylightbox-title").html('');
          $("#mylightbox-photo").html('<div id="mylightbox-throbber"></div>');
          $("#mylightbox-description").html('');
          if(typeof(myLightboxCurrentWidth)=='undefined') centerPopup($("#mylightbox").outerWidth(), $("#mylightbox").outerHeight());
          var objImagePreloader = new Image();
          objImagePreloader.onload = function() 
           {
            $("#mylightbox #mylightbox-title").html(titles[image]);
            $("#mylightbox-photo").hide();
            if(typeof(next)!='undefined')
             {
              $("#mylightbox-photo").html('<a id="mylightbox-next-img" href="'+srcs[next-1]+'"><img src="'+srcs[image]+'" /></a>');
              $("#mylightbox-next-img").click(function(e){ $(this).attr('href', '#'+myLightboxSettings['hash']+'='+next); });
             }  
            else
             {
              $("#mylightbox-photo").html('<img src="'+srcs[image]+'" />');      
             }
            $("#mylightbox-description").html(descriptions[image]);
            $("#mylightbox-photo").fadeIn(150);
            myLightboxCurrentWidth = objImagePreloader.width;
            $("#mylightbox").css({"width":myLightboxCurrentWidth+'px'}); 
            centerPopup($("#mylightbox").outerWidth(), $("#mylightbox").outerHeight());
           };
          objImagePreloader.src = srcs[image];
         }
       }
     }
    else
     {
      disablePopup();
     }
   }
 });
 $(window).hashchange();
});

