﻿//Victoria Upton fun jquery bits - Richard Wilson March 2010

var CurrentImageID = "";
$(document).ready(function () {

	//if we have an intro then fade it in and out again
	if ($(".intro")) {
		$(".welcome").fadeIn(2000);
		$(".intro").delay(5000).fadeOut('slow');
	}

	//fade in all the images one after each other
	$('.thumbs img').each(function (index) {
		$(this).delay(100 * index).fadeTo("slow", 0.6);
		if (index == 0) {
			document.getElementById("forward").href = "#" + this.id.replace("thumb", "");
			setImage(this.id.replace("thumb", ""));
		}
	});

	//$(".thumbs img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads

	//set rollover fades
	$(".thumbs img").hover(function () {
		$(this).fadeTo("fast", 1.0); // This should set the opacity to 100% on hover
	}, function () {
		var thisid = this.id.replace("thumb", "");
		//alert(thisid + " - " + CurrentImageID);
		if (thisid != CurrentImageID) {
			$(this).fadeTo("fast", 0.6); // This should set the opacity back to 60% on mouseout
		}
	});
});

function setImage(imageid) {
	//fade out old one and set new ID;
	$("#thumb" + CurrentImageID).fadeTo("fast", 0.6);
	CurrentImageID = imageid;
	$("#thumb" + imageid).fadeTo("fast", 1);
	
	//first hide the main image, then load it then show it...
	$(".photo").hide();
	$(".photo").css("background-image", "url(/images/photos/full/" + imageid + ".jpg)");
	$(".photo").css("background-position", "center center");

	document.getElementById("phototitle").innerHTML = document.getElementById("thumb" + imageid).alt;
	$(".photo").fadeIn('slow');

	var backhref = "";
	var forwardhref = "";
	var backhrefset = false;
	var forwardhrefset = false;
	
	var currentindex = -1;

	$('.thumbs img').each(function(index) {

		var thisid = this.id.replace("thumb", "");
	
		if (!forwardhrefset)
			forwardhref = thisid;
			
		if (currentindex != -1) {
			forwardhrefset = true;
			currentindex = -1;
		}

		if (thisid == CurrentImageID) {
			currentindex = index;
		}
		if (currentindex != -1) {
			backhrefset = true;
		}
		if (!backhrefset)
			backhref = thisid;		
	});

	document.getElementById("back").href = "#" + backhref;
	document.getElementById("forward").href = "#" + forwardhref;
}

$(function() {
	// Bind an event to window.onhashchange that, when the hash changes, gets the
	// hash and adds the class "selected" to any matching nav link.
	$(window).bind('hashchange', function() {
		var hash = location.hash;
		if (hash != "") {
			//alert(hashvars[1] + " - " + hashvars[0]);
			setImage(hash.replace("#",""));
		}
	})

	// Since the event is only triggered when the hash changes, we need to trigger
	// the event now, to handle the hash the page may have loaded with.
	$(window).trigger('hashchange');
});
