var d=document;
var saveContent = "";
var maxlistLength = "1"; //the "one of" nav presentation 
var rotatorObj = new Array();
var activeObj = 0;
var zInterval = null;
var listLength = 0;
var adsList = new Array("adtop", "adright", "topad");
var startRotation  = 0;
var images = new Array();
var headlines = new Array();
var timerID = '';
var slideShowState = "off";
var hashprefix="#histNum=";
var curhash=hashprefix+"0";
var curNum=0;
var histNum=curNum;
var isSafari=navigator.appVersion.indexOf("Safari")>-1;
var isPreview=location.pathname.indexOf("/previewer/")>-1;
var enableBookmark=(!isSafari&&!isPreview);
var isGecko=navigator.appCodeName.indexOf("Mozilla")>-1;
window.onblur = function () {pauseSlideShow();}
window.onfocus = function () {resumeSlideShow();}

function updateHash(update){
	if(update!="")
		histNum=parseInt(update.substring( hashprefix.length));
	else
		histNum=0;
	if(histNum!=curNum)
		slideNum(histNum);
}
function reloadAd() {
	for (var i = 0; i < adsList.length; i++)
		setTimeout("reloadAnAd('"+adsList[i]+"')",0);
}
function reloadAdTop() {
	reloadAnAd(adsList[0]);
}
function reloadAdRight() {
	reloadAnAd(adsList[1]);
}
function reloadAdFlash(page) {
	reloadAdRight();
	reloadOmniture(1);
}
function reloadAnAd(adboxId){
	var adbox = document.getElementById(adboxId);
	if(adbox!=null){
		var frame=adbox.getElementsByTagName("IFRAME")[0].cloneNode(true);
		frame.style.height="";
		frame.style.width="";
		adbox.innerHTML="";
		adbox.appendChild(frame);
	}
}

function init() {
	if(!document.getElementById)return;
	ecObj = d.getElementById("gallery");
	for(i=0;i<ecObj.childNodes.length;i++) {
		cObj = ecObj.childNodes[i];
		if(cObj.className == "slide") {
			// cObj.style.display = "none";
			rotatorObj[rotatorObj.length] = cObj;
		}
	}
	endInit();
}
function endInit(){
	if( !isGecko || document.getElementById("galsidebar")!=null){
		doEndInit();
	}else
		setTimeout("endInit()",10);
}
var started = false;
function doEndInit(){
	var pageNumber = getQueryStringValue("page");
	listLength = rotatorObj.length;
 if (pageNumber.toLowerCase()=="all")
		displayAll();
	else if (pageNumber.length > 0 && pageNumber > 0 && pageNumber <= listLength){
		slideNum(pageNumber-1);
	}else{
		if(enableBookmark&&location.hash!=""&&location.hash!=curhash){
			curhash=location.hash;
			updateHash(curhash);
		}else{
			slideNum(0);
		}
	}
	started=true;
}

function displayList(){
	init();
	clearInterval(zInterval);
}
function displayAll () {
for (var i = 0; i < listLength; i++)
rotatorObj[i].style.display="block";
}

function hideContent(key) {
var divObj = d.getElementById(key);
if (divObj != null)
divObj.style.display="none";
}

function hideAllContent() {
var pageNumber = getQueryStringValue("page");
if (pageNumber.toLowerCase()=="all") {
hideContent("content1");
hideContent("footer");
hideContent("google");
}}

function fillInImage(num) {
for (var i = 0; i < document.images.length; i++) {
if (document.images[i].name=="image"+num) {
document.images[i].src = images[num-1];
document.images[i].style.display="block";
break;
}}
//    displayAllImages(); for debugging purposes; do not remove;
}

function displayAllImages() {
for (var i = 0; i < document.images.length; i++) {
if (document.images[i].name.substring(0, 5)=="image") {
alert(document.images[i].name + "  src=" + document.images[i].src);
}}}
function changeNav() {
	reloadOmniture(activeObj + 1);
	if (listLength <= maxlistLength) 
		changeNavLine();
	else
		changeCurrentNum();
}
function changeCurrentNum () {
	var divObj = document.getElementById("nav");
	if (divObj != null)
		divObj.firstChild.data = (activeObj+1) + " of " + listLength;
}
function changeNavLine() {
if (listLength <= maxlistLength) {
var divObj = document.getElementById("nav");
if (divObj != null)
divObj.innerHTML = getNav();
}}

function getNav () {
var outString = "";
var pipe = "&nbsp;|&nbsp;"
for (var i = 0; i < listLength; i++) {
if (activeObj==i)
outString += "<span class='pagecur'>" + (i+1) + "</span>";
else
outString += "<a class='pagenum' href='#' onclick='slideNum(" + i + ")'>" +
(i+1) + "</a>";
if ((i+1) != listLength)
outString += pipe;
}
return outString;
}

//This changes the style of the current article to the next article in the list of
//latest articles navigation
//NOTE: This is currently only being used on LookOfTheDay gallery.  
function toggleListNav(currNumber, toNumber) {
	
	var currentHeadline,
		currentURL,
		nextHeadline,
		nextURL,
		currentArticleID,
		nextArticleID,
		currentLi,
		nextLi;
		
	//Grab the current article id from gallery 
	var currentSlideID = rotatorObj[currNumber].id;
	if (currentSlideID) {
		currentArticleID = currentSlideID.split("_")[2];
	}
	
	//Grab the next article id from gallery
	var nextSlideID = rotatorObj[toNumber].id;
	if (nextSlideID) {
		nextArticleID = nextSlideID.split("_")[2];
	}
	
	//If we have both the current and next article ids, then
	//perform changing the style on the list items in the gallery navigation.
	if (currentArticleID && nextArticleID) {
		//Grab the current list element in the nav.
		if (currentArticleID) {
			currentLi = document.getElementById("latest_article_"+currentArticleID);
			//If we have a current list element, then change its style to be a link.
			if (currentLi) {
				if (latestHeadlineList && latestURLList) {
					currentHeadline = latestHeadlineList[currentArticleID];
					currentURL = latestURLList[currentArticleID];
					currentLi.innerHTML = '<a href="' + currentURL + '">' + currentHeadline + '</a>';
				}
			}
		}
	
		//Grab the next list element in the nav
		nextLi = document.getElementById("latest_article_"+nextArticleID);
		//If we have a next list element, then change its style to note that 
		//it is the current element viewed.
		if (nextLi) {
			if (latestHeadlineList) {
				nextHeadline = latestHeadlineList[nextArticleID];
				nextLi.innerHTML = '&#9668'+nextHeadline;
			}
		}
	}
	
	return;

}

function changeHeadline(toHeadlineNum) {
	var headlineDivObj = document.getElementById("galhead");
	//If we have headlines to change
	if (headlineDivObj != null && headlines != null && headlines.length > 0) {
		var headline = headlines[toHeadlineNum];
		if (headline != null && headline != "undefined")
			headlineDivObj.innerHTML = "<h3>" + headline + "</h3>";	
	}
	return;
}

//Peforms moving slide action.  If on the last slide, we must
//move to the "toURL" instead of the first slide if a value is provided.
function moveContent(dir, toURL) {
	clearInterval(zInterval);
	if(dir) {
		//If we have a url to go to, and we are going to the next gallery
		if (toURL && activeObj+1 >= listLength) {
			window.location.href = toURL;
		} else {
			if(!startRotation) startRotation = 1;
			rotateContent();
		}
	} else {
		slideNum(getNextNum(-1))
	}
}

function rotateContent() {
slideNum(getNextNum(1))
}
function getNextNum(dir){
	var newNum=activeObj+dir;
	if(newNum<0)newNum = listLength-1;
	else if(newNum>=listLength)newNum=0;
	return newNum;
}
function slideNum(toNumber) {
	//retain slide just on.
	var previousNum = activeObj;
	
	//Hide current slide
	clearInterval(zInterval);
	if(!startRotation) startRotation = 1;
	if(activeObj!=toNumber){
		rotatorObj[activeObj].style.display="none";
		activeObj = toNumber;
	}
	//Display next slide
	fillInImage(activeObj+1);
	rotatorObj[activeObj].style.display="block";
	if(enableBookmark){
		try{
		location.replace(location.pathname+hashprefix+toNumber);
		curhash=hashprefix+toNumber;
		}catch(error){}
	}
	curNum=activeObj;
	changeNav();
	if(started)
		reloadAd();
	changeHeadline(activeObj);
	toggleCurrTimeLine(activeObj);
	toggleListNav(previousNum, activeObj);
}

function startSlideShow() {
	window.focus();
	toggleSlideShow();
}

function toggleSlideShow() {
    //Change anchor text
	var ssAnchor = document.getElementById("ssAnchor");
	ssAnchor.innerHTML = (ssAnchor.innerHTML == "Stop Slideshow") ? "Start Slideshow" : "Stop Slideshow";
	
	//Stop or start slide show
	(ssAnchor.innerHTML == "Stop Slideshow") ? nextSlide() : stopSlideShow();
}

function nextSlide() {
    window.focus();
	document.slideShowState = "on";
	moveContent(1);
	document.timerID = setTimeout('nextSlide()', 4000);
}

function pauseSlideShow() {
	document.slideShowState = "pause";
	clearTimeout(document.timerID);
}

function resumeSlideShow() {
    window.focus();
	if (document.getElementById("ssAnchor")) { 
		if (document.slideShowState == "pause" && document.getElementById("ssAnchor").innerHTML == "Stop Slideshow") { 
			clearTimeout(document.timerID); 
			nextSlide();
		}
	}
}

function stopSlideShow() {
	document.slideShowState = "off";
	clearTimeout(document.timerID);
}

function setCurrTimeLine(toNumber) {
	toggleCurrTimeLine(toNumber);
	slideNum(toNumber);
}

function toggleCurrTimeLine(toNumber) {
	var timeLine = document.getElementById("timeline");
	if (timeLine) { 
		var liList = timeLine.getElementsByTagName('li');
		for (var i = 0; i < liList.length; i++) {
			liObj = liList.item(i);
			if (toNumber == i) {
				liObj.className = "cur";
			} else {
				liObj.className = "none";
			}
		}
	}
}

function getMaxLength () {
var tmpLength = 0;
var maxLength = listLength;
while (maxLength > 0)
{
tmpLength++;
maxLength -= (maxLength % 10);
maxLength /= 10;
}
return tmpLength;
}
function getQueryStringValue(key) {
	var queryString = document.location.search.substring(1);
	var keys = queryString.split("&");
	var value = "";
	for (var i=0;i<keys.length && value.length==0;i++)
	{
		var pos = keys[i].indexOf('=');
		var keyName = "";
		if (pos >= 0)
		{
			keyName = keys[i].substring(0, pos);
			if (keyName==key)
				value = keys[i].substring(pos+1);
		}
	}
return value;
}
function stripOutIFrame(inString) {
var ua=navigator.userAgent.toLowerCase();
var browserName = navigator.appName;
var tinString = inString.toLowerCase();
var closingTag = "</iframe>";
var openingIndex = tinString.indexOf("<iframe");
var outString = "";
var closingIndex = -1;
if (ua.indexOf("safari") >= 0)
closingIndex = tinString.length;
else
closingIndex = tinString.indexOf(closingTag);
if (openingIndex >= 0 && closingIndex >= 0)
outString = inString.substring(openingIndex, closingIndex+closingTag.length);
return outString;
}

function checkBrowserSupportApplyFix () {
var browserName = navigator.appName;
var browserVersion = parseInt(navigator.appVersion);
var ua=navigator.userAgent.toLowerCase();
var rv;

if (browserName != 'Netscape' || ua.indexOf('safari') >= 0) {
rv = true;
} else {
rv = false;
}
return rv;
}

function fillInAd () {
var browserName = navigator.appName;
var browserVersion = parseInt(navigator.appVersion);
var ua=navigator.userAgent.toLowerCase();
var ctr = 1;

if (browserName=='Microsoft Internet Explorer' || browserName=='Netscape') { 
for (var i = 0; i < magicNumberData.length; i++) {
if (magicNumberData[i][3]=="1") {
var divObj = document.getElementById("ad" + ctr);
if (divObj != null) {
if (checkBrowserSupportApplyFix ())	{
divObj.innerHTML = stripOutIFrame(divObj.innerHTML);
}}
ctr++;
}}}}

function fillInLink () {
var whichLink = activeObj+1;
if (subscribeLink.length > 0 && whichLink > 1 && whichLink < rotatorObj.length) {
var divObj = document.getElementById("gal_text" + whichLink);
var seenFlag = false;
if (divObj != null)	{
if (activeObj < linkArraySeen.length) {
if (linkArraySeen[activeObj]==true) 
seenFlag = true;
}
if (seenFlag==false) {
linkArraySeen[activeObj] = true;
divObj.innerHTML += subscribeLink;
//alert("adding link to " + activeObj);
}}}}

