// ********************************************
// *  Animation functions
// *  ** Add custom animation functions here **
// ********************************************

var timer = 0;
var task = 0;
var step = 0;
var max_step = 0;

var FADE_OUT = -1;
var FADE_IN = +1;

var animation_element = new Array();
	animation_element.push("image0");
	animation_element.push("intro_link");
	animation_element.push("intro_logo");
	animation_element.push("intro_subtitle1");
	animation_element.push("intro_subtitle2");
	animation_element.push("intro_logo");
	animation_element.push("intro_subtitle1");
	animation_element.push("intro_subtitle2");
	animation_element.push("intro_link");
	animation_element.push("panel1");
	animation_element.push("logo1");
	animation_element.push("logo2");
	animation_element.push("panel3");
	animation_element.push("buttons");
	animation_element.push("panel2");
	animation_element.push("title1");
	animation_element.push("content");

var element_step_order = new Array();
	element_step_order.push(0);
	element_step_order.push(0);
	element_step_order.push(1);
	element_step_order.push(2);
	element_step_order.push(2);
	element_step_order.push(3);
	element_step_order.push(3);
	element_step_order.push(3);
	element_step_order.push(3);
	element_step_order.push(4);
	element_step_order.push(5);
	element_step_order.push(6);
	element_step_order.push(6);
	element_step_order.push(7);
	element_step_order.push(8);
	element_step_order.push(8);
	element_step_order.push(8);

var current_setting = new Array();
	current_setting.push(0.0);
	current_setting.push(0.0);
	current_setting.push(0.0);
	current_setting.push(0.0);
	current_setting.push(0.0);
	current_setting.push(2.0);
	current_setting.push(2.0);
	current_setting.push(2.0);
	current_setting.push(2.0);
	current_setting.push(0.0);
	current_setting.push(0.0);
	current_setting.push(0.0);
	current_setting.push(0.0);
	current_setting.push(0.0);
	current_setting.push(0.0);
	current_setting.push(0.0);
	current_setting.push(0.0);

var max_setting = new Array();
	max_setting.push(1.0);
	max_setting.push(1.0);
	max_setting.push(1.0);
	max_setting.push(1.0);
	max_setting.push(1.0);
	max_setting.push(0.0);
	max_setting.push(0.0);
	max_setting.push(0.0);
	max_setting.push(0.0);
	max_setting.push(0.7);
	max_setting.push(1.0);
	max_setting.push(1.0);
	max_setting.push(0.18);
	max_setting.push(1.0);
	max_setting.push(0.5);
	max_setting.push(1.0);
	max_setting.push(1.0);

var setting_multiplier = new Array();
	setting_multiplier.push(1);
	setting_multiplier.push(1);
	setting_multiplier.push(2);
	setting_multiplier.push(2);
	setting_multiplier.push(1);
	setting_multiplier.push(2);
	setting_multiplier.push(2);
	setting_multiplier.push(2);
	setting_multiplier.push(2);
	setting_multiplier.push(1);
	setting_multiplier.push(1);
	setting_multiplier.push(1);
	setting_multiplier.push(1);
	setting_multiplier.push(1);
	setting_multiplier.push(1);
	setting_multiplier.push(1);
	setting_multiplier.push(1);

var setting_increment = new Array();
	setting_increment.push(0.3);
	setting_increment.push(0.2);
	setting_increment.push(0.01);
	setting_increment.push(0.10);
	setting_increment.push(0.10);
	setting_increment.push(0.01);
	setting_increment.push(0.01);
	setting_increment.push(0.01);
	setting_increment.push(0.01);
	setting_increment.push(0.03);
	setting_increment.push(0.05);
	setting_increment.push(0.07);
	setting_increment.push(0.01);
	setting_increment.push(0.05);
	setting_increment.push(0.05);
	setting_increment.push(0.05);
	setting_increment.push(0.05);

// one array element for each step
var animation_step_type = new Array();
	animation_step_type.push("FADE_IN");
	animation_step_type.push("FADE_IN");
	animation_step_type.push("FADE_IN");
	animation_step_type.push("FADE_OUT");
	animation_step_type.push("FADE_IN");
	animation_step_type.push("FADE_IN");
	animation_step_type.push("FADE_IN");
	animation_step_type.push("FADE_IN");
	animation_step_type.push("FADE_IN");

/* 
// obsolete function
function expandTitle() {
	titleFontSize += sizeIncrement;

	if (titleFontSize > 660) { return; }

	document.getElementById('logo1').style.fontSize = titleFontSize + "%";
	document.getElementById('title2').style.fontSize = (titleFontSize + 5) + "%";
}
*/

// determine the number of the highest step
for (var z = 0; z < element_step_order.length; z++) {
	if (element_step_order[z] > max_step) { max_step = element_step_order[z]}
}

function performAnimation() {
	var max_step_elements = 0;
	var completion_count = 0;
	var direction = eval(animation_step_type[step]);
	var new_setting;
	var new_increment

	for (var x = 0; x < animation_element.length; x++) {
		if (element_step_order[x] == step) {

			// count the number of elements in the current step
			max_step_elements += 1;

			if (current_setting[x] == max_setting[x]) {
				completion_count++;
			} else {
				new_increment = (setting_increment[x] * setting_multiplier[x]) * direction;
				new_setting = current_setting[x] + new_increment;
				
				// check if the new_setting will overshoot the max_setting
				if (direction < 0) {
					if (new_setting < max_setting[x]) { new_setting = max_setting[x]; }
				} else {
					if (new_setting > max_setting[x]) { new_setting = max_setting[x]; }
				}
				
				current_setting[x] = new_setting;

				animateElement(document.getElementById(animation_element[x]), (current_setting[x] * setting_multiplier[x]));
			}
		}
	}

	// if all elements in the current step are complete, increment the step
	if (completion_count == max_step_elements) {
		window.clearInterval(task);
		window.clearTimeout(timer);
		step++;

		if (step == 2) {
			// add a short pause after step 2
			timer = setTimeout(startFadeInterval, 300);
		} else if (step <= max_step) {
			// only continue the interval if there are more steps
			startFadeInterval();
		}
	}
}

function startFadeInterval() {
	// set the timer interval to constantly check the field values for changes
	task = self.setInterval(performAnimation, 1);
}

function validateIntro() {
	var caution_text = "Caution: You are attempting to view this website with a browser " +
	                   "that is not supported.\n\nTo view this site the way it is intented, " +
	                   "please download and install the latest version of any of the " +
	                   "following browsers:\n\n" +
	                   "Firefox, Internet Explorer, Netscape, Opera, or Safari.";
	
	// if the browser is not known, skip the intro by refreshing the page
	// and re-reading the (now set) session variable
	if (isUnknown) { 
		//alert(caution_text);
		location.assign(location.href); 
	}
}

function skipIntro() {
	// reset the Interval and Timeout
	window.clearInterval(task);
	window.clearTimeout(timer);

	for (var x = 0; x < animation_element.length; x++) {
		animateElement(document.getElementById(animation_element[x]), max_setting[x]);
	}
}

function animateElement(element, new_value) {
	if (animation_step_type[step].indexOf('FADE') + 1) {
		if  (isNS9 || isFF || isOpera9 || isSafari) {
			element.style.opacity = new_value;
		} else if (isNS7) {
			element.style.MozOpacity = new_value;
		} else if (isIE) {
			element.filters.alpha.opacity = new_value * 100;
		}
	}
}


