/*****************************************
** Projet Picto 2.0 made by CLP - 2010 **
** Copyright LPG 2010                  **
****************************************/

/*code init*/

/*event champs de recherche*/
 $(function(){ 
      $("#addressInput").click(function() {
             $(this).val("");
             $(this).unbind( "click");
             });    
 });//fin du onLoad      

 /*event champs des pays*/
 function changeCountry() {    
    if(document.getElementById('country').value == 'France')
    {
        distinctKine = true;
        document.getElementById('divSelectType').style.visibility = "visible";
        document.getElementById('CertifiedQuality').innerHTML = "Formation Pro";
    } else {
        distinctKine = false;
        document.getElementById('divSelectType').style.visibility = "hidden";
        document.getElementById('CertifiedQuality').innerHTML = "Certified Quality";
    }
      document.getElementById('map').style.visibility = "hidden";
      document.getElementById('Legend').style.visibility = "hidden";
      document.getElementById('sidebar').innerHTML = ''; 
   } 
 
/*event selection picto*/
 function checkPicto(idChkbox,idDiv)
 {
    if(document.getElementById(idChkbox).checked)  {
        document.getElementById(idChkbox).checked = false;
        document.getElementById(idDiv).className = "picto";  
    } else {
        document.getElementById(idChkbox).checked = true;
        document.getElementById(idDiv).className = "picto-select";
    }
 }
 
/* recuperation valeur radius*/
function getRadius() {
    var val = 0;

    for( i = 0; i < document.myForm.radRadius.length; i++ )
    {
    if( document.myForm.radRadius[i].checked == true )
        val = document.myForm.radRadius[i].value;
    }
    return val; 
} 
 
 /*event cliquet type de centre*/
 function selectType() {
        if(document.myForm.chkKine.checked) 
        {
            document.getElementById('imgCliquet').src = "img/cliquet-right.png";
            document.myForm.chkKine.checked = false;
            document.getElementById('Institut').style.fontWeight = "bold";
            document.getElementById('kine').style.fontWeight = "normal";
            document.getElementById('divSpineforce').style.display = "none";
            document.getElementById('divFace').style.width = "235px";
            document.getElementById('divBody').style.width = "235px";
            document.getElementById('divWellbox').style.width = "235px";
            document.getElementById('divCosmetic').style.width = "235px";
            document.getElementById('chkSpineforce').checked = false;
            document.getElementById('divSpineforce').className = "picto";
        } else {
            document.getElementById('imgCliquet').src = "img/cliquet-left.png";
            document.myForm.chkKine.checked = true;  
            document.getElementById('Institut').style.fontWeight = "normal";
            document.getElementById('kine').style.fontWeight = "bold";
            document.getElementById('divSpineforce').style.display = "block";
            document.getElementById('divFace').style.width = "188px";
            document.getElementById('divBody').style.width = "188px";
            document.getElementById('divWellbox').style.width = "188px";
            document.getElementById('divCosmetic').style.width = "188px";  
        }        
   }
 
/* code google maps*/
 
/* code maps */
//<![CDATA[
    var map;
    var geocoder;
    var iconLpg;
    var distinctKine = false;
    var bSel = false;

/*  google maps V3

function initialize() {    
    var myOptions = {
      zoom: 8,
      ScrollWheelZoom: false,           
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };             
    map = new google.maps.Map(document.getElementById("map"),myOptions);
    geocoder = new google.maps.Geocoder();
  }*/
    
    
    
    function load() {
      if (GBrowserIsCompatible()) {
        var baseIcon = new GIcon();
         baseIcon.iconSize=new GSize(32,32);
         baseIcon.iconAnchor=new GPoint(16,16);
         baseIcon.infoWindowAnchor=new GPoint(16,16);
         iconLpg = new GIcon(baseIcon, 'img/picto-lpg.png', null, '');       
          
        geocoder = new GClientGeocoder();
        map = new GMap2(document.getElementById('map'));       
        var customUI = map.getDefaultUI();
        customUI.maptypes.hybrid = false;
        map.setUI(customUI);
         map.disableScrollWheelZoom();    
      }
    }       

   function searchLocations() {             
      var address = document.getElementById('addressInput').value + ',' + document.getElementById('country').value;
     
      if(document.getElementById('addressInput').value == YOUR_ADDRESS) {
          alert(EMPTY_ADDRESS);          
      } else {
       geocoder.getLatLng(address, function(latlng) {
       if (!latlng) {
         alert(address + NOT_FOUND);
       } else {
         searchLocationsNear(latlng);
       }
     });      
     }    
   }      
   

  /*function searchLocations() {
    var address = $('#addressInput').val() + ',' +  $('#country').val();    
    $("#map").css('visibility', 'visible');    
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
  }
  
  function searchLocationsNear(center) { 
    $("#wait").show();  
    var radius = getRadius();
    var searchUrl = 'data/center.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius + '&body=' + document.myForm.chkBody.checked + '&distinctKine=' + distinctKine;
    searchUrl += '&face=' + document.myForm.chkFace.checked + '&spineforce=' +  document.myForm.chkSpineforce.checked + '&kine=' + document.myForm.chkKine.checked;       
    searchUrl += '&cosmetic=' + document.myForm.chkCosmetic.checked + '&wellbox=' + document.myForm.chkWellbox.checked;
     
     
     
  }      */

   
   

   function searchLocationsNear(center) {
     $("#wait").show();
     var radius = getRadius();    
     var searchUrl = 'data/center.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius + '&body=' + document.myForm.chkBody.checked + '&distinctKine=' + distinctKine;
     searchUrl += '&face=' + document.myForm.chkFace.checked + '&spineforce=' +  document.myForm.chkSpineforce.checked + '&kine=' + document.myForm.chkKine.checked;       
     searchUrl += '&cosmetic=' + document.myForm.chkCosmetic.checked + '&wellbox=' + document.myForm.chkWellbox.checked + '&address=' +  document.myForm.addressInput.value + '&country=' + document.getElementById('country').value;  
     
     GDownloadUrl(searchUrl, function(data) {
       var xml = GXml.parse(data);             
       var markers = xml.documentElement.getElementsByTagName('marker');
       map.clearOverlays();

       var sidebar = document.getElementById('sidebar');
       sidebar.innerHTML = '';
       if (markers.length == 0) {
         sidebar.innerHTML = 'Aucun centre LPG ne correspond aux termes de recherche spécifiés';
         document.getElementById('map').style.visibility = "hidden";               
         $("#wait").hide();
         return;
       }
       var bounds = new GLatLngBounds();
       for (var i = 0; i < markers.length; i++) {
         var name = markers[i].getAttribute('name');
         var address = markers[i].getAttribute('address');
         var zip = markers[i].getAttribute('zip');
         var city = markers[i].getAttribute('city');
         var tel = markers[i].getAttribute('tel');
         var website = markers[i].getAttribute('website');  
         var gen_cellu = markers[i].getAttribute('gen_cellu');
         var spineforce = markers[i].getAttribute('spineforce');
         var lipomassage = markers[i].getAttribute('lipomassage');
         var endermolift = markers[i].getAttribute('endermolift');
         var wellbox = markers[i].getAttribute('wellbox');
         var cosmetic = markers[i].getAttribute('cosmetic');
         var mvf = markers[i].getAttribute('mvf');
         var quality = markers[i].getAttribute('quality');
         var distance = parseFloat(markers[i].getAttribute('distance'));         
         var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                 parseFloat(markers[i].getAttribute('lng')));
         
         var marker = createMarker(point, name, address, zip, city, tel, gen_cellu, spineforce, lipomassage, endermolift, wellbox, cosmetic, mvf, quality, i );
         map.addOverlay(marker);
         var sidebarEntry = createSidebarEntry(marker, name, address, zip, city, tel, website, distance, gen_cellu, spineforce, lipomassage, endermolift, wellbox, cosmetic, mvf, quality, i);
         sidebar.appendChild(sidebarEntry);
         bounds.extend(point);         
       }
       map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));       
       document.getElementById('map').style.visibility = "visible";
       document.getElementById('Legend').style.visibility = "visible";
       $("#wait").hide();    
     });
   }   

    function createMarker(point, name, address, zip, city, tel, gen_cellu, spineforce, lipomassage, endermolift, wellbox, cosmetic, mvf, quality, id) {
      var marker = new GMarker(point, {icon:iconLpg});
      var html = '<div style="height:129px;width:350px;font-size: 13px;"><b>' + name + '</b> <br/>' + address + '<br />' + zip + ' ' + city + '<br /><b>Tél. : ' + tel + '</b><br /><br />'; 
     if(gen_cellu > 0)
        html += '<img src="img/picto-cellu-gen' + gen_cellu + '.png" style="margin-left:3px;">';
      if(endermolift == 1)
        html += '<img src="img/picto-face-over.png" style="margin-left:3px;">';  
      if(lipomassage == 1)
        html += '<img src="img/picto-body-over.png" style="margin-left:3px;">';
      if(spineforce == 1)
        html += '<img src="img/picto-spineforce-over.png" style="margin-left:3px;">';     
      if(wellbox == 1)
        html += '<img src="img/picto-wellbox-over.png" style="margin-left:3px;">';  
      if(cosmetic == 1)
        html += '<img src="img/picto-cosmetique-over.png" style="margin-left:3px;">';     
      if(mvf == 1)
        html += '<img src="img/picto-mvf.png" style="margin-left:3px;">';
      if(quality == 1)
        html += '<img src="img/picto-quality.png" style="margin-left:3px;">';                 
      html += "</div>";
      /*GEvent.addListener(marker, 'mouseover', function() {
        marker.openInfoWindowHtml(html);        
        document.getElementById('sidebar').scrollTop=document.getElementById(id).offsetTop;
      });
      GEvent.addListener(marker, 'mouseout', function() {
        if(!bSel)
            marker.closeInfoWindow();                
      });  */
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
        map.setCenter(point,15);
        document.getElementById('sidebar').scrollTop=document.getElementById(id).offsetTop;
        $("#sidebar div").addClass("lpg-center");
        $("#sidebar div").removeClass("lpg-center-select");
        document.getElementById(id).className = 'lpg-center-select';
        bSel = true;
      });
      return marker;
    }

    function createSidebarEntry(marker, name, address, zip, city, tel, website, distance, gen_cellu, spineforce, lipomassage, endermolift, wellbox, cosmetic, mvf, quality, id) {
      var div = document.createElement('div');
      div.id = id;
      var html = '<div class="lpg-center-text"><b>' + name + '-&gt; <font color="#aa0001">' + distance.toFixed(1) + ' km</font></b><br/>' + address + '<br/>' + zip + ' ' + city + '<br /><b>Tél. : ' + tel + '</b><br />' + website + '</div>&nbsp;';
      if(gen_cellu > 0)
        html += '<img src="img/picto-cellu-gen' + gen_cellu + '.png" alt="Génération Cellu M6">';
      if(endermolift == 1)
        html += '<img src="img/picto-face-over.png" alt ="Soin du visage">';
      if(lipomassage == 1)
        html += '<img src="img/picto-body-over.png" alt="Soin du corps">';
      if(spineforce == 1)
        html += '<img src="img/picto-spineforce-over.png" alt="Soin du dos / Spineforce">';  
      if(wellbox == 1)
        html += '<img src="img/picto-wellbox-over.png" alt="Wellbox">';
      if(cosmetic == 1)
        html += '<img src="img/picto-cosmetique-over.png" alt="Cosmetique">';         
      if(mvf == 1)
        html += '<img src="img/picto-mvf.png">';              
      if(quality == 1)
        html += '<img src="img/picto-quality.png">';        
      div.innerHTML = html;
      div.style.cursor = 'pointer';
      div.className = 'lpg-center';
      
      GEvent.addDomListener(div, 'click', function() {
        GEvent.trigger(marker, 'click');
        map.setCenter(marker,15); 
        bSel = true;
      });
      return div;
    }
    //]]>
