/* WebAppers Progress Bar, version 0.2
* (c) 2007 Ray Cheung
*
* WebAppers Progress Bar is freely distributable under the terms of an Creative Commons license.
* For details, see the WebAppers web site: http://wwww.Webappers.com/
*
/*--------------------------------------------------------------------------*/

//var initial = -120;
//var imageWidth=240;
var initial = -200;
var imageWidth = 400;
var eachPercent = (imageWidth/2)/100;

function setText (id, percent) {
    $(id+'-text').innerHTML = 'Uploading ... ' + percent + "%";
}

function display ( id, percentage ) {	
	var percentageWidth = eachPercent * percentage;
	var actualWidth = initial + percentageWidth ;
	if( is_IE6 ) {
//		$('progress-bar').innerHTML += '<img id="'+id+'" src="/img/design/progress-bar.gif" />';
		$('progress-bar').innerHTML += '<img id="'+id+'"  width="200px" height="20px" src="/img/design/blank.gif" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader (src="/img/design/progress.png");" alt="'+percentage+'%" class="progress-image" style="background-position: '+actualWidth+'px 0px;"/>';		
	} else {
		$('progress-bar').innerHTML = '<img id="'+id+'" src="/img/design/progress.png" alt="'+percentage+'%" class="progress-image" style="background-position: '+actualWidth+'px 0px;"/>';
	}
	$('progress-bar').innerHTML += '<p id="'+id+'-text"></p>';
	setText( id, percentage );
}

function emptyProgress(id) {
    var newProgress = initial+'px';
    $(id).style.backgroundPosition=newProgress+' 0';
}

function getProgress(id) {
    var nowWidth = $(id).style.backgroundPosition.split("px");
    return (Math.floor(100+(nowWidth[0]/eachPercent))+'%');
	
}

function setProgress(id, percentage) {
    var percentageWidth = eachPercent * percentage;
    var newProgress = eval(initial) + eval(percentageWidth) + 'px';
    $(id).style.backgroundPosition=newProgress + ' 0';
    setText(id,percentage);
}

function plus ( id, percentage ) {
    var nowWidth = $(id).style.backgroundPosition.split("px");
    var nowPercent = Math.floor(100+(nowWidth[0]/eachPercent))+eval(percentage);
    var percentageWidth = eachPercent * percentage;
    var actualWidth = eval(nowWidth[0]) + eval(percentageWidth);
    var newProgress = actualWidth+'px';
    if(actualWidth>=0 && percentage < 100)
    {
        var newProgress = 1+'px';
        $(id).style.backgroundPosition=newProgress+' 0';
    }
    else
    {
        $(id).style.backgroundPosition=newProgress+' 0';
    }
}

function minus ( id, percentage ) {
    var nowWidth = $(id).style.backgroundPosition.split("px");
    var nowPercent = Math.floor(100+(nowWidth[0]/eachPercent))-eval(percentage);
    var percentageWidth = eachPercent * percentage;
    var actualWidth = eval(nowWidth[0]) - eval(percentageWidth);
    var newProgress = actualWidth + 'px';
    if(actualWidth <= -200 )
    {
        var newProgress = -200 + 'px';
        $(id).style.backgroundPosition = newProgress + ' 0';
    }
    else
    {
        $(id).style.backgroundPosition=newProgress+' 0';
    }
}

function fillProgress(id, endPercent) {
    var nowWidth = $(id).style.backgroundPosition.split("px");
    startPercent = Math.ceil(100+(nowWidth[0]/eachPercent))+1;
    var actualWidth = initial + (eachPercent * endPercent);
    if (startPercent <= endPercent*2 && nowWidth[0] <= actualWidth*2)
    {
        plus(id,'1');
        setTimeout("fillProgress('"+id+"',"+endPercent+")", 10);
    }
}
