jQuery(document).ready(function() { 
	/*var countMajPlus = 1;//Quand le document est chargé on exécute une fonction
    $(".bouton_maj_plus").click(addItemsEventList('all')); //On ferme la fonction du onclick*/
	

	/* Carte de l'ile de France */
	if ( jQuery('#idf').size() )
	{
		jQuery('#carte_idf img.image_idf').each(function(){
			jQuery(this).mouseout(function(){
				jQuery(this).hide();
			});
		});
		
		jQuery('#idf .idf').mouseover(function(){
			var fond = jQuery('#image_' + jQuery(this).attr('id')).attr('src');
			var haut = jQuery('#image_' + jQuery(this).attr('id')).css('margin-top');
			var gauche = jQuery('#image_' + jQuery(this).attr('id')).css('margin-left');
			
			jQuery('#idf_affiche').css('background-image', 'url("' + fond + '")');
			jQuery('#idf_affiche').css('background-position', gauche + ' ' + haut);
		});
		

		jQuery('#idf .idf').mouseout(function(){
		//alert('ok');
			jQuery('#idf_affiche').removeAttr('style');
		});		
	}

	/* Carte de Paris */
	if ( jQuery('#paris').size() )
	{
		jQuery('#carte_paris img.image_paris').each(function(){
			jQuery(this).mouseout(function(){
				jQuery(this).hide();
			});
		});
		
		jQuery('#paris .paris').mouseover(function(){
			var fond = jQuery('#image_' + jQuery(this).attr('id')).attr('src');
			var haut = jQuery('#image_' + jQuery(this).attr('id')).css('margin-top');
			var gauche = jQuery('#image_' + jQuery(this).attr('id')).css('margin-left');
			
			jQuery('#paris_affiche').css('background-image', 'url("' + fond + '")');
			jQuery('#paris_affiche').css('background-position', gauche + ' ' + haut);
		});
		

		jQuery('#paris .paris').mouseout(function(){
			jQuery('#paris_affiche').removeAttr('style');
		});		
	}
	
	/* Carte de France */
	if ( jQuery('#departements').size() )
	{
		jQuery('#carte_france img.image_departement').each(function(){
			jQuery(this).mouseout(function(){
				jQuery(this).hide();
			});
		});
		
		jQuery('#departements .departement').mouseover(function(){
			var fond = jQuery('#image_' + jQuery(this).attr('id')).attr('src');
			var haut = jQuery('#image_' + jQuery(this).attr('id')).css('margin-top');
			var gauche = jQuery('#image_' + jQuery(this).attr('id')).css('margin-left');
			
			jQuery('#departement_affiche').css('background-image', 'url("' + fond + '")');
			jQuery('#departement_affiche').css('background-position', gauche + ' ' + haut);
		});
		

		jQuery('#departements .departement').mouseout(function(){
			jQuery('#departement_affiche').removeAttr('style');
		});		
	}
	
	
	$(".note").click(function() 
	{
		var id=idNum($(this).attr('id'));
		var type = idType($(this).attr('id'));
		for (var i=1;i<=5;i++)
			{
			  if(i<=id) $('#'+type+'_'+i).attr({src:srcNoteIn});	
			  else if(i>id) $('#'+type+'_'+i).attr({src:srcNoteOut});
			  if(i==id) $('#'+type).attr({value:i}); // affectation de la note au formulaire
			}
		
	});
	$("#commentaire").keyup(function(){
	if($("#commentaire").val()!=""){
		$("#commentaire").css("border","2px green solid");
	}
	});
	
	$(".example5").colorbox();
	
	removeAllCat();
	
	$(".categorie_element_type").toggle(
		function(){
			removeAllCat();
			$(this).parent().find(":last-child").find("img").attr({src:PATH_GEN+'img/plus_bleu.png'}).attr({src:PATH_GEN+'img/moins.png'});
			$(this).parent().next('.categorie_element_detail').toggle();
			$(this).parent().addClass("element_actif");
		}
		,function(){
			removeAllCat();
			$(this).parent().find(":last-child").find("img").attr({src:PATH_GEN+'img/moins.png'}).attr({src:PATH_GEN+'img/plus_bleu.png'});
			$(this).parent().next('.categorie_element_detail').css('display','none');
		}
	);
	$(".categorie_element_image").toggle(
		function(){
			//$(this).parent().css("background","#00A6FF");
			removeAllCat();
		//$(this).parent().css("color","#FFFFFF");
			$(this).parent().addClass("element_actif");
			$(".categorie_element_image").find(":first-child").attr({src:PATH_GEN+'img/plus_bleu.png'});
			$(this).find(":first-child").attr({src:PATH_GEN+'img/moins.png'});
			$(this).parent().next('.categorie_element_detail').css('display','block');
		},function(){
			removeAllCat();
			$(this).parent().find(":last-child").find("img").attr({src:PATH_GEN+'img/moins.png'}).attr({src:PATH_GEN+'img/plus_bleu.png'});
			$(this).parent().next('.categorie_element_detail').css('display','none');
		}
	);
	
	/*$(".categorie_element_type").click(function(){
		//$(this).parent().css("background","#00A6FF");
		removeAllCat();
		//$(this).parent().css("color","#FFFFFF");
		$(this).parent().addClass("element_actif");
		$(".categorie_element_image").find(":first-child").attr({src:PATH_GEN+'img/plus_bleu.png'});
		$(this).find(":first-child").attr({src:PATH_GEN+'img/moins.png'});
		$(this).parent().next('.categorie_element_detail').toggle();
	});
	*/

	 
	$(".catDetail").click(function(){
		if($(this).html()==' + ')
			{$(this).html(' - ');
			 $('.categorie_element_detail_sub').css('display','none');
			 $(this).parent().next('.categorie_element_detail_sub').css('display','block');}
		else{
			$(this).html(' + ');
			$(this).parent().next('.categorie_element_detail_sub').css('display','none');
		}
	});
	
	$(".catDetailSub").click(function(){
		if($(this).html()==' + ')
			{$(this).html(' - ');
			 $('.categorie_element_detail_sub2').css('display','none');
			 $(this).parent().next('.categorie_element_detail_sub2').css('display','block');}
		else{
			$(this).html(' + ');
			$(this).parent().next('.categorie_element_detail_sub2').css('display','none');
		}
	});
	
/*  var map = new GMap2(document.getElementById('map_canvas')); 

  var city = new GLatLng(44.797916,-93.278046); 

  map.setCenter(city, 8); */


	//initializeMapStore();
 });
 
 
var PATH_GEN ="/";
//var PATH_GEN ="/tmp/";
 
var countMajPlus = 1;
var actionEventList = 'all';

var jaimeOff=PATH_GEN+'img/fiche/bouton_jaime_off.png';

var tel_off=PATH_GEN+'img/fiche/bouton_telephone_off.png';

var srcNoteIn=PATH_GEN+'img/fiche/note_bleue_1.png'; //image au survol
var srcNoteOut=PATH_GEN+'img/fiche/note_bleue_0.png'; // image non survolée

var postRating=false;
var postPhoto=false;
var postPhotoCity=false;

function idNum(id)
  {
    var id=id.split('_');
    var id=id[1];
    return id;
  }
  
 function idType(id)
  {
    var id=id.split('_');
    var id=id[0];
    return id;
  }

function loadEvent(action,limit){
	$.ajax({                    //On débute ajax
          async: "true",          //Asynchrone "true" pour vrai "false" pour faux
          type: "GET",            // Type, "GET" ou "POST"
          url: PATH_GEN+"ajax/events_feed.php",      //Url de la page à charger
          data: "limit="+limit+"&action="+action,   //Données s'il y en a dans ce cas oui(l'email et une action)
          error: function(errorData) { $("#error").html(errorData); },  //S'il y a une erreur on écrit quelque chose
          success: function(data) { $("#maj_liste ul").html(data); $("#error").append("Contenu chargé");}
		 });
}


function addItemsEventList(){
	countMajPlus++;
	var limit=countMajPlus*10;
	loadEvent(actionEventList,limit);
}



function changeEventList(action){
	
	$('#filtre_all').removeClass("actif");
	$('#filtre_photo').removeClass("actif");
	$('#filtre_store').removeClass("actif");
	$('#filtre_rating').removeClass("actif");
	$('#filtre_favorite').removeClass("actif");
	$('#filtre_'+action).addClass("actif");
	loadEvent(action,10);
	actionEventList=action;
	countMajPlus = 1; 
}

/*function login(email,password){
	$.ajax({                    //On débute ajax
          async: "true",          //Asynchrone "true" pour vrai "false" pour faux
          type: "GET",            // Type, "GET" ou "POST"
          url: "ajax/login.php",      //Url de la page à charger
          data: "email="+email+"&password="+password,   //Données s'il y en a dans ce cas oui(l'email et une action)
          error: function(errorData) { $("#error").html(errorData); },  //S'il y a une erreur on écrit quelque chose
          success: function(data) { $("#maj_liste").html(data); $("#error").append("Contenu chargé");}
		 });
}*/

function resultIsLogged(){
		$.ajax({                    //On débute ajax
          async: "true",          //Asynchrone "true" pour vrai "false" pour faux
          type: "GET",            // Type, "GET" ou "POST"
          url: PATH_GEN+"ajax/userLogged.php",      //Url de la page à charger
          error: function(errorData) { $("#error").html(errorData); },  //S'il y a une erreur on écrit quelque chose
          success: function(data) { 
			$("#recherche_login").addClass("logged");
			$("#login").addClass("login_logged");
			$("#login").html(data);}
		 });
}




/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
	request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
	request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http = createObject();



/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function login() {
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var email = encodeURI(document.getElementById('login_email').value);
	var psw = encodeURI(document.getElementById('login_mdp').value);
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', PATH_GEN+'ajax/login.php?email='+email+'&psw='+psw+'&nocache = '+nocache);
	http.onreadystatechange = loginReply;
	http.send(null);
	}
	function loginReply() {
	if(http.readyState == 4){ 
	var response = http.responseText;
	if(response == 0){
	// if login fails
	// else if login is ok show a message: "Welcome + the user name".
	} else {
		window.location.reload();
	}
	}
}

function addRating(user_id,store_id){
	if($("#commentaire").val()==""){
		$("#commentaire").css("border","2px red solid");
	}
	else{
		if(user_id!=0){
			submitRating(user_id,store_id, $("#reception").attr('value'), $("#environment").attr('value'), $("#vfm").attr('value'), $("#service").attr('value'), $("#commentaire").val());
		}
		else{
			postRating = store_id;
			$.colorbox({href:PATH_GEN+'ajax/ajaxLogin.php'});
		}
	}
}

function addPhoto(user_id,store_id){
		if(user_id!=0){
			$.colorbox({href:PATH_GEN+'ajax/ajaxAddPhoto.php'});
		}
		else{
			postPhoto = store_id;
			$.colorbox({href:PATH_GEN+'ajax/ajaxLogin.php'});
		}
}

function addPhotoCity(user_id,city_id){
		if(user_id!=0){
			$.colorbox({href:PATH_GEN+'ajax/ajaxAddPhotoCity.php'});
		}
		else{
			postPhotoCity = city_id;
			$.colorbox({href:PATH_GEN+'ajax/ajaxLogin.php'});
		}
}

function toggleRegister(){
	$.colorbox({href:PATH_GEN+'ajax/ajaxRegister.php'});
}



function submitRating(user_id,store_id,rec,env,vfm,ser,com){
	$.ajax({                    //On débute ajax
          async: "true",          //Asynchrone "true" pour vrai "false" pour faux
          type: "GET",            // Type, "GET" ou "POST"
          url: PATH_GEN+"ajax/add_rating.php",      //Url de la page à charger
          data: "user="+user_id+"&store="+store_id+"&rec="+rec+"&env="+env+"&vfm="+vfm+"&ser="+ser+"&com="+com,   //Données s'il y en a dans ce cas oui(l'email et une action)
          error: function(errorData) { $("#error").html(errorData); },  //S'il y a une erreur on écrit quelque chose
          success: function(data) {$("#bloc_votre_avis_milieu").html("Merci d'avoir donné votre avis il sera modéré dans les plus brefs delais.")}
		 });
}

function submitFavorite(user_id,store_id){
	$.ajax({                    //On débute ajax
          async: "true",          //Asynchrone "true" pour vrai "false" pour faux
          type: "GET",            // Type, "GET" ou "POST"
          url: PATH_GEN+"ajax/add_favorite.php",      //Url de la page à charger
          data: "user="+user_id+"&store="+store_id,   //Données s'il y en a dans ce cas oui(l'email et une action)
          error: function(errorData) { $("#error").html(errorData); },  //S'il y a une erreur on écrit quelque chose
          success: function(data) { $("#cadre_aiment").html(parseInt($("#cadre_aiment").html())+1);/*$("#cadre_aiment").html(parseInt($("#cadre_aiment").html)+1)*/; $("#error").append("Contenu chargé");}
		 });
}

function likeStore(store_id){
	$.ajax({                    //On débute ajax
          async: "true",          //Asynchrone "true" pour vrai "false" pour faux
          type: "GET",            // Type, "GET" ou "POST"
          url: PATH_GEN+"ajax/like_store.php",      //Url de la page à charger
          data: "store="+store_id,   //Données s'il y en a dans ce cas oui(l'email et une action)
          error: function(errorData) { alert(errorData);},  //S'il y a une erreur on écrit quelque chose
          success: function(data) { $("#cadre_aiment").html(parseInt($("#cadre_aiment").html())+1);$(".btn_jaime").attr('onclick','').unbind('click');$(".btn_jaime").attr('src',jaimeOff);$(".btn_jaime").css("cursor","default");}
		 });
}

function likeArea(area_id){
	$.ajax({                    //On débute ajax
          async: "true",          //Asynchrone "true" pour vrai "false" pour faux
          type: "GET",            // Type, "GET" ou "POST"
          url: PATH_GEN+"ajax/like_area.php",      //Url de la page à charger
          data: "area="+area_id,   //Données s'il y en a dans ce cas oui(l'email et une action)
          error: function(errorData) { alert(errorData);},  //S'il y a une erreur on écrit quelque chose
          success: function(data) { $("#cadre_aiment").html(parseInt($("#cadre_aiment").html())+1);$(".btn_jaime").attr('onclick','').unbind('click');$(".btn_jaime").attr('src',jaimeOff);$(".btn_jaime").css("cursor","default");}
		 });
}

function likeCity(city_id){
	$.ajax({                    //On débute ajax
          async: "true",          //Asynchrone "true" pour vrai "false" pour faux
          type: "GET",            // Type, "GET" ou "POST"
          url: PATH_GEN+"ajax/like_city.php",      //Url de la page à charger
          data: "city="+city_id,   //Données s'il y en a dans ce cas oui(l'email et une action)
          error: function(errorData) { alert(errorData);},  //S'il y a une erreur on écrit quelque chose
          success: function(data) { $("#cadre_aiment").html(parseInt($("#cadre_aiment").html())+1);$(".btn_jaime").attr('onclick','').unbind('click');$(".btn_jaime").attr('src',jaimeOff);$(".btn_jaime").css("cursor","default");}
		 });
}


function removeAllCat(){
	$('.categorie_element').removeClass('element_actif');
	$('.categorie_element_detail').css('display','none');
	$('.categorie_element_image').attr({src:PATH_GEN+'img/plus_bleu.png'});
}

function getTel(store,mobile){
	$.ajax({                    //On débute ajax
          async: "true",          //Asynchrone "true" pour vrai "false" pour faux
          type: "GET",            // Type, "GET" ou "POST"
          url: PATH_GEN+"ajax/ajax-gettel.php",      //Url de la page à charger
          data: "store="+store,   //Données s'il y en a dans ce cas oui(l'email et une action)
          error: function(errorData) { $("#error").html(errorData); },  //S'il y a une erreur on écrit quelque chose : $("#petit_caracteres").html("* 1,35€/appel + 0,34€/min - numéro de mise en relation valable 7 minutes</div>");
          success: function(data) { 
        	  if(mobile=='1')
    		  {
        		  data="<a href='tel:"+data+"' style='text-decoration:none;color:#000000;margin-top:50px;'>"+data+"</a>";
    		  }
        	  $("#bouton_telephone A").remove(); 
        	  if(data.length=="14")
        	  {
	        	  $("#bouton_telephone").html("<div style='line-height:60px;margin-left:55px;'>" +
	        	  									"<span style='vertical-align:middle;font-size:18px;'>"+
	        	  										data+
	        	  									"</span>" +
	        	  							   "</div>" +
	        	  							   "<div style='line-height:60px;display:inline; margin-left:8px;' onmouseover='tooltip.show(this)' onmouseout='tooltip.hide(this)' title='Num&eacute;ro de mise en relation<br />1,35&euro;/appel + 0,34&euro;/min'> * </div>");
        	  }
        	  else if (data.length=="107")
        	  {
        		  $("#bouton_telephone").html("<div style='line-height:60px;margin-left:55px;'>" +
							"<span style='vertical-align:middle;font-size:18px;'>"+
								data+
							"</span>" +
					   "</div>" +
					   "<div style='line-height:60px;display:inline; margin-left:8px;' onmouseover='tooltip.show(this)' onmouseout='tooltip.hide(this)' title='Num&eacute;ro de mise en relation<br />1,35&euro;/appel + 0,34&euro;/min'> * </div>");
              }
        	  else if (data.length<"5")
        	  {
        		  if (data.length=="0")
        		  {
        			  /*$("#bouton_telephone").html("<div style='line-height:60px;margin-left:49px;'>" +
  							"<span style='vertical-align:middle;font-size:18px;'>"+
  								"Non communiqu&eacute;"+
  							"</span>" +
  					   "</div>" +
  					   "");*/
        		  }
        		  else
        		  {
        			  $("#bouton_telephone").html("<div style='line-height:60px;margin-left:55px;'>" +
  							"<span style='vertical-align:middle;font-size:18px;'>"+
  								data+
  							"</span>" +
  					   "</div>" +
  					   "");  
        		  }
        		  
              }
        	  else
        	  {
        		  $("#bouton_telephone").html("<div style='line-height:25px;margin-left:55px;'>" +
							"<span style='vertical-align:middle;font-size:18px; padding-top:5px;'>"+
								data+
							"</span>"+
							"<div style='line-height:25px;display:block; margin-left:75px; margin-top:-31px;' onmouseover='tooltip.show(this)' onmouseout='tooltip.hide(this)' title='Num&eacute;ro de mise en relation<br />1,35&euro;/appel + 0,34&euro;/min'> * </div>"+
					   "</div>"
					   );  
        	  }
          }
		 });
}
/*
function initializeMapStore() {
    var latlng = new google.maps.LatLng(48.875554,2.326849);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
 }
*/
 //On ferme le document.ready
