/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!                  
/***************************/


var LoadBar = function(){
    this.value = 0;
    this.sources = Array();
    this.sourcesDB = Array();
    this.totalFiles = 0;
    this.loadedFiles = 0;
};
//Show the loading bar interface
LoadBar.prototype.show = function() {
    this.locate();
    document.getElementById("loadingZone").style.display = "block";
};
//Hide the loading bar interface
LoadBar.prototype.hide = function() {
    document.getElementById("loadingZone").style.display = "none";
};
//Add all scripts to the DOM
LoadBar.prototype.run = function(){
    this.show();
    var i;
    for (i=0; i<this.sourcesDB.length; i++){
        var source = this.sourcesDB[i];
        var head = document.getElementsByTagName("head")[0];
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = source;
//alert(script.src);
//"js/" + source
        head.appendChild(script);
    }   
};
//Center in the screen remember it from old tutorials? ;)
LoadBar.prototype.locate = function(){
    var loadingZone = document.getElementById("loadingZone");
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = loadingZone.clientHeight;
    var popupWidth = loadingZone.clientWidth;
    loadingZone.style.position = "absolute";
    loadingZone.style.top = parseInt(windowHeight/2-popupHeight/2) - 50 + "px";
    loadingZone.style.left = parseInt(windowWidth/2-popupWidth/2) + "px";
};
//Set the value position of the bar (Only 0-100 values are allowed)
LoadBar.prototype.setValue = function(value){
    if(value >= 0 && value <= 100){
        document.getElementById("progressBar").style.width = value + "%";
        document.getElementById("infoProgress").innerHTML = parseInt(value) + "%";
    }
};
//Set the bottom text value
LoadBar.prototype.setAction = function(action){
    var obj = document.getElementById("infoLoading");

    if(obj == null || obj == undefined || obj == '') return false;

    obj.innerHTML = action;

    return true;
};
//Add the specified script to the list
LoadBar.prototype.addScript = function(source){
    this.totalFiles++;
    this.sources[source] = source;
    this.sourcesDB.push(source);
};
//Called when a script is loaded. Increment the progress value and check if all files are loaded
LoadBar.prototype.loaded = function(file) {
    this.loadedFiles++;
    delete this.sources[file];
    var pc = (this.loadedFiles * 100) / this.totalFiles;
    this.setValue(pc);

    var phase = '';

    if(file.match(/engine(?=[.])/)){
       file = document.getElementById("infoLoading_system").innerHTML;
    }else if(file.match(/custom(?=[.])/)){
       file = document.getElementById("infoLoading_setting").innerHTML;
    }else if(file.match(/ui(?=[.])/)){
       file = document.getElementById("infoLoading_ui").innerHTML;
    }else{
       file = document.getElementById("infoLoading_default").innerHTML;
    }
    //alert(file.match(/engine/));
    var result = this.setAction(file);

    if(result == false) return;

    //Are all files loaded?
    //alert(this.loadedFiles +'-'+ this.totalFiles);
    if(this.loadedFiles == this.totalFiles){
        //setTimeout("myBar.hide()",300);
        //load the reset button to try one more time!
        //document.getElementById("restart").style.display = "block";
        //window.location = '/?shva=1';

        document.getElementById('id_redirect_page').submit();
    }
};

//if(typeof(myBar) != 'undefined') setTimeout("myBar.loaded('http://static.wiwos.com/j/engine/core/engine.pre.loading.js')", 500);