/*
         ___                         _____       __
        /  /\        ________       /    /\     / /\
(-----//  /  \//----/_____/__\\--) /____/  \___/_/ //--------------------------)
      /  /   /      \     | //     \    \  /    /\/    this code was created by
     /__/   /        \    |///-----\\____\/____/  \                       sigea
(---\\  \  /   (-----\\___|/        /    /\    \  /       
      \__\/                        /____/ /\____\/            __       sigea.ch
                                   \____\/  \  /             /\_\--------------)
                                         \___\//-------------\/_/  

*/
var slider;
function Slider(){
	var selectionID='slider';
	var selectionWrapperID='sliderWrapper';
	var slideTimeout,imageArray,imageCount,imageWidth,imageMargin,imageSpace;
	var leftPos=0;
	//var elementpos=0;
	var interval=40;
	var speed=1;
	this.show=function(element){
		element.src=element.getAttribute('src').replace(/\/medium/i,'/thumb');
		return true;
	}
	this.hidde=function(element){
		element.src=element.getAttribute('src').replace(/\/thumb/i,'/medium');
	}
	this.loop=function(){
//		var left=parseInt(document.getElementById(selectionWrapperID).style.left,10);
		if(-imageSpace>=leftPos){
			var firstChild=document.getElementById(selectionWrapperID).firstChild;
			document.getElementById(selectionWrapperID).removeChild(firstChild);
			document.getElementById(selectionWrapperID).appendChild(firstChild);
			leftPos=0;
		}else{
			leftPos=leftPos-speed;
		}
		document.getElementById(selectionWrapperID).style.left=leftPos+'px';
		slideTimeout=setTimeout('slider.loop();',interval);
	}
	this.start=function(){
		speed=1;
	}
	this.stop=function(){
		speed=0;
	}
	var preloadImages=function(){
		var imgs = document.getElementById(selectionID).getElementsByTagName('img');
		var source;
		imageArray=new Array();
		for ( var i=0; i < imgs.length; i++){
			source = imgs[i].getAttribute('src').replace(/\/medium/i,'/thumb');
			imageArray[i]=new Image();
			imageArray[i].src=source;
		}
	}
	this.prepare=function(){
		if (!document.getElementsByTagName) return false;
		if (!document.getElementById) return false;
		if (!document.getElementById(selectionID)) return false;

		var aTags = document.getElementById(selectionID).getElementsByTagName('a');
		imageWidth=0;
		imageMargin=0;
		imageCount=0;
		for(var i=0; i < aTags.length; i++){
			if(aTags[i].getElementsByTagName('img').length==1){
				aTags[i].onmouseover = function(){
					slider.show(this.getElementsByTagName('img')[0]);
				}
				aTags[i].onmouseout=function(){
					slider.hidde(this.getElementsByTagName('img')[0]);
				}
			}
			imageCount++;
		}
		aTags=null;
		if(imageCount>1){
			imageWidth=parseInt(document.getElementById(selectionID).getElementsByTagName('img')[0].offsetWidth,10);
			imageMargin=parseInt(document.getElementById(selectionID).getElementsByTagName('a')[1].offsetLeft,10)-imageWidth;
			imageSpace=imageWidth+imageMargin;
			document.getElementById(selectionWrapperID).style.width=((imageWidth+imageMargin)*imageCount)+'px';
			document.getElementById(selectionID).onmouseover=function(){
				slider.stop();
			}
			document.getElementById(selectionID).onmouseout=function(){
				slider.start();
			}
			document.getElementById(selectionWrapperID).style.left='0';
			document.getElementById(selectionWrapperID).style.position='absolute';
			slider.loop();
			preloadImages();
		}
		return true;
	}
}
addLoadEvent(function(){
	slider=new Slider();
	slider.prepare();
});
