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


//Globals - better dont touch anything below
var newimg = new Image();
var loaded = false;
var fadein = false;
var interval;
var ie = document.all ? true : false;
var thumb;

function initSmoothImageLoader()
{
	var linklist = document.getElementById(galleryID).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);}
		}
	}
}

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);
		//set clicked object as global thumb variable
		thumb = whichpic;
		
		return false;
	} else {
		return true;
	}
}

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

function toogleFade()
{	
	if(actualopacity == minopacity)
	{
		loaded = newimg.complete ? true : false;
		if(loaded)
		{
			document.getElementById(bigimgID).src = newimg.src;
			
			//set thumbnail as selected
			var tmplist = document.getElementById(galleryID).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;
	}
		
	//set new opacity
	actualopacity = fadein ? actualopacity+2 : actualopacity-2;
	
	aim = document.getElementById(bigimgID);
	ie ? aim.style.filter = "alpha(opacity="+actualopacity+")" : aim.style.opacity = actualopacity/100;
}

function bi(img)
{
	document.getElementById('bigimg').src = "/img/"+img+".jpg";
}

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

addLoadFunction(initSmoothImageLoader);
