// $.noConflict();
var page_html; var accueil;
$(document).ready(function(){
///////////////////////////////////////////////////////////// menu	
 $("#menu li a").addClass("menunonsurvole");
 $("#menu li a").mouseover(function(){
  $(this).stop().animate({width: '70%'},100);
  $(this).animate({width: '85%'},100);
  $(this).addClass("menuover");
 });
 $("#menu li a").mouseout(function(){
//  $(this).stop().animate({width: '40%'},100);
//  $(this).animate({width: '85%'},100);
  $(this).removeClass("menuover");
 });			
// $("#menu li a").stop().click(function(){  //////////actions menu général
 $("#menu li").click(function(){
  $("#menu li ul").hide();
  $(this).children('ul').show();
 });
 $("#menu li a").stop().click(function(){  //////////actions menu général
  clearInterval(refreshImage);// pour diaporama
  $("#menu li a").removeClass("menuclic");
  $(this).addClass("menuclic");
  page_html = $(this).attr('id'); repert="./inc/"; 
  //if(page_html=="contact" || page_html=="galeries"){ repert="../constitusite/inc/";}else{ repert="./inc/"; }
  //if(page_html=="contact"){ repert="../constitusite/inc/";}

  if(page_html=="livredor"){ 
   repert="../constitusite/livredor/"; if(backo==true){page_html="lor_admin.php"}else{page_html="livredor.php";}
  }else{
   page_html="_"+page_html+".htm";
  }
  //$("#right_col").hide(500);  
  charge_page_htm(page_html,"#right_col",repert);  //$("#right_col").show(500);
 });
 ////////////////////////////////////////////////////////////// fin menu
 $("#menu li a[id='accueil']").click(); //au 1er passage
 $("#menu li a").animate({width: '85%'},100);
 ////////////////////////////////////////////////////////////// ouverture rideaux
 ouvrir_rideaux();
});

function liens_divers(){ // recherche propriétés div dans la page chargée pour gérér futurs clics
 $("#mf").show();// bouton modif fenetre visible
 $(".lien_right_col").click(function(){  //////////actions href vers right_col
   page_html = $(this).attr('id'); //.substr(1);
   //$("#right_col").hide(500); 
   charge_page_htm(page_html,"#right_col",""); //$("#right_col").show(500);							   
 });
 $(".lien_right_col_b").click(function(){  //////////actions href vers right_col 
  page_html = "_"+$(this).attr('id')+".htm";  //$(this).attr('id').substr(1)
  //$("#right_col").hide(500);
  charge_page_htm("inc/"+page_html,"#right_col",""); //$("#right_col").show(500);							   
 });
 $(".lien_fenetre").click(function(){  //////////actions id vers fenetre
  $("#mf").show();// bouton modif visible
  pagef_html = "_"+$(this).attr('id')+".htm";
  charge_page_htm("inc/"+pagef_html,"#centre_fenetre","");
  if(backo==true){ //backoffice ou non
   $("#dialog").dialog({ modal: false, buttons:{Fermer: function() {$( this ).dialog( "close" );}}, height: 600, width: 800, title: '&nbsp;' ,show: 800 });
  }else{
   $("#dialog").dialog({ modal: true, buttons:{Fermer: function() {$( this ).dialog( "close" );}}, height: 600, width: 800, title: '&nbsp;' ,show: 800 });
  }
 });
  $(".lien_pdf").click(function(){  //////////actions href vers right_col 
  page_pdf = "pdf/"+$(this).attr('id')+".pdf";  //$(this).attr('id').substr(1)
  fenetre(page_pdf,"y",1024,768) });
 //$("#albums").ready(albums_picasa(););
}

//////////////////////////////////////////////////
function charge_galerie(titre_album,albm){
 nom_album=albm;
 $("#dialog").dialog({ modal: true, height: 700, width: 950, title: titre_album  ,show: 800 });
 $("#mf").hide(); //cache bouton modif
 $("#centre_fenetre").load("../constitusite/inc/_galerie.htm");
}

////////////////////////////////////////////////// Diaporama Picasa accueil

var nom_photos = new Array();  var titre_photos= new Array(); var nb_photos=0;
var randomnumber=0; var randomnumber0=0;  var fin_interval=false; var refreshImage;

function diaporama_picasa(){  //pwi.js lance photos tournantes1
 accueil="diaporama";
 $(".diaporama").append("<table><tr><td id='diaporama' width='600px' height='380px' align='center' valign='middle'>Chargement du diaporama en cours ...<br /><br /><br />  <img src='../constitusite/images/charge.gif' width='20' height='20' />  </td></tr></table>");
 drapeau="accueil";
 $(document).ready(function() {
  $("#right_col").pwi({
   username: nom_picasa,
   maxresults: 1,
   mode: 'album',
   album: 'accueil'  /*obligatoire*/
  });
 });
}

function photos_tournantes1(){
 if(nb_photos>0){
  fin_interval=false;
  ii=randomnumber0=Math.floor(Math.random()* nb_photos);
  charge_aff_photo(ii); //pour eviter attente boucle setInterval
  refreshImage = setInterval(function() {
   //randomnumber=randomnumber0; while(randomnumber==randomnumber0){ randomnumber=Math.floor(Math.random()* nb_photos); }
   //ii=randomnumber0=randomnumber;
   ii++; if(ii>=nb_photos){ii=0;}
   charge_aff_photo(ii);
  }, 6500);
 }else{ ii=0; alert("Pas de photo dans l'album Picasa: 'Accueil'");}
}

function charge_aff_photo(ii){
 $("#diaporama").fadeTo(1500, 0.2, function(){ 
  $('#diaporama').empty();
  $('<img id="imgdiap" />')   
   .attr('src',nom_photos[ii]) //width=550 height=420
   .load(function(){ 
	 $('#diaporama').append($(this)); 
      h=$("#imgdiap").height();  l=$("#imgdiap").width();
	  if(h/450 > l/565){$("#imgdiap").height(450);}else{$("#imgdiap").width(565);}
	 $('#diaporama').fadeTo(2000,1);
   });
 });
}
/////////////////////////////////////////////  Fin diaporama

//////////////////////////////////////////// Galeries Picasa
function albums_picasa(){
 drapeau="galeries"; //pour appeler seulement les galeries
 $(document).ready( function() {
      $(".albums_picasa").pwi({
         username: nom_picasa,
		 maxResults: 5,
		 showAlbumDescription: false,
		 showPhotoCaption: true,
		 photoCaptionLength: 5,
		 showPhotoCaptionDate: false
   });
 });
}
///////////////////////////////////////////
///////////////////////////////////////////  Formulaire contact
function formulaire_contact(){
 $(document).ready( function() {
  load_page_html("_contact.htm",".form_contact","../constitusite/inc/");
 });
}
///////////////////////////////////////////
function charge_page_htm(pagehtml,destination,repertoire){ // ex: pagehtml="test.html"  destination="#results"
 // nota : le load n'est pas utilisé car si la page n'est pas chargée liens_divers ne marche pas
 if(backo==true){vcache=false;}else{vcache=true;} //backo; pour forcer le chargement sur le serveur
 $.ajax({
   url: repertoire+pagehtml,
   async: false,
   cache: vcache, // si vcache false force le chargement serveur
   success: function(html){
    html=html.replace('<img src="../constitusite/images/diaporama_picasa.png" />',"<div class='diaporama'><script type='text/javascript'>diaporama_picasa();</script></div>");
    html=html.replace('<img src="../constitusite/images/albums_picasa.png" />',"<div class='albums_picasa'><script type='text/javascript'>albums_picasa();</script></div>");
    html=html.replace('<img src="../constitusite/images/form_contact.png" />',"<div class='form_contact'><script type='text/javascript'>formulaire_contact();</script></div>");
	//html=utf8_encode(html);
    $(destination).empty(); $(destination).hide(); $(destination).append(html); $(destination).show(500);
	liens_divers(); //pour attribuer les propriétés éventuelles à la page chargée ci-dessus
   }
  });
// }else{
//  $(destination).load(repertoire+pagehtml); // pour accélérer si pas retour backo.
// }
}

function load_page_html(pagehtml,destination,repertoire){
 $(destination).load(repertoire+pagehtml); // pas de sous fonction ajax possible par le div
}

////////////////////  pour submit contact.htm  livreor.php
function submit_form(destina){
 $('#form_g').submit(function() { // catch the form's submit event - should I use the form id? 
    $.ajax({ // create an AJAX call... 
        //async: false,
        data: $(this).serialize(), // get the form data 
        type: $(this).attr('method'), // GET or POST 
        url: $(this).attr('action'), // the file to call 
        success: function(response) { // on success.. 
          if(response=="ok"){ window.location = "backoffice.php"; }else{ $(destina).html(response); } // update the DIV - should I use the DIV id? 
        } 
 }); 
 return false; // cancel original event to prevent form submitting 
 });
}

function connexion(){
 $("#dialog").load("../constitusite/backoffice/identification.php"); 
 $("#dialog").dialog({ modal: true,  height: 330, width: 400, title: 'Connexion a Constitusite.'  ,show: 800 });
}

function fermef(){$("#dialog").hide();}

//////////////////////////////////////////////// chargement style général et menu en fonction de la variable styleset
function charge_css(){
 document.write('<link rel="stylesheet" type="text/css" href="../constitusite/css/stylesheet"+styleset+".css" media="screen"/>');
 document.write('<link rel="stylesheet" type="text/css" href="../constitusite/menu/stylemenu"+styleset+".css" media="screen"/>');
 document.write('<link rel="stylesheet" type="text/css" href="../constitusite/css/panneaux"+styleset+".css" media="screen"/>');
}
var fen1 = null;
function fenetre(ob,sb,l,h) {
    if ( fen1 )	fen1.close();
	fen1 = window.open(ob,"Popup","alwaysRaised=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+sb+",resizable=yes,dependent=yes,width="+l+",height="+h);
   	fen1.focus();  fen1.resizeTo(l,h);
}

