// Global preload functions
var g_iStep = 0;
// var g_AllLoaded = false;
// set to true temporary until firefox image preload bug is found and solved
var g_AllLoaded = true
var oPreload = null;

function OnImgUpdate( iProgress )
{  
   if( (iProgress >= 1) && (iProgress <= 10) && (iProgress > g_iStep) )
   {  
       g_iStep++;
       var oSpan = document.getElementById( "sDot" + iProgress + "" );
       if (oSpan != null)
           oSpan.className = 'FullDot';
   }
}

function OnCompletion()
{  
//  document.location.replace('main.html');
//    g_AllLoaded = true;

}

function StartPreload()
{
  var szImages = new Array( "./images/pic_1_5.jpg", "./images/pic_1_6.jpg", "./images/pic_1_A.jpg", "./images/pic_1_4.jpg", "./images/pic_1_9.jpg", "./images/pic_1_H.jpg", "./images/pic_1_1.jpg", "./images/pic_1_2A.jpg", "./images/pic_1_7.jpg", "./images/pic_1_8.jpg");

//showDebug();

   // Execute Image Preloader
   oPreload = new ImagePreload( szImages, OnImgUpdate, OnCompletion );
}



//Gradually fade-in/-out images, works with mozilla and ie
var baseopacity=30
function fadehigh(whichImg){

  if (g_AllLoaded==false) return;

  imgobj=whichImg
  browserdetect = whichImg.filters ? "ie" : typeof whichImg.style.MozOpacity=="string" ? "mozilla" : ""
  setDegree(baseopacity)
  highlighting = setInterval("doFade(imgobj)",50)
}

function fadelow(){

   if (g_AllLoaded==false) return;

   cleartimer()
   setDegree(baseopacity)
}

function setDegree(degree){
   if (browserdetect=="mozilla")
      imgobj.style.MozOpacity=degree/100
   else if (browserdetect=="ie")
      imgobj.filters.alpha.opacity=degree
}

function cleartimer(){
   if (window.highlighting) clearInterval(highlighting)
}

function doFade(baseop){
  if (browserdetect=="mozilla" && baseop.style.MozOpacity<1)
    baseop.style.MozOpacity=Math.min(parseFloat(baseop.style.MozOpacity)+0.1, 0.99)
  else if (browserdetect=="ie" && baseop.filters.alpha.opacity<100)
    baseop.filters.alpha.opacity+=10
  else if (window.highlighting)
    clearInterval(highlighting)
}

//Change an image instantle dynamically
function changeImage(filesource) {

  if (g_AllLoaded==false) return;

   document.getElementById('MyMainImg').src=filesource;
}


//Change an image dynamically using an ie filter effect
var filterstring="progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.0 Duration=0.5 Motion=Reverse)"

function modifyImage(loadarea, imgFile) {

  // Is imageset pre-loaded?
  if (g_AllLoaded==false) return;

  if (document.getElementById) {
    var txtobj = document.getElementById("textarea");
    if (txtobj) txtobj.style.height="130px";

    var imgobj = document.getElementById(loadarea);
    imgobj.style.height="349px";

    if (browserdetect=="ie" && imgobj.filters && window.createPopup) {

      imgobj.style.filter=filterstring;
      imgobj.filters[0].Apply();
    }

    imgobj.innerHTML = '<img src="'+imgFile+'" height=349 border=0>'

    if (browserdetect=="ie" && imgobj.filters && window.createPopup)
      imgobj.filters[0].Play();


  }
}


function ImagePreload( p_aImages, p_pfnPercent, p_pfnFinished )
{   // Call-back routines
   this.m_pfnPercent = p_pfnPercent;
   this.m_pfnFinished = p_pfnFinished;

   // Class Member Vars
   this.m_nLoaded = 0;
   this.m_nProcessed = 0;
   this.m_aImages = new Array;
   this.m_nICount = p_aImages.length;

   debug("No images to preload: "+p_aImages.length);

   // Preload Array of Images
   for( var i = 0; i < p_aImages.length; i++ )
       this.Preload( p_aImages[i] );
}

ImagePreload.prototype.Preload = function( p_oImage )
{   var oImage = new Image;
   this.m_aImages.push( oImage );

   oImage.onload = ImagePreload.prototype.OnLoad;
   oImage.onerror = ImagePreload.prototype.OnError;
   oImage.onabort = ImagePreload.prototype.OnAbort;

   oImage.oImagePreload = this;
   oImage.bLoaded = false;
   oImage.source = p_oImage;
   oImage.src = p_oImage;

   debug("img obj create "+oImage.source+" "+oImage.complete);
}

ImagePreload.prototype.OnComplete = function()
{  this.m_nProcessed++;

   // Update progress bar
   this.m_pfnPercent( Math.round( (this.m_nProcessed / this.m_nICount) * this.m_nICount ) );

   // Are we done?
   if ( this.m_nProcessed == this.m_nICount ) {
       this.m_pfnFinished();
   }
   debug("complete");
}

ImagePreload.prototype.OnLoad = function()
{   // 'this' pointer points to oImage Object
   debug("onload called");
   this.bLoaded = true;
   this.oImagePreload.m_nLoaded++;
   this.oImagePreload.OnComplete();
}

ImagePreload.prototype.OnError = function()
{   // 'this' pointer points to oImage Object
   this.bError = true;
   this.oImagePreload.OnComplete();

   debug("onerror");
}

ImagePreload.prototype.OnAbort = function()
{   // 'this' pointer points to oImage Object
   this.bAbort = true;
   this.oImagePreload.OnComplete();

   debug("onabort");
}

// Debug functions
function showDebug() {
  window.top.debugWindow =
      window.open("",
                  "Debug",
                  "left=0,top=0,width=300,height=700,scrollbars=yes,"
                  +"status=yes,resizable=yes");
  window.top.debugWindow.opener = self;
  // open the document for writing
  window.top.debugWindow.document.open();
  window.top.debugWindow.document.write(
      "<HTML><HEAD><TITLE>Debug Window</TITLE></HEAD><BODY><PRE>\n");
}

// If the debug window exists, then write to it
function debug(text) {
  if (window.top.debugWindow && ! window.top.debugWindow.closed) {
    window.top.debugWindow.document.write(text+"\n");
  }
}
