var	pp_activecover=0;
var	pp_maxcover=0;

function overCover(){
  var index = $("#titles li").index(this);
  pp_activecover=index;
  showCover(index);
  setTimer(false);
}

function outCover(){
  setTimer(true);
}

function showCover(show){
  var cover = $("#covers li").eq(show);
  var title = $("#titles li").eq(show);
  cover.stop().fadeTo("normal",1);
  hideOtherCovers(show,"fade");
  title.addClass("selected");

  $("#headline .title").html($("h2",title).html());
  $("#headline .subtitle").html($(".subtitle",title).html());
};

function hideOtherCovers(show,mode){
  $("#covers li").each(function(index){
    if (index!=show && index!=pp_activecover) {
      if (mode=="fade")
        $(this).stop().fadeOut();
      else
        $(this).stop().hide();
      var title = $("#titles li").eq(index);
      title.removeClass("selected");
    };
  });
};

function initTitles(){
  $("#titles li").each(function(index){
    $(this).hover(overCover,outCover);
    var end=$(this).position().top;
    end+=$(this).height();
    if (end>350) {
      $(this).hide();
      if (pp_maxcover==0) pp_maxcover=index;
    };
  });
  $("#headline").show();
};

function cycleCover(){
  pp_activecover=(pp_activecover<pp_maxcover-1)?pp_activecover+1:0;
  showCover(pp_activecover);
};

function setTimer(mode){
  if (mode)
    $("#prima_pagina").everyTime(5000, cycleCover);
  else
    $("#prima_pagina").stopTime();
};

function initPrima(){
  initTitles();
  hideOtherCovers(0,"hide");
  showCover(0);
  setTimer(true);
};