// JavaScript Document - Photo Gallery - GlobalTex

var number_of_images = 0;
var currentPic = 0;
var path_to_thumbs = "";
var path_to_fullsize = "";
var path_to_hires = "";
var image_prefix = "";
var titleArray = {};
var image = ""
var stuff = "";
var href = "";
var full = "";
var id = 0;
var title = "";
var gallery_title = "";
var xmlFilePath = "";

$(document).ready(function(){
	xmlFilePath = $("#gallery_xml").text();
	initiateGallery();
});

function initiateGallery() {
	$("a.closeBigImage").click(function(){
		animatedcollapse.toggle('bigImage');
		$(".highlight").removeClass("highlight");
		document.location.hash = gallery_title;
		return false;
	});
	
	$("a.prev").click(function(){
		if(currentPic == 101) {
			changeImage(number_of_images);	
		} else {
			changeImage(currentPic - 1);
		}
		return false;
	});
	
	$("a.next").click(function(){
		if(currentPic == number_of_images) {
			changeImage(101);	
		} else {
			changeImage(currentPic + 1);
		}
		return false;
	});
	
	$.get(xmlFilePath, function(xmlFile) {
		gallery_title = $(xmlFile).find("gallerytitle").text();
		path_to_thumbs = $(xmlFile).find("pathtothumbs").text();
		path_to_fullsize = $(xmlFile).find("pathtofullsize").text();
		path_to_hires = $(xmlFile).find("pathtohires").text();
		image_prefix = $(xmlFile).find("imgprefix").text();
		number_of_images = parseInt($(xmlFile).find("numberofimages").text());
		titleArray.length = number_of_images;			
		$(xmlFile).find("imgprop").each(function(){
			image = $(this).attr("url");
			title = $(this).text();
			if (title == "") {
				title = gallery_title;	
			}
			titleArray[currentPic] = title;
			currentPic++;								
			stuff += "<li><a href=\"" + path_to_fullsize + image + "\" class=\"thumbnail\" target=\"_blank\" title=\"" + title + "\" id=\"img" + (currentPic + 100) + "\"><img src=\"" + path_to_thumbs + image + "\" alt=\"Image Thumbnail\" width=\"50\" height=\"50\" /></a></li>";
		});
		$("#thumbs").html(stuff);
		activateLinks();
		setImage(); //see if the link has a hash location on it
	});
	
	animatedcollapse.addDiv('bigImage');
	animatedcollapse.init();
}

function setImage() {
	var _hash = document.location.hash;
	if ((_hash.substring(1,4)) == "img" && (_hash.substring(4)) <= number_of_images) {
		changeImage(parseInt(_hash.substring(4)));
	} else if (_hash == "" || _hash.substring(1) == gallery_title){
		//do nothing
	} else {
		changeImage(101);
	}
}

function activateLinks() {
	$("#thumbs a.thumbnail").click(function(){
		id = parseInt($(this).attr("id").substring(3));
		changeImage(id);
		return false;
	});
}

function changeImage(thumbNum) {
	href = (path_to_hires + image_prefix + thumbNum + ".jpg"); //hires image
	full = (path_to_fullsize + image_prefix + thumbNum + ".jpg"); //fullsize image
	title = titleArray[(thumbNum-101)]; //title description
	$(".highlight").removeClass("highlight"); //remove previous red border
	$("a#img" + thumbNum + " img").addClass("highlight"); //add new red border
	$("#finalImage").fadeOut("fast", function(){
		$(this).attr("src", full); //fullsize image
	}).fadeIn("fast");
	$("#finalLink").attr("href", href); //hires image
	$("#finalDescription").text(title); //title description
	animatedcollapse.show('bigImage');
	currentPic = thumbNum;
	document.location.hash = ("img" + thumbNum);
}