/* Cool Javascript Progress Bar for HTML Page 
/*  You may edit the following variables */
var bar_length = 40; // the bar will be this many pixels long
var bar_height = 12; // the bar will be this many pixels high
var bar_color = ""; // the progress bar color
var bar_background = "white"; // the color of the empty part of bar
var bar_border = "black";  // the color of the bar border
var window_background = "black"; // the color of the pop-up window
var window_border = "black"; // the border color of pop-up window
var text_color = "black"; // the color of the percentage text (50%)
var display_close_button = 0; // 1 = on; 0 = off
var wait = 5000; // How many milliseconds to wait for other browsers

//window.onload = saydone();

/* Do NOT edit anything below this point */

var more = 0; // Add more to the bar ever second
var doneyet = 0;  // changes to 1 when the DOM is done loading


function setup_bar()
{
	window_width = bar_length + 50;
	window_height = bar_height + 50;
	
	if (document.all) // if IE
	{
		bar_height2 = bar_height - 2; 
		bar_width2 = bar_length + 3;
	}
	else
	{
		bar_height2 = bar_height;
		bar_width2 = bar_length + 1;
	}
	
	document.write('<DIV ID="bar_window" onClick="close_bar()">');

	if (display_close_button)
	{
		document.write('<DIV STYLE="position=absolute;'
			+ 'top: 0'
			+ ';left: 0'
			+ ';width: ' + (window_width - 3)
			+ ';background-color: ' + window_background
			+ ';color: ' + text_color
			+ ';text-align: right'
			+ ';">');
		document.write('[X]</DIV>');
	}
	
	document.write('<DIV ID="empty_bar" STYLE="position: absolute;'
		+ 'top: ' + 25
		+ ';left: ' + 25
		+ ';border: none ' + bar_border
		+ ';background-color: ' + bar_background
		+ ';width: ' + bar_width2
		+ ';height: ' + bar_height
		+ ';">');
	document.write('</DIV>'); // close DIV for empty_bar
	
	document.write('<DIV ID="bar" STYLE="position: absolute;'
		+ 'top: ' + 26
		+ ';left: ' + 26
		+ ';background-color: ' + bar_color
		+ ';width: ' + 0
		+ ';height: ' + bar_height2
		+ ';">');
	document.write('</DIV>'); // close DIV for bar
	
	document.write('<DIV ID="percent" STYLE="position: absolute;'
		+ 'top: ' + 25
		+ ';width: ' + window_width
		+ ';text-align: center'
		+ ';vertical-align: middle'
		+ ';color: ' + text_color
		+ ';font-size: ' + bar_height + 'px'
		+ ';">');
	document.write('0%'); // Display 0%
	document.write('</DIV>');  // close DIV for percent
	
	document.write('</DIV>'); // close DIV for bar_window
	
		
} // end function setup_bar()

function progress_bar()
{

var image_count = document.getElementsByTagName("img").length;

var image_array = document.getElementsByTagName("img");

var bar_part = Math.round(bar_length / image_count);

var bar_perc = Math.round(100 / image_count);
	
	var new_width = 0; // Will become new width of progress bar
	var j = 0;  // count how many images are complete
	var percent = 0; // Add up the percentage
	
	for (var i = 0; i < image_count; i++)
	{
		if (image_array[i].complete)
		{
			percent = percent + bar_perc;
			new_width = new_width + bar_part;
			j++;
		}
	}
	
	 if (new_width <= parseFloat(document.getElementById('bar').style.width)
		&& new_width < (j*bar_part + bar_part))
	{
		more = more + .50;
		new_width = new_width + Math.round(more);	 
		percent = percent + Math.round(more);
		
	}
	else
		more = 0;  // reset more if we loaded next image 
	
	if (percent >= 100) { percent = 100; doneyet = 1; }
	
	document.getElementById('percent').innerHTML = percent + '%';
	document.getElementById('bar').style.width = new_width;
	
	if (j < image_count || j == 0 || doneyet == 0)
		setTimeout('progress_bar();', 500); 
	else // if done then close the progress bar pop-up window
		setTimeout('close_bar();', 500); // in half a second
} // end function progress_bar()




function close_bar()
{
	document.getElementById('bar_window').style.visibility = 'hidden';

}  // end function close_bar()




if(document.readyState)	
{
	document.onreadystatechange=checkstate;
}
else if (document.addEventListener) // if Mozilla or Netscape
{
	document.addEventListener("DOMContentLoaded", saydone, false);
}

	
function checkstate()
{
	if (document.readyState=="complete" ||
		document.readyState=="complete")
		doneyet = 1;

} // end function checkstate()

function saydone()
{
	doneyet = 1;
}  // end function saydone()

//setTimeout('saydone();', wait);

setup_bar(); // call the function setup_bar() first
progress_bar(); // then call the progress_bar() function
