/*
ON MENU SELECTION.

To mark a link from the navigation as selected, either:

1. Apply to the link element a class="selected", or
2. Apply to the link an id="btn_sectionName", where sectionName is the BODY element's id.

Additionally, if a link targets the current file, it will be automatically marked as selected.
*/



autoSelectMenu = false; 

var prevEl;


function init(){

	// quit if this function has already been called
	if (arguments.callee.done) return;
	
	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	
	// kill the timer
	if (_timer) {
		clearInterval(_timer);
		_timer = null;
	}
	
	labels_init();
	
	nav = navigator.userAgent.toLowerCase();
	isIEMac = (nav.indexOf('mac') > -1 && ( nav.indexOf('ie') > -1 && nav.indexOf('opera') == -1));
	
	// Using attachEvent as a filter for IE Win and Opera,
	// fix the EOLAS border for media objects
	if (document.attachEvent){
		embeddedContent.reinsertContent()
	}

	/* Add class="selected" to any link with id="btn_sectionName", 
	   where sectionName is the BODY element's id.*/
	links = document.getElementsBySelector('a');
	for (var i=0, t=links.length; i<t ;i++){
		if (linkHasBodyId(links[i].id)){
			links[i].className += ' selected';	
		}
	}
		
	/* Mark as selected any link that targets the current file */
	if (autoSelectMenu){
		fileName = getFileName(location.href);
		linksProdMenu = document.getElementsBySelector('a');

		for (var i=0, t=linksProdMenu.length; i<t; i++){
			if (fileName == getFileName(linksProdMenu[i].href)){
				linksProdMenu[i].className += ' selected';
			}
		}
	}	


	/* For all image links with class="rollover"
	*/
	var links = document.getElementsBySelector('a.rollover');
	var tmpImg = [];

	for (var i=0, t=links.length; i<t ;i++){
		var img = links[i].getElementsByTagName('img')[0];

		addEvent(links[i], 'focus', rollOver); /* FIX apply for all links not just images */
		addEvent(links[i], 'blur', rollOut);			

		if (img && !isIEMac){
			// Assign over and out events
			addEvent(links[i], 'mouseover', rollOver);
			addEvent(links[i], 'mouseout', rollOut);
			
			// Preload images
			tmpImg[i] = new Image(img.width, img.height);
			tmpImg[i].setAttribute('src', getImg(img, 'over'));
			
			// Where a link has class="selected", apply image with the same name and  "_over" suffix		
			if (links[i].className.indexOf('selected') != -1){
				img.setAttribute('src', getImg(img, 'selected'));
			}	
		}
	}
	if (document.getElementById("mediaNav")){
		var mediaNavs = document.getElementsBySelector('#mediaNav li a');
		for (var i=0, t=mediaNavs.length; i<t ;i++){
			addEvent(mediaNavs[i], 'click', getMovie);
		}
	}

	if (document.getElementById("browser")){
		var browserLists = document.getElementsBySelector('#browser ul ul');
		for (var i=0, t=browserLists.length; i<t ;i++){
			if (browserLists[i].className != 'unfolded'){
				browserLists[i].style.display = 'none';
			}
		}
		var browserLIs = document.getElementsBySelector('#browser ul li');
		for (var i=0, t=browserLIs.length; i<t ;i++){
			if (browserLIs[i].getElementsByTagName("UL").length == 1){
				addEvent(browserLIs[i], 'click', toggleBrowserList);
			}
		}		
		
		var browserLinks = document.getElementsBySelector('#browser ul li a');
		for (var i=0, t=browserLinks.length; i<t ;i++){
			addEvent(browserLinks[i], 'click', function(e){e.stopPropagation();});
		}			
	}
	
	// Some media player setup.
	allowMediaNav = false; // Don't allow clicking the thumbnails until the player is loaded
	lastMediaType = 'movie';

	if (document.getElementById("mediaScreen")){
		if (!document.getElementById("mediaScreen_movie")){ 
			lastMediaType = 'photo';	
			onTemplateLoaded();
		}
		
		// Since JS is available, hide the No JS warning
		document.getElementById("mediaNoScript").style.display = 'none';
	} 
	


}


// DEAN EDWARDS ONLOAD FIX http://dean.edwards.name/weblog/2006/06/again/  
/* for Mozilla */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32 && @_jscript_version > 5.5)
	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") {
			init(); // call the onload handler
		}
	};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			init(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
window.onload = init;



function getFileName(str){
	var arr = str.split("/");
	return arr[arr.length-1];
}


function rollOver(e){ 
	this.className += ' selected'; 
	var img = this.getElementsByTagName('img')[0];
	if (img){img.src = getImg(img, 'over');}
}

function rollOut(e){
	this.className = this.className.replace('selected','');

	var img = this.getElementsByTagName('img')[0];	
	if (img){
		if (!linkHasBodyId(this.id) && this.className.indexOf('selected') == -1){
			var imgNormal = img.getAttribute('src').replace('_over.','.')
		} else {
			var imgNormal = img.getAttribute('src').replace('_over.','_over.')	
		}
		img.src = imgNormal;
	}
}

function getImg(img, status){
	return img.getAttribute('src').replace(/(_over|_over)?.(gif|jpg|png)/ig, '_' + status + '.$2')	
}

function linkHasBodyId(linkId){
	if (!linkId){return false;}
	bodyId = document.getElementsByTagName("BODY")[0].id;
	linkSection = linkId.split("_");
	if (linkSection.length == 2){linkSection = linkSection[1];} else {linkSection = false;}
	return (linkSection && linkSection == bodyId);
}



function toggleBrowserList(e){ // for the archives pages
	theChild = this.getElementsByTagName("UL");
	theChild = theChild[0];
	if (theChild.style.display == 'none'){
		theChild.style.display = '';
		this.className += ' unfolded';
	} else {
		theChild.style.display = 'none';
		this.className = this.className.replace('unfolded','');
	}
	
	e.preventDefault();
}





/* 
Brightcove Player
*/ 

function onTemplateLoaded(message) {
	/* Code here is run once player has loaded;
	   the message argument will contain an error message if there was some issue 
	   with loading the player */
	
	/* Since the player doesn't allow interaction until it's loaded,
	   we've set the class "disableHover" on the main div, which places
	   a "wait" cursor over the video thumbnails and fades them out. 
	   Here, as the player has loaded, we enable interaction. */
	var mp =  document.getElementById("mediaPlayer");
	mp.className = mp.className.replace('disableHover','');
	allowMediaNav = true;
}


function getMovie(e) {

	// Get the media type, movie | photo, from the class attribute of the link
	if (this.className.indexOf('movie') != -1){mediaType = 'movie';}
	else if (this.className.indexOf('photo') != -1){mediaType = 'photo';}	
	
	// Get the ID of the link, ie 'media_123'. The part after the _ is the ID
	// from either the video provider or the article.
	titleId = this.getAttribute("id");
	
	// We make sure we got the ID and a media type, to avoid JS warnings.
	// allowMediaNav makes sure you can only click once the player has loaded.
	if (typeof titleId == 'string' && mediaType != '' && allowMediaNav){
		titleId = titleId.split("_")[1];

		if (mediaType == 'photo' || (mediaType == 'video' && mediaType != lastMediaType)){
			var allMedias = document.getElementsBySelector("#mediaScreen .mediaObject");
			forEach(allMedias, function(media){
				if (media.className.indexOf('hiddenMedia') == -1){
					media.className += ' hiddenMedia';
				}
			})									 
		}

		if (mediaType == 'photo'){
			if (lastMediaType == 'movie'){ // Stop the movie player
				callFlash("pauseVideo", "true");
			}
			var newScreen = document.getElementById("mediaScreen_" + titleId);
			
		} else if (mediaType == 'movie'){ 
			if (typeof lastVideoId == 'undefined'){lastVideoId = config["videoId"];}
			if (titleId == lastVideoId){
				callFlash("pauseVideo", "false");
			} else {
				callFlash("fetchTitleById", titleId);
				callFlash("loadTitleById", titleId);
			}
			lastVideoId = titleId			
			var newScreen = document.getElementById("mediaScreen_movie");		
		}
		
		newScreen.className = newScreen.className.replace('hiddenMedia','');	
		
		lastMediaType = mediaType;

		var otherLinks = document.getElementsBySelector("#mediaNav li a");
		for (var i=0, t=otherLinks.length; i<t; i++){
			otherLinks[i].className = otherLinks[i].className.replace('selected','');
			otherLinks[i].hideFocus = false;
		}
		
		this.className += ' selected';
		this.hideFocus = true;
		
	}
	
	e.preventDefault();
}






/*

HERE BE DRAGONS ;)
(Libraries below)

*/





/*
addEvent(element, type, handler)
  ie: addEvent(window, "load", init);
  Includes removeEvent, preventDefault and stopPropagation.
  Source: http://therealcrisp.xs4all.nl/upload/addEvent_dean.html
*/
function addEvent(element, type, handler)
{
	if (element.addEventListener){
		element.addEventListener(type, handler, false);
	}else
	{
		if (!handler.$$guid) {handler.$$guid = addEvent.guid++;}
		if (!element.events) {element.events = {};}
		var handlers = element.events[type];
		if (!handlers)
		{
			handlers = element.events[type] = {};
			if (element['on' + type]){ handlers[0] = element['on' + type];}
			element['on' + type] = handleEvent;
		}
	
		handlers[handler.$$guid] = handler;
	}
}
addEvent.guid = 1;

function removeEvent(element, type, handler)
{
	if (element.removeEventListener){
		element.removeEventListener(type, handler, false);
	}else if (element.events && element.events[type] && handler.$$guid){
		delete element.events[type][handler.$$guid]; }

}

function handleEvent(event)
{
	event = event || fixEvent(window.event);
	var returnValue = true;
	var handlers = this.events[event.type];

	for (var i in handlers)
	{
		if (!Object.prototype[i])
		{
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false){ returnValue = false;}
		}
	}

	if (this.$$handler) {this.$$handler = null;}

	return returnValue;
}

function fixEvent(event)
{
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}
fixEvent.preventDefault = function()
{
	this.returnValue = false;
}
fixEvent.stopPropagation = function()
{
	this.cancelBubble = true;
}

// This little snippet fixes the problem that the onload attribute on the body-element will overwrite
// previous attached events on the window object for the onload event
if (!window.addEventListener)
{
	document.onreadystatechange = function()
	{
		if (window.onload && window.onload != handleEvent)
		{
			addEvent(window, 'load', window.onload);
			window.onload = handleEvent;
		}
	}
}




/*
document.getElementsBySelector(selector) 
  ie: elements = document.getElementsBySelect('div#main p a.external')
  Source: http://simon.incutio.com/archive/2003/03/25/getElementsBySelector
  Works in Phoenix 0.5, Mozilla 1.3, Opera 7, Internet Explorer 6, Internet Explorer 5 on Windows
  Opera 7 fails 
*/

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector) {
  // Attempt to fail gracefully in lesser browsers
  if (!document.getElementsByTagName) {
    return new Array();
  }
  // Split selector in to tokens
  var tokens = selector.split(' ');
  var currentContext = new Array(document);
  for (var i = 0; i < tokens.length; i++) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');
    if (token.indexOf('#') > -1) {
      // Token is an ID selector
      var bits = token.split('#');
      var tagName = bits[0];
      var id = bits[1];
      var element = document.getElementById(id);
      if (tagName && element.nodeName.toLowerCase() != tagName) {
        // tag with that ID not found, return false
        return new Array();
      }
      // Set currentContext to contain just this element
      currentContext = new Array(element);
      continue; // Skip to next token
    }
    if (token.indexOf('.') > -1) {
      // Token contains a class selector
      var bits = token.split('.');
      var tagName = bits[0];
      var className = bits[1];
      if (!tagName) {
        tagName = '*';
      }
      // Get elements matching tag, filter them for class selector
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue; // Skip to next token
    }
    // Code to deal with attribute selectors
    if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
      var tagName = RegExp.$1;
      var attrName = RegExp.$2;
      var attrOperator = RegExp.$3;
      var attrValue = RegExp.$4;
      if (!tagName) {
        tagName = '*';
      }
      // Grab all of the tagName elements within current context
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      var checkFunction; // This function will be used to filter the elements
      switch (attrOperator) {
        case '=': // Equality
          checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
          break;
        case '~': // Match one of space seperated words 
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
          break;
        case '|': // Match start with value followed by optional hyphen
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
          break;
        case '^': // Match starts with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
          break;
        case '$': // Match ends with value - fails with "Warning" in Opera 7
          checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
          break;
        case '*': // Match ends with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
          break;
        default :
          // Just test for existence of attribute
          checkFunction = function(e) { return e.getAttribute(attrName); };
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (checkFunction(found[k])) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      // alert('Attribute Selector: '+tagName+' '+attrName+' '+attrOperator+' '+attrValue);
      continue; // Skip to next token
    }
    // If we get here, token is JUST an element (not a class or ID selector)
    tagName = token;
    var found = new Array;
    var foundCount = 0;
    for (var h = 0; h < currentContext.length; h++) {
      var elements = currentContext[h].getElementsByTagName(tagName);
      for (var j = 0; j < elements.length; j++) {
        found[foundCount++] = elements[j];
      }
    }
    currentContext = found;
  }
  return currentContext;
}

/* That revolting regular expression explained 
/^(\w+)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/
  \---/  \---/\-------------/    \-------/
    |      |         |               |
    |      |         |           The value
    |      |    ~,|,^,$,* or =
    |   Attribute 
   Tag
*/







/*
	forEach, version 1.0
	Copyright 2006, Dean Edwards
	License: http://www.opensource.org/licenses/mit-license.php
*/

// array-like enumeration
if (!Array.forEach) { // mozilla already supports this
	Array.forEach = function(array, block, context) {
		for (var i = 0; i < array.length; i++) {
			block.call(context, array[i], i, array);
		}
	};
}

// generic enumeration
Function.prototype.forEach = function(object, block, context) {
	for (var key in object) {
		if (typeof this.prototype[key] == "undefined") {
			block.call(context, object[key], key, object);
		}
	}
};

// character enumeration
String.forEach = function(string, block, context) {
	Array.forEach(string.split(""), function(chr, index) {
		block.call(context, chr, index, string);
	});
};

// globally resolve forEach enumeration
var forEach = function(object, block, context) {
	if (object) {
		var resolve = Object; // default
		if (object instanceof Function) {
			// functions have a "length" property
			resolve = Function;
		} else if (object.forEach instanceof Function) {
			// the object implements a custom forEach method so use that
			object.forEach(block, context);
			return;
		} else if (typeof object == "string") {
			// the object is a string
			resolve = String;
		} else if (typeof object.length == "number") {
			// the object is array-like
			resolve = Array;
		}
		resolve.forEach(object, block, context);
	}
};




