/* ============================================================
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)

In This File:
================
+addLoadEvent
+insertAfter
+getElementsByClassName
+addClassName
+removeClassName
+popUp
+prepareLinks_kvp
+popUp_kvp (Kids Video Player)
+prepareLinks_vp
+popUp_vp (Video Player)
+checkReadyState
+run_after_body_load

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

/* ------------------------------------------------------------
+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, "");
}


/* ------------------------------------------------------------
+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++) {
		links[i].setAttribute("title","This link will open in a new browser window");
		links[i].onclick = function() {
			var linkURL = this.getAttribute("href");
			window.open(linkURL,"_blank");
			return false;
		}
	}
}


/* ------------------------------------------------------------
+popUp_kvp
------------------------------------------------------------ */
// Opens a new window for the current 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") {
			links[i].setAttribute("title","This link will open the National Geographic Kids Video Player in a new browser window");
			links[i].onclick = function() {
				popUp_kvp(this.getAttribute("href"));
				return false;
			}
		}
	}
}

function popUp_kvp(winURL) {
  window.open(winURL,"_blank","width=917,height=630");
}

/* ------------------------------------------------------------
+popUp_vp
------------------------------------------------------------ */
// Opens a new window for 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")  {
			links[i].setAttribute("title","This link will open the National Geographic Video Player in a new browser window");
			links[i].onclick = function() {
				popUp_vp(this.getAttribute("href"));
				return false;
			}
		}
	}
}

function popUp_vp(winURL) {
  window.open(winURL,"_blank","width=991,height=662");
}

// CHECK TO SEE IF THIS IS BEING CALLED AFTER THE <BODY> TAG
if(typeof(document.body)=="undefined"||!document.body) {
	// --- Loading all of these functions upon page/DOM rendering (simple innerHTML string length test) --- //
	// --- WARNING: this will not wait for images or other assets to complete loading, only the raw source code --- //
	function checkReadyState() {
		dom_str_len = ((typeof(dom_str_len)=="undefined")?-1:dom_str_len);
		var is_ready = document.body.innerHTML.length==dom_str_len;
		if(!is_ready) { dom_str_len=document.body.innerHTML.length; };
		return is_ready;
	}
	function run_after_body(func) {
		run_after_body_failsafe = ((typeof(run_after_body_failsafe)!="undefined")?run_after_body_failsafe+1:1);
		if(run_after_body_failsafe < 10000 && typeof(document.body)!="undefined" && typeof(func)=="function") {
			if(document.body && typeof(document.body.innerHTML)!="undefined") {
				if(typeof(after_body_tmr)!="undefined"&&after_body_tmr>0) { 
					clearTimeout(after_body_tmr); 
				}
				if(checkReadyState()) {
					func();
					return;
				}
			}
			after_body_tmr = setTimeout(function() { run_after_body(func); },10);
		}
		else if(typeof(func)=="function"){
			// if we can't test the length of the 'document.body.innerHTML' return value...
			// try to run our function anyway, but add a 10sec timeout to give it a better shot
			after_body_tmr = setTimeout(func,10000);
			return false;
		}
		return;
	}

	run_after_body(function() {
		popUp();
		prepareLinks_vp();
		prepareLinks_kvp();
	});
} else {
	// this is being called after the <BODY> tag, run the original code
	addLoadEvent(popUp);
	addLoadEvent(prepareLinks_vp);
	addLoadEvent(prepareLinks_kvp);
}