
// http://jehiah.com/archive/simple-swap

function SimpleSwap(el,which){
  el.src=el.getAttribute(which || "origsrc");
}

function SimpleSwapSetup(){
  var x = document.getElementsByTagName("img");
  for (var i=0;i<x.length;i++){
    var oversrc = x[i].getAttribute("oversrc");
    if (!oversrc) continue;
      
    // preload image
    // comment the next two lines to disable image pre-loading
    x[i].oversrc_img = new Image();
    x[i].oversrc_img.src=oversrc;
    // set event handlers
    x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
    x[i].onmouseout = new Function("SimpleSwap(this);");
    // save original src
    x[i].setAttribute("origsrc",x[i].src);
  }
}

var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup();}





// Java to support jquery-1.2.3.pack


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];
	
	// Basis of movements position 100 left edge 2 middle 1 right edge
	var x = parseInt($(this).parent().css("left").replace("px","")) + Math.round($(this).width()/101);
	x_target = -x;
	
	animation_interval = window.setInterval("animate()",50);
	
	return false;
}

function animate()
{
	// JQuery is too slow
	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");
}

$(document).ready(
	function()
	{
		var n = (location.hash) ? parseInt(location.hash.replace("#","")) : 1;
		
		$("#thumbnails a")
			.hover(
				function() 
				{
					$("#subheadings img:not(.hidden)").toggleClass("hidden");
					var id = $(this).attr("class");
					$("#" + id).toggleClass("hidden");
				},
				function() 
				{
					var id = $(this).attr("class");
					$("#" + 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");
			}
		)
		
	}
);



