/*

Based off http://www.dynamicdrive.com/dynamicindex11/xpprogressbar.htm

var myfoobar = createProgressBar(
  * width- Total width of the entire bar in pixels.
  * height- Total height of the entire bar in pixels.
  * backgroundColor- Background color of the bar. Use valid CSS color or HEX color code value.
  * borderWidth- The width of the border around the bar, in pixels.
  * borderColor- The color of the border around the bar. Use valid CSS color or HEX color code value.
  * blockColor- The darkest color of the individual blocks. The color will progressively become more transparent. Use valid CSS color or HEX color code value.
  * scrollSpeed- The delay, in milliseconds, between each scroll step. Use smaller values for faster scroll speeds.
  * blockCount- The total number of blocks to use.
  * actionCount - The number of times the bar is to scroll before actionString is performed.
  * actionString - The javascript function, in string form, to execute once the bar has scrolled actionCount times. Set this to an empty string to do nothing. If doing nothing, you can use any number as actionCount.
  * hidden - if non-empty, it starts out hidden
  );

Ex: var bar1= createProgressBar(300,15,'white',1,'black','blue',85,7,3,"");

*/
var xpbar_w3c = (document.getElementById) ? true:false;
var xpbar_ie = (document.all) ? true:false;
var xpbar_N = -1;

function createProgressBar(w, h, bgc, brdW, brdC, blkC, speed, blocks, count, action, hidden)
{
    if(xpbar_ie || xpbar_w3c)
    {
        ++xpbar_N;
        //h2 = Math.round(h/2) - 1;
        h2 = h;
        var t='<div id="_xpbar' + xpbar_N + '" style="visibility: ' + (hidden ? 'hidden' : 'visible') + ';'
            +' position:relative; overflow:hidden; z-index: 999;'
            +' width: ' + w + 'px; height: ' + h + 'px; left: 0px; top: 0px;'
            +' background-color:' + bgc + '; border-color:' + brdC + ';'
            +' border-width:' + brdW + 'px;'
            +' border-style:solid; font-size:1px;">';
        t+='<span id="blocks' + xpbar_N + '" style="left:-'+(h*2+1)+'px; position:absolute; font-size: 1px">';
        for(i=0; i < blocks; i++)
        {
            t+='<span style="background-color:'+blkC+'; left:-'+((h2*i)+i)+'px; font-size:1px; position:absolute; width:'+h2+'px; height:'+h2+'px; '
            t+=(xpbar_ie) ? 'filter:alpha(opacity='+(100-i*(100/blocks))+')':'-Moz-opacity:'+((100-i*(100/blocks))/100);
            t+='"></span>';
        }
        t+='</span><span id="_xpcaption' + xpbar_N + '" style="font-size: 10px; width:' + w + 'px; height:' + h2 + 'px; background-color: ' + bgc + ';"></span></div>';
        document.write(t);
        var bA = document.getElementById('blocks' + xpbar_N);
        bA.bar = document.getElementById('_xpbar' + xpbar_N);
        bA.caption = document.getElementById('_xpcaption' + xpbar_N);
        bA.bar_id=xpbar_N;
        bA.blocks=blocks;
        bA.w=w;
        bA.h=h;
        bA.blockw=h2;
        bA.blockh=h2;
        bA.speed=speed;
        bA.ctr=0;
        bA.count=count;
        bA.action=action;
        bA.togglePause=xpbar_togglePause;
        bA.start=xpbar_start;
        bA.stop=xpbar_stop;
        bA.centerFocus=xpbar_centerFocus;
        bA.setCaption=function(text){
            this.caption.innerHTML=text;
        }
        bA.showBar=function(){
            this.bar.style.visibility="visible";
        }
        bA.hideBar=function(){
            this.bar.style.visibility="hidden";
        }
        bA.tid=0;
        //bA.tid=setInterval('xpbar_startBar(' + this.bar_id + ')',speed);

        return bA;
    }
}

function xpbar_startBar(bar_n)
{
    var t = document.getElementById('blocks' + bar_n);
    if(parseInt(t.style.left) + t.blockw + 1 - (t.blocks * t.blockw + t.blocks) > t.w)
    {
        t.style.left=-(t.blockw * 2+1) + 'px';
        t.ctr++;
        if(t.ctr>=t.count)
        {
            eval(t.action);
            t.ctr=0;
        }
        //window.status = 'Progress: ' + t.ctr;
    }
    else 
    {
        t.style.left=(parseInt(t.style.left)+t.blockw+1) + 'px';
    }
}

function xpbar_togglePause(startstop)
{
    if(this.tid == 0 && startstop != 'stop')
    {
        this.start();
    }
    else if(startstop != 'start')
    {
        this.stop();
    }
}

function xpbar_start() { 
    if (this.tid == 0)
    {
        this.tid = setInterval('xpbar_startBar(' + this.bar_id + ')', this.speed);
    }
}
function xpbar_stop() {
    if (this.tid != 0)
    {
        clearInterval(this.tid);
        this.tid = 0;
    }
}


// Move layer to center of window to show
function xpbar_centerFocus()
{
    if (xpbar_ie) {
        var myWidth = 0, myHeight = 0;
        if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
            myHeight = document.body.clientHeight;
        }
    
        this.bar.style.position = 'absolute';
        this.bar.style.left = (myWidth - this.w) / 2 + document.documentElement.scrollLeft;
        this.bar.style.top = (myHeight - this.h) / 2 + document.documentElement.scrollTop;
        //this.bar.style.position = 'fixed';
        //this.bar.style.left = (myWidth - this.w) / 2;
        //this.bar.style.top = (myHeight - this.h) / 2;
    }
    else if (xpbar_w3c) {
        this.bar.style.position = 'fixed';
        this.bar.style.left = '' + ((window.innerWidth - this.w) / 2) + 'px';
        this.bar.style.top = '' + ((window.innerHeight - this.h) / 2) + 'px';
    }
}
