/*

PHOTO MORPHER v1.0beta (c) 2004-2005 Angus Turnbull, http://www.twinhelix.com
Altering this notice or redistributing this file is prohibited.

*/

//

function addEvent(o, n, f, l) { var a = 'addEventListener', h = 'on'+n; if (o[a] && !l) { return o[a](n, f, false); } if (o[h]) {  o._c |= 0;  var b = '_b' + (++o._c); o[b] = o[h]; } o[h] = function(e) {  e = e || self.event;  var r = true; if (o[b]) r = o[b](e) != false && r; o._f = f; r = o._f(e) != false && r; return r; }};

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;
this.caption=null
this.captionText=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;
		caption=document.getElementById('captionArea');
		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'));
				//addEvent(list.childNodes[i],'mouseover',new Function('e',myName+'.hover(this);if(e.stopPropagation)e.stopPropagation();if(e.cancelable&&e.preventDefault)e.preventDefault();e.returnValue=false;return false'));
				//addEvent(list.childNodes[i],'mouseout',new Function('e',myName+'.unhover(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{}; if (!link.title) { link.title=link.getAttribute('href'); link.setAttribute('href','javascript://'); } var w,h,src=link.title;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.captionize=function() {
	this.caption.innerHTML=this.captionText;
}

PhotoMorpher.prototype.getItemCaption=function(item){
	with(this){
		var thumb='&nbsp; &nbsp;',thumbs=item.getElementsByTagName('img');
		for(var i=0;i<thumbs.length;i++){
			if(thumbs[i].getAttribute('longdesc')) {thumb=thumbs[i].getAttribute('longdesc'); break;}
		}
		return thumb;
	}};

PhotoMorpher.prototype.hover=function(thisItem) {
	if (thisItem.className!='photoItemActive') { thisItem.className='hover' }
};

PhotoMorpher.prototype.unhover=function(thisItem) {
	if (thisItem.className=='hover') { thisItem.className='' }
};

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;
		if (!navigator.vendor||navigator.vendor.indexOf('Apple')<0) { 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; //alert(image.src); alert(item.src);
		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; //alert('here2');
		captionText = getItemCaption(currentItem);
	}
};


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()}};