var slideShow=function(){
	var bxs,bxe,fxs,fxe,ys,ye,ta,ia,ie,st,ss,ft,fs,xp,yp,ci,t,tar,tarl;
    var ta = document.getElementsByName(thumbid);                                           
    var arrayTag = [];
    
    var ia = document.getElementsByName(imgid);
    
    ie=document.all?true:false;
	st=3; ss=3; ft=10; fs=5; xp,yp=0;
	return{
		init:function(){
			document.onmousemove=this.pos; 
            //window.onresize=function(){setTimeout("slideShow.lim()",500)};
			ys=this.toppos(ta); 
            ye=ys+ta.offsetHeight;
            
            for(x=0;x<ta.length;x++)
            {
                var tar = [];
                var t = ta[x].getElementsByTagName('li');
                var len=t.length;
                for(i=0;i<len;i++){
				    var id=t[i].value; 
                    tar[i]=id;
				    t[i].onclick=new Function("slideShow.getimg('" + id + "', '" + x + "')");
				    if(i==0){
                        this.getimg(id, x);
                    }
			    }
                arrayTag[x] = tar; 
            }
		},
		scrl:function(d, idcategoria){
			
            clearInterval(ta[idcategoria].timer);
            
            var t = ta[idcategoria].getElementsByTagName('li');
            tarl = t.length;
                        
            
            
			var l=(d==-1)?0:(t[tarl-1].offsetLeft-(ta.parentNode.offsetWidth-t[tarl-1].offsetWidth)+10)
			ta[idcategoria].timer=setInterval(function(){slideShow.mv(d,l,idcategoria)},st);
		},
		mv:function(d,l, idcategoria){
			ta[idcategoria].style.left=ta[idcategoria].style.left||'0px';
			var left=ta[idcategoria].style.left.replace('px','');
			if(d==1){
				if(l-Math.abs(left)<=ss){
					this.cncl(idcategoria); 
                    ta[idcategoria].style.left='-'+l+'px';
				}else{ta[idcategoria].style.left=left-ss+'px'}
			}else{
				if(Math.abs(left)-l<=ss){
					this.cncl(idcategoria); 
                    ta[idcategoria].style.left=l+'px';
				}else{ta[idcategoria].style.left=parseInt(left)+ss+'px'}
			}
		},
		cncl:function(idcategoria){
            clearTimeout(ta[idcategoria].timer)
        },
		getimg:function(id, idcategoria){
            
            if(auto){
                clearTimeout(ia[idcategoria].timer)
            }
			if(ci!=null){
				var ts,tsl,x;
				ts=ia[idcategoria].getElementsByTagName('img'); 
                tsl=ts.length;
                x=0;
				for(x;x<tsl;x++){
					if(ci.id!=id){var o=ts[x]; clearInterval(o.timer); o.timer=setInterval(function(){slideShow.fdout(o)},fs)}
				}
			}
			if(!document.getElementById(id)){
            
                var thid= "th" + this.padleft(ia[idcategoria].attributes['value'].value , "0", "3") + this.padleft(id, "0", "2");                     var anteprima = document.getElementById(thid); 	
                var i = document.createElement('img');
                ia[idcategoria].appendChild(i);
				ia[idcategoria].href = anteprima.attributes['value'].value; 
                i.id=id; i.av=0; 
                i.style.opacity=0;
				i.style.filter='alpha(opacity=0)';
                i.width = 442;
                i.src = anteprima.attributes['file'].value;
            }
            else
            {
				i=document.getElementById(id); clearInterval(i.timer);
			}
			i.timer=setInterval(function(){slideShow.fdin(i, idcategoria)},fs);
		},
		nav:function(d, idcategoria){
			var c=0;
            
            var tar = arrayTag[idcategoria];
            
			for(key in tar)
            {
                if(tar[key]==ci.id)
                {
                    c=key;
                }
            }
			if(tar[parseInt(c)+d])
            {
				this.getimg(tar[parseInt(c)+d], idcategoria);
			}
            else
            {
				if(d==1)
                {
					this.getimg(tar[0], idcategoria);
				}
                else
                {
                    this.getimg(tar[tarl-1], idcategoria);
                }
			}
		},
		auto:function(idcategoria){
            ia[idcategoria].timer=setInterval(function(){slideShow.nav(1, idcategoria)},autodelay*1000)},
		fdin:function(i, idcategoria){
			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(idcategoria)
                }; 
                clearInterval(i.timer); 
                ci=i
            }
		},
        padleft: function (val, ch, num) {
            var re = new RegExp(".{" + num + "}$");
            var pad = "";
            if (!ch) ch = " ";
            do  {
                pad += ch;
            }while(pad.length < num);
            return re.exec(pad + val)[0];
        },
        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(idcategoria){
			var taw,taa,len; 
            taw=ta[idcategoria].parentNode.offsetWidth; 
            taa=taw/4; 
			bxs=slideShow.leftpos(ta[idcategoria]); bxe=bxs+taa; fxe=bxs+taw; fxs=fxe-taa;
		},
		pos:function(e){
			xp=ie?event.clientX+document.documentElement.scrollLeft:e.pageX; 
			yp=ie?event.clientY+document.documentElement.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(0)}
		},
		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;
		},
		stop:function(idcategoria){clearInterval(ia[idcategoria].timer)}
	};
}();

window.onload=function()
{
    slideShow.init(); 
    //slideShow.lim();
};
