/*  To add to the body of the javascript file */
cnn_addEventHandlerOnElementLoad ( 'enlargeLink', 'click', function (event){displayPopUp(event);}, false);
cnn_addEventHandlerOnElementLoad ( 'enlargeImg', 'click', function (event){displayPopUp(event);}, false);
cnn_addEventHandlerOnElementLoad ( 'popClose', 'click', function (event){hidePopUp(event);}, false);

var keyevent = cnn_issafari || cnn_isie ? 'keydown' : 'keypress';
cnn_addEventHandlerOnElementLoad ( 'enlargeLink', keyevent , function (event){displayPopUp(event);}, false);
cnn_addEventHandlerOnElementLoad ( 'enlargeImg', keyevent , function (event){displayPopUp(event);}, false);
cnn_addEventHandlerOnElementLoad ( 'popClose', keyevent , function (event){hidePopUp(event);}, false);


var docTop;
var docHeight;
var popUp;
var photoImg;
var preLoadDiv;
var popUpBkgd;
var popUpBg;
var popUpOverlay;
var preLoadImg;
var popTxt;
var debug=0;
var banner;
var rgtAd;
/* popup functions */
function displayPopUp(event){
	/*  Check for IE */
	var eventSource = typeof event.target != 'undefined' ? event.target : window.event.srcElement;
	/*  Get the height of the window browser */

	//docTop=(cnn_isie)? document.documentElement.scrollTop : window.pageYOffset;
	if (cnn_isie) {
		docTop = document.documentElement.offsetHeight;
	} else {
		docTop = window.pageYOffset;
	}
	//alert(docTop);
	//docHeight=(cnn_isie)? document.documentElement.clientHeight: window.innerHeight;
	if (cnn_isie) {
		docHeight = document.body.offsetHeight;
	} else {
		docHeight = window.innerHeight;
	}
	//alert(docHeight);
	/* get the image file */
		/*  Create the background div */
	/*  get the pop up display element */
	
	/* Access the Pop Up Div */
	banner = document.getElementById('adBannerTable');
	rgtAd = document.getElementById('adsquare');
	popUp= document.getElementById('popup');
	
	if (popUp!=null){
		banner.style.display = 'none';
		rgtAd.style.display = 'none';
		createBackground();
		photoImg = document.getElementById('popUpImg');
		/*  Display preload logo image */
		
	/*  IE needs to preload the image in order to access width -- load a new image */
		var newImg = new Image();
		newImg = photoImg.cloneNode(true);
		popUp.style.width = newImg.width + 'px';
		/*  get the height of the pop up area */
		var objHeight=newImg.height;
		//alert(objHeight);
		var evtHeight=eventSource.offsetHeight;
		/*  set the top display to half the browser window */
		if (cnn_isie) {
			popUp.style.top=docTop + evtHeight + (docHeight/2) - (objHeight/2) - 150 + "px";
		} else {
			popUp.style.top=docTop + (docHeight/2) - (objHeight/2) - 50 + "px";
		}
		//alert(popUp.style.top);
		/* Display the pop up */
		popUp.style.display = 'block';
		/* Display the pop up Background */
		popTxt = document.getElementById('popTxt');
		
		/* adjust the Text width to the width of the Image */
		popTxt.style.width= photoImg.width + ' px';
		scrollOverlay();
		
		/*  Center the popup horizontally */
		popUp.style.marginLeft = (-(photoImg.width / 2)) + 'px';
		/* Move the focus to the close window tag */
		popUp.getElementsByTagName('a')[0].focus();
		/* Hide preloading image */
		preLoadDiv.style.display = 'none';
	}
}

function createBackground(){
	/* Create the popup background div */	
	popUpBkgd = document.createElement('div');
	popUpBkgd.setAttribute('id', 'popUpBg');
	
	/*  Create the background anchor */
	popUpBg = document.createElement('a');
	popUpBg.setAttribute('id', 'overlay');
	popUpBg.setAttribute('href', '#');
	popUpBkgd.appendChild(popUpBg);
	
	/*  Create the background Image */
	popUpOverlay = document.createElement('img');
	popUpOverlay.setAttribute('id', 'bgImg');
	popUpOverlay.setAttribute('src', 'http://i.cnn.net/money/.element/img/1.0/misc/overlay.png');
	popUpOverlay.setAttribute('height', '100%');
	popUpOverlay.setAttribute('width', '100%');
	popUpBg.appendChild(popUpOverlay);
	
	/*  Create the Pre Loading Image Div */
	preLoadDiv = document.createElement('div');
	preLoadDiv.setAttribute('id', 'preLoadImg');
	preLoadDiv.style.top=docTop + (docHeight/2) - 42 - 50 + "px";
	preLoadDiv.style.display = 'block';
	
	/*  Create the pre loading image */
	preLoadImg = document.createElement('img');
	preLoadImg.setAttribute('height', '84 px');
	preLoadImg.setAttribute('width', '256 px');
	preLoadImg.setAttribute('src', 'http://img.timeinc.net/time/i/logo_time_preload.gif');
	preLoadDiv.appendChild(preLoadImg);

	var allDivs = document.getElementsByTagName('div');
	
	/*append the div to the containing div */
	allDivs[0].parentNode.appendChild(popUpBkgd);

	/*  Add Event Handlers to the background overlay */
	cnn_addEventHandler( popUpBg, 'click', function (event){hidePopUp(event);}, false);
	cnn_addEventHandler( popUpBg, keyevent , function (event){hidePopUp(event);}, false);
	popUpBkgd.style.display = 'block';

}

function scrollOverlay(){
	/*  finds the full length of the document by finding the greater of scroll height or offset height */
	var scrollHeight = document.body.scrollHeight;
	var scrollWidth = document.body.scrollWidth;
	var offsetHeight = document.body.offsetHeight;
	var fullHeight = (scrollHeight > offsetHeight) ? scrollHeight : offsetHeight;
	var overlay = document.getElementById('bgImg');
	var background = document.getElementById('popUpBg');
	overlay.style.height = fullHeight + 37 + "px";
	overlay.style.width = scrollWidth;
	background.style.height = fullHeight + 37 + "px";
	background.style.width = scrollWidth;
}

function hidePopUp(event){
	var popUp = document.getElementById('popup');
	var popUpBkgd = document.getElementById("popUpBg");
	popUpBkgd.parentNode.removeChild(popUpBkgd);
	popUp.style.display = 'none';
	banner.style.display = 'block';
	rgtAd.style.display = 'block';
}

/***** cnn Global Functions *****/
/***** cnn Global Browser Sniffing Variables *****/

var cnn_isopera = typeof window.opera != 'undefined';

var cnn_isie = typeof document.all != 'undefined'

   	&& !cnn_isopera && navigator.vendor != 'KDE';

var cnn_issafari = navigator.vendor == 'Apple Computer, Inc.';
/* Gets the total offset position of the element, assuming none of its ancestors have a float of left or right.
   Direction is 'x' for horizontal, and 'y' for vertical */
function cnn_getTotalOffsetPosition (element, direction)
{
  var pos = direction == 'x' ? element.offsetLeft : element.offsetTop;
  var tmp = element.offsetParent;
  while (tmp != null)
  {
    pos += direction == 'x' ? tmp.offsetLeft : tmp.offsetTop;
    tmp = tmp.offsetParent;
  }
  return pos;
}

/* Stops the default action for the event, such as jumping to an anchor when clicking on a hyperlink */
function cnn_stopDefaultAction (event)
{
	event.returnValue = false;
	if (typeof event.preventDefault != 'undefined')
	{
		event.preventDefault ();
	}
}

/* Create a new element node with attributes */
function cnn_dom_createElement (nodeName, attributes)
{
	var isopera = typeof window.opera != 'undefined';
	var isie = typeof document.all != 'undefined'
   		&& !isopera && navigator.vendor != 'KDE';
		
	var newElement;
	try
	{
		newElement = document.createElement (nodeName);
	}
	catch (error)
	{
		return null;
	}
	
	var attributesLength = attributes.length;
	for (var i = 0; i < attributesLength; i++)
	{
		var attribute = attributes [i] [0];
		var value = attributes [i] [1];
		newElement.setAttribute (attribute, value);
		switch (attribute)
		{
			case 'id':
				newElement.id = value;
				break;
			case 'class':
				if (isie)
				{
					newElement.setAttribute ('className', value);
				}
				newElement.className = value;
				break;
			case 'style':
				newElement.style.cssText = newElement.style.cssText + ' ' + value;
				break;
			case 'for':
				if (isie)
				{
					newElement.setAttribute ('htmlFor', value);
				}
				newElement.htmlFor = value;
		}
	}
	
	return newElement;
}

/* Removes all the unwanted whitespace text nodes from inside the tree
   (including tabs, spaces, and line breaks between list items) */
function cnn_dom_removeWhitespaceTextNodes (node)
{
  for (var x = 0; x < node.childNodes.length; x++)
  {
    var child = node.childNodes [x];
    if (child.nodeType == 3 && !/\S/.test (child.nodeValue))
    {
      node.removeChild (node.childNodes [x]);
      x--;
    }
    if (child.nodeType == 1)
    {
      cnn_dom_removeWhitespaceTextNodes (child);
    }
  }
}

/***** ^^^ cnn Global Functions Placed in >cnn>Shared>JavaScript>cnn LIB - JavaScript Event Listeners ^^^ *****/
/* Adds a function for the window load event */
function cnn_callFunctionOnWindowLoad (functionToCall)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener ('load', functionToCall, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener ('load', functionToCall, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent ('onload', functionToCall);
  }
  else
  {
    var oldFunctionToCall = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = functionToCall;
    }
    else
    {
      window.onload = function ()
      {
        oldFunctionToCall ();
        functionToCall ();
      };
    }
  }
}

/* Calls functionToCall as soon as the targetElement is loaded, even if the document hasn't completely loaded yet. 
   Place the parameter list for functionToCall in order after cnn_callFunctionOnElementLoad (targetId, functionToCall),
   e.g., cnn_callFunctionOnElementLoad (targetId, functionToCall, parameter1, parameter 2, parameter 3, ...) */  
function cnn_callFunctionOnElementLoad (targetId, functionToCall)
{
	var myArguments = arguments;
	cnn_callFunctionOnWindowLoad (function ()
		{
			window.loaded = true;
		});
	var targetElement = document.getElementById (targetId);
	if (targetElement == null && !window.loaded)
	{
		var pollingInterval = setInterval (function ()
			{
				if (window.loaded)
				{
					clearInterval (pollingInterval);
				}
				targetElement = document.getElementById (targetId);
				if (targetElement != null)
				{
					clearInterval (pollingInterval);
					var argumentsTemp = new Array ();
					var argumentsTempLength = myArguments.length - 2;
					for (var i = 0; i < argumentsTempLength; i++)
					{
						argumentsTemp [i] = myArguments [i + 2];
					}		
					functionToCall.apply (this, argumentsTemp);
				}
			}, 10);
	}
}

/* Attaches an event handling function to the targetElement as soon as the targetElement is loaded
   (even if the document hasn't completely loaded yet). */  
function cnn_addEventHandlerOnElementLoad (targetId, eventType, functionToCall, bubbleEventUpDOMTree)
{
	cnn_callFunctionOnWindowLoad (function ()
		{
			window.loaded = true;
		});
	var targetElement = document.getElementById (targetId);
	if (targetElement == null && !window.loaded)
	{
		var pollingInterval = setInterval (function ()
			{
				if (window.loaded)
				{
					clearInterval (pollingInterval);
				}
				targetElement = document.getElementById (targetId);
				if (targetElement != null)
				{
					clearInterval (pollingInterval);
					cnn_addEventHandler (targetElement, eventType, functionToCall, bubbleEventUpDOMTree);
				}
			}, 10);
	}
}

/* Attaches an event handling function to the targetElement. 
   Examples of eventType values are 'mouseover' and 'keyup', as opposed to 'onmouseover' and 'onkeyup'. 
   bubbleEventUpDOMTree is a boolean variable specifying whether the event should activate the event listeners
   of all the ancestors of the element (up to the window object) */
function cnn_addEventHandler (targetElement, eventType, functionToCall, bubbleEventUpDOMTree)
{
  if (!targetElement)
  {
	  window.status = 'Warning: Tried to attach event to null object';
	  return false;
  }
  if (typeof targetElement.addEventListener != 'undefined')
  {
    targetElement.addEventListener (eventType, functionToCall, bubbleEventUpDOMTree);
  }
  else if (typeof targetElement.attachEvent != 'undefined')
  {
    targetElement.attachEvent ('on' + eventType, functionToCall);
  }
  else
  {
    eventType = 'on' + eventType;
    if (typeof targetElement [eventType] == 'function')
    {
      var oldListener = targetElement [eventType];
      targetElement [eventType] = function ()
      {
        oldListener ();
        return functionToCall ();
      }
    }
    else
    {
      targetElement [eventType] = functionToCall;
    }
  }

  return true;
}

/* Removes an event handling function from the targetElement. 
   Examples of eventType values are 'mouseover' and 'keyup', as opposed to 'onmouseover' and 'onkeyup'. 
   bubbleEventUpDOMTree is a boolean variable specifying whether the event should activate the event listeners
   of all the ancestors of the element (up to the window object)
   ***NOTE: This function does not support removing anonymous functions; a reference to the added function is needed */
function cnn_removeEventHandler (targetElement, eventType, functionToRemove, bubbleEventUpDOMTree)
{
  if (typeof targetElement.removeEventListener != "undefined")
  {
    targetElement.removeEventListener (eventType, functionToRemove, bubbleEventUpDOMTree);
  }
  else if (typeof targetElement.detachEvent != "undefined")
  {
    targetElement.detachEvent ("on" + eventType, functionToRemove);
  }
  else
  {
    targetElement ["on" + eventType] = null;
  }
  
  return true;
}