var slideShow=function(){
	var bxs,bxe,fxs,fxe,ys,ye,ta,ia,ie,st,ss,ft,fs,xp,yp,ci,t,tar,tarl,tas;
	var indexactual = 0;
	ie=document.all?true:false;
	st=3; ss=3; ft=10; fs=5; xp,yp=0; imgcurrentid='';
	return{
	    setmasinfo: function(id){
    		var masInfo = document.getElementById("ctl00_ContentPlaceHolder1_imgMasinfo");
            masInfo.style.display = "block";
            masInfo.onclick = function(){ document.location.href = '/ficha.aspx?id=' + id}
    		var lnkmasInfo = document.getElementById("ctl00_ContentPlaceHolder1_lnkMasinfo");
            lnkmasInfo.style.display = "block";
            lnkmasInfo.href = "/ficha.aspx?id=" + id;
	    },
		init:function(){
            ta=document.getElementById(thumbid); ia=document.getElementById(imgid);
            tas = document.getElementById("thumbscroll")
	        t=ta.getElementsByTagName('li'); 		
			document.onmousemove=this.pos; 
			window.onresize=function(){setTimeout("slideShow.lim()",500)};
			ys=this.leftpos(tas); ye=ys+tas.offsetWidth;
			len=t.length;tar=[];
			//var aleat = Math.floor(Math.random()*len);
			for(i=0;i<len;i++){
				var id=t[i].getAttribute("tag"); tar[i]=id;
			    t[i].onclick=new Function("slideShow.click('"+id+"','" + t[i].id + "'," + (i+1) + ")");
			    t[i].onmouseenter = new Function("slideShow.changeopacity('"+t[i].id+"',100);");
			    t[i].onmouseout = new Function("slideShow.changeopacity('"+t[i].id+"',70);");
				if(i==0 && document.location.search.indexOf('idcat=') != -1)
				{
				    slideShow.setpaginacion(1);
				    slideShow.changeopacity(t[i].id,100)
				    imgcurrentid = t[i].id;
				    this.getimg(id);
                    var img = document.getElementById(t[i].id).getElementsByTagName("img")
		            var texto = img[0].getAttribute("title");
		            document.getElementById("lblTitulo").innerHTML = texto;		        
		            document.getElementById("div" + t[i].id).style.display = "block";
		            this.setmasinfo(id.substr(0,id.indexOf('_')));
				}
				else{
                    slideShow.changeopacity(t[i].id,70)
				}
			}
			tarl=tar.length;
		},
		setpaginacion: function(indice)
		{
		    var paginacion = document.getElementById("ctl00_ThumbFotos_lblPaginacion");
		    paginacion.innerText = indice + "/" + paginacion.getAttribute("tag");
		    indexactual = indice;
		},
		click: function(idbig, id, indice)
		{
		    slideShow.getimg(idbig);
		    var actual = imgcurrentid;
		    slideShow.changeopacity(id,100);
		    imgcurrentid= id;
		    if (actual != '')
		    {
		        slideShow.changeopacity(actual,70);
		        document.getElementById("div" + actual).style.display = "none";
		    }
		    else
		    {
		        document.getElementById("ctl00_ContentPlaceHolder1_imgHome").style.display = "none";
		    }
		    slideShow.setpaginacion(indice);
            var img = document.getElementById(id).getElementsByTagName("img")
		    var texto = img[0].getAttribute("title");
		    var titulo = document.getElementById("lblTitulo");
		    titulo.href = '/ficha.aspx?id=' + idbig.substr(0,idbig.indexOf('_'));
		    titulo.innerHTML = texto;		        
		    document.getElementById("div" + id).style.display = "block";
		    this.setmasinfo(idbig.substr(0,idbig.indexOf('_')));
		    pageTracker._trackPageview("/maquinas/" + texto); 
		},
		changeopacity: function(id, valor){
		    if (id != imgcurrentid)
		    {
		        document.getElementById(id).style.filter = 'alpha(opacity=' + valor + ')';
		        document.getElementById(id).style.opacity = valor/100;
		    }
		},
		scrl:function(d){
			clearInterval(ta.timer);
			try
			{
			    //var l=(d==-1)?0:(t[tarl-1].offsetLeft-(ta.parentNode.offsetWidth-t[tarl-1].offsetWidth)+10)
			    var l=(d==-1)?0:(t[tarl-1].offsetTop-(ta.parentNode.offsetHeight-t[tarl-1].offsetHeight)+10)
			    ta.timer=setInterval(function(){slideShow.mv_v(d,l)},st);
			}
			catch(exc)
			{}
		},
		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'}
			}
		},
        mv_v:function(d,l){
            try
            {
			    ta.style.top=ta.style.top||'0px';
			    var top=ta.style.top.replace('px','');
			    if(d==1){
				    if(l-Math.abs(top)<=ss){
					    this.cncl(ta.id); ta.style.top='-'+l+'px';
				    }else{ta.style.top=top-ss+'px'}
			    }else{
				    if(Math.abs(top)-l<=ss){
					    this.cncl(ta.id); ta.style.top=l+'px';
				    }else{ta.style.top=parseInt(top)+ss+'px'}
			    }
			}
			catch(exc)
			{}
		},		
		cncl:function(){clearTimeout(ta.timer)},
		getimg:function(id){
			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!=id){var o=ts[x]; clearInterval(o.timer); o.timer=setInterval(function(){slideShow.fdout(o)},fs)}
				}
			}
			if(!document.getElementById(id)){
				var i=document.createElement('img');
				ia.appendChild(i);
				i.id=id; i.av=0;
				i.style.filter='alpha(opacity=0)';
				i.style.cursor='pointer';
				i.onclick = function(){ document.location.href = '/ficha.aspx?id=' + id.substr(0,id.indexOf('_'))}
				i.style.opacity=0;
				//i.style.width = '542px';
				i.src=imgdir+'/'+id+imgext;
			}else{
				i=document.getElementById(id); clearInterval(i.timer);
			}
			i.timer=setInterval(function(){slideShow.fdin(i)},fs);
		},
		navigation:function(delta){
		    var index = indexactual + delta;
		    if (index >= 1 && index <= tar.length)
		    {
                var idbig = tar[index-1];
			    var idtn = 'tn' + idbig.substr(0,idbig.indexOf('_'));
//			    if (parseInt(index/9) != parseInt(indexactual/9))
//			    {
//			        t[index-1].scrollIntoView();
//			    }
			    slideShow.click(idbig, idtn, index);			    
		    }
		},
		nav:function(d){
			var c=0; var idbig;
			for(key in tar){if(tar[key]==ci.id){c=key}}
			if(tar[parseInt(c)+d]){
//				this.getimg(tar[parseInt(c)+d]);
                idbig = tar[parseInt(c)+d];
			}else{
				if(d==1){
//					this.getimg(tar[0]);
                    idbig = tar[0];
				}
				else
				{
//				    this.getimg(tar[tarl-1])
                    idbig = tar[tarl-1];
				}
			}
			var idtn = 'tn' + idbig.substr(0,idbig.indexOf('_'));
			slideShow.click(idbig, idtn);
		},
		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 taw,taa,len;
			taw=tas.parentNode.offsetHeight; 
			taa=15; //taw/8; 
			bxs=slideShow.toppos(tas); 
			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(yp>bxs&&yp<bxe&&xp>ys&&xp<ye){
				slideShow.scrl(-1);
			}else if(yp>fxs&&yp<fxe&&xp>ys&&xp<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;
		}
	};
}();

window.onload=function(){slideShow.init(); slideShow.lim()};