/********************************************************
 * VMaxProgressBar Lib  v1.03                           *
 * (C)2000-2001   Virtual_Max  ( http://come.to/vmax )  *
 * This script and it's derivatives is free for use for * 
 * non commertial purposes  until  this notice  remains *
 * unchanged and  present in all copies and derivatives *
 * revision: 07/04/2001                                 *
 *******************************************************/

var barN    = 40;
var barW    = 8;
var barText = "..Loading images.."
var inactiveColor="#336633";
var barTextColor="#00cc66";
var barBGcolor="#000000";

function getById(id,o){
    if(document.getElementById) return document.getElementById(id);
    if(document.all)            return document.all[id]
    if(o==null) o=window.document;

    if(o.layers[id]){
      return o.layers[id]
    }

    if(o.images[id]){
      return o.images[id]
    }

    for(var i=0; i<o.layers.length; i++){
       var oo=getById(id,o.layers[i].document); 
       if(oo!=null) return oo;
    }
    return null;
}

function Dimension(x,y,w,h) {
  this.w=w; this.h=h; this.x=x; this.y=y;
}

function getDivSizes(id){
 var o = getById(id);
 if(o==null) return null;
 var x,y,w,h;
 if(document.layers) {
     x=o.left;
     y=o.top;
     if(o.height==null) o.height=o.clip.height;
     if(o.width==null)  o.width=o.clip.width;
     h=o.height;
     w=o.width;
  }
  else if(document.all){
     x=o.offsetLeft;
     y=o.offsetTop;
     w=o.scrollWidth;
     h=o.scrollHeight;
 }
 else{
     x=o.offsetLeft;
     y=o.offsetTop;
     w=o.offsetWidth;
     h=o.offsetHeight;
 }
 return new Dimension(x,y,w,h);
}


function getViewPort(){
  var x=0,y=0,w=640,h=480;
  if(document.all) {
     var o=window.document.body;
     x=o.scrollLeft;
     w=o.offsetWidth;
     y=o.scrollTop;
     h=o.offsetHeight;
  } 
  else {
     x=window.pageXOffset;
     w=window.innerWidth;
     y=window.pageYOffset;
     h=window.innerHeight;
  }

  return new Dimension(x,y,w,h);
}


function showDiv(id,vis) {
  var o = getById(id);
  if( o==null ) return;
  if(!document.layers) o=o.style;
    o.visibility = vis ? "visible" : "hidden";
}



function moveDiv(id,x,y){
  var o = getById(id);
  if( o==null ) return;
  if(document.layers){
     o.moveTo(x,y);     
  }
  else {
    o=o.style;
    if(document.all) {
       o.pixelLeft=x;
       o.pixelTop=y;
    }
    else {
       o.left=x;
       o.top=y;     
    }
  }
}


function createOne(i,d,n) {
    var x = Math.floor((d.w-barN*barW)/2+i*barW);
    var y = Math.floor(d.h/2);
    var s='<div ID="PB'+i+'" class="PB" '
    if(!document.layers && ! document.all) {
     s+='style=\"top:'+y+';left:'+x+'"';
    }
    s+='>';
    if(!document.all) s+='<spacer type=block height=15 width='+(barW-2)+'>'
    s+='</div>'
    document.writeln(s);
    if(document['PB'+i]) document['PB'+i].bgColor=inactiveColor;
    moveDiv('PB'+i,x,y);
}

function createProgressBar(){
    var d=getViewPort();
    var x = Math.floor((d.w-barN*barW)/2)-2;
    var y = Math.floor(d.h/2)-2;
    document.writeln('<style>.PB {position:absolute;background-color:'+inactiveColor+'; width:'+(barW-2)+';z-Index:10;visibilty:visible;}</style>');
    for(var i=0;i<barN;i++) {
       createOne(i,d,barN); 
    }
//    var d = getDivSizes('PB0');
//    var h = d.h+4;


    document.writeln('<style>.PBMCLASS{position:absolute;top:'+(y-16)+';left:'+x+';background-color:'+barBGcolor+'; width:'+(barN*barW+2)+';height:40;z-Index:9;color:'+barTextColor+';font-family:Arial;font-size:14px;font-weight:bold;}</style>');    
    var s='<div ID="PBM" class="PBMCLASS"><center>'+barText+'</center>';
    s+='<spacer type=block height=20 width='+(barN*barW+2)+'>';
    s+='</div>';
    document.writeln(s);    

    if(document['PBM'])     document['PBM'].bgColor=barBGcolor;


}



function colorFor(i){
    var threshold = barN*8/10 
    if(i<threshold) {
       var r=Math.floor(255*i/threshold);
       r=r.toString(16);
       if(r.length<2) r="0"+r
       return '#'+r+'ff00';
    } else {
       var g=255-Math.floor(255*(i-threshold)/(barN-threshold));
       g=g.toString(16);
       if(g.length<2) g="0"+g
       return '#ff'+g+'00';      
    }

}

function showProgress(percent){
    var n = Math.floor(barN * percent / 100);
    if(n>barN) n = barN;
    for(var i=0;i<n;i++){
       if(document.layers) document.layers['PB'+i].bgColor=colorFor(i)
       else getById('PB'+i).style.backgroundColor = colorFor(i);              
    }
}

function hideProgressBar() {
    for(var i=0;i<barN;i++){
       showDiv('PB'+i,false)
    }   
    showDiv('PBM',false);
}


var allIms=new Array();
function showImagesProgress(){
  for(var i=0;i<document.images.length;i++) {
     n = allIms.length;
     allIms[n]=new Image();
     allIms[n].src=document.images[i].src
     allIms[n].errored=false; 
     allIms[n].onerror=new Function("allIms["+i+"].errored=true"); 
     allIms[n].onload=updateProgress; 
  }
  updateProgress();
}



function updateProgress( refresh ){ 
   var cnt=0; 
   var tmpim = new Image();
   for(var i=0;i<allIms.length;i++){ 
     if(allIms[i].complete || allIms[i].errored) cnt++;       
   } 

   if(allIms.length>0) showProgress(100*cnt/allIms.length); 
   setTimeout("updateProgress()",50); 
   if(cnt>=allIms.length) setTimeout("hideProgressBar()",500);
   else if(refresh) setTimeout("updateProgress(true)",200);
} 

createProgressBar();
showImagesProgress();
