
/*  To add to the body of the javascript file */
tii_addEventHandlerOnElementLoad ( 'enlargeLink', 'click', function (event){displayPopUp(event);}, false);
tii_addEventHandlerOnElementLoad ( 'enlargeImg', 'click', function (event){displayPopUp(event);}, false);
tii_addEventHandlerOnElementLoad ( 'popClose', 'click', function (event){hidePopUp(event);}, false);

var keyevent = tii_issafari || tii_isie ? 'keydown' : 'keypress';
tii_addEventHandlerOnElementLoad ( 'enlargeLink', keyevent , function (event){displayPopUp(event);}, false);
tii_addEventHandlerOnElementLoad ( 'enlargeImg', keyevent , function (event){displayPopUp(event);}, false);
tii_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;
/* 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=(tii_isie)? document.documentElement.scrollTop : window.pageYOffset;
	docHeight=(tii_isie)? document.documentElement.clientHeight: window.innerHeight;
	/* get the image file */
		/*  Create the background div */
	/*  get the pop up display element */
	
	/* Access the Pop Up Div */
	banner = document.getElementById('topBanner');
	popUp= document.getElementById('popup');
	
	if (popUp!=null){
		banner.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;	
		var evtHeight=eventSource.offsetHeight;
		/*  set the top display to half the browser window */
		popUp.style.top=docTop + (docHeight/2) - (objHeight/2) - 50 + "px";
		/* 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://img.timeinc.net/time/i/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 */
	tii_addEventHandler( popUpBg, 'click', function (event){hidePopUp(event);}, false);
	tii_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';			
}