var xmlHttp;
var xmlDoc = null;

var months = [ 'nope', 'January', 'February', 'Mars', 'April', 'May', 'June',
               'July', 'August', 'September', 'October', 'November', 'December' ];

var currentMonth;
var currentYear;
var currentPage = 1;
var pages;
var maxImages = 32;
var preferredWidth = 160;
//An array to store results from album requests, so that we can do prev and next
var albumsHash = new Object();
//The interval object to keep track of slideshows
var interval = "uninitialized";
slideshowMillis = 5000;

function init() {
   var today = new Date();
   currentMonth = today.getMonth() + 1;
   currentYear = y2k(today.getYear());
   addKeyListener();
   disableKeyListener();
   if (location.hash == "") {
      //Default: Just show pictures from the current date, opening the
      //last page.
      getAlbumPictures("default", false, false, true, false);
      buildMenu();
   } else if (inputDate(location.hash)) {
      //Show a specific picture.
      //Picture names should be in the form 2008-11-01_15-02-48
      var picname = location.hash.substring(1); //remove the leading #-sign
      currentMonth = parseInt(picname.split("-")[1], 10);
      currentYear = parseInt(picname.split("-")[0], 10);
      var date = picname.split("_")[0];
      var time = picname.split("_")[1].replace(/-/g, ":"); 

      //Get the index in the query for this picture, so we can calculate
      //What page it's on.
      var index=1;
      $.ajax({
         type: "GET",
         url: "getIndex.php",
         datatype: "xml",
         data:  {month: currentMonth, date: date, time: time, year: currentYear},
         async: false,
         success: function(xml) {
            index = $("indexno", xml).text();
         }});
         
      //Then that picture should be on page...
      var page = Math.ceil(index / maxImages);
      currentPage = page;

      //setStatus("Index: " + index + ", Maximages: " + maxImages + ", page: " +page);

      //Set stuff upp and show the picture
      getAlbumPictures("default", false, false, false, false);
      buildMenu();
      build_bigpicture(picname, picname.split("_")[0], "default");

   } else {
      //album. We have a href that looks like http://www.loodin.se/#japan
      var albumname = location.hash.substring(1); //remove the leading #-sign
      getAlbumPictures(albumname, false, false, false, false);
      buildMenu();
   }
}

function y2k(number) {
   return (number < 1000) ? number + 1900 : number;
}

function getAlbumPictures(album, openfirst, openlast, goLastPage, slideshow) {
    //Set subtitle.
    if (album == "default") {
	$("#subtitle").html("&rarr;Photostream&rarr;" + months[currentMonth] +
			       ", " + currentYear);
    } else {
	$("#subtitle").html("&rarr;Album&rarr;" + album);
    }

   //Make sure goLastPage is either true or false, not unset.
   if (goLastPage) {
      goLastPage = true;
   } else {
      goLastPage = false;
   }
   $.get('getPics.php',
      {album : album, limit: (maxImages), offset: ( (currentPage - 1) * maxImages), 
      month: currentMonth, year: currentYear, lastpage: goLastPage},
      function(xml) {
         albumsHash[album] = xml;
         showAlbumPics(xml, album, openfirst, openlast, goLastPage, slideshow);
      }
   );
}


function showAlbumPics(xml, album, openfirst, openlast, goLastPage, slideshow) {
   var baseUrl = "http://www.loodin.se/pics/";
   var html = "<ul>";
   var totalPictures = $("count", xml).text();
   var picturesCurrentPage = $("picture", xml.responseText).size();

   if (goLastPage) {
      currentPage = Math.max(Math.ceil(totalPictures / maxImages), 1);
   }
   
   buildLinks(picturesCurrentPage, totalPictures, album);

   $("picture", xml).each(function() {
      var name;
      var date;
      var description;
      name = $("name", this).text();
      date = $("date", this).text();
      description = $("description", this).text();

      var imgsrc = baseUrl + date + "/thumbs/" + name + "-thumb.jpg";

      html += "<li><div class=\"picture\">";
      html += "<a href=\"javascript:build_bigpicture('"+name
      + "','" + date + "','" + album + "')\">";

      html += "<img src=\"" + imgsrc + "\"/>";
      html += "</a></div></li>"; 

   }); 
   html += "</ul>";
   $("#txtHint").html(html);
   
   if (openfirst) {
      build_bigPictureFromName(getFirstPicture(album), album);
   } else if (openlast) {
      build_bigPictureFromName(getLastPicture(album), album);
   }
   
   if (slideshow) {
      picture = getFirstPicture(album);
      name= $('name', $(picture)).text();
      playSlideshow(name, album);
   }

}

//Checks if the input is on the form #2008-11-01_15-02-42
function inputDate(input) {
   patt = /.*\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}/;
   match = input.match(patt);
   res = (match != null); //If there is match, there is valid input
   return res;
}

function getIndex(picname) {
   currentMonth = parseInt(picname.split("-")[1], 10);
   currentYear = parseInt(picname.split("-")[0], 10);
   var date = picname.split("_")[0];
   var time = picname.split("_")[1].replace(/-/g, ":"); 

   //Get the index in the query for this picture, so we can calculate
   //What page it's on.
   var index=1;
   $.ajax({
      type: "GET",
      url: "getIndex.php",
      datatype: "xml",
      data:  {month: currentMonth, date: date, time: time, year: currentYear},
      async: false,
      success: function(xml) {
         index = $("indexno", xml).text();
      }});
   
   return index;
}


