/*This software is governed by a creative commons license whose terms of use are explained in the readme file */

//shut error report: return true
onerror= function(){return true;};
//for debug only
//onerror= function(msg, url, line){alert("error: "+ msg +"\n"+url +":"+line);return true;}

/*This set of functions implement prefteching of the thumbnail page. An onload prefetchNextPage event handler is added at the beginning of the page body. When this handler is triggered, the location property of an iframe is set to the next thumbnail page in order to prefetch the next page. If the user clicks on the link to load the next page, the function getPrefeteched is called and  the contents element of the current page is substituded for the contents element of the preloaded hidden frame. If  getPrefetched should fail due to a previous silent failure of prefetchNextPage, the onclick event bubbles and standard loading is performed instead , as a fall back strategy. This is also the case should  javascript be disabled on the client machine. This part is an add-on for performances, it can be easily disabled by defining prefetchNextPage and getPrefetched as empty functions.
*/
   function getObj(elemRef) {
    //convert elemRef into a valid object reference
    if (typeof elemRef == "string") {
     if (document.getElementById) {
      return document.getElementById(elemRef);
     } else {
      if (document.all) {
       return document.all[elemRef];
      } else
       return null;
     }
    } else
     return elemRef;
   } //getObj

   function addOnLoadHandler(handler) {
    //add a handler for the onload event of the page: use DOM 0
		//handlers are executed in the call order
    var previousHandler = (window.onload) ? window.onload : function() { };
    window.onload = function() {
		 previousHandler(); //call the previous handler
		 handler();         // call the new handler
    }
   }                    //addOnLoadHandler

   function prefetchNextPage() {
    //This function is called either from the top window or from the frames prefetch
    var oIframe = null;
    try {
     if (self.parent != self) {
      //I'm a frame and have a top window which created me
      self.loaded = true;
      //I've been loaded as a result of prefetch: I don't prefetch other album pages nor the slideshow
      return;
     }
/* ignore the following: a staic iframe is preferred to a dynamically cretaed one*/
     /*if (!frames["frameprefetch"]) {
      first call: create a hidden iframe an append it at the end of the document
       if (!createIframe("frameprefetch"))
       throw "iframe not created";
     }*/
     if (getObj("nextThumbnailPage")) {
      //prefetch into the hidden frame
      oIframe = frames["frameprefetch"];
			//don't register in the browser history
      oIframe.location.replace (getObj("nextThumbnailPage").getAttribute("href"));
     }
		 /*prefetch diaporama*/
		 if (getObj("diaplink")) {
      //prefetch into the hidden frame
      oIframe = frames["frameprefetchdiap"];
			if (! oIframe.loaded)
				//load viewDiaporama.php, normally once only on the first call to viewAlbum.php
				//don't register in the browser history
      	oIframe.location.replace (getObj("diaplink").getAttribute("href"));
     }
    } catch (ex) {
     return; //fail silently
    }
   }         //prefetchNextPage

   function getPrefetched() {
    var oIframe = null;
    try {
     oIframe = frames["frameprefetch"];
     if (!oIframe || !oIframe.loaded){
      throw "nothing has been prefetched";
		 }
     //copy the innerHTML of the <div id="contents"> element from the document in the hidden frame into the document <div id="contents"> of the current window
     document.title = oIframe.document.title;
     self.getObj("PJGalleryAlb").innerHTML = oIframe.getObj("PJGalleryAlb").innerHTML;/*Firefox doesn't seem to be happy with the copying of the prefetch frame and doesn't recognize it*/
     //launch the prefetching for the next page, since the copying above won't fire the onload event
		 setTimeout("prefetchNextPage()",5);
    } catch (ex) {
     return true; //let the event bubble so as to fall back on loading without prefetch
    }
    return false;
   } //getPrefetched
	 
/*************************
* OBSOLETE CODE
*************************/
   function createIframe(frameName) {
		 //not used
    //create an iframe and append it at the end of the document
    var obj = document.createElement("iframe");
    obj.width = 0;
    obj.height = 0;
    obj.frameBorder = 0;
    obj.src = "about:blank";
    obj.name = frameName;
    obj.id = frameName;
    document.body.appendChild(obj);
    return obj;
   } //createIframe
