/**
*	WB GlobalNav
*	@author:		Jose Ramil Jugao
*	@created:	11/19/2009
*	@updated:	03/19/2010
*	@usage:		This javascript is included in globalnav.html/whvnav-bottom.html
*		<script type="text/javascript" src="/all/us/reducednav/globalnav.js"></script>
*/

/////////////////////////////////////////////////////
//	CONFIGURATIONS
/////////////////////////////////////////////////////

// skips the initialization method. check if it has been declared already
var skipInit = (skipInit) ? true : false;
// forces the global nav to stick to the bottom of the browser window
var stickToBottom = (stickToBottom) ? true : false;



/////////////////////////////////////////////////////
//	UTLITY METHODS
/////////////////////////////////////////////////////

/**
*	Dynamically loads external JavaScript and CSS files
*/
function loadJsCssFile(filename, filetype){
	if (filetype=="js") { //if filename is a external JavaScript file
		var fileref=document.createElement("script");
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", filename);
	} else if (filetype=="css") { //if filename is an external CSS file
		var fileref=document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", filename);
	}
	
	if (typeof fileref!="undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref);
}


/**
*	Verifies if an element exists
*/
function elementExists(id) {
	try {
		var obj = document.getElementById(id);
		if (obj) return true;
		return false;
	} catch (err) {
		return false;	
	}
}

/**
*	Returns the environment for the global nav directory
*/
function getEnvironment() {
	if (window.location.hostname.indexOf(".warnerbros.com") != -1) {
		return "http://" + window.location.hostname;
	} else {
		return "http://www.warnerbros.com"
	}
}

/**
*	Cross-browser implementation of element.addEventListener()
*	Usage: addListener(window, 'load', myFunction);
*/
function addListener(element, type, expression, bubbling) {
	bubbling = bubbling || false;
	if(window.addEventListener) { // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if(window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
		return true;
	} else 
		return false;
}
	  
/**
*	This function inserts newNode after referenceNode
*/
function insertAfter(referenceNode, newNode) {
	 referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );
}

/**
*	Encodes spaces, ampersands and backslashes
*/
function cleanQuery(string) {
	return encodeURIComponent(string.replace(/ /g, '+')).replace(/%2F/g, '%252F').replace(/%26/g, '%2526');
} 


/////////////////////////////////////////////////////
//	VIDEOSURF-SPECIFIC METHODS
/////////////////////////////////////////////////////

/**
*	Adds Video Surf Search Bar style and functionality
*/
function configVideoSurf() {
	// attach the external style sheet for this search bar
	loadJsCssFile(getEnvironment() + "/all/us/reducednav/videosurf/css/videosurf.css", "css");

	try {	
		runSearch = function (event) {
			var searchString = document.getElementById('vs_headerSearchInput').value.replace(/^\s+|\s+$/g, '');

			if(searchString.length < 1) {
				return;
			}

			if((event.type == 'keydown' && event.keyCode == 13) || event.type == 'click') {
				var newWin = window.open('http://www.videosurf.com/videos/' + cleanQuery(searchString) + '?vlt=wb', '_blank');
				return false;
			}
		}


		var headerSearchInput = document.getElementById('vs_headerSearchInput');
		addListener(headerSearchInput, 'keydown', runSearch);

		var headerSearchButton = document.getElementById('vs_headerSearchButton');
		addListener(headerSearchButton, 'click', runSearch);
	} catch (e) {
		// the page is missing div elements: vs_headerSearchInput, vs_headerSearchButton
	}
}

configVideoSurf();



/**
*	Attaches the external style sheet for the Global Nav to stick to the bottom of the page.
*	Note: To make sure the 'stickToBottom' feature properly works (especially for flash sites), 
*	the content of the site needs to be within a "gn_wrapper" div. 
*	This script must be included before closing the "gn_wrapper" div.
*	For example:
*	<body>
*		<div id="gn_wrapper">
*			<!-- all site content, including flash movies go here -->
*			
*			<!--#include virtual="/all/us/reducednav/globalnav.html"-->
*		</div>
*	</body>
*/
if (stickToBottom) loadJsCssFile(getEnvironment() + "/all/us/reducednav/globalnav.css", "css");




/**
*	If the browser is IE8 then add the meta-tag for IE8 Compatibility View fix to render in IE7
*	Purpose: the input field becomes unclickable in IE8. This meta tag fixes that.
*	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
*/

