/* configuration */
var path = "images/061109/";
var nameExtension = ".jpg";
var start = 1;
var end = 1;
/* setup */
var ie = (navigator.appName == "Internet Explorer") ? 1 : 0;
var current = start;

/* jquery */
$(document).ready(function(){
// working on
	$("#galleryBox a").click(function(){
		end = $(this).attr("title");
		path = TMParseGalleryPath( $(this).attr("href") );
		current = start;
		if(!ie) $("#imageBox img").hide();
		TMReplaceImage(path + current + nameExtension);
		TMDisplaySidebarList(path, end);
		return false;
	});


	$("#imageBox .back").click(function(){
		$(".mainContent").removeClass("floatLeft");
		$("#galleryBox").toggle();
		$("#imageBox").toggle();
		$("#mainSidebar").toggle();
		return false;
	});

// for next/previous bit
	$("#imageBox .next").click(function(){
		if(current >= end)
			current = start;
		else
			++current;
		TMReplaceImage(path + current + nameExtension);
		return false;
	});
	$("#imageBox .previous").click(function(){
		if(current <= start)
			current = end;
		else
			--current;
		TMReplaceImage(path + current + nameExtension);
		return false;
	});
});

/* functions */
// replace one image with another
// html: with specified #imageBox containing an image to replace
// css: .loading
// based on: 		
function TMReplaceImage(src){
	var imageBox = $("#imageBox");
	// set min height to current height so page doesn't jump up
	imageBox.css("min-height", imageBox.height()+"px").css("height", imageBox.height()+"px");
	// show loading symbol as set up with CSS loading class
	imageBox.addClass("loading");
	// fadeout animation
	$("#imageBox img").fadeOut("slow").remove();
	var newImage = new Image();
	$(newImage).attr("src", src)
		.load(function(){
			$(newImage).hide();
			imageBox.removeClass("loading").append(newImage);
			// fadein animation
			$(newImage).fadeIn("slow");
			// remove min height so that smaller images will work properly
			imageBox.css("min-height", "400px").css("height", "auto");
			// set height of sidebar
			$("#mainSidebar").css("max-height", $(".mainContent").height()+"px").css("height", $(".mainContent").height()+"px");
	});
}
// given path of file, strip directories and given extension, leaving just name
function TMParseFileName(path, extension){
	var pathParts = path.split("/");
	var pathLastPart = pathParts[pathParts.length - 1];
	var substrLength = pathLastPart.length - extension.length;
	return pathLastPart.substring(0,substrLength);
}

function TMParseGalleryPath(path){
	path = path.split("#");
	path = path[path.length - 1];
	return path;
}


function TMDisplaySidebarList(galleryPath, numberOfImages){
	$(".mainContent").addClass("floatLeft");
	if($("#mainSidebar").size() < 1)
		$("#main").prepend("\t<div id='mainSidebar'></div>");
	TMFillSidebarList(galleryPath, numberOfImages);
// for sidebar imagelist onclick
	$(".imageList a").click(function(){
		var newImagePath = $(this).attr("href");
		current = TMParseFileName(newImagePath, nameExtension);
		TMReplaceImage( newImagePath );
		return false;
	});
	$("#galleryBox").toggle();
	$("#imageBox").toggle();
	$("#mainSidebar").show();
	
}
function TMFillSidebarList(galleryPath, numberOfImages){
	var start = 1;
	var fillText = "<ul class='imageList'>\n";
	for(var i = start; i <= numberOfImages; ++i){
		fillText += "\t<li><a href='" + galleryPath + i + ".jpg'><img src='" + galleryPath + "Thumbs/" + i + ".jpg' width='100px' /></a></li>\n";
	}
	fillText += "</ul>";
	$("#mainSidebar").html(fillText);
}

