
var slider;
function Slider(){
	var className='slider';
	var selectionID='homeSlider';
	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=5000;
	var autoShow=false;
	var wrapperWidth;
	var imageArray=[];
	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);
			prepareDots(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);
			prepareDots(no);
	}
	this.showElement=function(no,pos){
		if(autoShow && no==0){
			if(showTimeout){
				clearTimeout(showTimeout);
			}
		}
		sliderArray[no]['pos']=pos;
		slider.moveElement(no,-(sliderArray[no]['pos']*sliderArray[no]['width']),0);
		//if(document.getElementById('imgBackForward'))
			prepareBackForward(no);
			prepareDots(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);
			}
		}
	}
	var prepareDots=function(no){
		var divTags=sliderArray[no]['dots'].getElementsByTagName('div');
		for(var i=0; i<divTags.length; i++){
			if(i==sliderArray[no]['pos']){
				addClass(divTags[i],'active');
			}else{
				removeClass(divTags[i],'active');
			}
		}
	}
	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]['slide']=sliderArray[arraycount]['wrapper'].getElementsByTagName('ul')[0];
			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;
		wrapperWidth =924;
		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']);
		//alert(position+' '+wrapperWidth);
		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);
		
		sliderArray[arraycount]['dots']=document.createElement('div');
		sliderArray[arraycount]['dots'].no=arraycount;
		addClass(sliderArray[arraycount]['dots'],'dots');
		var innerTag;
		for(var i=0; i<elementCount; i++){
			divTag=document.createElement('div');
			addClass(divTag,'dot');
			innerTag=document.createElement('span');
			divTag.appendChild(innerTag);
			innerTag=document.createElement('span');
			divTag.appendChild(innerTag);
			divTag.pos=i;
			divTag.onclick=function(){
				slider.showElement(this.parentNode.no,this.pos);
			}
			sliderArray[arraycount]['dots'].appendChild(divTag);
		}
		sliderArray[arraycount]['wrapper'].appendChild(sliderArray[arraycount]['dots'])
		prepareDots(arraycount);
		
		var liTags = sliderArray[arraycount]['wrapper'].getElementsByTagName('li');
		var realCount=0;
		for ( var i=0; i < liTags.length; i++){
			if(liTags[i].getElementsByTagName('img').length==1){
				preloadImage(liTags[i].getElementsByTagName('img')[0],i);
				liTags[i].arg=i;
				liTags[i].onclick = function(){
					return !slider.show(this,this.arg);
				}
				liTags[i].onkeypress = liTags[i].onclick;
			}
		}
	}
	this.show=function (whichpic,pos){
		//if (!document.getElementById(placeholderID)) return false;
		var source = imageArray[pos].src;
		var placeholder=whichpic.parentNode.parentNode.parentNode.getElementsByTagName('div')[0];
		var imgTag=placeholder.getElementsByTagName('img')[1].cloneNode(true);
		placeholder.appendChild(imgTag);
		placeholder.getElementsByTagName('img')[2].src=source;
		placeholder.removeChild(placeholder.getElementsByTagName('img')[0]);
		addClass(placeholder.getElementsByTagName('img')[0],'disabled');
		/*var links = document.getElementById(selectionID).getElementsByTagName('a');
		for ( var i=0; i < links.length; i++){
			removeClass(links[i].parentNode,'active');
		}
		addClass(whichpic.parentNode,'active');*/
		whichpic.blur();
		//alert('test '+pos);
		return true;
	}
	var preloadImage=function(element,i){
		var links = document.getElementById(selectionID).getElementsByTagName('a');
		var source=element.getAttribute("src").replace(/\/thumb\//i,'/large/');
		var placeholder=element.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('div')[0];
		if(placeholder.getElementsByTagName('img').length==1){
			placeholder.getElementsByTagName('img')[0].style.position='absolute';
			var imgTag=placeholder.getElementsByTagName('img')[0].cloneNode(true);
			placeholder.appendChild(imgTag);
		}
		imageArray[i]=new Image();
		imageArray[i].src=source;
		imageArray[i].loaded=false;
		imageArray[i].onload=function(){
			this.loaded=true;
		}
	}
	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;
	}
}
addLoadEvent(function(){
	slider=new Slider();
	slider.prepare();
});
