/*

PHOTO MORPHER v1.0beta (c) 2004-2006 Angus Turnbull, http://www.twinhelix.com
Altering this notice or redistributing this file is prohibited.

*/

if(typeof addEvent!='function')
{	var addEvent=function(o,t,f,l)
	{var d='addEventListener',n='on'+t,rO=o,rT=t,rF=f,rL=l;if(o[d]&&!l)
		return o[d](t,f,false);
		if(!o._evts)o._evts={};
	if(!o._evts[t])
	{
		o._evts[t]=o[n]?{b:o[n]}:{};o[n]=new Function('e','var r=true,o=this,a=o._evts["'+t+'"],i;for(i in a){o._f=a[i];r=o._f(e||window.event)!=false&&r;o._f=null}return r');
		if(t!='unload')
		addEvent(window,'unload',function(){removeEvent(rO,rT,rF,rL)})

	}
	if(!f._i)
		f._i=addEvent._i++;o._evts[t][f._i]=f};
	addEvent._i=1;var removeEvent=function(o,t,f,l)
	{
		var d='removeEventListener';
		if(o[d]&&!l)
		return o[d](t,f,false);
		if(o._evts&&o._evts[t]&&f._i)
		delete o._evts[t][f._i]
	}
}
function PhotoMorpher(myName,areaID,listID,itemClass,applyClasses)
{
	this.myName=myName;
	this.areaID=areaID;
	this.listID=listID;
	this.itemClass=itemClass;
	this.applyClasses=applyClasses;
	this.list=null;
	this.area=null;
	this.images=[];
	this.activeImage=0;
	this.currentItem=null;
	this.loaded=false;
	this.loading=false;
	this.fadeSteps=5;
	this.fadeVal=0;
	this.fadeTimer=null;
	this.autoPlay=false;
	this.autoDelay=10000;
	this.autoPreload=true;
	this.autoTimer=null;
	if(document.getElementById&&document.createElement&&document.documentElement)
		addEvent(window,'load',new Function(myName+'.init()'))
};
PhotoMorpher.prototype.init=function()
{
	with(this)
	{
		list=document.getElementById(listID);
		area=document.getElementById(areaID);
		if(!list||!area)
			return;
		var container=document.createElement('div');
		container.style.position='relative';
		area.appendChild(container);
		images[0]=document.createElement('img');
		images[0].style.position='relative';
		images[0].style.zIndex=2;
		container.appendChild(images[0]);
		images[1]=document.createElement('img');
		images[1].style.position='absolute';
		images[1].style.left=images[1].style.top='0px';
		images[1].style.zIndex=1;container.appendChild(images[1]);
		for(var i=0;i<2;i++)
		{
			images[i]._pgWidth=images[i]._pgHeight=-1;images[i]._pgWidth=images[i]._pgHeight=-1;
			addEvent(images[i],'load',new Function('clearTimeout('+myName+'.fadeTimer);'+myName+'.fadeTimer=setInterval("'+myName+'.fade(0)",100)'));
			if(images[i].filters)
				images[i].style.filter+='alpha(opacity=0)';
			else
				images[i].style.opacity=images[i].style.MozOpacity=0
		}
		for(var i=0;i<list.childNodes.length;i++)
		{
			if(list.childNodes[i].nodeName.toLowerCase()=='li')
			{
				addEvent(list.childNodes[i],'click',new Function('e',myName+'.display(this);if(e.stopPropagation)e.stopPropagation();if(e.cancelable&&e.preventDefault)e.preventDefault();e.returnValue=false;return false'))
			}

		}
		for(var id in applyClasses)
		{
			var elm=document.getElementById(id);
			elm.className+=(elm.className?' ':'')+applyClasses[id]
		}
		loaded=true;
		if(this.onsetup)
			this.onsetup();
		display(list.getElementsByTagName('li')[0])
	}
};
PhotoMorpher.prototype.getItemDetails=function(item)
{
				with(this)
				{
					var link,links=item.getElementsByTagName('a');
					for(var i=0;i<links.length;i++)
					{
						if(links[i].href&&links[i].id)
						{
							link=links[i];break
						}
					}
					if(!link)
						return{};
					var w,h,src=link.getAttribute('href');
					if(link.id.match(/(\d+)x(\d+)/))
						w=parseInt(RegExp.$1),h=parseInt(RegExp.$2);
					if(!src||!w||!h)
						return{};
					return{'src':src,'width':w,'height':h}
				}
};
PhotoMorpher.prototype.display=function(newItem)
{
	with(this)
	{
		if(!newItem||!loaded||loading)
			return;
		var isVeryFirst=currentItem?0:1;
		var item=this.getItemDetails(newItem);
		if(item.src==images[activeImage].src)
			return;
		loading=true;
		activeImage=1-activeImage;
		clearTimeout(autoTimer);
		if(itemClass)
		{
			if(currentItem)
					currentItem.className=currentItem.className.replace(new RegExp('\\s*'+itemClass+'$'),'');
			newItem.className+=(newItem.className?' ':'')+itemClass}currentItem=newItem;
			var image=images[activeImage];
			image._pgWidth=item.width;
			image._pgHeight=item.height;
			if(isVeryFirst)
			{
				area.style.width=(images[0]._pgWidth=item.width)+'px';
				area.style.height=(images[0]._pgHeight=item.height)+'px'
			}
			if(this.onbeforeload)
					this.onbeforeload();
			image.src=item.src
		}
};
PhotoMorpher.prototype.fade=function()
{
	with(this)
	{
		var newI=images[activeImage];
		var oldI=images[1-activeImage];
		if(!fadeVal&&this.onload)
					this.onload();
		if(fadeVal>100)
		{
			clearTimeout(fadeTimer);
			fadeVal=0;
			loading=false;
			if(oldI.filters&&oldI.filters.length&&oldI.filters.alpha)
			{
					newI.filters.alpha.enabled=false;oldI.filters.alpha.opacity=0
			}
			else
				oldI.style.opacity=oldI.style.MozOpacity=0;
			if(autoPlay)
			{
				autoTimer=setTimeout(myName+'.next()',autoDelay);
				if(autoPreload)
				{
						var nextItem=currentItem;
						do
						{
							nextItem=nextItem.nextSibling||nextItem.parentNode.firstChild
						}while(nextItem.nodeType!=1);
						this.autoPreloadImage=new Image();
						autoPreloadImage.src=this.getItemDetails(nextItem).src

				}
			}return
		}
		if(!fadeVal||(oldI._pgWidth!=newI._pgWidth)||(oldI._pgHeight!=newI._pgHeight))
		{
			var newWidth=parseInt(oldI._pgWidth+fadeVal*(newI._pgWidth-oldI._pgWidth)/100);
			newI.style.width=oldI.style.width=newWidth+'px';
			area.style.width=newWidth+'px';
			var newHeight=parseInt(oldI._pgHeight+fadeVal*(newI._pgHeight-oldI._pgHeight)/100);
			newI.style.height=oldI.style.height=newHeight+'px';area.style.height=newHeight+'px'
		}
		if(newI.filters&&newI.filters.length&&newI.filters.alpha)
		{
			if(!newI.filters.alpha.enabled)
					newI.filters.alpha.enabled=true;
			newI.filters.alpha.opacity=parseInt(fadeVal)
		}
		else
			newI.style.opacity=newI.style.MozOpacity=fadeVal/100.1;newI.style.zIndex=2;
		oldI.style.zIndex=1;
		fadeVal+=100/fadeSteps
	}
};
PhotoMorpher.prototype.next=function()
{
	with(this)
	{
		if(!loaded)
			return;
		var newItem=currentItem;
		do
		{
			newItem=newItem.nextSibling||newItem.parentNode.firstChild
		}while(newItem.nodeType!=1);
		display(newItem)
		}
};
PhotoMorpher.prototype.prev=function()
{
	with(this)
	{
		if(!loaded)
			return;
		var newItem=currentItem;
		do
		{
			newItem=newItem.previousSibling||newItem.parentNode.lastChild
		}while(newItem.nodeType!=1);
				display(newItem)
	}
};
PhotoMorpher.prototype.play=function(timer)
{
	with(this)
	{
		if(timer)autoDelay=timer;
			autoPlay=!autoPlay;
		if(!autoPlay)
			clearTimeout(autoTimer);
		else next()
	}
};