var t; 
var speed=4000;var tspeed=1000;
//in - all the images,  z1 - bottom image, z2 - top image

window.onload = function start(){
var newdiv = document.createElement('div');newdiv.setAttribute('id', "data");newdiv.style.display="none";document.getElementById('in').appendChild(newdiv);
var newdiv = document.createElement('div');newdiv.setAttribute('id', "active");newdiv.style.display="none";document.getElementById('in').appendChild(newdiv);

document.getElementById('prev').onclick=Function("clearTimeout(t);next(-1);t=setTimeout('loop()',6000);return false");
document.getElementById('next').onclick=Function("clearTimeout(t);next(1);t=setTimeout('loop()',6000);return false");
document.getElementById('stop').onclick=Function("stop_this();return false");
loop();


}


function new_date(obj){
document.getElementById('cur_date').innerHTML=obj.innerHTML;
}

function activate(p){
var x=0;
while(document.getElementById('a'+x)){
document.getElementById('a'+x).style.display="none";
x++;
}
document.getElementById('a'+p).style.display="block";
}



function stop_this(){if(t){clearTimeout(t);t=0;}else{loop();}}
function loop(){clearTimeout(t);next(1);t=setTimeout("loop()",speed);}


function next(dir){
var ac=document.getElementById("active").innerHTML;
if(!ac){
nr=parseInt(document.getElementById('data').innerHTML);
if(!nr && nr+" "!="0 "){nr=-1;}
var al=parseInt(document.getElementById('in').getElementsByTagName('img').length);

if(dir>0){  nr=nr+1;if(nr>(al-1)){nr=0;}  }else{  nr=nr-1;if(nr<0){nr=al-1;}  }
change(nr)
}
}


function change(nr){
document.getElementById("active").innerHTML=1;
document.getElementById('z1').src=document.getElementById("in").getElementsByTagName('img')[nr].src;
opacity("z2", 1, 0, tspeed,'opacity');
setTimeout('document.getElementById("z2").src=document.getElementById("in").getElementsByTagName("img")[nr].src;',(tspeed+100));
setTimeout('opacity("z2", 0, 1, 1,"opacity");',(tspeed+100));
document.getElementById('data').innerHTML=nr;
setTimeout('document.getElementById("active").innerHTML="";',(tspeed+100));
}


function opacity(id, opacStart, opacEnd, speed,type) {
exampleFx = new Fx.Tween(id, {property: type,duration: speed, transition: Fx.Transitions.Quart.easeInOut});
exampleFx.start(opacStart,opacEnd);
return exampleFx;
}


