var lastActive 		= 0;
var arrImages 		= new Array();
var arrThumbnails = new Array();
var arrCaptions 	= new Array();
var reloadAdOK 		= true;
var startingID 		= "1";
var strRelatedLink = "";
var intSizeChecker = 0;

var intViewableArea 		= 0;
var intContentArea 			= 0;
var intCurrentPosition 	= 0;
var intNewPosition 			= 0;
var intScrollInterval 	= 0;
var intScrollSpeed 			= 8; // 1=slow, 10=fast
var intAmountToJump 		= 150;
var RTR_PictureURL = '';
var RTR_PictureTitle = '';
var RTR_PictureBlurb = '';

if (document.location.hash != '') {
	if (document.location.hash.search("a=") != -1) {
		startingID = parseInt(document.location.hash.split("a=")[1]);
		if (isNaN(startingID)) {
			startingID = 1;
		}
	}
}

/* Navigation */

function nextPhoto(objClicked) {
	blurAll();
	var nextActive = lastActive + 1;
	if (nextActive >= arrImages.length) {
		nextActive = 0;
	}
	loadImage(nextActive);
}

function prevPhoto(objClicked) {
	blurAll();
	var nextActive = lastActive - 1;
	if (nextActive < 0) {
		nextActive = arrImages.length - 1
	}
	loadImage(nextActive);
}

function blurAll() {
	if (document.getElementById("photo").blur) {
		document.getElementById("prevButton").blur();
		document.getElementById("nextButton").blur();
		document.getElementById("photo").blur();
	}
}

function scrollRowDown() {
	if ((intViewableArea != 0) && (intContentArea != 0) && (intViewableArea < intContentArea)) {
		var intSpaceRemaining = intContentArea - intViewableArea + intCurrentPosition;
		if (intSpaceRemaining < intAmountToJump) {
		// if (intSpaceRemaining < intViewableArea) { intNewPosition = intCurrentPosition - intSpaceRemaining;
			intNewPosition = intCurrentPosition - intSpaceRemaining;
		} else {
			intNewPosition = intCurrentPosition - intAmountToJump; // intViewableArea;
		}
		scrollToNewPosition();
	}
}

function scrollRowUp() {
	if ((intViewableArea != 0) && (intContentArea != 0) && (intViewableArea < intContentArea)) {
		var intSpaceRemaining = -(intCurrentPosition);
		if (intSpaceRemaining < intAmountToJump) { // intViewableArea) {
			intNewPosition = 0;
		} else {
			intNewPosition = intCurrentPosition + intAmountToJump; // intViewableArea;
		}
		scrollToNewPosition();
	}
}

function scrollUntil() {
	if (intCurrentPosition < intNewPosition) {
		var intScrollSegment = Math.ceil((intNewPosition - intCurrentPosition)/intScrollSpeed);
		intCurrentPosition += intScrollSegment;
		document.getElementById("thumbTab").style.cssText = "margin-top:" + intCurrentPosition + "px";
	} else if (intCurrentPosition > intNewPosition) {
		var intScrollSegment = Math.ceil((intCurrentPosition - intNewPosition)/intScrollSpeed);
		intCurrentPosition -= intScrollSegment;
		document.getElementById("thumbTab").style.cssText = "margin-top:" + intCurrentPosition + "px";
	} else {
		clearInterval(intScrollInterval);
		//reloadAd();
	}
}

function scrollToNewPosition() {
	clearInterval(intScrollInterval);
	intScrollInterval = setInterval(scrollUntil, 50);
}

function scrollToPosition(intPosition) {
	intNewPosition = intPosition;
	scrollToNewPosition();
}

function scrollToPhoto(intPhotoIndex) {
	var intPhotoPosition = document.getElementById("thumb" + intPhotoIndex).offsetTop;
	var intNewCalculatedPosition = intCurrentPosition - intPhotoPosition + 20;
	if (intContentArea > intViewableArea) {
		if ((intNewCalculatedPosition+20) > -(intContentArea - intViewableArea)) {
			scrollToPosition(intNewCalculatedPosition);
		} else {
			scrollToPosition(-(intContentArea - intViewableArea));
		}
	}
}

function scrollHome() {
	scrollToPosition(0);
}

function scrollEnd() {
	scrollToPosition(-(intContentArea - intViewableArea));
}

function buttonPush(e) {
	if (!e) var e = window.event;
	if (e.button != 2) {
		document.getElementById(this.id).style.cssText = "border-top: 1px solid #000; border-left: 1px solid #000;";
	}
}

function buttonRelease(e) {
	if (!e) var e = window.event;
	if (e.button != 2) {
		document.getElementById(this.id).style.cssText = "";
	}
}

function buttonMenu() {
	return false;
}

/* Ads */

function reloadAd() {
	if (reloadAdOK) {
		document.getElementById("ReutersAd1").src = document.getElementById("ReutersAd1").src
		reloadAdOK = false;
		intCountdown = setTimeout(allowReloads, "4000");
	}
}

function reloadTracking(intImageIndex) {
	if (typeof(dcsMultiTrack) == "function") {
		var onebased = intImageIndex + 1;
		dcsMultiTrack('DCSext.PageNumber', onebased.toString());
	}
}

function allowReloads() {
	reloadAdOK = true;
}

/* Image Loading */

function loadImage(intImageIndex) {
	if (arrImages.length == 1) {
		document.getElementById("photoFrame").className = "photoFrame expanded";
	}
	document.getElementById("photo").src = "http://www.reuters.com/resources/images/loadingImage.gif";
	document.getElementById("thumb" + lastActive).className = "thumbnail";
	document.getElementById("photo").src = arrImages[intImageIndex];
	document.getElementById("caption").innerHTML = strRelatedLink + fixAttribution(arrCaptions[intImageIndex]);
	document.getElementById("thumb" + intImageIndex).className = "thumbnail active";
	document.getElementById("photoId").innerHTML = (intImageIndex+1) + " of " + arrImages.length;
	if (typeof strAudioFile == "undefined") {
		document.location.hash = "a=" + (intImageIndex+1);
	}
	lastActive = intImageIndex;
	//checkViewableArea();

	scrollToPhoto(intImageIndex);
	//reloadAd();
	reloadTracking(intImageIndex);
	RTR_PictureURL = arrImages[intImageIndex];
	RTR_PictureBlurb = arrCaptions[intImageIndex];

}

function loadThumbs() {
	for (j=0; j<arrImages.length; j++) {
		document.write('<div id="thumb' + j + '" class="thumbnail" onclick="javascript:loadImage(' + j + ');return false;">');
		document.write('<table cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle"><img id="img' + j + '" src="' + arrThumbnails[j] + '" alt="Thumbnail" title="Click to view full image" /></td></tr></table></div>');
	}
	if (arrImages.length == 1) {
		document.getElementById("thumbFrame").className = "hidden";
		document.getElementById("prevButton").className = "hidden";
		document.getElementById("nextButton").className = "hidden";
	}
}

function fixAttribution(strRawCaption) {
	var re = new RegExp('(.*)(REUTERS/)(.*)', "g");
	var arrProcessedCaption = re.exec(strRawCaption);
	if (arrProcessedCaption != null) {
		return '<p>' + arrProcessedCaption[1] + '</p><p><span class="label"><strong>' + arrProcessedCaption[2] + arrProcessedCaption[3] + '</strong></span></p>';
	} else {
		return '<p>' + strRawCaption + '</p>';
	}
}

/* Initialization */

function initImage(intImageIndex) {
	intImageIndex = intImageIndex - 1;
	if (intImageIndex >= arrImages.length) {
		intImageIndex = 0;
	}
	loadImage(intImageIndex);
	checkViewableArea();
	document.getElementById("slideshowTitle").innerHTML = strSlideshowTitle;
	if (typeof(window['strAudioFile']) != 'undefined'){
		document.getElementById("photoCaption").className = "photoCaption withAudio";
	}
	initButtons();
	intSizeChecker = setTimeout(checkViewableArea, 1000);
	if (arrImages.length == 1) {
		document.getElementById("photo").title = "";
	}
}

function checkViewableArea() {
	intViewableArea = document.getElementById("thumbnails").offsetHeight;
	intContentArea = document.getElementById("thumbTab").offsetHeight;
	if ((intViewableArea - 30) > (intContentArea - 72)) {
		disableButton("controlUp");
		disableButton("controlDown");
	} else {
		enableButton("controlUp");
		enableButton("controlDown");
		clearInterval(intSizeChecker);
	}
}

function disableButton(buttonId) {
	document.getElementById(buttonId).className = "hidden";
}

function enableButton(buttonId) {
	document.getElementById(buttonId).className = "";
}

function initButtons() {
	document.getElementById("controlUp").onclick = scrollRowUp;
	document.getElementById("controlUp").ondblclick = scrollHome;
	document.getElementById("controlDown").onclick= scrollRowDown;
	document.getElementById("controlDown").ondblclick = scrollEnd;
	document.getElementById("controlUp").oncontextmenu = buttonMenu;
	document.getElementById("controlDown").oncontextmenu= buttonMenu;
	
	document.getElementById("controlUp").onmousedown = buttonPush;
	document.getElementById("controlDown").onmousedown = buttonPush;
	document.getElementById("controlUp").onmouseup = buttonRelease;
	document.getElementById("controlDown").onmouseup = buttonRelease;
}