/* ============================================================
National Geographic Global Helpers JavaScript
* Created by Brian Talbot on 2008-08-05.
* Copyright (c) 2008 National Geographic. All rights reserved.

Notes:
================
* 2008-08-05 - Began list of global helpers
* 2008-08-07 - Added current Video Player Popups
* 2009-11-10 - Added workaround for broken 'addLoadEvent' when helpers.js is loaded before the <body> tag/prior window.onload events (-bgraham)
* 2010-01-26 - Fixed workaround for broken 'addLoadEvent' when helpers.js is loaded before the <body> tag/prior to window.onload events (-bgraham)

In This File:
================
+addLoadEvent
+insertAfter
+getElementsByClassName
+addClassName
+removeClassName
+appendToTitle
+setOnClick
+popUp
+prepareLinks_kvp
+popUp_kvp (Kids Video Player)
+prepareLinks_vp
+popUp_vp (Video Player)
+onloadFunctions
+initOnPageLoad

============================================================ */

/* ------------------------------------------------------------
+addLoadEvent
------------------------------------------------------------ */
// addLoadEvent is a function made by Simon Willison that serves as a manageable window.onload replacement. //
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/* ------------------------------------------------------------
+insertAfter
------------------------------------------------------------ */
function insertAfter(newElement,targetElement) {
    var parent = targetElement.parentNode;
    if (parent.lastChild == targetElement) {
        parent.appendChild(newElement);
    } else {
        parent.insertBefore(newElement,targetElement.nextSibling);
    }
}


/* ------------------------------------------------------------
+getElementsByClassName
------------------------------------------------------------ */
// A function that allows us to retrieve elements by their class name  //
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}

/* ------------------------------------------------------------
+addClassName
------------------------------------------------------------ */
// A function that allows an easy way to add class names  //
function addClassName(oElm, strClassName){
	var strCurrentClass = oElm.className;
	if(!new RegExp(strClassName, "i").test(strCurrentClass)){
		oElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
	}
}

/* ------------------------------------------------------------
+removeClassName
------------------------------------------------------------ */
// A function that allows an easy way to remove class names  //
function removeClassName(oElm, strClassName){
	var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
	oElm.className = oElm.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
}


/* ------------------------------------------------------------
+appendToTitle
------------------------------------------------------------ */
// Appends to the title attribute of the element supplied (usually an <a> element/link) the text string supplied
function appendToTitle(link,appendText) {
	var curTitleText = link.getAttribute("title");
	link.setAttribute("title",((curTitleText==null)?"":curTitleText+" - ") + appendText);
}

/* ------------------------------------------------------------
+setOnClick
------------------------------------------------------------ */
// Sets the onclick event of the element supplied (usually an <a> element/link) to the function supplied
function setOnClick(link,onclickFunction) {
	link.onclick = onclickFunction;
}

/* ------------------------------------------------------------
+popUp
------------------------------------------------------------ */
// An accessible way to open links in new windows //
function popUp() {
	var links = getElementsByClassName(document, "a", "popup");
	for (var i=0; i<links.length; i++) {
		appendToTitle(links[i],"Note: This link will open in a new browser window.");
		setOnClick(links[i],function() {
			var linkURL = this.getAttribute("href");
			window.open(linkURL,"_blank");
			return false;
		});
	}
}


/* ------------------------------------------------------------
+prepareLinks_kvp
------------------------------------------------------------ */
// Prepares 'popup_kvp' class <a> elements to popup the NG Kids Video Player
function prepareLinks_kvp() {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		if (links[i].getAttribute("class") == "popup_kvp" || links[i].className == "popup_kvp") {
			appendToTitle(links[i],"Note: This link will open the National Geographic Kids Video Player in a new browser window.");
			setOnClick(links[i],function() {
				popUp_kvp(this.getAttribute("href"));
				return false;
			});
		}
	}
}

/* ------------------------------------------------------------
+popUp_kvp
------------------------------------------------------------ */
// Opens a new window for the current NG Kids Video Player
function popUp_kvp(winURL) {
  window.open(winURL,"_blank","width=917,height=630");
}

/* ------------------------------------------------------------
+prepareLinks_vp
------------------------------------------------------------ */
// 
// Prepares 'popup_vp' class <a> elements to popup the current NG Video Player
function prepareLinks_vp() {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		if (links[i].getAttribute("class") == "popup_vp" || links[i].className == "popup_vp")  {
			appendToTitle(links[i],"Note: This link will open the National Geographic Video Player in a new browser window.");
			setOnClick(links[i],function() {
				popUp_vp(this.getAttribute("href"));
				return false;
			});
		}
	}
}

/* ------------------------------------------------------------
+popUp_vp
------------------------------------------------------------ */
// Opens a new window for the current NG Video Player
function popUp_vp(winURL) {
  window.open(winURL,"_blank","width=991,height=662");
}


/* ------------------------------------------------------------
+onloadFunctions
------------------------------------------------------------ */
// A 'catch-all' function to execute other functions when the 'initOnPageLoad' runs
onloadFunctions = function() {
	popUp();
	prepareLinks_vp();
	prepareLinks_kvp();
}

// CHECK TO SEE IF THIS IS BEING CALLED AFTER THE <BODY> TAG AND DEFINE SOME NON-CONFLICTING 'onload' FUNCTIONS IF SO
if(typeof(document.body)=="undefined"||!document.body) {

/* ------------------------------------------------------------
+initOnPageLoad
------------------------------------------------------------ */
// A cross-browser 'document.ready' type functionality w/out jQuery's $(document).ready(function{});
	function initOnPageLoad() {
		if (arguments.callee.done) return;
		arguments.callee.done = true;
		if (initOnPageLoadTmr) clearInterval(initOnPageLoadTmr);
		onloadFunctions();
	};

	/* Internet Explorer */
	/*@cc_on @*/
	/*@if (@_win32)
		document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
		var script = document.getElementById("__ie_onload");
		script.onreadystatechange = function() {
			if (this.readyState == "complete") {
				initOnPageLoad(); // call the onload handler
			}
		};
	/*@end @*/

	/* Mozilla/Opera9 */
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", initOnPageLoad, false);
	}
	/* Safari */
	else if (/WebKit/i.test(navigator.userAgent)) { // sniff
		var initOnPageLoadTmr = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
				initOnPageLoad(); // call the onload handler
			}
		}, 10);
	}
	/* all other browsers */
	else {
		window.onload = initOnPageLoad;
	}
} else {
	// this is being called after the <BODY> tag, run the original code
	onloadFunctions();
}
