var nextOff, nextOn, prevOff, prevOn;
var thumbnailCount, currentPhoto, thumbnailDisplayMax;
var slideDistance, currentSlidePosition, animationInterval, photoGalleryThumbTitleTopOffset;
var hideLargePhoto;

$(document).ready(function() {
	// video
	$(".watchVideo").click(function() {
		$(this).blur();
		darkenScreen();
		loadVideo(this.href);
		return false;
	});
	
	// tabs
	$("div.hidden").hide();
	$("div.tabHeaders ul li a").click(function() { 
		$(this).parent().parent().children().removeClass('selected'); // remove currently selected
		$(this).parent().addClass('selected'); // make this selected
		var linkHash = this.hash; // find selected tab
		if (linkHash.length > 1) {
			linkHash = linkHash.substring(1,linkHash.length);
		}
		$("#"+linkHash).siblings().hide(); // hide siblings
		$("#"+linkHash).show(); // show selected tab
		$(this).blur();
		return false;
	});
	// accordion
	$("div.accordionItem h2").click(function() {
		if ($(this).attr("class") != "selected") {
			$("div.accordionItem h2.selected").next().slideUp("normal");
			$("div.accordionItem h2").removeClass("selected");
			$(this).addClass("selected");
			$(this).next().slideDown("normal");
		}
	});
	
	// photo gallery	
	currentSlidePosition = 0;
	currentPhoto = 1;
	
	// hide large photo?
	if (hideLargePhoto === true) {
		$("#photoGalleryPhoto").hide();
		$("#photoGalleryInfo").hide();
		$("ul li a img.selectedThumb").removeClass("selectedThumb");
	}
	
	// activate next button with correct state
	if (thumbnailCount > thumbnailDisplayMax) {
		$("#photoGalleryNext img").attr("src",nextOn); 
	} else {
		$("#photoGalleryNext img").attr("src",nextOff);
	}
	
	// activate off state of previous button
	$("#photoGalleryPrev img").attr("src",prevOff);
	
	$("#photoGalleryPrev").click(function() { slideThumbnail(-1); $(this).blur(); return false; });
	$("#photoGalleryNext").click(function() { slideThumbnail(1); $(this).blur(); return false; });
	$("ul#photoGalleryThumbsList li a").click(function() { 
		photoId = $(this).attr("id").split("_"); // get index number
		$("#photoGalleryPhoto img").attr("src", this.href); // update large image
		$("#photoGalleryThumbsList li a img").removeClass("selectedThumb"); // updated selectedThumb
		$(this).children().addClass("selectedThumb");
		$("#photoGalleryCountCurrent").text(photoId[1]); // update count
		$("#photoGalleryCaption").text($(this).attr("rel")); // update caption
		// reveal large photo if needed
		if (hideLargePhoto === true) {
			$("#photoGalleryPhoto").show();
			$("#photoGalleryInfo").show();
			hideLargePhoto = false;
		}
		return false;
	});
	
	$("ul#photoGalleryThumbsList li a").hover(function(){
		currentElementId = $(this).attr("id");
		showPhotoGalleryThumbTitle(this, $(this).attr("rev"));
	},function(){
		clearPhotoGalleryThumbTitle();
	});

});

function slideThumbnail(x) {
	// do we need sliding at all?
	if (thumbnailCount <= thumbnailDisplayMax) {
		return;
	}
	
	if (x == 1) {
		// next
		// is this button active?
		if ($("#photoGalleryNext img").attr("src") == nextOn) {
			currentSlidePosition = currentSlidePosition + slideDistance;
			$("#photoGalleryThumbsList").animate({right: currentSlidePosition}, animationInterval);
			currentPhoto++;
			updateNextPrevious();
		}
		return false;
		
	} else {
		// previous
		if ($("#photoGalleryPrev img").attr("src") == prevOn) {
			currentSlidePosition = currentSlidePosition - slideDistance;
			$("#photoGalleryThumbsList").animate({right: currentSlidePosition}, animationInterval);
			currentPhoto--;
			updateNextPrevious();
		}
		return false;
	}
}

function updateNextPrevious() {
	if ((thumbnailCount - thumbnailDisplayMax) >= currentPhoto) {
		$("#photoGalleryNext img").attr("src",nextOn);
	} else {
		$("#photoGalleryNext img").attr("src",nextOff);
	}
	
	if (currentPhoto > 1) {
		$("#photoGalleryPrev img").attr("src",prevOn);
	} else {
		$("#photoGalleryPrev img").attr("src",prevOff);
	}
}


function showPhotoGalleryThumbTitle(e, thumbTitle) {
	if (document.getElementById("photoGalleryThumbTitle")) {
		clearPhotoGalleryThumbTitle();
	}
	
	if (thumbTitle) {
	
		var bodyDiv = document.getElementById("body");
		var wrapper = document.getElementById("wrapper");
		var wrapperLeft = getRealLeft(wrapper);
		
		var photoGalleryThumbTitle = document.createElement("div");
		photoGalleryThumbTitle.setAttribute('id', 'photoGalleryThumbTitle');
		photoGalleryThumbTitle.style.display = "none";
		var top = getRealTop(document.getElementById('photoGalleryThumbs'));
		var left = getRealLeft(e);
		photoGalleryThumbTitle.style.top = top + photoGalleryThumbTitleTopOffset + "px";
		photoGalleryThumbTitle.style.left = left + (e.offsetWidth / 2) - 25 + "px";
		photoGalleryThumbTitle.style.display = 'none';
		var thumbDescText = document.createElement("p");
		thumbDescText.style.margin = '0';
		var textNode = document.createTextNode(thumbTitle);
		thumbDescText.appendChild(textNode);
		photoGalleryThumbTitle.appendChild(thumbDescText);
		bodyDiv.appendChild(photoGalleryThumbTitle);
		photoGalleryThumbTitle.style.display = "block";
	}
}

function clearPhotoGalleryThumbTitle() {
	var bodyDiv = document.getElementById("body")
	var photoGalleryThumbTitle = document.getElementById("photoGalleryThumbTitle")
	if(photoGalleryThumbTitle) {
		var rubbish = bodyDiv.removeChild(photoGalleryThumbTitle);
	}
}

function darkenScreen() {
	$("#darkenDiv").show();
	$("#darkenContents").show();
	
	var theBody = document.getElementById("body");
	$("#darkenDiv").css("height", theBody.scrollHeight + 135);

	// create hit area for closing darkened area
	$("#darkenDiv").click(function() {
		clearDarkenScreen();
	});
	return false;
}

function clearDarkenScreen() {
	$("#darkenContents").hide();
	$("#darkenDiv").hide();
	return false;
}

function loadVideo(url) {
	return;
}

/*
function darkenScreen() {
	// make sure screen hasn't already been darkened
	if (document.getElementById("darkenDiv")) {
		clearDarkenScreen();
	}
	
	$("#infoBoxOverlayIFrame").after("<div id='darkenDiv'></div><div id='darkenContents'></div>");
	// set full height
	var theBody = document.getElementById("body");
	$("#darkenDiv").css("height", theBody.scrollHeight + 135);

	// create hit area for closing darkened area
	$("#darkenDiv").click(function() {
		clearDarkenScreen();
	});
	
}

function clearDarkenScreen() {
	$("#darkenContents").remove();
	$("#darkenDiv").remove();
}

function loadVideo(url) {
	// this functionality will likely need to be updated once we have a working video player
	// it may(?) make more sense to display/position an existing (but hidden) layer rather than use an iframe
	$("#darkenContents").html("<p id='closeButton'><a href='#' onclick='clearDarkenScreen()'>Close</a></p><iframe src='" + url + "' width='900' height='500' frameborder='0' id='videoPlayerWindow' ></iframe>");
	// center align video window
	$("#darkenContents").css("left", (($("html").width())/2) - (($("iframe#videoPlayerWindow").attr("width"))/2));
}
*/
