// Spinner
//
// Add a spinner option to the page.
//
// The following code must be inserted into the page (preferably just after the body tag:
//   <div id="SpinnerPage"><div id="SpinnerDiv"></div></div>
//
// To use, call 'StartSpinner()' from an event. It will stop when the page reloads
// (or call 'StopSpinner()' to stop it from a specified event.
//
// Dependencies
// ------------
//  Spinner.css - styles
//  jQuery			- base library
//	jSpin				- spinner library
//

// Spinner reference.
var _spinner = null;

// Page onload event hook.
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function () {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

// Stop the spinner when the page reloads.
addLoadEvent(StopSpinner);
addLoadEvent(function () { });

// Start the spinner.
function StartSpinner() {
	// Call real function after a short delay - needed for some browsers to prevent the animation halting.
	setTimeout(StartSpinnerDelayed, 300);
}

// Define and enable the spinner.
function StartSpinnerDelayed() {
	var opts = {
		lines: 12, // The number of lines to draw
		length: 7, // The length of each line
		width: 5, // The line thickness
		radius: 15, // The radius of the inner circle
		color: '#FFF', // #rbg or #rrggbb
		speed: 1, // Rounds per second
		trail: 100, // Afterglow percentage
		shadow: false // Whether to render a shadow
	};

	var spinnerPage = document.getElementById('SpinnerPage');
	var spinnerDiv = document.getElementById('SpinnerDiv');
	if ((spinnerPage != null) && (spinnerDiv != null)) {
		spinnerPage.style.display = "block";
		_spinner = new Spinner(opts).spin(spinnerDiv);
	}
}

// Stop the spinner.
function StopSpinner() {
	if (_spinner) {
		_spinner.stop();
		var spinnerPage = document.getElementById('SpinnerPage');
		if (spinnerPage != null)
			spinnerPage.style.display = "none";
	}
}

