var sliptimer;
var marginToReinstate;
var widthToReinstate;
var imageMode = "normal";

$(document).ready(function() {
	
	var timer, slipTimer;
	
	/* Menu */
	$("#top p a").css("opacity",0);
	$("#top p#nav").append('<a href="Show Menu" onclick="fnShowMenu();return false;" id="menuBtn"><img src="/inc/menuBtn.gif" alt="" /></a>');
	
	/* Thumbnails */	
	$(".thumb").hover(
		function(){
			clearTimeout(timer);
			$("#sub").html("<span>" + $("img",this).attr("alt") + "</span>");			
		},
		function(){
			timer = setTimeout("hideH3()",200);			
		})
	
	/* Large Images */	
	$("#slip").hide();
	$("#slip").append('<a href="#" onclick="closeSlip();return false" id="close"><img src="/inc/close.gif" alt="" /></a>');
	
	$("#large").click(
		function(){
			if(imageMode=="normal") {
				
				marginToReinstate 	= parseInt($(this).css("margin-left").split("px")[0]);
				widthToReinstate	= parseInt($(this).css("width").split("px")[0]);
	
				if(marginToReinstate < 310) {
					if(widthToReinstate > 600) {
						$("#large").animate({width:"600px",marginLeft:"0px"},function() {
							$("#slip").fadeIn(500);
						})
					} else {
						$("#large").animate({marginLeft: (910-310-widthToReinstate)+"px"},function() {
							$("#slip").fadeIn(500);
						})	
					}
					imageMode			= "shrunk";
				} else {
					$("#slip").fadeIn(500);
					imageMode			= "normal";
				}
			}			
		}
	);


	$('#slideshow').cycle({ 
	    fx:  	'fade', 
		timeout: 5000,
	 	next:   '#slideshow',
		before:  slipPopulate
	});
	
	$("#slideshow img,#slip").hover(
		function() {
			$('#slideshow').cycle('pause');
		},
		function() {
			$('#slideshow').cycle('resume');
		}
	);
	

	
})


function slipPopulate() {
	bits = this.alt.split("/");
	out = '<h2>'+bits[0] +'</h2><table border="0"><tr><th>Dimension.</th><td>'+bits[1] +'</td></tr><tr><th>Material.</th><td>'+bits[2] +'</td></tr>';
		if(bits[3] != "0000") {
			out += '<tr><th>Date.</th><td>'+bits[3] +'</td></tr>';
		}			
	out += '</table><a href="'+bits[4] +'" id="back">'+bits[4] +'</a>'
	
	$("#slip").hide().html(out);
} 

function hideH3() {
	$("#sub span").fadeOut();
}

function closeSlip() {
	if(imageMode == "shrunk") {
		if(widthToReinstate > 600) {
			$("#slip").fadeOut(200, function() {
				$("#large").animate({width:widthToReinstate+"px",marginLeft:marginToReinstate+"px"})		
			});
		} else {
			$("#slip").fadeOut(200, function() {
				$("#large").animate({marginLeft:marginToReinstate+"px"})		
			});	
		}		
	} else {
		$("#slip").fadeOut();
	}
	imageMode = "normal";
}


function fnShowMenu() {
	var i 			= 6;
	var animSpeed 	= 400;
	$("#menuBtn").fadeOut(function(){
		$("#top p a:not('#menuBtn')").each(function() {
			$(this).animate({opacity:1},animSpeed*i).fadeIn("fast"); 
			i--;	
		});
	});
}