var load_next = true;
var x_target = 0;
var animation_interval = null;

function click_image()
{
	if (!$(this).hasClass("invisible")) return false;
	
	window.clearInterval(animation_interval);
	
	$("#scroll-images div div a:not(.invisible)")
		.click(click_image)
		.hover(rollover_image, rollout_image)
		.mousemove(rollover_image)
		.toggleClass("invisible");
	
	$(this)
		.click(
			function()
			{
				return false;
			}
		)
		.unbind("mouseenter")
		.unbind("mouseleave")
		.unbind("mousemove")
		.toggleClass("invisible")
		.removeClass("opacity50");
	
	var id = $(this).attr("id").split("-");
	if (location.hash != id[1]) location.hash = id[1];
	
	var x = parseInt($(this).parent().css("left").replace("px","")) + Math.round($(this).width()/2);
	x_target = -x;
	
	animation_interval = window.setInterval("animate()",25);
	
	return false;
}

function animate()
{
	var e = document.getElementById("scroll-images");
	var x = parseInt(e.style.left.replace("px",""));
	var x_diff = x_target - x;
	e.style.left = (x + (x_diff/4)) + "px";

	if (Math.round(x_diff) == 0) {
		window.clearInterval(animation_interval);
	}
}

function load_next_image()
{
	if (load_next)
	{
		if ($("#scroll-images div div:has(a.loaded):last").next().length > 0)
		{
			$("#scroll-images div div:has(a.loaded):last")
				.next()
				.children("a")
				.each(
					function(i)
					{
						var image_url = $(this).attr("href");
						$(this)
							.children("img")
							.attr("src", image_url)
							.load(load_next_image);
						$(this)
							.addClass("loaded");
					}
				);
				
			if ($("#scroll-images div div:has(a.loaded):first").prevAll().length > 0) load_next = false;
		}
		else
		{
			load_next = false;
			load_next_image();
		}
	}
	else
	{
		$("#scroll-images div div:has(a.loaded):first")
			.prev()
			.children("a")
			.each(
				function(i)
				{
					var image_url = $(this).attr("href");
					$(this)
						.children("img")
						.attr("src", image_url)
						.load(load_next_image);
					$(this)
						.addClass("loaded");
				}
			);
		
		if ($("#scroll-images div div:has(a.loaded):last").nextAll().length > 0) load_next = true;
	}
}

function rollover_image()
{
	$(this).addClass("opacity50");
}

function rollout_image()
{
	$(this).removeClass("opacity50");
}

function splash_image_resize()
{
	var splash_image = $("#splash img");
	
	var w = splash_image.attr("width");
	var h = splash_image.attr("height");
	var r = w/h;
	
	var window_w = $(window).width();
	var window_h = $(window).height();
	var window_r = window_w/window_h;

	var new_w; var new_h;
	if (window_r >= r)
	{
		new_w = window_w;
		new_h = window_w/r;
	}
	else
	{
		new_w = window_h*r;
		new_h = window_h;
	}
	
	splash_image
		.attr("width", new_w)
		.attr("height", new_h);
}

$(document).ready(
	function()
	{
		var n = (location.hash) ? parseInt(location.hash.replace("#","")) : 1;
		
		if (jQuery.browser.msie)
		{
			$("img[src$='.png']").each(function()
			{
				this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src="+this.src+")";
				//$(this).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+  $(this).attr('src') + '")');
				$(this).attr('src', 'images/empty.gif');
			});
		}
		
		$("#thumbnails a")
			.hover(
				function() 
				{
					$("#subheadings img:not(.hidden)").toggleClass("hidden");
					var id = $(this).attr("class");
					if (id) $("#" + id).toggleClass("hidden");
				},
				function() 
				{
					var id = $(this).attr("class");
					if (id) $("#" + id).toggleClass("hidden");
				}
			)
			.each(
				function()
				{
					if (screen.width >= 1400) {
						var href = $(this).attr("href").replace("/m/", "/l/");
						$(this).attr("href", href);
					}
				}
			)
			
		$("#scroll-images").css("display", "block");
			
		$("#scroll-images div div a")
			.click(click_image)
			.hover(rollover_image, rollout_image)
			.mousemove(rollover_image);
			
		$("#js-"+n).each(
			function(i)
			{
				var image_url = $(this).attr("href");
				$(this)
					.children("img")
					.attr("src",image_url)
					.load(load_next_image);
				var img = new Image();
				$(this)
					.addClass("loaded")
					.trigger("click");
			}
		);
		
		if ($("body").hasClass("splash"))
		{
			if (!$(document).attr('compatMode'))
			{
				// Work around jQuery/Safari 3 bug http://dev.jquery.com/ticket/4638
				$(document).attr('compatMode', 'CSS1Compat');
			}
			splash_image_resize();
			$(window).resize(splash_image_resize);
		}
		else if ($("body").hasClass("contact"))
		{
			if (GBrowserIsCompatible())
			{
				var point = new GLatLng(51.52327, -0.10963);
				var map = new GMap2(document.getElementById("map"));
				map.setCenter(point, 16);
				map.setUIToDefault();
				map.addOverlay(new GMarker(point));
			}
		}
	}
);