var bar_length = 300; // the bar will be this many pixels long
var bar_height = 5; // the bar will be this many pixels high
var bar_color = "#aaa"; // the progress bar color
var bar_background = "black"; // the color of the empty part of bar
var bar_border = "#ddd";  // 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 = "blue"; // the color of the percentage text (50%)
var display_close_button = 0; // 1 = on; 0 = off

var wait = 5000; 
//window.onload = saydone;

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() {
	//document.getElementById('header').style.visibility = 'hidden';
	document.write(
	"<div id=loading-screen>" +
	"<div id=loading-screen-inner>" +
		"<img src=\"res/img/logo.jpg\">" +
		"<div id=progress-bar>" + 
			"<div id=progress-bar-inner style=\"width:1%;\"></div>" +
		"</div>" +
	"</div>"+
	"</div>"
	);
} // end function setup_bar()

function progress_bar() {

	/* the following document element retreives the number of images on the HTML document */
	var image_count = document.getElementsByTagName("img").length;

	/* the following variable gets an array of all the images in the document */
	var image_array = document.getElementsByTagName("img");

	/* Each part of the progress bar will be bar_length divided by image_count rounded. For example: If there are 5 images and the total bar_length is 300 then each bar_part will be 60 */
	var bar_part = Math.round(bar_length / image_count);

	/* To display the correct percentage, bar_perc is 100 divided by the number of images on the page rounded */
	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++)
	{
		/* The javascript variable 'complete' when used on an
		image element retrieves whether an image is done
		loading or not.  It returns true or flase */
		if (image_array[i].complete) {
			percent = percent + bar_perc;
			new_width = new_width + bar_part;
			j++;
		}
	}
	
	// If the new_width is not growing because an image is still
	// loading then we want to make the bar go up 1% every 1 second
	// as long as it has not reached the next bar_part
	 if (new_width <= parseFloat(document.getElementById('progress-bar-inner').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 
	
	// The is so the percentage can never go past 100
	if (percent > 100) { percent = 100; } // Bug fix from Martijn189 on 4/13/10
	
	// Write the new percent in the progress bar window
	//document.getElementById('percent').innerHTML = percent + '%';
	// Make the width of the bar wider so that it matches the percent
	document.getElementById('progress-bar-inner').style.width = new_width + 'px';
	
	//checkstate(); // need for safari
	//document.getElementById('bar').innerHTML = document.readyState;
	
	/* If all the images have not loaded then call this
	function again in 500ms.  There must be at least one
	image in the document or the progress bar window
	never closes */
	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() {
	//if done then close the progress bar pop-up window
	document.getElementById('loading-screen').style.visibility = 'hidden';
	//document.getElementById('header').style.visibility = '';
	document.getElementById('header').style.display = '';

}  // end function close_bar()

// If IE
if(document.readyState)	
{
	document.onreadystatechange=checkstate;
}
else if (document.addEventListener) // if Mozilla or Netscape
{
	document.addEventListener("DOMContentLoaded", saydone, false);
}

	
function checkstate() {
	// Besides IE, Safari also can use document.readyState
	// but Safari does not support onreadystatechange, so
	// we will keep calling this function with progress_bar().
	
	// Check to see if document is not "complete" but
	// is loaded enough to be "interactive"
	if (document.readyState=="complete" || document.readyState=="complete")
		doneyet = 1;

} // end function checkstate()

function saydone() {
	doneyet = 1;
}  // end function saydone()

// for other browsers that don't have DOM complete variables
setTimeout('saydone();', wait);


setup_bar(); // call the function setup_bar() first
progress_bar(); // then call the progress_bar() function

