//*************************************************
// imagesController.js
//
//Summary: 
//
//Author:      Jeff Buhr
//
//Created:     28 Apr 08
//Summary of Modifications:
//       none
//*************************************************

// albums class
function imagesController(display,baseDir) {
   // constants
   this.NORMAL_STATE = 4;

   this.display = display;
   this.baseDir  = baseDir;
   this.http     = getHTTPObject();
   this.curImage = 0;
   
   // Ajax call to load an album into memory
   this.loadAlbum = function(album) {

      // now get the event dates
      var url= "photogallery/getImageList.php?album=" + album + "&time=";
      url += new Date().getTime(); // prevents browser loading from cache

      // try to make the request
      try {
         // send the request
         this.http.open('GET', url, true); 
         this.http.onreadystatechange = this.handleHttpGetImageNames;
         this.http.send(null);
      }
      catch (e) {
         ;
      }

      return;
   }

   // Call back function for handling the loadAlbum ajax
   this.handleHttpGetImageNames = function(album) {

      // if there hasn't been any errors
      if (picGallery.display.images.http.readyState == picGallery.display.images.NORMAL_STATE) {
         // split by the divider | 
         var results = picGallery.display.images.http.responseText.split('|');

         if (results[0] == 'true') {

            results.shift(); // get rid of "true" (1st param)

            // start preloading the images!
            picGallery.display.imagePreloader = new ImagePreload(results,
                                                                 picGallery.display.updatePercent,
                                                                 picGallery.display.updateFinished,
                                                                 picGallery.display.updateImageListing);

            // show list of the images in the picture list
            picGallery.display.images.updateImageList(results);

         }
         else { 
            picGallery.display.showError("Unable to get images. " + results[1]);
         }
      }

      return;
   }

   // updates image list with an array of image names (results)
   this.updateImageList = function(results) {
      var imageList;

      // check if any images exist
      if (isset(results[0])) {
         imageList = document.createElement('ul');
         imageList.id = "listOfImages";
       
         // list out the values
         for (x = 0; isset(results[x]); x++) {

            // create new nodes and set values
            var listItem = document.createElement('li');
            listItem.id   = "pictureElm" + x;
            var myLink = document.createElement('a');
            myLink.href = "javascript:picGallery.display.showImage(" + x + ");";
            var listItemText = document.createTextNode("Picture " + (x + 1));

            // connect nodes
            myLink.appendChild(listItemText);
            listItem.appendChild(myLink);
            imageList.appendChild(listItem);
         }

      }
      else {
         imageList = document.createTextNode("No Images available for this album.");
      }

      // update the DOM
      var imageListDiv = document.getElementById("imageList");
      this.display.removeAnyChildren(imageListDiv);
      imageListDiv.appendChild(imageList);

      // automatically load the first image of the album
      if (isset(results[1])) {
         this.display.showImage(0);
      }
   }
}
