/* JS script for the donhmarr.com website
	Created by :
	Emmanuel Randon
	eCollection LLC
	contact@ecollection.biz

	This code handles :
	- The duplication of the previous and next buttons on collection.php and recent.php
	- The hover mechanism and display of detail picture on the homepage
*/

// Function call on document.ready
$(document).ready(pageInit);

// Global variables declaration
var timeoutLeaveThumb = null;
var timeoutHoverThumb = null;
var imgThumbToShow = null;
var dispState = "statement";
var deprecateFeature = false;

// Page init function
function pageInit() {
	
	// If on collection.php or recent.php pages, this test is true
		if ($("#pageNavTop").html() !=null && $("#pageNavBottom").html() != null) {
			// Duplicate Previous and Next buttons to the top of the page
			$("#pageNavTop").html($("#pageNavBottom").html());
		}
		else if ($(".divImgDetail").html() != null) {
			
			$(".imgThumb").mouseover(hoverThumb);
			$(".imgThumb").mouseout(leaveThumb);
			
			var browser=navigator.appName;
			var b_version=navigator.appVersion;
			var version=parseFloat(b_version);
			if (((browser=="Microsoft Internet Explorer") && (version<=4)) || (navigator.userAgent.indexOf("Opera")!=-1)){
				deprecateFeature = true;
			}
			else {					
				// Associate hover events for both thumbnails and detail picture
				$(".imgDetail").mouseover(hoverDetail);
				$(".imgDetail").mouseout(leaveDetail);
				$(".imgDetail").load(loadedImage);
			}
		};
}

// Function called when the mouse hovers a thumbnail
function hoverThumb() {
		clearTimeouts();	
		imgThumbToShow = this;
		timeoutHoverThumb = setTimeout(loadDetailImage, 100);
}

// Function called when the mouse leaves a thumbnail
function leaveThumb() {
		clearTimeouts();
		timeoutLeaveThumb = setTimeout(hideDetailImage, 3000);
};

// Function called when the mouse hovers the detail picture
function hoverDetail() {
		clearTimeouts();
};

// Function called when the mouse leaves the detail picture
function leaveDetail() {
		timeoutLeaveThumb = setTimeout(hideDetailImage, 3000);
};

// Function called once the detail image is loaded and ready for display
function loadedImage() {
		$(".imgDetailLink").attr("href", getDetailPagePathFromThumb(imgThumbToShow.src));
		$(".divImgDetail").show();
		$(".divImgLoading").hide();
		dispState = "detail";
};

// Function called to show the loading div
function loadDetailImage() {
	if (deprecateFeature) {
		$(".imgStatement").attr("src", "imageresize.php?w=319&h=224&constrain=1&img=" + getLargeImagePathFromThumb(imgThumbToShow.src));
	}
	else {
		if (dispState == "statement") {
			$(".divImgLoading").fadeIn("slow", showDetailImage);
			dispState = "load";
		}
		else {
			$(".divImgLoading").show();
			dispState = "load";
			showDetailImage();
		}		
	}
}

// Function called to start loading the detail image
function showDetailImage(){
	$(".imgDetail").attr("src", "imageresize.php?w=319&h=224&constrain=1&img=" + getLargeImagePathFromThumb(imgThumbToShow.src));
}

// Function called to hide the detail image
function hideDetailImage() {
	if (deprecateFeature){
		$(".imgStatement").attr("src", "images/gra-homepage.gif");
	}
	else {
		if (dispState == "load") {
			$(".divImgLoading").fadeOut("slow");
		}
		else if (dispState == "detail") {
			$(".divImgDetail").fadeOut("slow");		
		}
	}
}

// Function that returns the Detail image path from the thumbnail image path
function getLargeImagePathFromThumb(thumbPath) {
		return thumbPath.substring(0, thumbPath.length - 7) + "-large.jpg";	
}

// Function that returns the detail page path from the thumbnail image path
function getDetailPagePathFromThumb(thumbPath) {
		return "detail.php?detail=" + thumbPath.substring(thumbPath.length - 11, thumbPath.length - 7);
}

// Function to reset all timers
function clearTimeouts() {
		if (timeoutHoverThumb != null) {
			clearTimeout(timeoutHoverThumb);
		};
		if (timeoutLeaveThumb != null) {
			clearTimeout(timeoutLeaveThumb);
		};	
}