var path_base_abs = "http://www.annecy.org/";
/* function init_js
 * Initialise les action JS de la page courante
 * @param : str_json     //str_json = ""
 */
function init_js(json_param)
{
  //alert(json_param);
  init_barre_citia();
  init_menu( json_param[0], json_param[1], json_param[2], json_param[3] );
  //ajout JC pour menu network
  init_menu_class( json_param[0], json_param[1], json_param[2], json_param[3] );  
  
  init_modal();
  resizeBackground();

  init_search();
  
  //$(".visionneuse").loopedSlider();
  $(window).resize(function(){
      resizeBackground();
  });
}

/* function init_barre_citia
 * Gère l'affichage ou non des intitulés de site dans la barre
 * @param : NULL
 */
function init_barre_citia()
{
  $('#bc_contenu .kwicks').kwicks({
    max: 90,
    min: 35,
    spacing: 2,
    duration: 300,
    sticky: true
  });
  
  $("a#btn_print").click(function() {
    window.print();
    return false;
  });
}

/* function init_menu
 * Initialise l'interactivité du menu
 * @param : NULL
 */
function init_menu( langue, diffusion, n1_actif, n2_actif )
{
  var index_1 = $( '#menu h3' ).index( $( '#mp_'+langue+'_'+diffusion+'_'+n1_actif ).parent() );

  var index_acco_1 = 0;
  var index_std = 0;

  $( "#menu h3" ).each( function() {
    if( $(this).hasClass( "is_menu" ) && index_std < index_1 )
    {
      index_acco_1++;
    }
    index_std++;
  } );
  
  if( $( '#mp_'+langue+'_'+diffusion+'_'+n1_actif ).parent().next().is( ".menu1" ) ) //index_acco_1 != 0 )
    $( "#menu" ).accordion( { header: "h3.is_menu", active: index_acco_1, autoHeight: false } );
  else
    $( "#menu" ).accordion( { header: "h3.is_menu", active: false, autoHeight: false } );
  // Si le N2 a un sous menu...
  if( n2_actif != "undefined" && $( '#mp_'+langue+'_'+diffusion+'_'+n2_actif ).parent().next().is( ".menu2" ) )
  {
    var index_2 = $( "#menu h3.is_menu" ).eq( index_acco_1 ).next().find( "h4" ).index( $('#mp_'+langue+'_'+diffusion+'_'+n2_actif ).parent() );
    $( "#menu h3.is_menu" ).eq( !index_acco_1 ).next( "div.menu1" ).accordion( { header: "h4", active: false, autoHeight: false } );
    $( "#menu h3.is_menu" ).eq( index_acco_1 ).next( "div.menu1" ).accordion( { header: "h4", active: index_2, autoHeight: false } );
  }
  else
    $( "div.menu1" ).accordion( { header: "h4", active: false, autoHeight: false } );

  $( "#menu h3.is_menu" ).click( function() {
    $( "#menu h3" ).each( function() {
      if( !$(this).hasClass( "is_menu" ) )
      {
        $(this).next( "div.menu1" ).hide( "fast" );
      }
    } );
  } );

  $( ".non_noeud a" ).click( function() {
    $(location).attr('href',$(this).attr( "href" ));
  } );
}

/* function init_menu_class
 * Initialise l'interactivité du menu
 * @param : NULL
 */
function init_menu_class( langue, diffusion, n1_actif, n2_actif , n3_actif )
{

  var index_1 = $('#menu_network h3').index( $( '#mp_'+langue+'_'+diffusion+'_w'+n2_actif ).parent() );

  var index_acco_1 = 0;
  var index_std = 0;
 

  $( "#menu_network h3" ).each( function() {
    
    if( $(this).hasClass( "is_menu" ) && index_std < index_1 )
    {
      index_acco_1++;
    }
    index_std++;
  } );
 
  //alert( "index_1 > "+index_1+"  || index_acco_1 > "+index_acco_1+' || #mp_'+langue+'_'+diffusion+'_w'+n1_actif +" >> "+$('#mp_'+langue+'_'+diffusion+'_w'+n1_actif).attr("href") );

  if( $( '#mp_'+langue+'_'+diffusion+'_w'+n2_actif ).parent().next().is( ".menu1" ) ) //index_acco_1 != 0 )
    $( "#menu_network" ).accordion( { header: "h3.is_menu", active: index_acco_1, autoHeight: false } ); 
  else
    $( "#menu_network" ).accordion( { header: "h3.is_menu", active: false, autoHeight: false } );
  // Si le N2 a un sous menu...
  if( n2_actif != "undefined" && $( '#mp_'+langue+'_'+diffusion+'_w'+n3_actif ).parent().next().is( ".menu2" ) )
  {
    var index_2 = $( "#menu_network h3.is_menu" ).eq( index_acco_2 ).next().find( "h4" ).index( $('#mp_'+langue+'_'+diffusion+'_w'+n3_actif ).parent() );
    $( "#menu_network h3.is_menu" ).eq( !index_acco_1 ).next( "div.menu1" ).accordion( { header: "h4", active: false, autoHeight: false } );
    $( "#menu_network h3.is_menu" ).eq( index_acco_1 ).next( "div.menu1" ).accordion( { header: "h4", active: index_2, autoHeight: false } );
  }
  else
    $( "div.menu1" ).accordion( { header: "h4", active: false, autoHeight: false } );

  $( "#menu_network h3.is_menu" ).click( function() {
    $( "#menu_network h3" ).each( function() {
      if( !$(this).hasClass( "is_menu" ) )
      {
        $(this).next( "div.menu1" ).hide( "fast" );
      }
    } );
  } );

  $( ".non_noeud a" ).click( function() {
    $(location).attr('href',$(this).attr( "href" ));
  } );
}

/* function resizeBackground
 * Gère l'affiche du background
 * @param : NULL
 */
function resizeBackground()
{
  var ImgH = $("#background #fond").height();
  var ImgW = $("#background #fond").width();

	var windowH = $(window).height();
	var windowW = $(window).width();

  var containerH = $(".container").height();
	var containerW = $(".container").width();
  if( containerH > windowH && containerW > windowW )
  {
    $("#logout").append("ahah");
    windowH = containerH;
    windowW = containerW;
  }

  if( windowH > windowW )
    var ratio = windowW / ImgW * 100 - 20;
  else
    var ratio = windowH / ImgH * 100 - 20;

  var newImgW = Math.round(ImgW * ratio / 100);
  var newImgH = Math.round(ImgH * ratio / 100);

	$("#background #fond").width( newImgW+"px");
  $("#background #fond").height( newImgH+"px");
}

/* function init_box
 * Gère l'amnmation des mini_box 4 blocs
 * @param : NULL
 */
function init_boxgrid()
{
  //Coulissement de légende (Une partie cachée à visible)
  $('.boxgrid.caption').hover(function() {
    $(".cover", this).stop().animate({top:'40px', height :'66px'},{queue:false,duration:160}).find("p").show();
  }, function() {
    $(".cover", this).stop().animate({top:'81px', height :'25px'},{queue:false,duration:160}).find("p").hide();
  });
}


/* function init_news_flash
 * Rend les news défilante selon les parametres
 * @dependances: jquery.easynews.js 
 * @param id_news:String
 * @param vitesse:Number 
 */
function init_news_flash()
{

  $.init_news();
}

/* function init_home_mifa
 * Initialise la home page du Mifa
 * @param NULL
 */
function init_home_mifa()
{
    $('dl.chiffres dt span').each(function(){
   // $('ul.chiffres li span').each(function(){
        id_courant = $(this).attr("id");
        s1 = new SWFObject("/resources/swf/mifa-chiffres.swf", "so_chiffres", "100", "30", "9", "#fff");
       	s1.addParam('flashvars','chiffre='+$(this).text());
       	s1.addParam('wmode','transparent');
        s1.write(id_courant);
    });
    
    $("div#slider").codaslider();
}

/* function init_modal
 * Initialise les comportements modaux sur tout type délements
 * @param NULL
 */
function init_modal()  
{
  $(".blc_modal").click(function() {
			$.nyroModalManual({ content: $($(this).attr("href")).html() });
  });
}
/* function init_modal_jury
 * Initialise les comportements modaux sur les jury
 * @param NULL
 */
function init_modal_jury()  
{
  $(".modal_jury").click(function() {
			$.nyroModalManual({ content: $($(this).attr("href")).html() });
  });
}

/* function init_modal_special
 * Initialise les comportements modaux sur les jury
 * @param NULL
 */
function init_modal_special(elmt_cible)  
{
  $(elmt_cible).click(function() {
			$.nyroModalManual({ content: $($(this).attr("href")).html() });
  });
}

function init_planlieux( mapname )
{
  $('a.detail_lieu').click( function()
  {
    var valeurhref = $(this).attr("href");
    var idhref     = valeurhref.substr( 1,9 ) + '('+mapname+')';
    
    $( "#details_lieux blockquote" ).each( function() {
      if( !$(this).hasClass( "masque" ) )
      {
        $(this).addClass( "masque" );
      }
      if( !$(this).hasClass( "hide" ) )
      {
        $(this).addClass( "hide" );
      }
    } );
    $( valeurhref ).removeClass( "masque" );
    $( valeurhref ).removeClass( "hide" );
    eval( idhref );
    return false;
  });
}

/* function init_liste_selection
 * Initialise la liste des films selectionnés (clic et tooltip)
 * @param NULL
 */
function init_liste_selection() 
{
  $("ul.liste_selection li").click(function() {
  $(location).attr('href',$("a.suite", this).attr("href"));
  });
  
  $("a.info_comp").simpletooltip();
}

/* function init_tooltip
 * Initialise les tooltip générique
 * @param NULL
 */
function init_tooltip()
{
  $("a.info_bulle").simpletooltip();
}

/* function init_player
 * Initialise un player
 * @param : player > type de player concerné
 * @param : id_html > id html concerné
 */
function init_player( player , id_html )
{
  switch(player)
  {
    case "jerowing":
      if(id_html == "extrait_son")
      {
        $("#"+id_html).flash({
          src: '/resources/swf/mediaplayer.swf',
          width: 344,
          height: 20,
          flashvars: {
            author: 'Etienne perruchon',
            description: 'Thême musical du festival d\'Annecy', 
            duration: '33',
            file :'/resources/son/annecy_festival.mp3',
            backcolor : '0x0d0d1a',
            frontcolor : "0xffffff" ,
            lightcolor : "0x96bd0d"
          }
        },
        { version: 9 }
        );
      }
    break;
  }
};

/* function init_init_home_networkplayer
 * Initialise les interaction du tableau de bord
 * @param NULL
 */
function init_home_network()  
{
  $(".liste_num li").click(function() {
    $(location).attr('href',$("a ", this).attr("href"));
  });
};

/* function init_agenda
 * Initialise les interaction de l'agenda
 * @param NULL
 */
function init_agenda()
{
  init_scroll();
  $('select[name="liste_type_evt[]"]').multiSelect({ oneOrMoreSelected: '*', selectAllText: 'Tout' }, function(obj_selected)  {
    /*
    jQuery.each(obj_selected, function(index, value) {
     console.log("index", index, "value", value);
    });
    */
    var id_type = obj_selected.val();
    
    if($(obj_selected).attr("checked")== false)
    {
      //console.log("cool"+obj_selected.val());
      $("#ss_type_"+id_type).parent().remove();
    }
      
    else
    {
      //Requette ajax qui va chercher la liste des sous-type (si il y en a)
      var param = 'param={"fct": "get_sous_type_evt_ajx", "param" : ["'+id_type+'"] }';
      $.ajax({ 
        type: "GET",
        url: "/ctrl_front.php",
        dataType : 'json',
        data : param,
        success: function(data_json)
        {
          succes_choix_type(data_json, id_type);
        }
      });
    }
  });

  $(".blc_rdv").hover(function(){
  
    var pos_blc = $(this).parent().position();
   
    var val_pos = pos_blc.left - ( $(this).parent().width() / 2 );
   
    //console.log("#agenda.width() / 3 > "+$("#agenda").width() / 4+" val_pos> "+val_pos+" $(#agenda).width() / 4 * 2> "+$("#agenda").width() / 4 * 2);
   
    if( val_pos < $("#agenda").width() / 4 )
    {
      var new_y = 15; 
      //console.log("if 1");
    }
    else if( val_pos > $("#agenda").width() / 4 * 2 ) 
    {
      var new_y = -( $(".blc_comp_rdv" , this).width() - 20 ); 
      //console.log("else 2");
    } 
    else 
    {
      var new_y = -( ( $(".blc_comp_rdv" , this).width() - 20 ) / 2 ); 
      //console.log("else 3");
    }

       
       
     
    $(".blc_comp_rdv" , this).css("left", new_y).show();
  },function(){
    $(".blc_comp_rdv" , this).hide();
  });
}

/* function succes_choix_type
 * Callback lorsqu'on choisi des types d'évènement
 * @param NULL
 */
function succes_choix_type(data_json, id_type)
{
  
  if(data_json.etat)
  {
    if( $("#recept_ss_types").hasClass("masque") )
      $("#recept_ss_types").removeClass("masque");
  
    //console.log(id_type);
    $("#recept_ss_types").append(data_json.code_html);
    $('select[name="projection_sstype_'+id_type+'[]"]').multiSelect({ oneOrMoreSelected: '*', selectAllText: 'Tout' }, function(obj_selected)  {
      actualisation_agenda();
    });
  }
  actualisation_agenda();
}

/* function init_scroll
 * Initialise le système de scroll
 * @param NULL
 */
function init_scroll()
{
  $('#content_agenda').jScrollHorizontalPane({showArrows:true, arrowSize:40});

  $("a.jScrollArrowLeft, a.jScrollArrowRight").fadeTo("fast", 0.5);
  
  $("a.jScrollArrowLeft, a.jScrollArrowRight").hover(function(){
    $(this).fadeTo("fast", 0.9);
  },
  function() {
    $(this).fadeTo("fast", 0.5);
  });
}

/* function init_search
 * Initialise le champ recherche
 * @param NULL
 */
function init_search()
{
  $('#inp_rech').val(search);
  
  $('#inp_rech').click(function(){
    if($(this).val() == search)
      $(this).val('');
  });
}

/* function actualisation_agenda
 * Récupère l'agenda en fonction des paramètres du formulaire
 * @param NULL
 */
function actualisation_agenda()
{
  //Ajaxisation du formulaire #form_agenda, renvoie des infos et actualisation dans #agenda
}

function init_fiche_prog()
{
  $('.infosbus a.link_infosbus').click(function(){
    $(this).parent().children('div.comp').toggle();
    $(this).toggleClass("suite").toggleClass("reduire");
    if($(this).html() == "<span>&nbsp;</span>"+cst_reduire)
      $(this).html( "<span>&nbsp;</span>"+cst_plus_infos )
    else
      $(this).html( "<span>&nbsp;</span>"+cst_reduire );
    return false;
  });

  if((window.location.hash) )
  {
    //alert(window.location.hash);
    $(window.location.hash).slideDown("fast").removeClass("masque").parent().find("a.fl_droite").html("<span>&nbsp;</span>"+cst_reduire).toggleClass("suite").toggleClass("reduire");
  }
}
