// <![CDATA[
var img_cont = 0;
var last_sel=null;
var timer = 8000;
var timeoutCarrusel;
var stopped_carrusel = false;

function start_carrusel() {
    carrusel();
}

function resume_carrusel() {
    timeoutCarrusel = setTimeout("carrusel()",timer);  
}

function carrusel() {
    if (stopped_carrusel)
      return;
    resume_carrusel();
    img_show("newsitem_"+img_cont);
    img_cont++;
    if (img_cont == 3)
      img_cont = 0;
    else {
      if ($("newsitem_"+img_cont)==null) {
          img_cont=0;      
      }
    }
}

function img_show(id) {      
    var sel = $(id);
    if (sel==null) {return}
    if (last_sel != null) {
        last_sel.style.zIndex = 1;
        last_sel.fade({ duration: 1.0 });
    }      
    last_sel = sel;
    sel.style.zIndex = 1000;
    sel.appear({ duration: 3.0 });
    $('controles').style.zIndex = 2000;
}

function stop_carrusel(){    
    stopped_carrusel = true;
    var msg = "Seguir (s)";
    if (lang == "en")
        msg = "Play (s)"
    $("cont_carrusel").value = msg;
}

function play_carrusel(){    
    resume_carrusel();
    stopped_carrusel = false;
    var msg = "Parar (s)";
    if (lang == "en")
        msg = "Stop (p)"
    $("cont_carrusel").value = msg;
}

function toggle_carrusel() {
    if (stopped_carrusel) {
        play_carrusel(); 
    }
    else {
        stop_carrusel();
    }
}

function goto_newsitem(item_number) {
  stop_carrusel();
  img_show($("newsitem_" + item_number));
}

Event.observe(window, 'load', function() {
    Event.observe(document, 'keypress', function(e){
        var code;
        if (!e) var e = window.event;
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;
        var character = String.fromCharCode(code);
        if (character == 'p') stop_carrusel();
        if (character == 's') play_carrusel();
    });
})

start_carrusel();

