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

*/
var slider;
function Slider(){
	var className='slider';
	var selectionID='feature';
	var contentID='maincontent';
	//var selectionWrapperID='featurewrapper';
	var sliderArray=[];
	var placeholder,imageDiv,source;
	var opacity,timeout,showTimeout,elementCount,ieFilters;
	var arraycount=0;
	var elementpos=0;
	var showInterval=4200;
	var autoShow=false;
	var wrapperWidth;
	this.showNextElement=function(no){
		sliderArray[no]['pos']++;
		if(sliderArray[no]['pos']>=sliderArray[no]['count']){
			sliderArray[no]['pos']=0;
		}
		slider.moveElement(no,-(sliderArray[no]['pos']*sliderArray[no]['width']),0);
		//if(document.getElementById('imgBackForward')){
			prepareBackForward(no);
		//}
		if(autoShow && no==0){
			if(showTimeout){
				clearTimeout(showTimeout);
			}
			showTimeout=setTimeout('slider.showNextElement(0)',showInterval);
		}
	}
	this.showPreviousElement=function(no){
		if(autoShow && no==0){
			if(showTimeout){
				clearTimeout(showTimeout);
			}
		}
		sliderArray[no]['pos']--;
		slider.moveElement(no,-(sliderArray[no]['pos']*sliderArray[no]['width']),0);
		//if(document.getElementById('imgBackForward'))
			prepareBackForward(no);
	}
	//domscripting by Jeremy Keith (domscripting.com)
	this.moveElement=function(no,finalX,finalY) {
		var element = sliderArray[no]['slide'];
		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 = 'slider.moveElement('+no+','+finalX+','+finalY+')';
		element.movement = setTimeout(repeat,5);
		return true;
	}
	var prepareBackForward=function(no){
		//var spans=document.getElementById('imgBackForward').getElementsByTagName('span');
		if(sliderArray[no]['pos']==0){
			removeClass(sliderArray[no]['back'],'active');
			addClass(sliderArray[no]['back'],'inactive');
			sliderArray[no]['back'].onclick=null;
		}else if(sliderArray[no]['pos']>0){
			removeClass(sliderArray[no]['back'],'inactive');
			addClass(sliderArray[no]['back'],'active');
			sliderArray[no]['back'].onclick=function(){
				slider.showPreviousElement(this.no);
			}
		}
		if(sliderArray[no]['pos']>=sliderArray[no]['count']-1){
			removeClass(sliderArray[no]['forward'],'active');
			addClass(sliderArray[no]['forward'],'inactive');
			sliderArray[no]['forward'].onclick=null;
		}else if(sliderArray[no]['pos']<sliderArray[no]['count']){
			removeClass(sliderArray[no]['forward'],'inactive');
			addClass(sliderArray[no]['forward'],'active');
			sliderArray[no]['forward'].onclick=function(){
				slider.showNextElement(this.no);
			}
		}
	}
	this.start=function(){
		if(showTimeout){
			clearTimeout(showTimeout);
		}
		showTimeout=setTimeout('slider.showNextElement(0)',showInterval);
	}
	this.stop=function(){
		if(showTimeout){
			clearTimeout(showTimeout);
		}
	}
	var prepareSlider=function(element){
		sliderArray[arraycount]={};
		sliderArray[arraycount]['wrapper']=element;
		if(autoShow && arraycount==0){
			sliderArray[arraycount]['slide']=sliderArray[arraycount]['wrapper'].getElementsByTagName('div')[1];
			sliderArray[arraycount]['wrapper'].onmouseover=function(){
				slider.stop();
			}
			sliderArray[arraycount]['wrapper'].onmouseout=function(){
				slider.start();
			}
		}else{
			sliderArray[arraycount]['slide']=sliderArray[arraycount]['wrapper'].getElementsByTagName('ul')[0];
		}
		wrapperWidth=sliderArray[arraycount]['wrapper'].offsetWidth;
		sliderArray[arraycount]['count']=0;
		var sibling=sliderArray[arraycount]['slide'].firstChild;
		var position=0;
		var elementCount=0;
		while(sibling != null){
			if(sibling.nodeType===1){
				sibling.style.position='absolute';
				sibling.style.top='0px';
				sibling.style.left=position+'px';
				position+=sibling.offsetWidth;
				elementCount++;
			}
			sibling=sibling.nextSibling;
		}
		sliderArray[arraycount]['slide'].style.position='absolute';
		sliderArray[arraycount]['slide'].style.width=position+'px';
		sliderArray[arraycount]['slide'].style.height=sliderArray[arraycount]['wrapper'].offsetHeight+'px';
		sliderArray[arraycount]['back']=document.createElement('div');
		sliderArray[arraycount]['back'].no=arraycount;
		addClass(sliderArray[arraycount]['back'],'slideback');
		var divTag=document.createElement('div');
		addClass(divTag,'i');
		sliderArray[arraycount]['back'].appendChild(divTag);
		divTag=document.createElement('div');
		addClass(divTag,'a');
		sliderArray[arraycount]['back'].appendChild(divTag);
		divTag=document.createElement('div');
		addClass(divTag,'h');
		sliderArray[arraycount]['back'].appendChild(divTag);
		sliderArray[arraycount]['wrapper'].appendChild(sliderArray[arraycount]['back']);
		sliderArray[arraycount]['forward']=document.createElement('div');
		sliderArray[arraycount]['forward'].no=arraycount;
		addClass(sliderArray[arraycount]['forward'],'slideforward');
		divTag=document.createElement('div');
		addClass(divTag,'i');
		sliderArray[arraycount]['forward'].appendChild(divTag);
		divTag=document.createElement('div');
		addClass(divTag,'a');
		sliderArray[arraycount]['forward'].appendChild(divTag);
		divTag=document.createElement('div');
		addClass(divTag,'h');
		sliderArray[arraycount]['forward'].appendChild(divTag);
		sliderArray[arraycount]['wrapper'].appendChild(sliderArray[arraycount]['forward']);
		
		sliderArray[arraycount]['pos']=0;
		sliderArray[arraycount]['count']=Math.ceil(position/wrapperWidth);
		var elementWidth=position/elementCount;
		sliderArray[arraycount]['width']=Math.floor(wrapperWidth/elementWidth)*elementWidth;
		prepareBackForward(arraycount);
	}
	this.prepare=function(){
		if (!document.getElementsByTagName) return false;
		if (!document.getElementById) return false;
		//if (!document.getElementById(selectionID)) return false;
		//var ulTags=document.getElementById(selectionID).getElementsByTagName('ul');
		thumbCount=0;
		if(document.getElementById(selectionID)){
			autoShow=true;
			prepareSlider(document.getElementById(selectionID));
			if(!document.getElementById('closeLayer')){
				slider.start();
			}
			arraycount++;
		}
		if(document.getElementById(contentID)){
			var divTags=document.getElementById(contentID).getElementsByTagName('div');
			for(var i=0; i < divTags.length; i++){
				if(divTags[i].className.indexOf('slider') != -1){
					prepareSlider(divTags[i]);
					arraycount++;
				}
			}
			divTags=null;
		}
		//contentID.
		/*ulTags = null;
		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();
		}*/
		return true;
	}
}
loadEvent.add(function(){
	slider=new Slider();
	slider.prepare();
});
