/* 
================================================================================================================================ CREDITS
Copyright 	: Copyright 2011 LearntobeHealthy.org. All Rights Reserved.
Date      	: 03/11/2011
Notes     	: JavaScript file for handling various events ... onLoad, onClick, etc
================================================================================================================================ BEGIN JAVASCRIPT
*/

/* ============================================================================================================================= MULTIPLE ONLOAD HANDLER */

var onLoadFunctions = new Array();
var iloadFunction = 0;

// Pass each function that needs to load
function add_on_load(func) {
    onLoadFunctions[iloadFunction] = func;
    iloadFunction++;
}
// Loops through all of the functions that were added
function loadAllFunctions() {
    for (var i = 0; i < onLoadFunctions.length; i++) {
        eval(onLoadFunctions[i]+"()");
    }
}
// Load all of the functions that you've set
window.onload = loadAllFunctions;

/* ============================================================================================================================= CALL FUNCTIONS TO BE LOADED */

add_on_load("dropdown_menus");
add_on_load("load_modals");
add_on_load("load_flash");
add_on_load("jump_menus");
add_on_load("search_btn_events");

/* ============================================================================================================================= COMMON FUNCTIONS */

// ---------------------------------------------------------- WINDOW LOCATION
var url = window.location.toString();

// ---------------------------------------------------------- USER AGENT
var isie = navigator.userAgent.toLowerCase().indexOf('msie');

// ---------------------------------------------------------- GET PREVIOUS SIBLING
function prevSibling(n) {
	do n = n.previousSibling;
	while (n && n.nodeType != 1);
	return n;
}

// ---------------------------------------------------------- GET NEXT SIBLING
function nextSibling(n) {
	do n = n.nextSibling;
	while (n && n.nodeType != 1);
	return n;
}

// ---------------------------------------------------------- GET CHILD BASED ON NODENAME YOU SPECIFY
function getChild(n,char) {
	for (var i = 0; i < n.childNodes.length; i++) {
		if (n.childNodes[i].nodeName == char) {
			n = n.childNodes[i];
			break;
		}
	}
	return n;
}

// ---------------------------------------------------------- GET VALUE OF QUERYSTRING PARAMETER
function getQueryValue(key) {
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

	var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if (qs == null) {
		return "";
	}
	return qs[1];
}

// ---------------------------------------------------------- DROPDOWN JUMP MENU
function jumpMenu(targ,selObj,restore){
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
}


/* ============================================================================================================================= MAIN NAV DROPDOWN MENUS */

function dropdown_menus() {
	var nav = document.getElementById("nav");
	if (nav != null) {
		// FOR IE6: UPDATE LI CLASS NAME SO THAT THE SUB-LISTS ARE DISPLAYED
		for (var i = 0; i < nav.childNodes.length; i++) {
			node = nav.childNodes[i];
			if (node.nodeName == "LI") {
				node.onmouseover = function() { 
					this.className += " iehover";
				}
				node.onmouseout = function() { 
					this.className = this.className.replace(" iehover", "");
				}
			 }
		}
	}
}

/* ============================================================================================================================= LOAD MODALS */

function load_modals() {
	if(typeof init_modal == 'function') {
		init_modal();
	}
}

/* ============================================================================================================================= LOAD FLASH FILES */

function load_flash() {
	var alt;
	var flashvars = { };
	var params = { wmode: "transparent", base: "." };
	var attributes = {};

	// HOME FLASH
	if (document.getElementById('home-flash') != null) {
        alt = document.getElementById('home-alt');

		swfobject.embedSWF(
			"/lib/swf/preloader.swf", "home-flash", "897", "410", 
			"9.0.0", "/lib/swf/expressInstall.swf", flashvars, params, attributes
		);
		if (alt != null) {
		    alt.style.visibility = "visible";
		}
    }
	// VIDEO PLAYER 1
	if (document.getElementById('video-player1') != null) {
        alt = document.getElementById('video-player1-alt');
		flashvars = { video: "acceleration" };
		params = { base: "." };

		swfobject.embedSWF(
			"/lib/swf/videoPlayer.swf", "video-player1", "514", "418", 
			"9.0.0", "/lib/swf/expressInstall.swf", flashvars, params, attributes
		);
		if (alt != null) {
		    alt.style.visibility = "visible";
		}
    }

	// VIDEO PLAYER 2
	if (document.getElementById('video-player2') != null) {
        alt = document.getElementById('video-player2-alt');
		flashvars = { video: "fourthQuarter" };
		params = { base: "." };

		swfobject.embedSWF(
			"/lib/swf/videoPlayer.swf", "video-player2", "514", "418", 
			"9.0.0", "/lib/swf/expressInstall.swf", flashvars, params, attributes
		);
		if (alt != null) {
		    alt.style.visibility = "visible";
		}
    }

	// VIDEO PLAYER 3
	if (document.getElementById('video-player3') != null) {
        alt = document.getElementById('video-player3-alt');
		flashvars = { video: "apex" };
		params = { base: "." };

		swfobject.embedSWF(
			"/lib/swf/videoPlayer.swf", "video-player3", "514", "418", 
			"9.0.0", "/lib/swf/expressInstall.swf", flashvars, params, attributes
		);
		if (alt != null) {
		    alt.style.visibility = "visible";
		}
    }

	// VIDEO PLAYER 4
	if (document.getElementById('video-player4') != null) {
        alt = document.getElementById('video-player4-alt');
		flashvars = { video: "athleticRepublic" };
		params = { base: "." };

		swfobject.embedSWF(
			"/lib/swf/videoPlayer.swf", "video-player4", "514", "418", 
			"9.0.0", "/lib/swf/expressInstall.swf", flashvars, params, attributes
		);
		if (alt != null) {
		    alt.style.visibility = "visible";
		}
    }
}

/* ============================================================================================================================= FLASH FUNCTIONS */

function thisMovie(movieName) {
	if (window.document[movieName]) return window.document[movieName];
	if (navigator.appName.indexOf("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[movieName]) return document.embeds[movieName]; 
	} else {
		return document.getElementById(movieName);
	} 
}

function openURL(url_target) {
	url_target = url_target.split(',');	
	if (url_target[1] == '_blank') {
		window.open(url_target[0], '_blank')
	} else {
		window.location = url_target[0];	
	}
}

var activeModal = '';

function openVideo(vid) {
	vid = vid.split('/flv/')[1].split('.flv')[0];
	
	if (vid == 'acceleration') {
		open_modal('#video1', 'modal');
		activeModal = 'video-player1';
	} else if (vid == 'fourthQuarter') {
		open_modal('#video2', 'modal');
		activeModal = 'video-player2';
	} else if (vid == 'apex') {
		open_modal('#video3', 'modal');
		activeModal = 'video-player3';
	} else if (vid == 'athleticRepublic') {
		open_modal('#video4', 'modal');
		activeModal = 'video-player4';
	}
}

function stopVideoPlayer() {
	if (document.getElementById(activeModal + '-alt') == null) {
		if (document.getElementById(activeModal) != null) {
			thisMovie(activeModal).stopVideoPlayer();
		}
	}
}

/* ====================================================================================================== LOAD JUMP MENUS */
function jump_menus() {
	if (document.getElementById('jumpToPartner') != null) {
		document.getElementById('jumpToPartner').onchange = function() {
			jumpMenu('parent',this,0);
		}
    }
}

/* ============================================================================================================================= SEARCH FOCUS/BLUR EVENTS */

function search_btn_events() {	
	var q = document.getElementById("q");
	if (q != null) {
		q.onfocus = function() { if (q.value == "search Bluestreak") q.value = ""; }
		q.onblur = function() { if (q.value == "") q.value = "search Bluestreak"; }
	}
}
