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

In This File:
================
+addLoadEvent
+getElementsByClassName
+addClassName
+removeClassName
+popUp
+popUp_kvp (Kids Video Player)
+popUp_vp (Video Player)

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

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

/* ------------------------------------------------------------
+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() {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		if (links[i].getAttribute("class") == "popup" || links[i].className == "popup") {
			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=691,height=730");
}

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

// --- Loading all of these functions upload page/DOM rendering --- //
addLoadEvent(popUp);
addLoadEvent(prepareLinks_vp);
addLoadEvent(prepareLinks_kvp);


/** fix for hiding initial javascript content (like calendar), then displaying it in the module javascript file when it loads on the page **/
var head = document.getElementsByTagName("head")[0];
if (head) {
	var scriptStyles = document.createElement("link");
	scriptStyles.rel = "stylesheet";
	scriptStyles.type = "text/css";
	scriptStyles.href = "/common/c/hidden.css";
	head.appendChild(scriptStyles);
}
