var bloginfo_url = null;
var map = null;


function loadURL(blog_url) {
	bloginfo_url = blog_url;
}

function loadDatas(blog_url) {
	
	bloginfo_url = blog_url;
	
	// If the browser is not compatible, 
    if (GBrowserIsCompatible()) {

		map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl3D());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());        
        map.removeMapType(G_HYBRID_MAP);
		map.addMapType(G_PHYSICAL_MAP);
		map.setMapType(G_PHYSICAL_MAP);
		map.addControl(new GOverviewMapControl());
		map.enableScrollWheelZoom(); 
		map.enableGoogleBar();        
        
        // Center the client on the map
        if(google.loader.ClientLocation != null) {
			map.setCenter(new GLatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude), 13)
		}
		else {
			map.setCenter(new GLatLng(47.9014925, 1.8875715), 13);
		}
		
        // set the Qibla
        //set_qibla(null, null);

        // load the markers
        loadMarkers();

		// ADD A LISTENER ON ZOOM OUT
		GEvent.addListener(map, "zoomend", function() {
			loadMarkers();
		});

		// ADD A LISTENER ON MAP MOVE	
		GEvent.addListener(map, "moveend", function() {
			loadMarkers();
		});

	}
	else {
		$("#map").html("Your browser is not compatible for this website. Please, upgrade you brower with Firefox : <a href='http://www.mozilla-europe.org/fr/firefox/'>http://www.mozilla-europe.org/fr/firefox/</a>");
		alert("Your browser is not compatible for this website. Please, upgrade you brower with Firefox for example : http://www.mozilla-europe.org/fr/firefox/");
	}

}

function loadMarkers() {
	
	var up_longitude = map.getBounds().getNorthEast().x;
	var up_latitude = map.getBounds().getNorthEast().y;

	var down_longitude = map.getBounds().getSouthWest().x;
	var down_latitude = map.getBounds().getSouthWest().y;
	
	var url = bloginfo_url +"/wp-content/themes/trouve-ta-mosquee_v2.0/server_ajax2.php?action=cab87afbe68edae64818f1a867025288&up_latitude="+up_latitude+"&down_latitude="+down_latitude+"&up_longitude="+up_longitude+"&down_longitude="+down_longitude;
	
	GDownloadUrl(url, function(data) {
		xml = GXml.parse(data);
		var datas_markers = xml.documentElement.getElementsByTagName("marker");
		
		if(datas_markers.length > 0) {
        	
        	var markers = [];
        
        	for (var i = 0; i < datas_markers.length; i++) {
            	
            	var type = datas_markers[i].getAttribute("type");
	            var place_name = datas_markers[i].getAttribute("mosquita_name");
	            var address = datas_markers[i].getAttribute("address");
	            var zip = datas_markers[i].getAttribute("zip_code");
	            var city = datas_markers[i].getAttribute("city");
	            var country = datas_markers[i].getAttribute("country");
	            
	            var point = new GLatLng(  parseFloat(datas_markers[i].getAttribute("latitude")),
	                                      parseFloat(datas_markers[i].getAttribute("longitude"))
	                                    );
	            var marker = setMarker(	point,
	            						type,
	            						place_name,
            							address,
            							zip,
            							city,
            							country,
            							parseFloat(datas_markers[i].getAttribute("latitude")),
            							parseFloat(datas_markers[i].getAttribute("longitude"))
	        							);
	            markers.push(marker);	            
	        }
	        var markerCluster = new MarkerClusterer(map, markers);
    	}
	});
}


/**
* Function that handle one marker
* param point GLatLng object
* param place_name string
* param address string
* param zip integer
* param country string
* param city string
* param latitude float
* param longitude float
* return marker
*/
function setMarker(point, type, place_name, address, zip, city, country, latitude, longitude) {

    var baseIcon = new GIcon();
    var marker = null;
    var width = null;
    var height = null;
    switch(type){
    	default:
    	case "mosque":
    		marker = "mosque.png";
    		width = 27;
    		height = 63;
    	break;
    	case "grand_mosque":
    		marker = "grand_mosque.png";
    		width = 65;
    		height = 100;
    	break;
    	case "prayer_room":
    		marker = "prayer_room.png";
    		width = 49;
    		height = 40;
    	break;
    }
	baseIcon.image = bloginfo_url + '/wp-content/themes/trouve-ta-mosquee_v2.0/images/'+ marker;
	baseIcon.iconSize = new GSize(width, height);
	baseIcon.iconAnchor = new GPoint(0, height);
	baseIcon.infoWindowAnchor = new GPoint(20, 10);
	markerOptions = { icon:baseIcon };
	
	var marker = new GMarker(point, markerOptions);
	var html = "<strong>" + place_name
				+ "</strong><br/>" + address
				+ "<br/>" + zip + " " + city
				+ "<br/>" + country;
	
	GEvent.addListener(marker, 'click', function() {

		set_qibla(latitude, longitude);		
		marker.openInfoWindowHtml(html);		
		getPrayTimes(country, city, latitude, longitude, 0);
	});
	
	return marker;
}
/**
* This method set the position in the map after a search of some mosque
* param country string
* param city string
* param latitude float
* param longitude float
*/
function setMapPosition(latitude, longitude, city, country){
		
		getPrayTimes(country, city, latitude, longitude, 0);
		set_qibla(latitude, longitude);
		
		map.setCenter(new GLatLng(latitude, longitude), 13);
		
	}

/**
* Function that displays the pray times in the sidebar
* param country string
* param city string
* param latitude float
* param longitude float
*/
function getPrayTimes(country, city, latitude, longitude, rub){
	
	var datas = $.ajax({
		type: "POST",
		url: bloginfo_url + "/wp-content/themes/trouve-ta-mosquee_v2.0/server_ajax.php",
		data: "action=10a7a36a20205e7e38329a5ae8206b76&country="+ country
			+	"&city="+ city
			+	"&latitude="+ latitude
			+	"&longitude="+ longitude
			+   "&rub="+ rub,
		async: false,
		success: function(msg){
			if(msg == "false"){
				alert("Connection error or permission denied. Please retry later.");
			}
		}
	}).responseText;
	$("#pray_infos").html(datas);
}

/**
* Function that shows the loading animation and put opacity on the map
*/
function set_qibla(latitude, longitude){
 
	if(latitude == null || longitude == null) {
		if(google.loader.ClientLocation != null) {
			latitude = google.loader.ClientLocation.latitude;
			longitude = google.loader.ClientLocation.longitude;
		}
		else {
			latitude = 47.9014925;
			longitude = 1.8875715;
		}
	}
	if (typeof(polyline) != 'undefined'){
		map.removeOverlay(polyline);
	}
	polyline = new GPolyline([new GLatLng(latitude, longitude), new GLatLng(21.4225, 39.8261)], "#ff0000", 5);
	map.addOverlay(polyline);
}