var nr_of_heads = 9;
var current_head = -1;
var mode=0; //1 - for all heads moving, else for 1 head moving

$(document).ready(function()
{
  $.ajaxSetup({
    cache:false
  });

  /**
   * Does updated a heads pictures to correct direction
   *
   * @author atom
   * @version 28.03.2011
   */
  function updateHeads(sel_head){
    if(mode== 1){
      for(i=1;i <= nr_of_heads;i++){
        if(i != sel_head)
        {
          $("#cezik-"+i).attr("class","sprite-"+i+"na"+sel_head);
        }
        else{
          $("#cezik-"+i).attr("class","sprite-"+i+"norma");
        }
      }
    }
    else{
      setHeadsToDirection(current_head);
      if(current_head == -1){
        $("#cezik-"+sel_head).attr("class","sprite-"+sel_head+"wybor");
      }
      else{
        $("#cezik-"+sel_head).attr("class","sprite-"+sel_head+"norma");
      }
    }
  }
  /**
   *Set heads to look in one direction
   *
   *@author Atom
   *@version 11.04.2010
   *
   */
  function setHeadsToDirection(sel_head)
  {
    for(i=1;i <= nr_of_heads;i++){
      if(i != sel_head)
      {
        $("#cezik-"+i).attr("class","sprite-"+i+"na"+sel_head);
        $("#cezik-"+i).removeClass("selected");
      }
      else{
        $("#cezik-"+i).attr("class","sprite-"+i+"wybor");
        $("#cezik-"+i).addClass("selected");
      }
    }
  }
  /**
   * Resets heads to normal state
   *
   * @author atom
   * @version 28.03.2011
   */
  function resetHeads(){
    setHeadsToDirection(current_head);
  }

  $('#cezik-player').click(function(event)
  {
    if(mode == 1)
    {
      mode =0;
      $("#overscreen").fadeOut("slow");
      $(this).addClass("on");
      $(this).removeClass("on");
    }
    else{
      mode =1;
      $("#overscreen").fadeIn("slow");
      $(this).addClass("on");
    }
  })

  /**
 * Reacts on hover on a face of CeZik.
 *
 * @author atom
 * @version 28.03.2011
 */
  $("li","#cezik-tv").hover(
    //in
    (function(event){
      var tmp = $(this).attr("id").replace("cezik-","");
      updateHeads(tmp);
    })
    ,//out
    (function(event){
      }));
  /**
 * Resets a TV to normal state
 *
 * @author atom
 * @version 28.03.2011
 */
  $("#cezik-tv-frame").bind("mouseleave",function(event){
    resetHeads();
  })

  $("a","#cezik-tv").click(function(event){
    var tmp =    $(this).parent("li").attr("id").replace("cezik-","");
    current_head = tmp;
    $("#overscreen").fadeOut("slow");
    $('#cezik-player').removeClass("on");
    mode = 0;
    setHeadsToDirection(tmp);
  })
  /**
   * Show propaganda stuff
   */
  setTimeout(function(){
    $('#txt_prod').show('slow');
  },1000);
  setTimeout(function(){
    $('#txt_cuda').show('slow');
  },1500);
  setTimeout(function(){
    $('#txt_perf').show('slow');
  },1700);





  
});


