$(document).ready(function(){

  // TICKER
  $('#ticker').bxSlider({
        auto: true,
		speed: 1500,
        pause: 5000,
		pager: false,
		nextImage: 'images/ticker_next.png',
		prevImage: 'images/ticker_prev.png',
  });

  // NEWS BOX
  $("#news_box_header ul li a").click(function(){

      var current_id=$('#news_box_header ul li a.active').attr("href");
      var target_id=$(this).attr("href");

      $("#news_box_header ul li a").removeClass('active');
      $(this).addClass('active');

      if(target_id!=current_id){

        var target_tab=target_id.substr(11);
        var left=parseInt(target_tab)*625-625;

        $("#news_box_container").animate({ 'left': '-'+left+'px' },800,'easeOutCubic');

      }

      return false;

  });

  $(".news_list ul li").click(function(){

      var id=$(this).parent().parent().attr('id');
      id=id.substr(10);
      var href = $(this).find('.href').html();
      var titulo = $(this).find('h4').html();
      var conteudo = $(this).find('.hide').html();

      if ($(this).is(".active")) {
         // return false;
      } else {
          $("#news_box_content_"+id).animate({ opacity: 0 },200, function(){
            $("#news_box_content_"+id).html('<a href="'+href+'" class="h3">'+titulo+'</a>'+conteudo).animate({ opacity: 1 },200);
          });
      }

      $("#news_list_"+id+" ul li").removeClass('active');
      $(this).addClass('active');
      //return false;

  });

  timeoutId=setTimeout("mudaTemaNewsBox()", 5000);

  $("#news_box_header, #news_box_window").mouseenter(function(){
    mouseHoverNewsBox=1;
    clearTimeout(timeoutId);
  }).mouseleave(function(){
    mouseHoverNewsBox=0;
    timeoutId=setTimeout("mudaTemaNewsBox()", 5000);
  });

});

var mouseHoverNewsBox=0;
var timeoutId;

function mudaTemaNewsBox(){

    var prox_tema=1;
    var tema_activo=$("#news_box_header ul li a.active").attr('href');
    tema_activo=parseInt(tema_activo.substr(11,1));
    if(tema_activo==4)
        prox_tema=1
    else
        prox_tema=tema_activo+1;

    $("#news_box_header ul li:nth-child("+prox_tema+")").find('a').trigger("click");

    if(mouseHoverNewsBox==0)
        timeoutId=setTimeout("mudaTemaNewsBox()", 5000);

}
