// Last change by Guus Ellenkamp on August 29, 2005: changed definition of preload function
// Continued finishing preload function

// This file will have the code for the preloading. Might be incorporated in the functions file.

var preloadObjects=new Array(); // This array will hold one preloadObject for each time the function is called
var preloadIndex=0; // Contains the index for the preloadObject currently loading
 var mozIndex=0;
function objectReady2(obj) { // added the 2 to prevent interaction with same function in functions file

        var tag;

        if (typeof(obj)!="object")
                window.alert("Invalid function call objectReady2, type must be object");
        else {
                if (typeof(obj.tagName)!="string")
                        window.alert("Invalid function call objectReady2, object must have tagName");
                else {
                        tag=obj.tagName.toUpperCase();
                        switch (tag) {
                                case "SCRIPT": {

                                        return ((obj.readyState=="loaded")||(obj.readyState=="complete"));
                                }
                                default:
                                        return (obj.readyState=="complete");
                        }
                }
        }
}

function stopPreloading() {
        // Not implemented yet. Seems it should just remove the objects from the array.
}

function checkLoadObjectStatus(preloadObject) { // Add just to simplify the code

        // checks whether a prelaodObject is loaded

        objectsLoaded=true;
        i=0;
        l=preloadObject.objects.length; // first get the number of objects to be loaded for the current preloadObject
        while ((i<l)&&(objectsLoaded)) {
                objectsLoaded=objectReady2(preloadObject.objects[i]);
                i++;
        }
        return objectsLoaded;
}

function checkStatus() {

        var i, l;

        // window.alert("This is checkStatus");
        // the function will only be called if preload has been called, so there will be always one object
        // this might change when we implement the "stop" method. The stop method starts to sound stupid to me now

        // We'll first check the object which is preloaded now, supposedly starting with the index

        if (checkLoadObjectStatus(preloadObjects[preloadIndex])) {
                // The current object is loaded, so check if we have more things to load
                preloadObjects[preloadIndex].loaded=true;
                if (preloadIndex<(preloadObjects.length-1)) {
                        preloadIndex++; // we'll check the next thing to be loaded
                        checkStatus(); // we'll just check the next object straight away
                }
                // else we don't have to do anything anymore
        }
        else // we'll just wait for another while
                window.setTimeout(checkStatus, 100);
}

function getFileType(filename) {

        var index, s, fileExtension;

        s=filename;
        index=s.lastIndexOf(".");
        // We have the last "."
        if (index!=-1) {
                s=s.substr(index);
                return s.toLowerCase();
        }
        else
                return "unknown";
}

function preload(files) {

        // input: array of filenames
        // Last change: shouldn't use objects, but filenames, otherwise we won't be able to control anything

        var i, l, preloadObject;

        if (files.length==0)
                return null;
        // else
        preloadObject=new Object();
        preloadObject.loaded=false;
        preloadObject.stop=stopPreloading;
        preloadObject.fileNames=files;
        preloadObject.objects=new Array();
        l=files.length;
        i=0;
        while (i<l) {
                switch (getFileType(files[i])) {
                        case ".jpg":
                        case ".png":
                        case ".bmp":
                        case ".gif":
                        case ".jpeg":
                                {
                                preloadObject.objects[i]=new Image();
                                preloadObject.objects[i].src=files[i];
                                //window.alert("Object for \""+files[i]+"\" created");
                                break;
                        } // end ".jpg"
                        case ".js": {
                                preloadObject.objects[i]=document.createElement("script");
                                preloadObject.objects[i].src=files[i];
                        //        window.alert("Object for \""+files[i]+"\" created");
                                break;
                        }
                        case ".swf":{
                                preloadObject.objects[i]=document.createElement("embed");
                                preloadObject.objects[i].src=files[i];
                                break;
                        }
                        default: { // just set to "complete"
                                preloadObject.objects[i]=new Object();
                                preloadObject.objects[i].tagName="unknown";
                                preloadObject.objects[i].readyState="complete";
                                break;
                        } // end default
                } // end switch
                i++;
        } // end while
        // prepare for further use, so put in array
        l=preloadObjects.length;
        preloadObjects[l]=preloadObject;
        checkStatus();

        if(preloadObjects.length==1){//we assume that we load first the important js file
                  if(navigator.appName=="Netscape"){//for mozilla
                          for(i=0; i<preloadObjects[0].objects.length; i++){
                                  preloadObjects[0].objects[i].onload=mozLoad;
                          }
                  }
                  else{ //Internet Explorer

                          myTime=window.setInterval("firstObject()", 10);
                  }
        }

        return preloadObjects[l];
}

function firstObject(){
           var i, fromNet, isLoad;
             allLoaded= true;

                        i=0;
                        myVal=0;

                        while(i<preloadObjects[0].objects.length){

                              if(preloadObjects[0].objects[i].readyState=="complete" || preloadObjects[0].objects[i].readyState=="loaded"){
                                        myVal++;

                                }
                        i++;
                        }

                        if(myVal==preloadObjects[0].objects.length){
             //                   clearBody()
                                init();
                                preloadObjects[0].onload=init;
                                window.clearInterval(myTime);
                        }
}

function clearBody(){
var i, myChild;
   //this is for the progress bar to be remove if we have final loader this is temporary for now
             bar1.togglePause();
             clearInterval(bar1.tid);
   //clear the children of the body
             for(i=0; i<document.body.childNodes.length; i++){
                      myChild=document.body.childNodes[i];
                      document.body.removeChild(myChild);
             }

    //double check if there is any element left;
             if(document.body.childNodes.length>0){
                         clearBody()
             }

}
function mozLoad(){

           mozIndex++;
         if(mozIndex==preloadObjects[0].objects.length){
     //            clearBody();
                 init();
                 preloadObjects[0].onload=init;

         }

}