/**
 * Windows onload function.
 */
function init() {

	preloadImages();

	if (photoId != null) {
		skipToPhoto(photoId);
	}
}

/**
 * Preloads images
 */
function preloadImages() {

	//preloading images
	preloadImgs[0] = new Image(); 
	preloadImgs[0].src = "images/grin_thumb_i.jpg";
	preloadImgs[1] = new Image(); 
	preloadImgs[1].src = "images/nav/living_over.gif";
	preloadImgs[2] = new Image(); 
	preloadImgs[2].src = "images/nav/one_world_over.gif";
	preloadImgs[3] = new Image(); 
	preloadImgs[3].src = "images/nav/holga_over.gif";
	preloadImgs[4] = new Image(); 
	preloadImgs[4].src = "images/nav/structure_over.gif";
	preloadImgs[5] = new Image(); 
	preloadImgs[5].src = "images/nav/everything_over.gif";
	preloadImgs[6] = new Image(); 
	preloadImgs[6].src = "images/nav/i_over.gif";
}

/**
 * Skips to a certain photo in the gallery. Do nothing if photoId is invalid.
 * 
 * @param photoId id of the photo to skip to
 */
function skipToPhoto(photoId) {

	var dataArray = galleryData[sectionId];

	for (var i = 0; i < dataArray.length; i++) {
		if (dataArray[i].id == photoId) {

			//clip found
			currentPhotoIndex = i;
			enterGallery();
			displayPhoto(dataArray[i]);
			return;
		}
	}
}

var sectionPhotoMoveDivTimeoutObject = null;
var sectionPhotoPortraitMoveDivTimeoutObject = null;
var sectionPhotoSquareFormatMoveDivTimeoutObject = null;

/**
 * Show rollover images.
 *
 * @param preloadImgIndex index of the image in the preloadImgs array
 * @param element the image element
 * @param src image source in case it is not yet in the preloadImgs array
 */
function rollOver(preloadImgIndex, element, src) {
	if (preloadImgs.length > preloadImgIndex + 1 && preloadImgs[preloadImgIndex] != null) {
		element.src = preloadImgs[preloadImgIndex].src;
	} else {
		element.src = src;
	}
}

/**
 * Switch to gallery mode.
 */
function enterGallery() {

	var dataArray = galleryData[sectionId];

	document.getElementById("sectionText").style.visibility="hidden";
	moveDiv('sectionPhoto', -25, 0, 5, 5, sectionPhotoMoveDivTimeoutObject);
	moveDiv('sectionPhotoPortrait', -25, 0, 5, 5, sectionPhotoPortraitMoveDivTimeoutObject);
	moveDiv('sectionPhotoSquareFormat', -25, 0, 5, 5, sectionPhotoSquareFormatMoveDivTimeoutObject);
	if (currentPhotoIndex > 0) {
		document.getElementById("prevControl").style.visibility="visible";
	} else {
		document.getElementById("prevControl").style.visibility="hidden";
	}
	if (currentPhotoIndex < dataArray.length - 1) {
		document.getElementById("nextControl").style.visibility="visible";
	} else {
		document.getElementById("nextControl").style.visibility="hidden";
	}
	document.getElementById("slideShowControl").style.visibility="visible";
	document.getElementById("controlCounter").innerHTML = (currentPhotoIndex + 1) + "/" + dataArray.length;
	document.getElementById("photoInfoBox").style.visibility = "visible";
	document.getElementById("shareThisPhoto").style.visibility = "visible";
	updatePhotoInfo(dataArray[currentPhotoIndex]);
	preloadGalleryImage();
}

var sliderMoveDivTimeoutObject = null;

/**
 * Shows splash page slider.
 */
function showSlider() {
	if (!isSliderOpen) {
		isSliderOpen = true;
		moveDiv('slider', 0, -5, 20, 5, sliderMoveDivTimeoutObject);
	} else if (hideSliderPending) {
		hideSliderPending = false;
		clearTimeout(hideSliderTimeout);
	}
}

/**
 * Hides splash page slider.
 */
function hideSlider() {
	if (isSliderOpen && hideSliderPending) {
		isSliderOpen = false;
		hideSliderPending = false;
		moveDiv('slider', 0, 5, 20, 5, sliderMoveDivTimeoutObject);
	}
}

/**
 * Preloads the next image in the gallery.
 */
function preloadGalleryImage() {
	var dataArray = galleryData[sectionId];
	if (currentPhotoIndex + 1 < dataArray.length) {
		var img = new Image();
		img.src = dataArray[currentPhotoIndex + 1].filePath;
	}
}

/**
 * shows next image in the slide show.
 */
function showNextPhoto() {
	if (infoBoxShowing) {
		toggleShowPhotoInfoBox(document.getElementById("showPhotoInfoLink"));
	}
	var dataArray = galleryData[sectionId];
	document.getElementById("prevControl").style.visibility="visible";
	displayPhoto(dataArray[currentPhotoIndex + 1]);
	document.getElementById("controlCounter").innerHTML = (currentPhotoIndex + 2) + "/" + galleryData[sectionId].length;
	currentPhotoIndex++;
	if(currentPhotoIndex == dataArray.length - 1) {
		document.getElementById("nextControl").style.visibility="hidden";
	} else {
		preloadGalleryImage();
	}
}

/**
 * shows previous image in the slide show.
 */
function showPrevPhoto() {
	if (infoBoxShowing) {
		toggleShowPhotoInfoBox(document.getElementById("showPhotoInfoLink"));
	}
	var dataArray = galleryData[sectionId];
	document.getElementById("nextControl").style.visibility="visible";
	displayPhoto(dataArray[currentPhotoIndex - 1]);
	document.getElementById("controlCounter").innerHTML = currentPhotoIndex + "/" + galleryData[sectionId].length;
	currentPhotoIndex--;
	if(currentPhotoIndex == 0) {
		document.getElementById("prevControl").style.visibility="hidden";
	} 
}

/**
 * Displays the actual photo by hiding and showing the appropriate divs.
 *
 * @param photo photo object
 */
function displayPhoto(photo) {
	updatePhotoInfo(photo);
	var placeholderDiv = null;
	document.getElementById("sectionPhoto").style.visibility="hidden";
	document.getElementById("sectionPhotoPortrait").style.visibility="hidden";
	document.getElementById("sectionPhotoSquareFormat").style.visibility="hidden";
	if (photo.dimensionId == 1) {
		placeholderDiv = document.getElementById("sectionPhoto");
	} else if (photo.dimensionId == 2) {
		placeholderDiv = document.getElementById("sectionPhotoPortrait");
	} else if (photo.dimensionId == 3) {
		placeholderDiv = document.getElementById("sectionPhotoSquareFormat");
	}
	placeholderDiv.innerHTML = "<img src='" + photo.filePath + "' border='0'/>";
	placeholderDiv.style.visibility="visible";
}

/**
 * Updates photo info box.
 *
 * @param photo photo object
 */
function updatePhotoInfo(photo) {

	document.getElementById("photoInfoTitle").innerHTML = "<p>" + photo.title + "</p>";
	//document.getElementById("photoStoryTitle").innerHTML = "<p>" + photo.title + "</p>";
	document.getElementById("photoStoryDate").innerHTML = "<p>" + photo.date + "</p>";
	document.getElementById("photoStoryLocation").innerHTML = "<p>" + photo.location + "</p>";
	document.getElementById("photoStoryDesc").innerHTML = "<p>" + photo.desc + "</p>";
	
	var permaLink = "http://" + location.hostname + "/?section=" + sectionId + "&id=" + photo.id;
	var escapedPermaLink = encodeURIComponent(permaLink);
	var escapedTitle = encodeURIComponent("fotowuj - photos by See Wah Cheng: " + photo.title);
	document.getElementById("sharePermaLinkAnchor").setAttribute("href", permaLink);
	document.getElementById("shareStumbleAnchor").setAttribute("href", "http://www.stumbleupon.com/refer.php?url=" + escapedPermaLink);
	document.getElementById("shareFacebookAnchor").setAttribute("href", "http://www.facebook.com/sharer.php?u=" + 
		escapedPermaLink + "&t=" + escapedTitle);
	document.getElementById("shareDiggAnchor").setAttribute("href", "http://digg.com/remote-submit?phase=2&url=" + 
		escapedPermaLink + "&title=" + escapedTitle + "&bodytext=" + escapedTitle);
	document.getElementById("shareDeliciousAnchor").setAttribute("href", "http://del.icio.us/post?v=4&noui&jump=close&url=" + 
		escapedPermaLink + "&title=" + escapedTitle);
}

var infoBoxShowing = false;

/**
 * Toggles on and off the photo info box.
 *
 * @param element the anchor element
 */
function toggleShowPhotoInfoBox(element) {
	if (!infoBoxShowing) {
		document.getElementById("photoStoryBox").style.visibility="visible";
		element.innerHTML = "- hide info";
		infoBoxShowing = true;
	} else {
		document.getElementById("photoStoryBox").style.visibility="hidden";
		element.innerHTML = "+ show info";
		infoBoxShowing = false;
	}
}