var Culture;
var json;
var clizm;
var tabmak = new Array();
var tabmakIt = {};
var Poleh;
var Proh;
var Prohlo;
var Esid = new Array();
var i=0;

function loadCulture(culture){
	Culture = culture;
}

function itemClick(Id,IdEst){
  $$('#card_list div.item').each(
    function(item){ 
     item.style.background = '#fff url(/images/Card_bg_1.gif) right top repeat-y';
    });
  $(Id).style.background = '#f6fedf url(/images/Card_bg_2.gif) right top repeat-y';
  // new Ajax.Updater('card_details', Culture+'/main/card_details//?id=' + Id +'&mode=hor', {evalScripts: true});
	new Ajax.Updater('phar_gard_cont',Culture+'/main/gardepha/?id='+ Id, {evalScripts: true});
	if(typeof(IdEst) !== 'undefined')
	{
	  PointtoCard(IdEst,0,"hor");	
	}
	else
	{
		
	  PointtoCard(Id,0,"hor");
	}
}

function PointtoCard(Id , nbetab, mode){
	var obj = {'hor': 'Professionals', 'all': 'PolesAPro'};
	  //alert(obj[mode]);
	  
  for (var i = 0; i < json[obj[mode]].length; i++) {
    if (json[obj[mode]][i].ProI == Id.replace("d","")){

	/*console.log(nbetab);
	console.log(json.Professionals[i].Establishments[nbetab]);
	console.log(json.Professionals[i].Establishments[0]);
	console.log(json.Professionals[i]);*/
	//console.log(tabmakIt[i]);
		tabmakIt[i][nbetab].openInfoWindowHtml(tabmakIt[i][nbetab].infow,{maxWidth:350, maxHeight:200,autoScroll:true});
		var estab = json[obj[mode]][i].Establishments[nbetab];
		if (clizm==0){
			map.setCenter(new google.maps.LatLng(estab.MapX, estab.MapY), map.getZoom());
			clizm=1;
		}
		else{
			if(map.getZoom()>16)
				map.setCenter(new google.maps.LatLng(estab.MapX, estab.MapY), map.getZoom());
			else
				map.setCenter(new google.maps.LatLng(estab.MapX, estab.MapY), 16);
			
			if (mode=="hor"){
				var bound = map.getBounds();
				locaAl(Id,bound.getSouthWest(),bound.getNorthEast() );
			}
		}
	
    }
  }
}

function locaAl(Id,x,y){

  if (map.getZoom()>15){
	  new Ajax.Request('/'+Culture+'/main/locaal/?locasw='+x+'&locane='+y+'&locaout='+Id+'&Estout='+Esid,
	  {
	    method:'get', 
	    onSuccess: function(transport){
	        jsonetab = transport.responseText.evalJSON(true);
			//console.log(map.getBounds()) ;
			if (tabmak.length != 0){
				for  (var p = 0; p < tabmak.length; p++) {
					map.removeOverlay(tabmak[p]);
				}
				tabmak = new Array();
			}		
			for (var i = 0; i < jsonetab.Etab.length; i++) {
				if ((jsonetab.Etab[i].MapX && jsonetab.Etab[i].MapY) != undefined) {
					//console.log(jsonetab.Etab[i]);
					var myIcon = new GIcon(G_DEFAULT_ICON);
					myIcon.image = "/images/" + jsonetab.Etab[i].Icon; 
					myIcon.iconSize = new GSize(20, 20);
					myIcon.shadow = '';
					myIcon.iconAnchor = new GPoint(0, 20);
					markerOptions = { icon: myIcon, draggable: false};
					var point = new GLatLng(jsonetab.Etab[i].MapX, jsonetab.Etab[i].MapY);
					
					tabmak[i] = new GMarker(point, markerOptions);
					tabmak[i].infow = jsonetab.Etab[i].MarkerInfo;
					
					GEvent.addListener(tabmak[i], "click", function() {
					  this.openInfoWindowHtml(this.infow,{maxWidth:350, maxHeight:200,autoScroll:true});
					});
					map.addOverlay(tabmak[i]);
					
				}	
			}
	    },
	    onFailure: function(text) {
			//alert('rater ajax : '+text);
			$(obj_id).innerHTML = text.responseText }
	  });

	}
}

function LocaPh(Id,x,y,docId){
  if (map == undefined){
	initialize();
  }
  clizm = 0;
  $('result_info').setStyle({display: 'none'});
  	new Ajax.Request(Culture+'/main/gardepha/?&mode=loca&id='+docId,
	  {
	    method:'get', 
	    onSuccess: function(transport){
		    $('content2').setStyle({display: 'block'});
	        json = transport.responseText.evalJSON(true);
	        proccessJsonCardList(json);
	        if (json.Professionals[0]){
				itemClick(Id);
				//new Effect.BlindDown(document.getElementById('content2'));	
			}
	    },
	    onFailure: function(text) {
			//alert('rater ajax : '+text);
			$('phar_gard_cont').innerHTML = text.responseText }
	  });
}    

function c3_menuClick(Id){


  /*$$('#content3 #menu div.header1_sw').each(
    function(item){ 
	  item.style.fontWeight = 'normal';
    });
  $(Id).up().style.fontWeight = 'bold';
  menu_spec
  last_header*/
  
  if (Id=='menu_spec'){
  	$('menu_spec').removeClassName('ht');
	$('last_header').removeClassName('tt');
  }
  else{
  	$('menu_spec').addClassName('ht');
	$('last_header').addClassName('tt');
  }
  
  new Ajax.Request('/'+Culture+'/main/alph_list/?id='+ Id,
    {
      method:'get',
      onSuccess: function(transport){
        $('items').innerHTML = transport.responseText || "no response text";
      },
      onFailure: function(text){ $('items').innerHTML = text.responseText }
    }); 
}

function init_s1(){
  var spec = $('Filter_Spe').getValue();
  switch(spec)
	{
	//tous
	case '0':
 	  $('inp_profpnm').setStyle({display: 'none'});
	  $('inp_spec').setStyle({display: 'none'});
	  $('inp_profpnml').setStyle({display: 'none'});
	  $('inp_specl').setStyle({display: 'none'}); 
	  $('inp_profpnm').setValue(''); 
	  $('inp_spec').setValue(''); 
		$('inp_paramedl').setStyle({display: 'none'});
		$('inp_paramed').setStyle({display: 'none'});
		$('inp_paramed').setValue('');
		$('inp_autrel').setStyle({display: 'none'});
		$('inp_autre').setStyle({display: 'none'});
		$('inp_autre').setValue('');
		// $('inp_h1n14l').setStyle({display: 'block'});
		// $('inp_h1n14').setStyle({display: 'block'});
		// $('inp_h1n16l').setStyle({display: 'none'});
		// $('inp_h1n16').setStyle({display: 'none'});
		// $('inp_h1n15l').setStyle({display: 'none'});
		// $('inp_h1n15').setStyle({display: 'none'});
		$('form_phar').setStyle({minHeight: '210px'});
	  break;
	//généralistes
	case '1':
 	  $('inp_profpnm').setStyle({display: 'block'});
	  $('inp_spec').setStyle({display: 'none'});
	  $('inp_profpnml').setStyle({display: 'block'});
	  $('inp_specl').setStyle({display: 'none'});
	  $('inp_spec').setValue('');
		$('inp_paramedl').setStyle({display: 'none'});
		$('inp_paramed').setStyle({display: 'none'});
		$('inp_paramed').setValue('');
		$('inp_autrel').setStyle({display: 'none'});
		$('inp_autre').setStyle({display: 'none'});
		$('inp_autre').setValue('');
		// $('inp_h1n14l').setStyle({display: 'none'});
		// 		$('inp_h1n14').setStyle({display: 'none'});
		// 		$('inp_h1n16l').setStyle({display: 'none'});
		// 		$('inp_h1n16').setStyle({display: 'none'});
		// 		$('inp_h1n15l').setStyle({display: 'block'});
		// 		$('inp_h1n15').setStyle({display: 'block'});
		$('form_phar').setStyle({minHeight: '241px'});
	  break;
	//spécialistes
	case '2':
 	  $('inp_profpnm').setStyle({display: 'block'});
	  $('inp_spec').setStyle({display: 'block'});
	  $('inp_profpnml').setStyle({display: 'block'});
	  $('inp_specl').setStyle({display: 'block'});
		$('inp_paramedl').setStyle({display: 'none'});
		$('inp_paramed').setStyle({display: 'none'});
		$('inp_paramed').setValue('');
		$('inp_autrel').setStyle({display: 'none'});
		$('inp_autre').setStyle({display: 'none'});
		$('inp_autre').setValue('');
		// $('inp_h1n14l').setStyle({display: 'none'});
		// $('inp_h1n14').setStyle({display: 'none'});
		// $('inp_h1n16l').setStyle({display: 'block'});
		// $('inp_h1n16').setStyle({display: 'block'});
		// $('inp_h1n15l').setStyle({display: 'none'});
		// $('inp_h1n15').setStyle({display: 'none'});
		$('form_phar').setStyle({minHeight: '274px'});
	  break;
	//dentistes
	case '3':
 	  $('inp_profpnm').setStyle({display: 'block'});
	  $('inp_spec').setStyle({display: 'none'});
	  $('inp_profpnml').setStyle({display: 'block'});
	  $('inp_specl').setStyle({display: 'none'});
	  $('inp_spec').setValue('');
		$('inp_paramedl').setStyle({display: 'none'});
		$('inp_paramed').setStyle({display: 'none'});
		$('inp_paramed').setValue('');
		$('inp_autrel').setStyle({display: 'none'});
		$('inp_autre').setStyle({display: 'none'});
		$('inp_autre').setValue('');
		// $('inp_h1n14l').setStyle({display: 'none'});
		// $('inp_h1n14').setStyle({display: 'none'});
		// $('inp_h1n16l').setStyle({display: 'none'});
		// $('inp_h1n16').setStyle({display: 'none'});
		// $('inp_h1n15l').setStyle({display: 'none'});
		// $('inp_h1n15').setStyle({display: 'none'});
		$('form_phar').setStyle({minHeight: '216px'});
	  break;
	//pharmacies
	case '4':
  	  $('inp_profpnm').setStyle({display: 'none'});
	  $('inp_spec').setStyle({display: 'none'});
	  $('inp_profpnml').setStyle({display: 'none'});
	  $('inp_specl').setStyle({display: 'none'});
	  $('inp_profpnm').setValue(''); 
	  $('inp_spec').setValue('');
		$('inp_paramedl').setStyle({display: 'none'});
		$('inp_paramed').setStyle({display: 'none'});
		$('inp_paramed').setValue('');
		$('inp_autrel').setStyle({display: 'none'});
		$('inp_autre').setStyle({display: 'none'});
		$('inp_autre').setValue('');
		// $('inp_h1n14l').setStyle({display: 'none'});
		// $('inp_h1n14').setStyle({display: 'none'});
		// $('inp_h1n16l').setStyle({display: 'none'});
		// $('inp_h1n16').setStyle({display: 'none'});
		// $('inp_h1n15l').setStyle({display: 'none'});
		// $('inp_h1n15').setStyle({display: 'none'});
		$('form_phar').setStyle({minHeight: '181px'});
	  break;
	//hopital
	case '5':
  	  $('inp_profpnm').setStyle({display: 'none'});
	  $('inp_spec').setStyle({display: 'none'});
	  $('inp_profpnml').setStyle({display: 'none'});
	  $('inp_specl').setStyle({display: 'none'});
	  $('inp_profpnm').setValue(''); 
	  $('inp_spec').setValue('');
		$('inp_paramedl').setStyle({display: 'none'});
		$('inp_paramed').setStyle({display: 'none'});
		$('inp_paramed').setValue('');
		$('inp_autrel').setStyle({display: 'none'});
		$('inp_autre').setStyle({display: 'none'});
		$('inp_autre').setValue('');
		// $('inp_h1n14l').setStyle({display: 'none'});
		// $('inp_h1n14').setStyle({display: 'none'});
		// $('inp_h1n16l').setStyle({display: 'none'});
		// $('inp_h1n16').setStyle({display: 'none'});
		// $('inp_h1n15l').setStyle({display: 'none'});
		// $('inp_h1n15').setStyle({display: 'none'});
		$('form_phar').setStyle({minHeight: '181px'});
	  break;
	//autre
	case '6':
  	$('inp_profpnm').setStyle({display: 'none'});
	  $('inp_specl').setStyle({display: 'none'});
	  $('inp_spec').setStyle({display: 'none'});
	  $('inp_profpnml').setStyle({display: 'none'}); 
		$('inp_paramedl').setStyle({display: 'none'});
		$('inp_paramed').setStyle({display: 'none'});
		$('inp_paramed').setValue('');
		$('inp_autrel').setStyle({display: 'block'});
		$('inp_autre').setStyle({display: 'block'});
		$('inp_autre').setValue('');
		// $('inp_h1n14l').setStyle({display: 'none'});
		// $('inp_h1n14').setStyle({display: 'none'});
		// $('inp_h1n16l').setStyle({display: 'none'});
		// $('inp_h1n16').setStyle({display: 'none'});
		// $('inp_h1n15l').setStyle({display: 'none'});
		// $('inp_h1n15').setStyle({display: 'none'});
		$('form_phar').setStyle({minHeight: '215px'});
	  break;
	//paramédical
	case '7':
			$('inp_profpnm').setStyle({display: 'block'});
	  	$('inp_specl').setStyle({display: 'none'});
	  	$('inp_spec').setStyle({display: 'none'}); 
			$('inp_paramedl').setStyle({display: 'block'});
			$('inp_paramed').setStyle({display: 'block'});
	  	$('inp_profpnml').setStyle({display: 'block'});
	  	$('inp_profpnm').setValue('');
			$('inp_autrel').setStyle({display: 'none'});
			$('inp_autre').setStyle({display: 'none'});
			$('inp_autre').setValue('');
			// $('inp_h1n14l').setStyle({display: 'none'});
			// $('inp_h1n14').setStyle({display: 'none'});
			// $('inp_h1n16l').setStyle({display: 'none'});
			// $('inp_h1n16').setStyle({display: 'none'});
			// $('inp_h1n15l').setStyle({display: 'none'});
			// $('inp_h1n15').setStyle({display: 'none'});
			$('form_phar').setStyle({minHeight: '251px'});
			
	  break;
	}
}

function s1_menuClick(){

  
	init_s1()
	
	if (($('inp_city').getValue()!='')||($('inp_street').getValue()!='')||($('inp_prof').getValue()!='')||($('inp_profpnm').getValue()!='')||($('inp_spec').getValue()!=''))
		search_key_press();
	
  /*console.log(spec);*/
  
  /*new Ajax.Request('/'+Culture+'/main/alph_sear/?id='+ Id,
    {
      method:'get',
      onSuccess: function(transport){
        $('search_g').innerHTML = transport.responseText || "no response text";
      },
      onFailure: function(text){ $('search_g').innerHTML = text.responseText }
    }); */
}

var Timer, params, page; 
function request_typeestab(typname)
{
	params = getSearchParams(page);
	params = params + "&typname="+ typname;
	make_request('card_list',params);
}
function make_request(obj_id, params){
//	alert('ajax! Obj: '+obj_id+" Params: "+params);
   clearTimeout(Timer);
   clizm=0;
   
   new Ajax.Request(Culture+'/main/search/?'+ params + '&mode=' + obj_id,
	  {
	    method:'get', 
	    onSuccess: function(transport){
	      if (obj_id == 'result_info') { 
				$('result_info').setStyle({display: 'block'});
			if (transport.responseText.indexOf("</li>") == -1){
				//new Effect.Blindup(document.getElementById('content2'));
				$('content2').setStyle({display: 'none'});
			}
			else
			    $('content2').setStyle({display: 'block'});
			$(obj_id).innerHTML = transport.responseText || "no response text";

	      } 
	      else if (obj_id == 'card_list') {
	        json = transport.responseText.evalJSON(true);
			//console.log(transport.responseText);
	        proccessJsonCardList(json);
	        if (json.Professionals[0]){
				itemClick($('card_list_content').down('div').next('div').identify());
				//new Effect.BlindDown(document.getElementById('content2'));
				//$('content2').setStyle({display: 'block'});
			}
			
			//else $('content2').setStyle({display: 'none'});
	        //else $('card_details').innerHTML = "<div id='card_details_mes'> no selected doctor <div>";  
	      };  
	    },
	    onFailure: function(text) {
			//alert('rater ajax : '+text);
			$(obj_id).innerHTML = text.responseText }
	  });
}

function getSearchParams(page){
	var type_spec;
  var city = $('inp_city').getValue();
  var street = $('inp_street').getValue();
  var prof = $('inp_prof').getValue();
	
		
  var profpnm = $('inp_profpnm').getValue();
  //var est_types = getUncheckedTypeIds();
  var est_types = $('Filter_Spe').getValue();
	// var h1n1 = 0;
	
	/*
	if (est_types == 0)
	{
		if($('inp_h1n14').checked)
		{
			h1n1 = 1;
		}
		else
			h1n1 = 0;
	}
	if (est_types == 2)
	{
		if($('inp_h1n16').checked)
		{
			h1n1 = 1;
		}
		else
			h1n1 = 0;
	}
	if (est_types == 1)
	{
		if($('inp_h1n15').checked)
		{
			h1n1 = 1;
		}
		else
			h1n1 = 0;
	}
	*/
  var spec = $('inp_spec').getValue();
	if(spec == '')
	{
		//recherche sur paramedical
		if($('inp_paramed').getValue() != '')
		{
			spec = $('inp_paramed').getValue();
			type_spec = 4;
		}
		if($('inp_autre').getValue())
		{
			spec = $('inp_autre').getValue();
			type_spec = 8;
		}
	}
	else
	{
		type_spec = 2;
	}
  //console.log(spec);
  var node = $('autocomp');
  //console.log(node);
 
  if ((spec!="")||(profpnm!="")||(prof!="")||(street!="")||(street!="")||(city!="")){

	if ((spec!="")&&(node != null)){
	  var fils = node.childNodes;
	  var nbfils = fils.length;
	  for(var i = 0; i < nbfils ; i++){
		if(fils[i].className == 'selected'){
		  spec = fils[i].childNodes[0].nodeValue;
		  break;
		}
	  }
	}
	// params = 'spec=' + spec + '&prof=' + prof + '&profpnm=' + profpnm + '&city=' + city + '&street=' + street + 
	//            '&page=' + page + '&types=' + est_types + '&type_spec='+type_spec + "&h1n1="+h1n1;
	params = 'spec=' + spec + '&prof=' + prof + '&profpnm=' + profpnm + '&city=' + city + '&street=' + street + 
           '&page=' + page + '&types=' + est_types + '&type_spec='+type_spec;
           
	return params; 
  }else
	return false;
}

function inclusion(fichier){
    if (fichier.search('\.js') >= 0) {
        var js_effets=document.createElement("script");
        js_effets.setAttribute("type", "text/javascript");
        js_effets.setAttribute("src", fichier);
        document.getElementsByTagName("head")[0].appendChild(js_effets);
    } else {
        var css_style=document.createElement("link");
        css_style.setAttribute("rel", "stylesheet");
        css_style.setAttribute("type", "text/css");
        css_style.setAttribute("href", fichier);
        css_style.setAttribute("media", "screen");
        document.getElementsByTagName("head")[0].appendChild(css_style);
    }
}

function key_delay(){
  clearTimeout(Timer);
  Timer = setTimeout("search_key_press()",500);
}

function search_key_press(page){
//alert(map);
  if (map == undefined){
	initialize();
  }
  var params = getSearchParams(page);
  clearTimeout(Timer);
  if (params !== false){
    if (page == undefined) {
      make_request('result_info', params);
      Timer = setTimeout("make_request('card_list', params)",200);
	} else {
      make_request('card_list', params);
	}
  }
}

function legendChange() {
  var params = getSearchParams();
  if (params !== false){
    make_request('result_info', params);
    make_request('card_list', params);
  }
};

function getUncheckedTypeIds() {
  var result = '';
  $$('.legend_inp').each( function(item) {
    var id = item.identify();
    if (!item.checked) result += id.substring(8, id.length) + ';' ;
  });
  return result;
}

function proccessJsonCardList(json){
//	alert('jsoncardlist');
  var strUnckIds = getUncheckedTypeIds();
  var MaxX, MaxY, MinX, MinY;
  //console.log(json);
  //alert(strUnckIds);
  Esid = json.Tabesid;
  if (map != undefined) map.clearOverlays();
  $('pages').innerHTML = json.pages;
  $('result_count').innerHTML = json.result_count;
  $('card_list_content').innerHTML = ''; 
  var estab;
  if (!json.Professionals[0]) { return 0;}
  for (var i = 0; i < json.Professionals.length; i++) {
    getCardListItem(json.Professionals[i]);
    for (var j = 0; j < json.Professionals[i].Establishments.length; j++) {
      estab = json.Professionals[i].Establishments[j];
      if (MaxX == null) MaxX = estab.MapX;
      if (MaxY == null) MaxY = estab.MapY;
      if (MinX == null) MinX = estab.MapX;
      if (MinY== null) MinY = estab.MapY;
      if (strUnckIds.indexOf(estab.TypeId) == -1) {   // Filter by Legend 
        if (estab.MapX && estab.MapY) {
			AddMapMarkerTab(estab,i,j)
	        if (MaxX < estab.MapX) MaxX = estab.MapX;
	        if (MaxY < estab.MapY) MaxY = estab.MapY;
	        if (MinX > estab.MapX) MinX = estab.MapX;
	        if (MinY > estab.MapY) MinY = estab.MapY;
	    }  
      }  
    }
  }
  centr_x = (MaxX + MinX)/2;
  centr_y = (MaxY + MinY)/2;
  if (centr_x && centr_y) map.setCenter(new google.maps.LatLng(centr_x, centr_y), 5);
  var bounds = new GLatLngBounds(new google.maps.LatLng(MinX*1.2, MinY*1.2), new google.maps.LatLng(MaxX*1.2, MaxY*1.2));
  map.setZoom(map.getBoundsZoomLevel(bounds));

}

function Timerer() {
	var dt=new Date()
	var mois1, jour1, min; 

	switch(dt.getDay()){
		case 1 : 
			jour1 = "Lundi"; 
			break;
		case 2 :
			jour1 = "Mardi";
			break;
		case 3 :
			jour1 = "Mercredi";
			break;
		case 4 :
			jour1 = "Jeudi";
			break;
		case 5 :
			jour1 = "Vendredi";
			break;
		case 6 : 
			jour1 = "Samedi"; 
			break;
		case 0 :
			jour1 = "Dimanche";
			break;
	}


	switch(dt.getMonth()+1){
		case 1 : 
		mois1 = "janvier"; 
		break;
		case 2 :
		mois1 = "f&#233vrier";
		break;
		case 3 :
		mois1 = "mars";
		break;
		case 4 :
		mois1 = "avril";
		break;
		case 5 :
		mois1 = "mai";
		break;
		case 6 : 
		mois1 = "juin"; 
		break;
		case 7 :
		mois1 = "juillet";
		break;
		case 8 : 
		mois1 = "ao&#251t"; 
		break;
		case 9 :
		mois1 = "septembre";
		break;
		case 10 :
		mois1 = "octobre";
		break;
		case 11 :
		mois1 = "novembre";
		break;
		case 12 :
		mois1 = "d&#233cembre";
		break;
	}
	
	
	min=dt.getMinutes();
	if(min < 10)
		min="0"+min;
		
   document.getElementById('top_heure').innerHTML="<b>"+jour1+", "+dt.getDate()+" "+mois1+" "+dt.getFullYear()+"</b><br/> Il est "+dt.getHours()+":"+min;
   setTimeout("Timerer()",60000);
}

function getCardListItem(doctor) {
  var list = $('card_list_content');
  list.innerHTML = list.innerHTML + doctor.DocHtml;
}

function showloginForm(){
  Dialog.info($('loginBlock').innerHTML, {className:"alphacube", width:400, closable: true}); 
  return false;
}

function checkLogin(){
	//alert('bonjour');
 // alert($('pass').value);
  new Ajax.Request(Culture+'/login/checklogin/', {
    encoding:      'UTF-8',         
    parameters: {'login': $('loginField').value, 'pass': $('pass').value},
    method: 'post',
    onSuccess: function(transport) {
      if (transport.responseText == 'register') 
        $('loginForm').submit();
      else             
        $('login_notice').update(transport.responseText);
    }}); 
}

//---------------------- catalog doctor page -----------------------

var Curest;

function updateDoctorMap() {
//  alert('updateDoctorMap Curest: '+Curest)
  if (map == undefined)
  {
    alert(map);   
    return false;
  }
  
  map.clearOverlays();
  var Uno; 
  var id = $('content_doctor_t').title;
  var spec = $('spec_select').value;
  var dist = $('distance_select').value;
  var MaxX, MaxY, MinX, MinY; 
  new Ajax.Request('/'+Culture+'/catalog/distDocs', {
    encoding:      'UTF-8',         
    parameters: {'id': id, 'spec': spec, 'dist': dist, 'estab_id': Curest},
    method: 'get',
    onSuccess: function(transport) {
      var json = transport.responseText.evalJSON(true);
      var list = $('doctor_distance_results');
      list.innerHTML = '';
	    for (var i = 0; i < 5; i++)
	    {  
	      if (json.groups[i] != undefined && json.groups[i] != 0)
				// if (json.groups[i] != undefined)
	        list.innerHTML = list.innerHTML + '<li>' + json.groups[i] + '</li>';
	    }
	    //console.log(json);
	    if ( json.estabs.length != 0 ){
		    MaxX = json.estabs[0].MapX;
		    MaxY = json.estabs[0].MapY;
		    MinX = json.estabs[0].MapX;
		    MinY = json.estabs[0].MapY;
			$('distance_result_count').innerHTML = json.groups[5];
			if (tabmak.length != 0){
				for  (var p = 0; p < tabmak.length; p++) {
					map.removeOverlay(tabmak[p]);
				}
				tabmak = new Array();
			}	
		    for (var i = 0; i < json.estabs.length; i++) {
				if ((json.estabs[i].MapX && json.estabs[i].MapY) != undefined) {
					var myIcon = new GIcon(G_DEFAULT_ICON);
					myIcon.image = "/images/" + json.estabs[i].Icon; 
					myIcon.iconSize = new GSize(20, 20);
					myIcon.shadow = '';
					myIcon.iconAnchor = new GPoint(0, 20);
					markerOptions = { icon: myIcon, draggable: false};
					var point = new GLatLng(json.estabs[i].MapX, json.estabs[i].MapY);
					
					tabmak[i] = new GMarker(point, markerOptions);
					tabmak[i].infow = json.estabs[i].MarkerInfo;
					
					GEvent.addListener(tabmak[i], "click", function() {
					  this.openInfoWindowHtml(this.infow,{maxWidth:350, maxHeight:200,autoScroll:true});
					});
					map.addOverlay(tabmak[i]);
					if (json.estabs[i].exte=='1')
						tabmak[i].openInfoWindowHtml(tabmak[i].infow,{maxWidth:350, maxHeight:200,autoScroll:true});
				}	
				if (MaxX < json.estabs[i].MapX) MaxX = json.estabs[i].MapX;
				if (MaxY < json.estabs[i].MapY) MaxY = json.estabs[i].MapY;
				if (MinX > json.estabs[i].MapX) MinX = json.estabs[i].MapX;
				if (MinY > json.estabs[i].MapY) MinY = json.estabs[i].MapY;
			}
		    map.setCenter(new google.maps.LatLng(json.estabs[0].MapX, json.estabs[0].MapY), 2);
		    var bounds = new GLatLngBounds(new google.maps.LatLng(MinX*1.2, MinY*1.2), new google.maps.LatLng(MaxX*1.2, MaxY*1.2));
	      map.setZoom(map.getBoundsZoomLevel(bounds));
       }
       else map.setCenter(new google.maps.LatLng(48.8, 2), 5);    
    }});  
}

function ItemDocClick(Id) {
  $$('#sub_infolh div.item').each(
    function(item){ 
     item.style.background = '#fff url(/images/Card_bg_1.gif) right top repeat-y';
    });
  $(Id).style.background = '#f6fedf url(/images/Card_bg_2.gif) right top repeat-y';
  Curest=Id.substring(1);
  updateDoctorMap();
}


//---------------------- doctor profile -----------------------


function profileUpdate(){ 
  if ($('data_profile_is_modified').value == '1')
  {
	  var items = $$("select.spec_select_tag");
	  var items2 = $$("select.lang_select_tag");
	  var items3 = $$("select.notion_select_tag");
	  var specList = Array();
	  var langList = Array();
	  for (var i = 0; i < items2.length; i++)
	  {
		  langList[i] = items2[i].value+'-'+items3[i].value;
		  //alert(langList[i]);
	  }
	  for (i = 0; i < items.length; i++)
	  {
	    specList[i] = items[i].value;  
	  }  
	  specList = specList.uniq();
	  langList = langList.uniq();
	  //alert(langList);
	  
	  var data = specList.toJSON();
	  var data2 = langList.toJSON();

	  new Ajax.Request(Culture+'/profile/editData/', {
	    encoding:      'UTF-8',         
	    parameters: {specs: data, langs: data2},
	    method: 'get',
	    onSuccess: function(transport) { 
	                 $('profile_edit_form').submit(); 
	               },
	    onFailure: function(transport) {} });
  }
  else alert('no changes');
}

function addSpecSelect(){ 
 new Ajax.Request(Culture+'/profile/getSpecs/', {
    encoding:      'UTF-8',         
    parameters: {},
    method: 'post',
    onSuccess: function(transport) {
      addLI(transport.responseText, 'spec');
      setModified();
    }});
}

function addLangSelect(){ 
 new Ajax.Request(Culture+'/profile/getLangs/', {
    encoding:      'UTF-8',         
    parameters: {},
    method: 'post',
    onSuccess: function(transport) {
      addLI(transport.responseText, 'lang');
      setModified();
    }});
}

function addLI(text, type){
  var max = 0;
  add = "#added_"+type+"_list li."+type+"_list_item";
  var items = $$(add);
  for (var i = 0; i < items.length; i++)
  {
    var id = items[i].identify();  
    var num = Number(id.substring(10, id.length));
    if (max < num) max = num;
  } 
  max++;
  var li = document.createElement('li');
  li.id = type+'_item_' + max;
  li.className = type+'_list_item';
  li.innerHTML = text + " <img alt='delete' src='/images/delete.gif' onclick=\"$(\'"+type+"_item_" + max + "\').remove(); setModified();\">";
  $("added_"+type+"_list").appendChild(li);

}

var dialog_id;

function establishmentUpdate(){
  if ($('data_estab_is_modified').value == '1')
  { 
	  $('profile_estab_form').request({
	  onComplete: function(req){
	    var s = req.responseText;
	    if (s[0] == '#')
	    {
	      s = s.substr(1);
	      $('modal_dialog_message').innerHTML = s;
	    } else  
	    {
	      $('estab_form_container').hide();
	      $('data_profile_is_modified').value = '1';
	      var id = 'estab_item_' + $('form_estab_id').value;
	      var elem = $(id);  
	      if (!elem)
	      {
	        var li = document.createElement('li');
	        li.id = id;
	        li.className = 'estab_list_item';
	        li.innerHTML = s;
	        $('doc_estab_list').appendChild(li);
	      } else elem.innerHTML = s;  
	      Windows.close(dialog_id, ''); 
	    }   
	  }});
  } else alert('no changes');
}

function removeEstab(id){
  new Ajax.Request(Culture+'/profile/estabEdit/', {
      encoding:      'UTF-8',         
      parameters: {del_id: id},
      method: 'post',
      onSuccess: function(transport) {
        $('estab_item_' + id).remove();
        setModified();
      }});
} 

function addEstab() {
  var max = 0;
  var items = $$("#doc_estab_list li.estab_list_item");
  for (var i = 0; i < items.length; i++)
  {
    var id = items[i].identify();  
    var num = Number(id.substring(11, id.length));
    if (max < num) max = num;
  } 
  max++;
  
  
  new Ajax.Request(Culture+'/profile/estabEdit/', {
      encoding:      'UTF-8',         
      parameters: {id: max, is_new: 1},
      method: 'get',
      onSuccess: function(req) {
        //$('estab_form_container').show();
        var s = req.responseText;
        s = s.substr(1);
        $('estab_form_container').innerHTML = s;
        showEstabForm(); 
      }});
}

function editEstab(id){
  new Ajax.Request(Culture+'/profile/estabEdit/', {
      encoding:      'UTF-8',         
      parameters: {id: id, is_new: ''},
      method: 'get',
      onSuccess: function(req) {
       // $('estab_form_container').show();
        var s = req.responseText;
        s = s.substr(1);
        $('estab_form_container').innerHTML = s;
        showEstabForm();
      }});
}

function showEstabForm(){
  var dialog = Dialog.info($('estab_form_container').innerHTML, {className:"alphacube", width:420, closable: true});
  dialog_id = dialog.options.id;
  return false;
}

function setModified(){
  $('data_profile_is_modified').value = '1';
}

function setEstabModif(){
  $('data_estab_is_modified').value = '1';
}
function verif(form){
	var heuredebut = parseFloat($('schedule_start_time_hour').value);
	var mindebut = parseFloat($('schedule_start_time_minute').value);
	
	var heurefin = parseFloat($('schedule_end_time_hour').value);
	var minfin = parseFloat($('schedule_end_time_minute').value);
	
	heuredebut = (heuredebut * 100) + mindebut;
	heurefin = (heurefin * 100) + minfin;
	
	
	//alert(heuredebut+" "+heurefin);
	if(heuredebut == heurefin)
	{
		alert("l'heure de d\351but est \351gale \340 l\'heure de fin ! la plage horaire ne sera pas ajout\351e");
		//break;
	}
	else if(heuredebut > heurefin)
	{
		alert('l\'heure debut est plus grande que l\'heure de fin la plage horaire ne sera pas ajout\351e');
		//break;
	}
}

//positionnement carte



//establlishment
 
function proccessJsonCardListPole(json_id){
//alert(json_id);
  if (json_id == '1' || json_id == '3'){
    $('card_details').setStyle({display: 'none'});
	$('card_details_h').setStyle({display: 'block'});
	$('right_title').setStyle({display: 'block'});
	
	$('top_sw_s').removeClassName('dis');
	$('top_sw_p').addClassName('dis');
	json=Poleh;
  }
  else{
    $('card_details').setStyle({display: 'block'});
	$('card_details_h').setStyle({display: 'none'});
	$('right_title').setStyle({display: 'none'});
	$('top_sw_p').removeClassName('dis');
	$('top_sw_s').addClassName('dis');
	json=Proh;
  }

  var strUnckIds = getUncheckedTypeIds();
  var MaxX, MaxY, MinX, MinY;
  $('pages').innerHTML = json.pages;
  $('result_count').innerHTML = json.result_count;
  $('card_list_content_h').innerHTML = ''; 
  var estab;
  if (!json.PolesAPro[0]) { return 0;}
  for (var i = 0; i < json.PolesAPro.length; i++) {
	$('card_list_content_h').innerHTML = $('card_list_content_h').innerHTML + json.PolesAPro[i].DocHtml;
  }
  
  if (((json_id=='1') || json_id == '3')&&(Poleh.PolesAPro[0]))
	{
		map.clearOverlays();
		if(map.getZoom()>16)
			map.setCenter(new google.maps.LatLng(Polpro[2].MapX, Polpro[2].MapY), map.getZoom());
		else
			map.setCenter(new google.maps.LatLng(Polpro[2].MapX, Polpro[2].MapY), 16);
		AddMapMarker(Prohlo,1);
		if(json_id == 3)
		{
			item_hClick($('card_list_content_h').down('div').identify(),'nopole');
		}
		else
		{
			item_hClick($('card_list_content_h').down('div').identify());
		}
		
	 }
  if ((json_id=='2')&&(Proh.PolesAPro[0]))
	item_pClick($('card_list_content_h').down('div').identify());
}

function make_request_h(obj_id, params){
//	alert('ajax! Obj: '+obj_id+" Params: "+params);
   new Ajax.Request('/'+Culture+'/establishment/search_h?pole='+ params + '&mode=' + obj_id,
	  {
	    method:'get', 
	    onSuccess: function(transport){
	      if (obj_id == 'init') {
		    Polpro = transport.responseText.evalJSON(true);
		  	Poleh=Polpro[0];
			Proh=Polpro[1];
			Prohlo=Polpro[2];
			//console.log(Polpro[2]);
			json=Polpro[1];
			intId = setInterval(function(){
				if(typeof map == 'undefined'){
					//console.log('undef');
				}else{
				    map.clearOverlays();
					proccessJsonCardListPole(params.substr(params.length-1, 1));
					clearInterval(intId);
				}
			},100);
					
			
	      } 
	      else if (obj_id == 'card_list') {
	        Poleh = transport.responseText.evalJSON(true);
					if(params.indexOf('mo=3') == -1)
					{
						proccessJsonCardListPole('1');
					}
					else
					{
						proccessJsonCardListPole('3');
					}
					
	        
	      }
		  else {
			Proh = transport.responseText.evalJSON(true);
	        proccessJsonCardListPole('2');
		  };
	    },
	    onFailure: function(text) {
			$(obj_id).innerHTML = text.responseText }
	  });
}
function item_hClick(Id,pole){
  $$('#card_list_content_h div.item_h').each(
    function(item){ 
     item.style.background = '#fff url(/images/Card_bg_1.gif) right top repeat-y';
    });
  $(Id).style.background = '#f6fedf url(/images/Card_bg_2.gif) right top repeat-y';
  
  make_request_details_h(Id,pole);

}

function make_request_details_h(id,pole){
	if (pole == 'nopole')
	{
		request = '/'+ Culture+'/establishment/card_details_h/?id='+id+'&mode=nopole'
	}
	else
	{
		request = '/'+Culture+'/establishment/card_details_h/?id=' + id
	}
  new Ajax.Request(request,
	  {
	    method:'get', 
	    onSuccess: function(transport){
	       //if (obj_id == 'card_list') {
	        propol = transport.responseText.evalJSON(true);
	        //proccessJsonCardListPole('1');
	        $('pages_h').innerHTML = propol.pages;
			$('result_count_h').innerHTML = propol.result_count;
			$('right_title').innerHTML = propol.right_title;
			$('card_list_details_h').innerHTML = ''; 
			if (!propol.PolesAPro[0]) { return 0;}
			for (var i = 0; i < propol.PolesAPro.length; i++) {
				$('card_list_details_h').innerHTML = $('card_list_details_h').innerHTML + propol.PolesAPro[i].DocHtml;
			}
	    },
	    onFailure: function(text) {
			$(obj_id).innerHTML = text.responseText }
	  });
	  

  /*PointtoCard(Id,0);*/
}

function item_pClick(Id){
	$$('#card_list_content_h div.item').each(
		function(item){ 
			item.style.background = '#fff url(/images/Card_bg_1.gif) right top repeat-y';
	});
	$(Id).style.background = '#f6fedf url(/images/Card_bg_2.gif) right top repeat-y';
	new Ajax.Updater('card_details', '/'+Culture+'/main/card_details//?id=' + Id + '&mode=all', {evalScripts: true});

	map.clearOverlays();
	var estab;
	var MaxX, MaxY, MinX, MinY;
	if (!json.PolesAPro[0]) { return 0;}
	for (var i = 0; i < json.PolesAPro.length; i++) {
		if (json.PolesAPro[i].ProI == Id.replace("d","")){
			for (var j = 0; j < json.PolesAPro[i].Establishments.length; j++) {
				estab = json.PolesAPro[i].Establishments[j];
				if (MaxX == null) MaxX = estab.MapX;
				if (MaxY == null) MaxY = estab.MapY;
				if (MinX == null) MinX = estab.MapX;
				if (MinY== null) MinY = estab.MapY;
				if (estab.MapX && estab.MapY) {
					AddMapMarkerTab(estab,i,j)
					if (MaxX < estab.MapX) MaxX = estab.MapX;
					if (MaxY < estab.MapY) MaxY = estab.MapY;
					if (MinX > estab.MapX) MinX = estab.MapX;
					if (MinY > estab.MapY) MinY = estab.MapY;
				}  
			}
		}
	}
	centr_x = (MaxX + MinX)/2;
	centr_y = (MaxY + MinY)/2;
	if (centr_x && centr_y) map.setCenter(new google.maps.LatLng(centr_x, centr_y), 5);
	var bounds = new GLatLngBounds(new google.maps.LatLng(MinX*1.2, MinY*1.2), new google.maps.LatLng(MaxX*1.2, MaxY*1.2));
	map.setZoom(map.getBoundsZoomLevel(bounds));

	PointtoCard(Id,0,"all");

}
/* affiche et masque les boutons editer / supprimer es horraires */
function affichImg(img) 
{
	// console.log($('#'+img));
	$('ED_'+img).setStyle({display: 'inline'});
	$('SP_'+img).setStyle({display: 'inline'});
}
function cacheImg(img)
{
	$('ED_'+img).setStyle({display: 'none'});
	$('SP_'+img).setStyle({display: 'none'});
}