var slideShow=function(){
	var bxs,bxe,fxs,fxe,ys,ye,ta,ia,ie,st,ss,ft,fs,xp,yp,ci,t,tar,tarl,defaultYs,defaultYe;

	ta=document.getElementById(thumbid); 
	ia=document.getElementById(imgid);

	t=ta.getElementsByTagName('li');
	ie=document.all?true:false;

	st=4; ss=4; ft=5; fs=5; xp,yp=5;
	return{
		init:function(){
			document.onmousemove=this.pos;
			window.onresize=function(){slideShow.init(); slideShow.lim()};
			ta.style.left = '0px';
			ys=this.toppos(ta);
			ye=ys+ta.offsetHeight;
			defaultYs = ys;
			defaultYe = ye;
			len=t.length;tar=[];
			for(i=0;i<len;i++){
				var id=t[i].id; tar[i]=id;
				var titlesplit = id.split("___");
				var id2 = titlesplit[0];			
				var description = titlesplit[1];
				var longdescription = titlesplit[2];
				    //alert(description);
				
				 //alert(title)
				t[i].onclick=new Function("slideShow.getimg('"+id2+"', '"+description+"', '"+longdescription+"')");
				if(i==0){this.getimg(id2, description, longdescription)}
			}
			tarl=tar.length;
		},
		scrl:function(d){
			clearInterval(ta.timer);
			var l=(d==-1)?0:(t[tarl-1].offsetLeft-(ta.parentNode.offsetWidth-t[tarl-1].offsetWidth)+10)
			ta.timer=setInterval(function(){slideShow.mv(d,l)},st);
		},
		mv:function(d,l){
			ta.style.left=ta.style.left||'0px';
			var left=ta.style.left.replace('px','');
			if(d==1){
				if(l-Math.abs(left)<=ss){
						this.cncl(ta.id); 
						ta.style.left='-'+l+'px';
					}else{ta.style.left=left-ss+'px'}
			}else{
				if(Math.abs(left)-l<=ss){
					this.cncl(ta.id); ta.style.left=l+'px';
				}else{ta.style.left=parseInt(left)+ss+'px'}
			}
		},
		cncl:function(){clearTimeout(ta.timer)},
		getimg:function(id2, description, longdescription){		
			if(auto){clearTimeout(ia.timer)}
			if(ci!=null){
				var ts,tsl,x;
				ts=ia.getElementsByTagName('img'); tsl=ts.length;x=0;
				for(x;x<tsl;x++){
					if(ci.id!=id2)
					{
						var o=ts[x]; 
						clearInterval(o.timer); 
						o.timer=setInterval(function(){slideShow.fdout(o)},fs)
					}
				}
			}
			if(document.getElementById('pic') && document.getElementById('pic').src != imgsvr+id2){
				ia.removeChild(document.getElementById('pic'));
			
				var i = document.createElement('img');
				i.id='pic'; i.av=0; i.style.opacity=0;
				i.style.filter='alpha(opacity=0)';
				i.onload = function()
				{
					fixImgs(i,400);
					if(i.height > 300){
						var heightDiff = this.height - 300;
						ia.style.height = this.height;
						ys=defaultYs + heightDiff;
						ye=defaultYe + heightDiff;
					}else{
						ia.style.height = 300;
						ys=defaultYs;
						ye=defaultYe;
					}
					ia.appendChild(i);
					i.timer=setInterval(function(){slideShow.fdin(i)},fs);
				}
				i.src=imgsvr+id2;
				if(popup){
					ia.style.cursor = 'pointer';
					ia.title = 'Click to enlarge';
					document.getElementById('vehInfo').value = vehInfo;
					document.getElementById('picLoc').value = this.urlencode(id2.replace('_400',''));
					document.getElementById('Description').value = description;
					document.getElementById('LongDescription').value = longdescription;
					
					ia.onclick=new Function("document.image_popup.submit();");
				}			
			}else{
				i = document.getElementById('pic');
				clearInterval(i.timer);
				i.timer=setInterval(function(){slideShow.fdin(i)},fs);
			}
		},
		nav:function(d){
			var c=0;
			for(key in tar){
				if(tar[key]==ci.id){
					c=key
				}
			}
			if(tar[parseInt(c)+d]){
				this.getimg(tar[parseInt(c)+d], description, longdescription);
			}else{
				if(d==1){
					this.getimg(tar[0], description, longdescription);
				}else{this.getimg(tar[tarl-1], description, longdescription)}
			}
		},
		auto:function(){ia.timer=setInterval(function(){slideShow.nav(1)},autodelay*1000)},
		fdin:function(i){
			if(i.complete)
			{
				i.av=i.av+fs;
				i.style.opacity=i.av/100; 
				i.style.filter='alpha(opacity='+i.av+')';
			}
			if(i.av>=100)
			{
				if(auto){this.auto()};
				clearInterval(i.timer); 
				ci=i;
			}
		},
		fdout:function(i){
			i.av=i.av-fs; i.style.opacity=i.av/100;
			i.style.filter='alpha(opacity='+i.av+')';
			if(i.av<=0){clearInterval(i.timer); if(i.parentNode){i.parentNode.removeChild(i)}}
		},
		lim:function(){
			var aw,taa,len; 
				taw=ta.parentNode.offsetWidth; 
				taa=taw/4; 
				bxs=slideShow.leftpos(ta); 
				bxe=bxs+taa; 
				fxe=bxs+taw; 
				fxs=fxe-taa;
		},
		pos:function(e){
			xp=ie?event.clientX+document.body.scrollLeft:e.pageX; 
			yp=ie?event.clientY+document.body.scrollTop:e.pageY;
			if(xp>bxs&&xp<bxe&&yp>ys&&yp<ye){
				slideShow.scrl(-1);
			}else if(xp>fxs&&xp<fxe&&yp>ys&&yp<ye){
				slideShow.scrl(1);
			}else{slideShow.cncl()}
		},
		leftpos:function(t){
			var l=0;
			if(t.offsetParent){
				while(1){l+=t.offsetLeft; if(!t.offsetParent){break}; t=t.offsetParent}
			}else if(t.x){l+=t.x}
			return l;
		},
		toppos:function(t){
			var p=0;
			if(t.offsetParent){
				while(1){
					p+=t.offsetTop; 
					if(!t.offsetParent){
						break
					} 
					t=t.offsetParent;
				}
			}
			else if(t.y)
			{
				p+=t.y;
			}
			return p;
		},
		urlencode:function(str) {
			str = escape(str);
			str = str.replace(/\+/g, '%2B');
			str = str.replace(/\%20/g, '+');
			str = str.replace(/\x2A/g, '%2A');
			str = str.replace(/\x2F/g, '%2F');
			str = str.replace(/\x40/g, '%40');
			return str;
		}
	};
}();

function fixImgs(pix, maxW) {
	w=pix.width;
	h=pix.height;
	f=1-((w - maxW) / w);
	pix.width=w * f;
	pix.height=h * f;
}

function popWindow(form){
	features = 'width=900,height=700,left=1,top=1,toolbar=no,location=no,menubar=no,directories=no,status=no,scrollbars=yes,resizable=yes';
	pop = window.open('',form.target,features);
	if(pop.focus){ pop.focus(); }
	return true;
}

var prev_onload_slide = window.onload;

window.onload=function(){if (typeof prev_onload_slide == 'function') {prev_onload_slide(); } slideShow.init(); slideShow.lim()};
