var images;

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function getImages(url,path,title)
{
 	$("#subtitle").text(title);
	$("#work").html("");
	$("#footer").html("");
	$.ajax({
		type: "GET",
        url: url,
        dataType: "xml",
        success: function(xml) {
			var year;
			var i = 1;
			var iplstring = "";
			images = new Array();
			$(xml).find("image").each(function() {
				images[i] = new Object;
				if(year != $(this).find("year").text())
				{
				 	year = $(this).find("year").text();
					var yeardiv = document.createElement("div");
					yeardiv.setAttribute("id","year"+year);
					yeardiv.setAttribute("class","clearfix");
					$("#work").append(year);
					$("#work").append("<br />");
					$("#work").append(yeardiv);
					$("#work").append("<br /><br />");
				}
				images[i].href = path+"/"+$(this).find("file").text();
				images[i].titel = $(this).find("titel").text();
				images[i].untertitel = $(this).find("untertitel").text();
				images[i].format = $(this).find("format").text();
				images[i].year = year;
				images[i].hv = "vertical";
				
				var newidiv = document.createElement("a");
				newidiv.setAttribute("image_id",i);
				newidiv.setAttribute("class","imagethumbnail vertical");
				newidiv.setAttribute("href",images[i].href);
				newidiv.setAttribute("titel",images[i].titel);
				newidiv.setAttribute("untertitel",images[i].untertitel);
				newidiv.setAttribute("format",images[i].format);
				newidiv.setAttribute("year",year);
				var newimage = document.createElement("img");
				newimage.setAttribute("src",path+"/thumbs/"+$(this).find("file").text());
				$(newidiv).append(newimage);
				$("#year"+year).append(newidiv);
				
				i++;
			});
			
			$(".imagethumbnail").click(function(){
				var id = parseInt($(this).attr("image_id"));
				swapImage(id);				
				return false;
			});
			
			$(".imagethumbnail").hover(
				function(){
					$(this).addClass("imagethumbnail_over");
					$("#footer").html($(this).attr("titel"));
				},
				function(){
					$(this).removeClass("imagethumbnail_over");
					$("#footer").html("");
				}
			);
			
			$(images).each(function() {
				$.preloadImages($(this).href);
        	});
		}
    });	
}

function swapImage(id){
	var imlen = images.length;
	$("#work").html(images[id].year+"<br /><br />");
	var newimage = document.createElement("img");
	newimage.setAttribute("src",images[id].href);
	$("#work").append(newimage);
	$("#footer").html(images[id].titel+"<br />"+images[id].untertitel+"<br />"+images[id].format+"<div id='footer-left'></div><div id='footer-right'></div>");
	if(id<10)
		var actualindex = "0"+id;
	else
		var actualindex = ""+id;
	if(imlen<10)
	{
		var totallength	= "0"+imlen;
	}
	else
	{
	 	var totallength	= ""+imlen;
	}
	if(imlen>1)
	{
		if(id == imlen)
		{
			var next = 1;
			var previous = id-1;
		}
		else 
		{
			if(id==1)
			{
				var next = 2;
				var previous = imlen;
			}
			else
			{		
				var next = id+1;
				var previous = id-1;
			}
		}
		$("#footer-right").html("<a class='footerlink' image_id='"+previous+"'><</a><a class='footerlink' image_id='"+next+"'>></a>");
		$(".footerlink").click(function(){
			var id = parseInt($(this).attr("image_id"));
			swapImage(id);				
			return false;
		});
	}
	$("#footer-left").html(actualindex+"/"+totallength);
}












$(function(){
	$("a").hover(function(){
		$(this).addClass("hoverlink");
	},function(){
		$(this).removeClass("hoverlink");		
	});
	$(".submenu").click(function(){
		getImages($(this).attr("data"),"img",$(this).text());
		return false;
	});
	$(".imagethumbnail").hover(
		function(){
			$(this).addClass("imagethumbnail_over");
			$("#footer").html($(this).attr("titel"));
		},
		function(){
			$(this).removeClass("imagethumbnail_over");
			$("#footer").html("");
		}
	);
});
