var currentShow;
var currentShowJson;
var currentIndex = 0;
var currentLanguage = "en";

$(document).ready(function() {
   $("#stories").hide();
   
   $("#stories_link").click(function() {
      $("#stories").slideToggle("normal");
      return false;
   });
   
   $("li li").mouseover(function() {
      $(this).css("border-right", "1px dotted #CCC");
   });
   
   $("li li").mouseout(function() {
      $(this).css("border-right", "0px");
   });
   
   $(".loadShow").click(function() {
      var jsonSrc = $(this).attr("href");
      currentShowJson = jsonSrc;
      $.getJSON(jsonSrc, {}, function(show) {
         currentShow = show;
         initShow();
      });
      
      return false;
   });
   
   $(".loadHtml").click(function() {
      var xhtml = $(this).attr("href");
      $("#content").load(xhtml);
      return false;
   });

   if(gup('load')) {
      var jsonSrc = gup('load');
      var position = gup('position');
      currentShowJson = jsonSrc;
            
      $.getJSON(jsonSrc, {}, function(show) {
         currentShow = show;
         initShow(position);
      });
   }
});
		
var initShow = function(position) {
   if(currentShow != null) {
      if(position && position >= 0 && position < currentShow.length) {
         currentIndex = position;
      } else {
         currentIndex = 0;
      }

      for(var i = 0; i < currentShow.length; i++) {
         jQuery("<img>").attr("src", currentShow[i].src);
      }
      showPhoto(currentShow[currentIndex]);
   }
   
   return false;
}

var showNext = function() {
   currentIndex++;
   if(currentIndex == currentShow.length) {
      currentIndex = 0;
   }

   showPhoto(currentShow[currentIndex]);
}

var showPrevious = function() {
   if(currentIndex > 0) {
      currentIndex--;
   } else {
      currentIndex = (currentShow.length - 1);
   }

   showPhoto(currentShow[currentIndex]);
}

var showPhoto = function(photo) {
   var html = "";
   var caption = photo.caption || "";
   
   html = '<p class="photo"><img src="'+photo.src+'" alt="'+caption[currentLanguage]+'" title="" /></p>';
   if(caption[currentLanguage]) {
      html += '<div class="previous"><img src="../icon_left.png" alt="Zurück"/></div><div class="permalink"><a title="Permalink" href="?load='+currentShowJson+'&amp;position='+currentIndex+'">&bull;</a></div><div class="next"><img src="../icon_right.png" alt="Weiter"/></div><p class="caption">'+caption[currentLanguage]+'</p>';
   }
   
   $("#content").html(html);
   
   $(".previous").click(function() {
      showPrevious();
   });
   $(".next").click(function() {
      showNext();
   });
   $(".photo").click(function() {
      showNext();
   });
   $(".photo").mousemove(function() {
      $(this).css("cursor", "e-resize");
   });
   
   return false;
}

var gup = function(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
