/*
         ___                         _____       __
        /  /\        ________       /    /\     / /\
(-----//  /  \//----/_____/__\\--) /____/  \___/_/ //--------------------------)
      /  /   /      \     | //     \    \  /    /\/    this code was created by
     /__/   /        \    |///-----\\____\/____/  \                       sigea
(---\\  \  /   (-----\\___|/        /    /\    \  /       
      \__\/                        /____/ /\____\/            __       sigea.ch
                                   \____\/  \  /             /\_\--------------)
                                         \___\//-------------\/_/  

*/
var gallery;
function Gallery(){
	var placeholderID='imageplaceholder';
	var selectionID='imageslist';
	var selectionWrapperID='imageslistwrapper';
	var placeholder,imageDiv,source;
	var opacity,timeout,showTimeout,imageArray,imageCount,arraypos,ieFilters;
	var arraypos=0;
	var elementpos=0;
	var showInterval=4200;
	var autoShow=false;
	var wrapperWidth,imageInBlock;
	this.show=function(whichpic,pos){
		if (!document.getElementById(placeholderID)) return false;
		if(pos){
			arraypos=pos;
		}
		if(whichpic){
			autoShow=false;
			source = whichpic.getAttribute('href');
			if(showTimeout){
				clearTimeout(showTimeout);
				//restart diashow
				//showTimeout=setTimeout('gallery.showNextPic();',showInterval);
			}
		}else{
			source = imageArray[arraypos].src;
			if(wrapperWidth){
//				if(arraypos==0){
//					gallery.moveElement(0,0);
//				}else if(arraypos>0 && arraypos%imageInBlock==0){
//					
//				}
				elementpos=Math.floor(arraypos/imageInBlock);
				gallery.moveElement(-(elementpos*wrapperWidth),0);
			}
		}
		//if the images not already loaded
		/*if(!imageArray[arraypos].loaded){
			imageArray[arraypos].onload=function(){
				this.loaded=true;
				gallery.show();
			}
			return true;
		}*/
		if(document.getElementById('imgBackForward'))
			prepareBackForward();
		placeholder = document.getElementById(placeholderID);
		//fadeImage=placeholder.getElementsByTagName('img')[0];
		placeholder.style.backgroundImage='url('+source+')';
		opacity=10;
		if(ieFilters){
			gallery.ieFadeOut();
//		}else if(fadeImage.style.opacity){
//			gallery.fadeOut();
		}else{
//			gallery.imgSwitch();
			gallery.fadeOut();
		}
		var links = document.getElementById(selectionID).getElementsByTagName('a');
		for ( var i=0; i < links.length; i++){
			removeClass(links[i],'active');
			if(links[i].href.indexOf(source)>=0)
				addClass(links[i],'active');
		}
		if(document.getElementById('caption')){
			var caption = document.getElementById('caption');
			while(caption.hasChildNodes()){
				caption.removeChild(caption.lastChild);
			}
			caption.appendChild(document.createTextNode(imageArray[pos]['title']));
		}
		return true;
	}
	var startShow=function(){
		autoShow=true;
		showTimeout=setTimeout('gallery.showNextPic();',3600);
	}
	this.showNextPic=function(){
		if(showTimeout){
			clearTimeout(showTimeout);
		}
		arraypos++;
		if(arraypos>=imageCount){
			arraypos=0;
		}
		gallery.show(null,arraypos);
		showTimeout=setTimeout('gallery.showNextPic();',showInterval);
	}
	this.showNextElement=function(){
		if(showTimeout)
			clearTimeout(showTimeout);
		autoShow=false;
		if(wrapperWidth){
			elementpos++;
			gallery.moveElement(-(elementpos*wrapperWidth),0);
			if(document.getElementById('imgBackForward'))
				prepareBackForward();
		}else{
			arraypos++;
			if(arraypos>=imageCount){
				arraypos=0;
			}
			gallery.show(null,arraypos);
		}
	}
	this.showPreviousElement=function(){
		if(showTimeout)
			clearTimeout(showTimeout);
		autoShow=false;
		if(wrapperWidth){
			elementpos--;
			gallery.moveElement(-(elementpos*wrapperWidth),0);
			if(document.getElementById('imgBackForward'))
				prepareBackForward();
		}else{
			arraypos--;
			if(arraypos<0){
				arraypos=imageCount-1;
			}
			gallery.show(null,arraypos);
		}
	}
	this.fadeOut=function(){
		var interval=40;
		if(timeout){
			clearTimeout(timeout);
		}
		if(opacity<=0){
			gallery.imgSwitch();
		}else{
			opacity--;
			imageDiv.style.opacity=opacity/10;
			timeout=setTimeout('gallery.fadeOut();',interval);
		}
	}
	this.ieFadeOut=function(){
		var interval=1000
		imageDiv.style.filter='progid:DXImageTransform.Microsoft.Fade(duration='+interval/1000+')';
		imageDiv.filters[0].Play();
		setTimeout('gallery.imgSwitch();',interval);
/*		if(timeout){
			clearTimeout(timeout);
		}
		if(opacity<=0){
			gallery.imgSwitch();
		}else{
			opacity--;
//			alert(opacity*10);
			fadeImage.style.filter='alpha(opacity='+opacity*10+')';
			timeout=setTimeout('gallery.ieFadeOut();',interval);
		}
		*/
	}
	this.imgSwitch=function(){
		imageDiv.style.backgroundImage='url('+source+')';
		if(imageDiv.style.filter){
			imageDiv.style.filter='alpha(opacity=100)';
		}else if(imageDiv.style.opacity){
			imageDiv.style.opacity=1;
		}
		placeholder.style.backgroundImage='none';
		var aTags=placeholder.getElementsByTagName('a');
		if(aTags.length>0){
			aTags[0].href=aTags[0].href.replace(/(\?|&)bID=(\d)+/i,'');
			var seperator='?';
			if(aTags[0].href.indexOf('?') != -1)
				seperator='&';
			aTags[0].href+=seperator+'bID='+arraypos;
		}
		aTags=null;
	}
	var preloadImages=function(){
		var links = document.getElementById(selectionID).getElementsByTagName('a');
		var source;
		imageArray=new Array();
		for ( var i=0; i < links.length; i++){
			source = links[i].getAttribute("href");
			imageArray[i]=new Image();
			imageArray[i].src=source;
			imageArray[i].title=links[i].title;
			imageArray[i].loaded=false;
			imageArray[i].onload=function(){
				this.loaded=true;
			}
		}
		if(imageArray.length>0){
			startShow();
		}
	}
	//domscripting by Jeremy Keith (domscripting.com)
	this.moveElement=function(finalX,finalY) {
		var element = document.getElementById(selectionID);
		if (element.movement) {
			clearTimeout(element.movement);
		}
		if(!element.style.left) {
			element.style.left = "0px";
		}
		if(!element.style.top) {
			element.style.top = "0px";
		}
		var xpos = parseInt(element.style.left);
		var ypos = parseInt(element.style.top);
		if(xpos == finalX && ypos == finalY) {
			return true;
		}
		if(xpos < finalX) {
			var dist = Math.ceil((finalX - xpos)/10);
			xpos = xpos + dist;
		}
		if(xpos > finalX) {
			var dist = Math.ceil((xpos - finalX)/10);
			xpos = xpos - dist;
		}
		if(ypos < finalY) {
			var dist = Math.ceil((finalY - ypos)/10);
			ypos = ypos + dist;
		}
		if(ypos > finalY) {
			var dist = Math.ceil((ypos - finalY)/10);
			ypos = ypos - dist;
		}
		element.style.left = xpos + "px";
		element.style.top = ypos + "px";
		var repeat = 'gallery.moveElement('+finalX+','+finalY+')';
		element.movement = setTimeout(repeat,5);
	}
	var prepareBackForward=function(){
		var spans=document.getElementById('imgBackForward').getElementsByTagName('span');
		if(wrapperWidth){
			if(elementpos==0){
				removeClass(spans[0],'active');
				spans[0].onclick=null;
			}else if(elementpos>0){
				addClass(spans[0],'active');
				spans[0].onclick=function(){
					gallery.showPreviousElement();
				}
			}
			if(elementpos>=imageCount/imageInBlock-1){
				removeClass(spans[1],'active');
				spans[1].onclick=null;
			}else if(elementpos<Math.floor(imageCount/imageInBlock)){
				addClass(spans[1],'active');
				spans[1].onclick=function(){
					gallery.showNextElement();
				}
			}
		}else{
			if(arraypos==0){
				removeClass(spans[0],'active');
				spans[0].onclick=null;
			}else if(arraypos>0){
				addClass(spans[0],'active');
				spans[0].onclick=function(){
					gallery.showPreviousElement();
				}
			}
			if(arraypos>=imageCount-1){
				removeClass(spans[1],'active');
				spans[1].onclick=null;
			}else if(arraypos<imageCount){
				addClass(spans[1],'active');
				spans[1].onclick=function(){
					gallery.showNextElement();
				}
			}
		}
	}
	this.prepare=function(){
		if (!document.getElementsByTagName) return false;
		if (!document.getElementById) return false;
		if (!document.getElementById(placeholderID)) return false;
		var placeholder=document.getElementById(placeholderID);
		if (!document.getElementById(selectionID)) return false;
		placeholder.style.backgroundRepeat='no-repeat';
		placeholder.style.backgroundPosition='center center';
		
		var ulTags=document.getElementById(selectionID).getElementsByTagName('ul');
		if(ulTags.length>0 && document.getElementById(selectionWrapperID)){
			imageInBlock=ulTags[0].getElementsByTagName('li').length;
			document.getElementById(selectionWrapperID).style.overflow='hidden';
			wrapperWidth=document.getElementById(selectionID).offsetWidth;
			for( var i=0; i < ulTags.length; i++){
				ulTags[i].parentNode.style.position='absolute';
				ulTags[i].parentNode.style.top='0px';
				ulTags[i].parentNode.style.left=(i*wrapperWidth)+'px';
				removeClass(ulTags[i].parentNode,'noscript');
			}
		}
		ulTags = null;
		//document.getElementById(placeholderID).getElementsByTagName('img')[0].setAttribute('style','opacity:1.0; filter:alpha(opacity=100)');
		var links = document.getElementById(selectionID).getElementsByTagName('a');
		imageCount=links.length;
		for(var i=0; i < links.length; i++){
			links[i].arg=i;
			links[i].onclick = function(){
				return !gallery.show(this,this.arg);
			}
			links[i].onkeypress = links[i].onclick;
			if(links[i].className.indexOf('active')>=0){
				arraypos=i;
			}
		}
		links=null;
		if(document.getElementById(placeholderID).getElementsByTagName('img')[0].filters)
			ieFilters=true;
		else
			ieFilters=false;
		imageDiv=document.createElement('div');
		imageDiv.style.backgroundRepeat='no-repeat';
		imageDiv.style.width='100%';
		imageDiv.style.height='100%';
		imageDiv.style.backgroundPosition='center center';
		imageDiv.style.backgroundImage='url('+placeholder.getElementsByTagName('img')[0].src+')';
		placeholder.replaceChild(imageDiv,placeholder.getElementsByTagName('img')[0]);
		
		if(document.getElementById('imgBackForward')){
			if(document.getElementById('imgBackForward').getElementsByTagName('a').length>0){
				var spanTags=document.getElementById('imgBackForward').getElementsByTagName('span');
				for(var i=0; i < spanTags.length; i++){
					if(spanTags[i].getElementsByTagName('a').length>0)
						spanTags[i].replaceChild(spanTags[i].getElementsByTagName('a')[0].lastChild,spanTags[i].getElementsByTagName('a')[0]);
				}
			}
			prepareBackForward();
		}
		preloadImages();
		return true;
	}
}
function prepareGallery(){
	gallery=new Gallery();
	gallery.prepare();
}
addLoadEvent(prepareGallery);
