$(document).ready(function() {
	initialize();
	$('#dialog').dialog({ autoOpen: false });	
});

function specificInit() {
	map.mapTypes.set('style1', style1MapType);   
	map.setMapTypeId('style1');
  	
	google.maps.event.addListenerOnce(map, 'tilesloaded', function() {getMarkers();initFiltersControls();});
	google.maps.event.addListener(map, 'dragend', function() {getMarkers();});
	google.maps.event.addListener(map, 'zoom_changed', function() {getMarkers();});
    google.maps.event.addListener(map, 'click', function(event) {showCoordinates(event.latLng);});
	
    createControls();	
}

function getMarkers() {
	cleanMarkers();
	
	var locationLat=map.getCenter().lat();
	var locationLng=map.getCenter().lng();
	var zoom=map.getZoom();
	var north = map.getBounds().getNorthEast().lat(); 
	var east = map.getBounds().getNorthEast().lng(); 	
	var south = map.getBounds().getSouthWest().lat();
	var west=map.getBounds().getSouthWest().lng();

	$.ajax({
		type: 'POST',
		url: "ajx.php",
		data: "action=getMarkers" +
				"&NORTH="+north+
				"&EAST="+east+
				"&SOUTH="+south+
				"&WEST="+west+
				"&filterAdType="+$('#filterAdType').val()+
				"&filterAdMinSurface="+$('#filterAdMinSurface').val()+
				"&filterAdMaxSurface="+$('#filterAdMaxSurface').val()+
				"&filterAdMaxPrice="+$('#filterAdMaxPrice').val()+
				"&filterAdMinNbrRoom="+$('#filterAdMinNbrRoom').val(),
		success: function(data){
			//$("#console").html(data);
			adsArray=jQuery.parseJSON(data);
			for (x in adsArray) {placeMarker(adsArray[x]);}
		}
	});
}

function placeMarker(ad) {
	var latlng = new google.maps.LatLng(ad.GEO_LAT, ad.GEO_LNG);
	
	var image = new google.maps.MarkerImage(ad.IMAGE,
		      // This marker is 20 pixels wide by 32 pixels tall.
		      new google.maps.Size(40, 40));

	var marker = new google.maps.Marker({ 
		id:ad.IDENT,
		position: latlng,  
		map: map,
		title: ad.TITLE+" "+ad.PRICE+"€",
		icon: image
	});	 
 
	google.maps.event.addListener(marker, 'click', function() {		
		  getAdInfos(ad.IDENT,marker);		  	  
	});  	
	markerArray.push(marker);
}

function getAdInfos(ID,marker) {	
	$.ajax({
		type: 'POST',
		url: "ajx.php",
		data: "action=getAdInfos" +
				"&ID="+ID,
		success: function(data){
			//$("#console").html(data);	
			closeAllInfoWindow();
			
			
			var infoWindowOptions = { 
				maxWidth:'700',
				content: data	
			}; 
			//map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
			
			infowindow = new google.maps.InfoWindow(infoWindowOptions);	
			
			infowindowArray.push(infowindow);	
			infowindow.open(map,marker);
		}
	});	
}

function getUI(UI,params) {
	var data="action=getUI" +"&UI="+UI;
	if (params) {
		data+='&'+params;
	}
	$.ajax({
		type: 'POST',
		url: "ajx.php",
		data: data,
		success: function(data){
			UI=jQuery.parseJSON(data);
			//$("#console").html(data);	
			$(function() {
				$( "#dialog" ).dialog( "option", "title", UI.TITLE);				
				$( "#dialog" ).dialog( "option", "width", UI.WIDTH );
				$( "#dialog" ).html(UI.HTML);
				$( "#dialog" ).dialog({ resizable: false });
				$( "#dialog" ).dialog({
					autoOpen: false,	
					modal:true,
					show: "Clip",
					hide: "Clip"
				});

				$( "#dialog" ).dialog('open');				
			});

		}
	});	
}

function showImgPreview(event,imgPath) {
	var img = new Image();
	img.src=imgPath;
	
	$('#bigPicture').hide();	
	$('#bigPicture').css("background-image", "url("+imgPath+")");
	$('#bigPicture').css("top",event.clientY-150+"px");
	$('#bigPicture').css("left", event.clientX-200+ "px");
	$('#bigPicture').css("width",img.width);
	$('#bigPicture').css("height",img.height);
	$('#bigPicture').fadeIn(1000);
}

function infoDialog(TITLE,WIDTH,DATA) {
	$( "#infoDialog" ).dialog({ buttons: { "Ok": function() { $(this).dialog("close"); } } });
	$( "#infoDialog" ).dialog( "option", "title", TITLE);	
	$( "#infoDialog" ).dialog( "option", "width", WIDTH );
	$( "#infoDialog" ).dialog({ resizable: false });
	$( "#infoDialog" ).html(DATA);
	$( "#infoDialog" ).dialog({
		autoOpen: false,	
		modal:true,
		show: "Clip",
		hide: "Clip"
	});

	$("#infoDialog").dialog('open');		
}


function uploadPic(input) {
	$.ajax({
		type: 'POST',
		url: "upload.php",
		data: "file="+input.value,
		   success: function(data){
			$("#console").html(data);	
		     
		   }
	});
}

function getMAPUI(MAPUI,element) {	
	$.ajax({
		type: 'POST',
		url: "ajx.php",
		data: "action=getMAPUI" +
				"&MAPUI="+MAPUI,
		success: function(data){
			$(element).html(data);
		}
	});	
}

