

$(document).ready(function() {
	
	//$("#contenu-encart").css('display','none') ;
	
	var ie_version = getIEVersion() ;
	
	// rectification hauteur encart banniere
	/*var height = $("#texte-encart").height() ;
	if(height != 35) {
		var new_height = parseInt($("#contenu-encart").height() - 19) ;
		$("#contenu-encart").css('height',new_height) ;
	}*/
	
	//$("#contenu-encart").css('height',new_height) ;
	//$("#contenu-encart").css('display','block') ;
	
	
	
	// evenements formulaire recherche
	initTextZoneForm('recherche-nom','ex : Entreprise Dupont') ;
	initTextZoneForm('recherche-activite','ex : Coiffure mixte, chignons') ;
	initTextZoneForm('recherche-ville','ex : Besançon') ;
	
	
	// prechargement images banniere
	prechargerImages(new Array(
		'./img/banniere1.jpg',
		'./img/banniere2.jpg',
		'./img/banniere3.jpg',
		'./img/banniere4.jpg',
		'./img/banniere5.jpg',
		'./img/banniere6.jpg',
		'./img/contenu/abcdaire.jpg',
		'./img/contenu/salon.jpg',
		'./img/contenu/animations-originales.jpg',
		'./img/contenu/globe.jpg',
		'./img/contenu/memo.jpg'
	)) ;
	
	// autocompletion
	if($('#recherche-ville').size() > 0) {
		autocompletion() ;
	}
	
	// checkbox
	if(!ie_version) { //!= 6 && ie_version != 5) {
		$('#form-recherche input[type=checkbox]').prettyCheckboxes();
	}
	
	// select
	if( $("#recherche-nomenclature").size() > 0) {
		selectReplacement(document.getElementById("recherche-nomenclature")) ;
	}
	
	// fenetres dialogues (etab)
	if($(".item-etab").size() > 0) {
		$(".item-etab .infos a").click(function() {
			dialogueEtab($(this).parent().parent().find(".id").html()) ;
			return false ;
		}) ;
		$(".item-etab .more a").click(function() {
			dialogueEtab($(this).parent().parent().find(".id").html()) ;
			return false ;
		}) ;
	}	
	
	
	// lien organiser
	$("#organiser a").click(function() {
		
		if($("#sous-menu").css('top') == '-102px') {
			$(location).attr('href',$("base").attr("href")+'organiser-evenement/');
		}else {
			$("#sous-menu").animate({
				top: -102
			},function() {
				if($("#sous-menu").css('top') == '-102px') {
					$("#curseur").fadeIn() ;
					$(location).attr('href',$("base").attr("href")+'organiser-evenement/');
				}
			}) ;
		}	
		return false ;
	}) ;
	
	// lien rechercher
	$("#rechercher a").click(function() {
		
		//alert('effectuer une recherche') ;
		
		if($("#sous-menu").css('top') == '-102px') {
			
			$("#curseur").css('display','none') ;
			
			$("#sous-menu").animate({
				top: -180
			},function() {
				$(location).attr('href',$("base").attr("href"));	
			}) ;
		}else {
			//alert($("#sous-menu").css('top')) ;
			$(location).attr('href',$("base").attr("href"));	
		}
		return false ;
	}) ;
	
	
	
	// liens sous menu
	evenementsLiensSousMenu() ;

	// curseur placé sur le lien sélectionné
	if($("#sous-menu a.selec").size() > 0) {
		bougeCurseur($("#sous-menu a.selec")) ;
	}
});

function ajusteHauteurEncartBanniere() {

	$("#contenu-encart").css('height',parseInt($("#contenu-encart").height() - 19)) ;

}

function prechargerImages(arr_chemins_images) {
	
	var i ;
	var src_img_tmp ;
	arr_images = new Array() ;
	
	for(i=0 ; i<arr_chemins_images.length ; i++) {
		arr_images[i] = new Image() ;
		arr_images[i].src = arr_chemins_images[i];
	}
	
}


function evenementsLiensSousMenu() {
	$("#sous-menu a").click(function() {
		bougeCurseur($(this)) ;
		return false ;
	}) ;
}

function bougeCurseur(lien) {
	
	
	
	var offset_menu = $("#sous-menu").offset() ;
	var offset_lien = lien.offset() ;
	var left = offset_lien.left - offset_menu.left + (lien.width() / 2) - 10  ;
	
	if($("#curseur").css('display') == 'none') {
		$("#curseur").css('left',left) ;
		$("#curseur").css('display','block') ;
	}else {
		$("#curseur").animate({
			left: left
		},
		300,
		function() {
			//alert($("base").attr("href")+lien.attr('href')) ;
			//$(location).attr('href',lien.attr('href'));
			//alert($("base").attr("href")+lien.attr('href')) ;
			$(location).attr('href',$("base").attr("href")+lien.attr('href'));
		}) ;
	}
}

function dialogueEtab(id_etab) {
	
	$("#dialog-etab").dialog('destroy') 
	
	$.post(	"ajax_etab.php",
			{ajax:1,
			id:id_etab},
			function(data) {
				
				$("#dialog-etab").html(data) ;
				
				$("#dialog-etab").dialog({
					//bgiframe: true,
					draggable: false,
					resizable: false,
					modal: true,
					minWidth: 550,
					width: 550
				}) ;
				
				$(".ui-dialog .ui-dialog-titlebar a").html('<span class="dore">X</span> Fermer') ;
				$(".ui-dialog-titlebar").addClass('clear-both') ;
			}
	) ;
	
	
}

function initTextZoneForm(input,txt) {
	
	// focus
	$("#"+input).focus(function() {
		// value
		if(	$(this).val() == txt) {
			$(this).val('') ;
		}
		// css
		$(this).css('color','#585857') ;
		$(this).css('font-style','normal') ;
	}) ;
	
	// blur
	$("#"+input).blur(function() {
		// value
		if(	$(this).val() == '') {
			$(this).val(txt) ;
			
			// css
			$(this).css('color','gray') ;
			$(this).css('font-style','italic') ;
		}
	}) ;
}

function autocompletion() {
	
	// chamgement du nom de l input commune
	// pour bug du div qui se met au dessus de
	// la liste de suggestions
	var input_commune = $('#recherche-ville') ;
	input_commune.attr('name','recherche-ville-javascript-'+timestamp()) ;
	
	// autocomplete
	$('#recherche-ville').keyup(function(event) { 
	
		var key_code =  toucheClavier(event) ;
		switch(key_code) {
			// haut
			case 38 :
				
				if($("#autocomplete-commune ul li.hover").size() > 0) {
				
					var li_current = $("#autocomplete-commune ul li.hover") ;
					var li_prev = li_current.prev() ;	
					
					if(li_prev.is('li')) {
						li_current.removeClass('hover') ;
						li_prev.addClass('hover') ;	
					}
				}
				
				break ;
				
			// bas
			case 40 :
				
				if($("#autocomplete-commune ul li.hover").size() == 0) {
					$("#autocomplete-commune ul li:first").addClass('hover') ;	
				}else {
					var li_current = $("#autocomplete-commune ul li.hover") ;
					var li_next = li_current.next() ;				
					
					if(li_next.is('li')) {
						li_current.removeClass('hover') ;
						li_next.addClass('hover') ;	
					}
				}
				
				break ;
				
			// entree
			case 13 :
				if($("#autocomplete-commune ul li.hover").size() == 1) {
					 majValueAutocomplete($("#autocomplete-commune ul li.hover .ville").html());
				}
				break ;
			
			// autocomplete
			default :
				autocomplete($(this).val()) ;
				break ;
		}
		
		
	
	}) ;
	
	$('#recherche-ville').blur(function() { majValueAutocomplete();}) ;
	
	// empeche la touche entrée sur commune
	$('#recherche-ville').keypress(function(event) {
		return noEnter(event) ;
	}) ;	
}


function autocomplete(txt) {
	
	if(txt.length == 0) {
		// Hide the suggestion box.
		$('#autocomplete-commune').hide();
	} else {
		$.post(	"ajax_commune.php", 
				{ 	ajax:1, 
					cdf: 1,
					texte : ""+txt+"" }, 
				function(data){
					if(data.length >0) {
						$("#autocomplete-commune").css('zIndex','100');
						$('#autocomplete-commune').show();
						$('#autocomplete-commune').html(data);
						
						$('#autocomplete-commune ul li').mouseover(function() {
							$('#autocomplete-commune ul li').removeClass('hover') ;
							$(this).addClass('hover') ;
						}) ;
						$('#autocomplete-commune ul li').mouseout(function() {
							$(this).removeClass('hover') ;
						}) ;
							
						$('#autocomplete-commune ul li span,#autocomplete-commune ul li').click(function() {
							if($("#autocomplete-commune ul li.hover").size() == 1) {
								 majValueAutocomplete($("#autocomplete-commune ul li.hover .ville").html());
							}
						});
					}else {
						majValueAutocomplete() ;
					}
		});
	}
} 

function majValueAutocomplete(value) {
	
	$('#recherche-ville').val(value);
	setTimeout("$('#autocomplete-commune').hide();", 200);

}


function timestamp() {
	
	var timestamp = Math.round((new Date()).getTime()/1000);  
	return timestamp ;
	
}

function noEnter(event) {
	return !(toucheClavier(event) == 13); 
	
}


function toucheClavier(event) {
	
	var touche = window.event ? event.keyCode : event.which;
	return touche ;
	
}

/*
function selectReplacement(obj) {
	  var ul = document.createElement('ul');
	  ul.className = 'selectReplacement';
	  // collect our object’s options
	  var opts = obj.options;
	  // iterate through them, creating <li>s
	  for (var i=0; i<opts.length; i++) {
	    var li = document.createElement('li');
	    var txt = document.createTextNode(opts[i].text);
	    li.appendChild(txt);
	    ul.appendChild(li);
	  }
	  // add the ul to the form
	  obj.parentNode.appendChild(ul);
}
*/

function selectReplacement(obj) {
	$("#recherche-nomenclature").css('display','none') ;
	
	var one_option = '' ;
	
    // append a class to the select
    obj.className += ' replaced';
    // create list for styling
    var ul = document.createElement('ul');
    ul.className = 'selectReplacement';
    var opts = obj.options;
    for (var i=0; i<opts.length; i++) {
      var selectedOpt;
      if (opts[i].selected) {
        selectedOpt = i;
        break;
      } else {
        selectedOpt = 0;
      }
    }
    
    
    for (var i=0; i<opts.length; i++) {
      var li = document.createElement('li');
      var txt = document.createTextNode(opts[i].text);
      li.appendChild(txt);
      li.selIndex = opts[i].index;
      li.selectID = obj.id;
      li.onclick = function() {
        selectMe(this);
      }
      if (i == selectedOpt) {
    	
    	one_option = opts[i].text ;    	
    	$(".ligne-form-nomen").append('<div id="new-select-nomenclature"><div id="one-option">'+one_option+'</div><div id="all-options"></div></div>') ;

        li.className = 'selected';
        
        $("#one-option").mouseover(function() {
        	if($("#all-options").css('display') == 'none') {
        		$("#gestion-focus").focus() ;
        		$(".selectReplacement").addClass('selectOpen') ;
            	$("#all-options").css('display','block') ;
        	}else {
        		/*$(".selectReplacement").removeClass('selectOpen') ;
            	$("#all-options").css('display','none') ;*/
        	}
        	
        	return false ;
        }) ;
      }
      
      if (window.attachEvent) {
        li.onmouseover = function() {
          this.className += ' hover';
        }
        li.onmouseout = function() {
          this.className = 
            this.className.replace(new RegExp(" hover\\b"), '');
        }
      }
      ul.appendChild(li);
    }
    
    $("#all-options").mouseover(function() {
    	return false ;
    }) ;
    
    $("body").mouseover(function() {
    	if($("#all-options").css('display') == 'block') {
    		$(".selectReplacement").removeClass('selectOpen') ;
    		$("#all-options").css('display','none') ;
    	}
	}) ;
    
    document.getElementById('all-options').appendChild(ul) ;

}


function selectMe(obj) {
	
	$("#all-options").css('display','none') ;
	
	$("#one-option").html(obj.innerHTML) ;
	
	if(obj.innerHTML == 'Sélectionnez une activité') {
		$("#one-option").css('color','gray') ;
	}else {
		$("#one-option").css('color','#585857') ;
	}
	
	setVal(obj.selectID, obj.selIndex);
	
    var lis = obj.parentNode.getElementsByTagName('li');
    for (var i=0; i<lis.length; i++) {
      if (lis[i] != obj) { // not the selected list item
        lis[i].className='';
        lis[i].onclick = function() {
          selectMe(this);
        }
     } else {
        obj.className='selected';
        obj.parentNode.className = 
          obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
        obj.onclick = function() {
          obj.parentNode.className += ' selectOpen';
          this.onclick = function() {
            selectMe(this);
          }
        }
      }
    }
  }

function setVal(objID, selIndex) {
	
	var obj = document.getElementById(objID);
	obj.selectedIndex = selIndex;
}


function getIEVersion() {
	  if (!!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1)) {
	    var ver = navigator.userAgent.match(/MSIE ([0-9.]+);/);
	    if (ver != null && ver[1] != undefined) {
	      ver = parseInt(ver[1]);
	      if (ver == 7) {
	        if (typeof (window.external.AddToFavoritesBar) != "undefined" ||
	            typeof (window.external.AddService) != "undefined" ||
	            typeof (window.external.AddService) != "undefined") return 8;
	      }
	      return ver;
	    }
	  }
	  return false;
}

	  
	  

