var theImgMarginLeft = 10;
var thePages = new Array();

function insertPage()   {
   var theDiv = document.getElementById('site-mask');
   var thePageCount = 0;
   for(var theIndex = 0; theIndex < theDiv.childNodes.length; theIndex++)  {
      //we have a page, and the page needs a new page inserted before it
      if(theDiv.childNodes[theIndex].nodeType == 1 )   {
		 
         if(thePages[thePageCount] != undefined)   {
			//var theResource = new Image();
			//theResource.src = thePages[thePageCount];
            var theImg = document.createElement('img');
            theImg.src = thePages[thePageCount];
            theImg.style.margin = '0 0 0 '+theImgMarginLeft+'px';
			//theImg.style.width = theResource.width+'px';
            theDiv.insertBefore(theImg,theDiv.childNodes[theIndex]);
            thePages[thePageCount] = undefined;
            return;
         }
         thePageCount++;
      
      //we have no pages left, but a new page needs to be appended
      }  
      if(theIndex == (theDiv.childNodes.length - 1)
         && thePages[thePageCount] != undefined
      )  {
		 //var theResource = new Image();
		 //theResource.src = thePages[thePageCount];
         var theImg = document.createElement('img');
         theImg.src = thePages[thePageCount];
         theImg.style.margin = '0 0 0 '+theImgMarginLeft+'px';
		 //theImg.style.width = theResource.width+'px';
         theDiv.appendChild(theImg);
         thePages[thePageCount] = undefined;
         thePageCount++;
         return;
      }
   }
}


function insertImage(inUrl)  {
   
   var theDiv = document.getElementById('site-mask');
   var theImg = document.createElement('img');
   var theResource = new Image();
   theResource.src = inUrl;
   theImg.src = inUrl;
   theImg.style.margin = '0 0 0 '+theImgMarginLeft+'px';
   theImg.style.width = theResource.width;
   theDiv.appendChild(theImg);

}


function updateScreenSize() {
   
   for(theIndex in thePages)  {
      if(thePages[theIndex] != undefined)  {
         var stopAt = theIndex;
         break;
      }
   }
   
   

   var theDiv = document.getElementById('site-mask');
   var theDivWidth = 0;
   
   for(var theIndex = 0; theIndex < theDiv.childNodes.length; theIndex++)  {
      if(theDiv.childNodes[theIndex].nodeType == 1
      )  {
         if(theDiv.childNodes[theIndex].nodeName.toLowerCase() == 'img') {
            theDivWidth += theDiv.childNodes[theIndex].width +  theImgMarginLeft;
         }  else if(theDiv.childNodes[theIndex].style
            && theDiv.childNodes[theIndex].style.width
         )  {
            theDivWidth += parseInt(theDiv.childNodes[theIndex].style.width) +  theImgMarginLeft;
         }
      }
   }
   theDiv.style.width = (theDivWidth-theImgMarginLeft) + "px";
   
}

function countCompletedImages()  {
   var theImagesCompleted = 0;
   var theImagesIncomplete = document.images.length
   for(var theIndex = 0; theIndex < document.images.length; theIndex++)  {
      //return maximum if browser does not support attribute
      //and update screen size
      //this applies to safari 1.3 for example
      if(document.images[theIndex].complete == undefined)   {
         updateScreenSize();
         return document.images.length;
      }  
      if(document.images[theIndex].complete == true)   {
         theImagesCompleted++;
      }
   }
   return theImagesCompleted;
}

function documentImageWatcher()  {
   
   var currentlyLoaded = countCompletedImages();
   if(documentImageWatcher.arguments.length == 1)  {
      var inCount = documentImageWatcher.arguments[0];
   }  else  {
      var inCount = currentlyLoaded;
   }
   if(inCount < currentlyLoaded)  {
      updateScreenSize();
      imageLoadNext()
      inCount++;
   }
   window.setTimeout('documentImageWatcher('+inCount+')',150);
   
   
   
}

function imageLoadNext()   {
   for(theIndex in thePages)  {
      if(thePages[theIndex] != undefined)  {
         insertPage();
         break;
      }
   }
}

function showLogo()  {
   
}

main = function() {
   
   var theLinks = document.getElementById('postload-images').getElementsByTagName('a');
   for(var theIndex = 0; theIndex < theLinks.length; theIndex++)  {
      thePages[parseInt(theLinks[theIndex].innerHTML)] = theLinks[theIndex].getAttribute('href');  
   }
   
   
   window.setTimeout('document.getElementById("site-mask").getElementsByTagName("img")[0].style.display = "block";',10);
   documentImageWatcher(document.images.length);
   imageLoadNext();
   
}

window.onload = main;

