//<![CDATA[

var link = 'http://www.tiptop-location.fr/';
var geocoder;
var map;
/*
 * Map de la page d'accueil permettant de sélectionner un point de location
 */

function load_map_loca(latitude, longitude) {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(latitude, longitude);
    var myOptions = {
        zoom: 6,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_loca"), myOptions);
    var fluster = new Fluster2(map);
    var infoWindow = new google.maps.InfoWindow;

    downloadUrl(link+"index.php/psl/listingComplet", function(data) {
        var xml = parseXml(data);
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
            var slug = markers[i].getAttribute("slug");
            var nom = markers[i].getAttribute("nom");
            var description = markers[i].getAttribute("description");
            var horaires = markers[i].getAttribute("horaires");
            var ouverture = markers[i].getAttribute("ouverture_permanente");
            var telephone = markers[i].getAttribute("telephone");
            var portable = markers[i].getAttribute("portable");
            var fax = markers[i].getAttribute("fax");
            var siteweb = markers[i].getAttribute("siteweb");
            var image = markers[i].getAttribute("image");
            var adresse = markers[i].getAttribute("adresse");
            var cp = markers[i].getAttribute("cp");
            var ville = markers[i].getAttribute("ville");
            var lat = markers[i].getAttribute("latitude");
            var lng = markers[i].getAttribute("longitude");
			
            var point = new google.maps.LatLng(
                parseFloat(lat),
                parseFloat(lng)
                );

            var images = "";

            if(ouverture == 1){
                images = link+'images/picto1.png';
            }else{
                images = link+'images/picto2.png';
            }
			
            var marker = new google.maps.Marker({
                map: map,
                position: point,
                icon: images
            });
			fluster.addMarker(marker);

            var html = "";

            if(ouverture == 1){
                html = getHtmlPermanent(slug, nom, description, horaires, telephone, portable, fax, siteweb, image, adresse, cp, ville);
            }else{
                html = getHtml(slug, nom, description, horaires, telephone, portable, fax, siteweb, image, adresse, cp, ville);
            }
            
            bindInfoWindow(marker, map, infoWindow, html);
        }
		fluster.styles = {
		// This style will be used for clusters with more than 0 markers
		0: {
			image: link+'images/picto1.png',
			//textColor: '#fff',
	
			width: 50,
			height: 50
		},
		// This style will be used for clusters with more than 10 markers
		1: {
			image: link+'images/picto1.png',
			//textColor: '#fff',
			
			width: 50,
			height: 50
		},
		20: {
			image: link+'images/picto1.png',
			//textColor: '#fff',
			
			width: 50,
			height: 50
		}
	};
		fluster.initialize();
    });
}

function getHtmlPermanent(slug, nom, description, horaires, telephone, portable, fax, siteweb, image, adresse, cp, ville) {
    html = "";
    html += "<div style='width:300px; height:auto;'>";
    html += "<strong>";
    html += nom+"<br /> Ouvert 24H/24";
    html += "</strong>";
    html += "<br />";
    html += "<p style='margin:5px 0 5px 0;'>"+adresse+" "+cp+" "+ville+"</p>";
    html += "<a href='"+link+"index.php/reservation/index/psl/"+slug+"'><img src='"+link+"/images/btn_choix_google.jpg' alt='Choisir ce point' title='Choisir ce point'/></a>";
    html += "<br /><br />";
    html += "</div>";
    return html;
}

function getHtml(slug, nom, description, horaires, telephone, portable, fax, siteweb, image, adresse, cp, ville) {
    html = "";
    html += "<div style='width:300px; height:auto;'>";
    html += "<strong>";
    html += nom+"<br /> Horaires d'ouverture: <br /></strong>"+horaires;
    html += "<br />";
    html += "<p style='margin:5px 0 5px 0;'>"+adresse+" "+cp+" "+ville+"</p>";
    html += "<a href='"+link+"index.php/reservation/index/psl/"+slug+"'><img src='"+link+"/images/btn_choix_google.jpg' alt='Choisir ce point' title='Choisir ce point'/></a>";
    html += "<br /><br />";
    html += "</div>";
    return html;
}

function choosePsl(slug, nom) {
    $('psl').value = slug;
    $('choix_psl').innerHTML = "<p>PSL: "+nom+"</p>";
}

function bindInfoWindow(marker, map, infoWindow, html) {
    google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
    });
}

function downloadUrl(url,callback) {
    var request = window.ActiveXObject ?
    new ActiveXObject('Microsoft.XMLHTTP') :
    new XMLHttpRequest;

    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            request.onreadystatechange = doNothing;
            callback(request.responseText, request.status);
        }
    };

    request.open('GET', url, true);
    request.send(null);
}

function parseXml(str) {
    if (window.ActiveXObject) {
        var doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.loadXML(str);
        return doc;
    } else if (window.DOMParser) {
        return (new DOMParser).parseFromString(str, 'text/xml');
    }
}


function recherche()
{
	var zoom;
	var address = document.getElementById("adresse").value;
	address = address.toLowerCase();
	//alert(address);
	if((address.indexOf("rue") != -1) || (address.indexOf("allée") != -1) || (address.indexOf("allee") != -1) || (address.indexOf("avenue") != -1) || (address.indexOf("impasse") != -1))
	{
		zoom = 15;	
		
	}
	else { 
		zoom = 11;
	}

	var address = document.getElementById("adresse").value+", france";
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
		  map.setZoom(zoom);
          /*var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });*/
        } else {
          alert("Cette destination n'existe pas " + status);
        }
      });
    }

	
}

function doNothing() {}

//]]> 
