var globalName;
var globalAlbum;
var keyListenerEnabled = false;

function playSlideshow(name, album) {
   globalName = name;
   globalAlbum = album;
   
   $(".container").hide();
   $("body").css("background-color", "DimGrey");
   interval = setInterval('goNext()',slideshowMillis);
}

function quitSlideShow() {
   clearInterval(interval);
   $(".container").show();
   $("body").css("background-color", "grey");
   interval = "uninitialized";
}

function goNext() {
   gotoNextPicture(globalName, globalAlbum);
   var picture = getPicture(globalName, globalAlbum).next();
   var name = $('name', $(picture)).text();
   if (name == "") { //Reached end of slideshow
      quitSlideShow();
   } else {
      globalName = name;
   }
}

function gotoNextPicture(name, album) {
   var currentPic = getPicture(name, album);
   var picturesOnPage = $(albumsHash[album]).find("picture").size()
   var pictureIndex = getPictureIndex(name, album);

   if ( pictureIndex < picturesOnPage -1 ) {
      build_bigPictureFromName(currentPic.next(), album);
   } else {
      if (currentPage < pages) { //should we switch page?
         if (interval != "uninitialized") {
            clearInterval(interval);
            goPage(currentPage + 1, album, true, false, true);
         } else {
            goPage(currentPage + 1, album, true, false, false);
         }
         window.scrollTo(0,0);
      } else if (album == "default") { // should we switch month?
         var now = new Date();
         now.setDate(1);
         now.setMinutes(0);
         now.setSeconds(0);
         var picDate = new Date();
         picDate.setFullYear(currentYear, currentMonth-1, 1);
         picDate.setMinutes(0);
         picDate.setSeconds(0);
         if (picDate < now) {
            if (interval != "uninitialized") {
               clearInterval(interval);
               nextMonth(true, true);
            } else { 
               nextMonth(true, false);
            }
         }
      }
   }
}

function gotoPrevPicture(name, album) {
   var currentPic = getPicture(name, album);
   var picturesOnPage = $(albumsHash[album]).find("picture").size()
   var pictureIndex = getPictureIndex(name, album);

   if ( pictureIndex > 0 ) {
      build_bigPictureFromName(currentPic.prev(), album);
   } else {
      if (currentPage > 1) {
         goPage(currentPage - 1, album, false, true);
         window.scrollTo(0,0);
      } else if (album == "default"){
         //if not a special album, Go to previous month, open last picture, 
         //open last page
         prevMonth(true, true)
      }
   }
}

function getPictureIndex(name, album) {
   if (album == "") {
      album = "default";
   }
   var xml = albumsHash[album];
   var pictureElem = $(xml).find("picture");
   var nameElem = $(pictureElem).filter( function() {
      return ($('name', $(this)).text() == name);
   });
   var index = $(pictureElem).index(nameElem);
   return index;
}

function getFirstPicture(album) {
   return $(albumsHash[album]).find("picture:first");
}

function getLastPicture(album) {
   return $(albumsHash[album]).find("picture:last");
}

function getPicture(name, album) {
   currentName = name;
   currentAlbum = album;
   if (album == "") {
      return $(xmlDoc).find("picture").filter( function() {
         return ($('name', $(this)).text() == name);
      });
   } else {
      return $(albumsHash[album]).find("picture").filter( function() {
         return ($('name', $(this)).text() == name);
      });
   }
}

function setStatus(statusString) {
   $("#status").text(statusString);
}

function appendStatus(statusString) {
   $("#status").text($("#status").text() + ", " + statusString);
}

function goPage(pageNo, album, openfirst, openlast, slideshow) {
   currentPage = pageNo;
   reload(album, openfirst, openlast, false, slideshow);
}

//Show a real album from the Album menu
function showAlbum(albumName) {
   currentPage = 1;
   getAlbumPictures(albumName);
   buildMenu();
}

//Reload the page for some reason, maybe having set currentPage before.
function reload(album, openfirst, openlast, goLastPage, slideshow) {
   getAlbumPictures(album, openfirst, openlast, goLastPage, slideshow);
   buildMenu();
}

function getYScroll() {
   scrOfY = 0;
   if (typeof (window.pageYOffset) == 'number') {
      // Netscape compliant
      scrOfY = window.pageYOffset;
   } else if (document.body && document.body.scrollTop) {
      // DOM compliant
      scrOfY = document.body.scrollTop;
   } else if (document.documentElement && document.documentElement.scrollTop) {
      // IE6 standards compliant mode
      scrOfY = document.documentElement.scrollTop;
   }
   // appendStatus("scroll: " + scrOfY);
   return scrOfY;
}

function getXScroll() {
   scrOfX = 0;
   if (typeof (window.pageXOffset) == 'number') {
      // Netscape compliant
      scrOfX = window.pageXOffset;
   } else if (document.body && document.body.scrollTop) {
      // DOM compliant
      scrOfX = document.body.scrollLeft;
   } else if (document.documentElement && document.documentElement.scrollLeft) {
      // IE6 standards compliant mode
      scrOfX = document.documentElement.scrollLeft;
   }
   // appendStatus("scroll: " + scrOfX);
   return scrOfX;
}

function resetYScroll() {
   scrOfY = 0;
   if (typeof (window.pageYOffset) == 'number') {
      // Netscape compliant
      window.pageYOffset = 0;
   } else if (document.body && document.body.scrollTop) {
      // DOM compliant
      document.body.scrollTop = 0;
   } else if (document.documentElement && document.documentElement.scrollTop) {
      // IE6 standards compliant mode
      document.documentElement.scrollTop = 0;
   }
   // appendStatus("scroll: " + scrOfY);
   return scrOfY;
}

function gotoCurrentMonth() {
   var today = new Date();
   currentMonth = today.getMonth() + 1;
   currentYear = y2k(today.getYear());
   reload("default", false, false, true);
}

function prevMonth(openLast, goLastPage) {
   if (currentMonth == 1) {
      currentMonth = 12;
      currentYear--;
   } else {
      currentMonth--;
   }
   currentPage = 1;
   reload("default", false, openLast, goLastPage);
}

function prevYear() {
   currentYear--;
   currentMonth = 1;
   currentPage = 1;
   reload("default", false, false);
}

function nextMonth(openfirst, slideshow) {
   if (currentMonth == 12) {
      currentMonth = 1;
      currentYear++;
   } else {
      currentMonth++;
   }
   currentPage = 1;
   reload("default", openfirst, false, false, slideshow);
}

function nextYear() {
   currentYear++;
   currentMonth = 1;
   currentPage = 1;
   reload("default", false, false, false);
}

function enableKeyListener() {
   keyListenerEnabled = true;
}

function disableKeyListener() {
   keyListenerEnabled = false;
}

//adds the keyListener.
//Notice that currentName and currentAlbum is defined in 
//bigpicture.js when opening a picture
function addKeyListener() {
   $(function() {
      $(document).keydown(function (evt) {
         if (keyListenerEnabled) {
            //alert("Key pressed: " + evt.keyCode);
            switch (evt.keyCode) {
            case 27: //esc
               closeBigPicture();
               break;
            case 37: //left
               gotoPrevPicture(currentName, currentAlbum);
               break;
            case 39: //right
               gotoNextPicture(currentName, currentAlbum);
               //goNext();
               break;
            }
         }
      });
   });
}

