/*
	produced by Jan Hagge, 2006
	www.n-try.de
*/
var bigimgID		= "placeholder";	// id="" of the big Image in the htmlsource
var actualopacity	= 100;				// Opacity in percent before js has done anything
var minopacity		= 20;				// Minimal Opacity while loading/fadeanimation
var maxopacity		= 100;				// Maximum Opacity while loading/fadeanimation (tip: take the actualopacity)


var newimg = new Image();
var loaded = false;
var fadein = false;
var interval;
var ie = document.all ? true : false;;

//für Christophe -----------------------------
var thumb;
//--------------------------------------------

function initSmoothImageLoader()
{
	var linklist = document.getElementsByTagName("a");
	for(var i = 0; i < linklist.length; i++)
	{
		var a = linklist[i];
		if (a.getAttribute("href") && (a.getAttribute("rel") == "smoothimageloader"))
		{
			a.onclick = function () {return showPic(this);}
		}
	}
	//für Christophe -----------------------------
	document.getElementById("photogrid").innerHTML += "<div id=\"loading\">Loading &hellip;</div>";
	//--------------------------------------------
}

function showPic(whichpic)
{
	if (document.getElementById)
	{
		//stop the running interval
		if( !isNaN(interval) )
			window.clearInterval(interval);
		//start new interval	
		interval = window.setInterval("toogleFade()", 20);
		//load new image
		loadImage(whichpic.href);
		
		//für Christophe -----------------------------
		thumb = whichpic;
		//--------------------------------------------
		
		return false;
	} else {
		return true;
	}
}

function loadImage(newsrc)
{
	newimg.src = newsrc;
}

function toogleFade()
{	
	if(actualopacity == minopacity)
	{
		loaded = newimg.complete ? true : false;
		document.getElementById("loading").style.display = "block";
		if(loaded)
		{
			document.getElementById(bigimgID).src = newimg.src;
			
			//für Christophe --------------------------------
			var tmplist = document.getElementsByTagName("a");
			for(var i = 0; i < tmplist.length; i++)
				if(tmplist[i].className == "selected")
					tmplist[i].className = "";
			thumb.className = "selected";
			//-----------------------------------------------
		}		
		fadein = true;
	}
	else if(actualopacity == maxopacity)
	{
		if(loaded && fadein)
		{
			window.clearInterval(interval);
		}
		fadein = false;
	}
		
	//neuen Opacitywert setzen
	actualopacity = fadein ? actualopacity+2 : actualopacity-2;
	
	aim = document.getElementById(bigimgID);
	ie ? aim.style.filter = "alpha(opacity="+actualopacity+")" : aim.style.opacity = actualopacity/100;
	
	//für Christophe --------------------------------
	var loader = document.getElementById("loading");
	ie ? loader.style.filter = "alpha(opacity="+(95-actualopacity)+")" : loader.style.opacity = (95-actualopacity)/100;
	//-----------------------------------------------
}

function addLoadFunction(addme)
{	
	var oldonload = window.onload;
	window.onload = typeof window.onload != 'function' ? addme : function(){ oldonload(); addme(); };
}

addLoadFunction(initSmoothImageLoader);