/*---------------------------------------*/
/*---------INFOBULLES------------*/
/*---------------------------------------*/
var xOffset=6
var yOffset=5    

var affiche = false; // La variable i nous dit si le bloc est visible ou non
var w3c=document.getElementById && !document.all;
var ie=document.all;

if (ie||w3c) {
  var laBulle
}

function ietruebody(){  // retourne le bon corps...
  return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function deplacer(e) {
  if(affiche){
    var curX = (w3c) ? e.pageX : event.x + ietruebody().scrollLeft;
    var curY = (w3c) ? e.pageY : event.y + ietruebody().scrollTop;

    var winwidth = ie && !window.opera ? ietruebody().clientWidth : window.innerWidth - 20;
    var winheight = ie && !window.opera ? ietruebody().clientHeight : window.innerHeight - 20;

    var rightedge = ie && !window.opera ? winwidth - event.clientX - xOffset : winwidth - e.clientX - xOffset;
    var bottomedge = ie && !window.opera ? winheight - event.clientY - yOffset : winheight - e.clientY - yOffset;

    var leftedge = (xOffset < 0) ? xOffset*(-1) : -1000

    // modifier la largeur de l'objet s'il est trop grand...
    if(laBulle.offsetWidth > winwidth / 2){
      laBulle.style.width = winwidth / 2
    }

    // si la largeur horizontale n'est pas assez grande pour l'info bulle
    if(rightedge < laBulle.offsetWidth){
      // bouge la position horizontale de sa largeur à gauche
      laBulle.style.left = curX - laBulle.offsetWidth + "px"
    } else {
      if(curX < leftedge){
        laBulle.style.left = "5px"
      } else{
        // la position horizontale de la souris
        laBulle.style.left = curX + xOffset + "px"
      }
    }

    // même chose avec la verticale
    if(bottomedge < laBulle.offsetHeight){
      laBulle.style.top = curY - laBulle.offsetHeight - yOffset + "px"
    } else {
      laBulle.style.top = curY + yOffset + "px"
    }
  }
}
function montre(text) {
  if (w3c||ie){
    laBulle = document.all ? document.all["bulle"] : document.getElementById ? document.getElementById("bulle") : ""
    laBulle.innerHTML = text; // fixe le texte dans l'infobulle
    laBulle.style.visibility = "visible"; // Si il est cachée (la verif n'est qu'une securité) on le rend visible.
    affiche = true;
  }
}
function cache() {
  if (w3c||ie){
    affiche = false
    laBulle.style.visibility="hidden" // avoid the IE6 cache optimisation with hidden blocks
    laBulle.style.top = '-1000px'
    laBulle.style.backgroundColor = ''
    laBulle.style.width = ''
  }
}

document.onmousemove = deplacer; // des que la souris bouge, on appelle la fonction move pour mettre a jour la position de la bulle.

/*---------------------------------------*/
/*AFFICHER-CACHER UN DIV*/
/*---------------------------------------*/

  
  function AfficheCache(Id)
{
	if (document.getElementById(Id))
	{
		var div = document.getElementById(Id);
		if(div.style.display == 'none')
		{
			div.style.display = 'block';
		}
		else
		{
			div.style.display = 'none';
		}
	}
	return false; 
}
/*------------------------------------------------------*/
/*AGRANDIRE FORMULAIRE AREA TUTO*/
/*-----------------------------------------------------*/
function textarea_resize(pix)
{
        var box = document.getElementById('textarea'); ;
        var cur_height = parseInt( box.style.height ) ? parseInt( box.style.height ) : 400;
        var new_height = cur_height + pix;
       
        if ( new_height > 0 )
        {
                box.style.height = new_height + "px";
        }
       
        return false;
}
function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}
function EcrireCookie(nom, valeur)
{
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}
function getCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
		while (i<clen)
		{
			var j=i+alen;
			if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
			i=document.cookie.indexOf(" ",i)+1;
			if (i==0) break;

		}
return null;
}

function EffaceCookie(nom)
{
	date=new Date;
	date.setFullYear(date.getFullYear()-1);
	EcrireCookie(nom,null,date);
}

/*---------------------------------------*/
/*CHARGEMENT D'UNE PARTIE DE LA PAGE AVEC AJAX ET JQUERY*/
/*Utilisé dans Profil - ... - ...*/
// On peut modifier la vitesse par slow, fast, 1500, ... ET l'effet par slideUp, fadeOut, ...
/*---------------------------------------*/


function ajax_page_advanced(ele,msg,url){
        $(ele).slideUp("slow", function(){
                $(ele).html(msg).show("slow", function(){
                        $(ele).load(url+" "+ele, null, function(){
                                var tampon = $(ele).html();
                                $(ele).html(msg).hide("slow",function(){
                                        $(ele).html(tampon);
                                        $(ele).slideDown("slow");
                                });
                        });
                });
        });
}
/* Fonction de chargement ajax simple */
function ajax_page(ele,msg,url){
        $(ele).html(msg).load(url+" "+ele);
}
/* Une fois la page chargée */
$(document).ready(function(){
        //Application du chargement ajax simple sur tous les liens se trouvant dans le conteneur "menu"
        $("#liensAjaxer a").click(function(){
                ajax_page('#DivACharger','<p><img src="img/icones/loader.gif" alt="Chargement"></p>',this.href);
                return false;
        });
});
	
			$().ready(function() {
			function findValueCallback(event, data, formatted) {
				$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
			}
			function formatItem(row) {
				return row[0] + " (<strong>id: " + row[1] + "</strong>)";
			}
			function formatResult(row) {
				return row[0].replace(/(<.+?>)/gi, '');
			}
			$("#localite").autocomplete('/inc/array_villes.php', {
				width: 170,
				max: 6,
				minChars: 0,
				formatResult: formatResult
			});
			$("select").change(function() {
			
			if($('#statut:option').val() == 2) $('#recherche_musicien_input').slideDown();
			else $('#recherche_musicien_input').slideUp();
			});
			
			
			
			function multi_select(element_id,nb_max){
			  var sel = $(element_id);
			  var opts = $(element_id+' option');
			  for(var i=0,l=opts.length;i<l;i++) {
			    opts[i].onmousedown = save_selected;
			    opts[i].onclick = (function(option,index) {
			      var bool = false;
			      return function() {
			        option.selected = bool = !bool;
			        nb += bool ? 1 : -1;
			        restore_selected(index);
			        if(nb>nb_max) {
			          alert("Vous ne pouvez en choisir que seulement "+nb_max+" !");
			          option.selected = bool = !bool;
			        }
			      };
			    })(opts[i],i);
			  }
			  var save, nb = 0;
			  function save_selected() {
			    save = [];
			    for(var i=0,l=opts.length;i<l;i++) {
			      save.push(opts[i].selected);
			    }
			    nb = save.filter(function(el) { return el; }).length;
			  }
			  function restore_selected(index) {
			    for(var i=0,l=opts.length;i<l;i++) {
			      if(i!=index) {
			        opts[i].selected = save[i];
			      }
			    }
			  }
			  }
			
$("select").click(multi_select("#instru",6));
$("select").click(multi_select("#instru_rech",6));
$("select").click(multi_select("#style",10));


			});
