var map;
var infowindow;
var gmarkers = [];
var markerClusterer = null;
var theSelectedMarker = null;
var infoBubbleWindow = new InfoBubble({
    maxWidth: 380,
    borderWidth: 0,
    borderColor: '#FFFFFF',
    disableAutoPan: true
});
var minHeight = 190;
infoBubbleWindow.setMinHeight(minHeight);
var minWidth = 280;
infoBubbleWindow.setMinWidth(minWidth);
function initialize() {
    var latlng = new google.maps.LatLng(-26.327884, 28.204651);
    var myOptions = {
	zoom: 9,
	center: latlng,
	mapTypeId: google.maps.MapTypeId.ROADMAP,
	panControlOptions: {
	    position: google.maps.ControlPosition.LEFT_BOTTOM
    	},
	zoomControlOptions: {
	    position: google.maps.ControlPosition.LEFT_BOTTOM,
	    style: google.maps.ZoomControlStyle.DEFAULT
	},
	streetViewControl: true,
	streetViewControlOptions: {
	    position: google.maps.ControlPosition.LEFT_BOTTOM,
	},
	mapTypeControl: false
    };
    map = new google.maps.Map(document.getElementById('map'), myOptions);
    infowindow = new google.maps.InfoWindow();
    if (property_id != 0) {
	openDetailsPopup(property_id);
    }
    else {
	askForOptionView();
    }
}

function dataLoad(loadVal) {
    clearOverlays();
    gmarkers = [];
    var str = '';
    $.ajax({
	type: 'POST',
	url: 'index.php',
	data: {
	    mode: 'searchData',
	    loadVal: loadVal
	},
	async: false,
	dataType: 'json',
	success: function(msg) {
	    var html = '';
	    for (i = 0; i < msg.length; i++) {
		pnt = new google.maps.LatLng(msg[i]['normalData'].lat,msg[i]['normalData'].lang);
		html = '<table width="100%" border="0" cellspacing="3" cellpadding="0">';
		if (msg[i]['images'].length > 0) {
		    html += '<tr><td><img src="proparty_image/thumb/' + msg[i]['images'][0] + '" /></td></tr>';
		}
		html += '<tr><td width="12%"><strong>' + msg[i]['normalData'].property_title + '</strong></td></tr><tr><td>' + CURRENCY + ' ' + formatWithComma(msg[i]['normalData'].price) + '</td></tr><tr><td>' + formatWithComma(msg[i]['normalData'].prop_size) + ' - Meter sqaure</td></tr><tr><td>' + msg[i]['normalData'].bedrooms;
		if (msg[i]['normalData'].bedrooms > 1) {
		    html += ' bedrooms';
		}
		else {
		    html += ' bedroom';
		}
		//html += '</td></tr><tr><td>' + starHtml(msg[i]['normalData'].rating) + '</tr></td></table>';
		var ratingID = 'star-rating';
		html += '</td></tr><tr><td><div id="' + ratingID + '" class="rating"></div></tr></td></table>';
		
		createMarker(pnt, html,'images/marker.png',msg[i]['normalData'].property_id, {ID:ratingID, curvalue:msg[i]['normalData'].rating, propertyID: msg[i]['normalData'].property_id});
	    }
	    markerClusterer = new MarkerClusterer(map, gmarkers, {
		maxZoom: 11,
		gridSize: 50
	    });
	}
    });
}

function moreInfo(marker) {
    google.maps.event.addListener(marker, 'click', function () {
	infoBubbleWindow.close();
	this.setIcon('images/the-marker-selected.png');
	map.panTo(latlng);
	if (theSelectedMarker != null) {
	    theSelectedMarker.setIcon('images/marker-selected.png');
	}
	theSelectedMarker = marker;
	openDetailsPopup(property_id);
    });
}

function clearOverlays() {
    if (gmarkers) {
	for (var i in gmarkers) {
	    gmarkers[i].setMap(null);
	    markerClusterer.removeMarker(gmarkers[i]);
	}
    }
}

function createMarker(latlng, content, ikon, property_id, rating) {
    if (ikon != '') {
	var marker = new google.maps.Marker({
	    position: latlng,
	    icon: ikon
	});
    }
    else {
	var marker = new google.maps.Marker({
	    position: latlng
	});
    }
    google.maps.event.addListener(marker, 'click', function () {
	infoBubbleWindow.close();
	
	this.setIcon('images/the-marker-selected.png');
	map.panTo(latlng);
	if (theSelectedMarker != null) {
	    theSelectedMarker.setIcon('images/marker-selected.png');
	}
	theSelectedMarker = marker;
	openDetailsPopup(property_id);
    });
    google.maps.event.addListener(marker, 'mouseover', function () {
	infoBubbleWindow.open(map, this);
	infoBubbleWindow.setContent(content);
	
	if (rating) {
	    var _interval = setInterval(function(){
		if ($('#' + rating.ID).length > 0) {
		    clearInterval(_interval);
		    $('#' + rating.ID).rating1('edit-proparty.php?mode=favouriteSave&property_id=' + rating.propertyID, {maxvalue:5,increment:.5,curvalue:rating.curvalue});
		}
	    }, 100);
	}
	//if (rating) {
	    //alert('Rating: ' + $('#' + rating.ID).length);
	    //$('#rate').rating('edit-proparty.php?mode=favouriteSave&property_id='+$('#prop_id').val(), {maxvalue:5,increment:.5,curvalue:rating});
	    //$('#' + rating.ID).rating('edit-proparty.php?mode=favouriteSave&property_id=1', {maxvalue:5,increment:.5,curvalue:2});
	//}
    });
    google.maps.event.addListener(marker, 'mouseout', function () {
	infoBubbleWindow.close();
    });
    gmarkers.push(marker);
}

function formatWithComma(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
	x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function searchData() {
    clearOverlays();
    gmarkers = [];
    var str ="";
    $.ajax({
	type: 'POST',
	url: 'filter.php',
	data: 'mode=filterData&' + $('#searchfrm').serialize(),
	async: false,
	dataType: "json",
	success: function(msg){
	    var html = '';
	    
	    for (var i = 0; i < msg.length; i++) {
		pnt = new google.maps.LatLng(msg[i]['normalData'].lat,msg[i]['normalData'].lang);
		html = '<table width="100%" border="0" cellspacing="3" cellpadding="0">';
		if (msg[i]['images'].length > 0) {
			html +='<tr><td><img src="proparty_image/thumb/'+msg[i]['images'][0]+'" /></td></tr>';
		}
		html +='<tr><td width="12%"><strong>'+ msg[i]['normalData'].property_title +'</strong></td></tr><tr><td>'+CURRENCY+' '+ formatWithComma(msg[i]['normalData'].price) +'</td></tr><tr><td>'+ formatWithComma(msg[i]['normalData'].prop_size) +' - Meter sqaure</td></tr><tr><td>'+ msg[i]['normalData'].bedrooms;
		if(msg[i]['normalData'].bedrooms>1)
			html += ' bedrooms';
		else
			html += ' bedroom';
		//html += '</td></tr><tr><td>'+starHtml(msg[i]['normalData'].rating)+'</tr></td></table>';
		var ratingID = 'star-rating';
		html += '</td></tr><tr><td><div id="' + ratingID + '" class="rating"></div></tr></td></table>';
		createMarker(pnt, html,'images/marker.png',msg[i]['normalData'].property_id, {ID:ratingID, curvalue:msg[i]['normalData'].rating, propertyID: msg[i]['normalData'].property_id});
	    }
	    markerClusterer = new MarkerClusterer(map, gmarkers, {
		    maxZoom: 11,
		    gridSize: 50
	    });
	}
    });
}	

function starHtml(rate) {
	html = '<div>';
	if( rate == 0)
	{
		html += '<img src="images/gray.png" /><img src="images/gray.png" /><img src="images/gray.png" /><img src="images/gray.png" /><img src="images/gray.png" />';	
	}
	else if( rate == .5)
	{
		html += '<img src="images/half.png" /><img src="images/gray.png" /><img src="images/gray.png" /><img src="images/gray.png" /><img src="images/gray.png" />';	
	}
	else if( rate == 1)
	{
		html += '<img src="images/full.png" /><img src="images/gray.png" /><img src="images/gray.png" /><img src="images/gray.png" /><img src="images/gray.png" />';	
	}
	else if( rate == 1.5)
	{
		html += '<img src="images/full.png" /><img src="images/half.png" /><img src="images/gray.png" /><img src="images/gray.png" /><img src="images/gray.png" />';	
	}
	else if( rate == 2)
	{
		html += '<img src="images/full.png" /><img src="images/full.png" /><img src="images/gray.png" /><img src="images/gray.png" /><img src="images/gray.png" />';	
	}
	else if( rate == 2.5)
	{
		html += '<img src="images/full.png" /><img src="images/full.png" /><img src="images/half.png" /><img src="images/gray.png" /><img src="images/gray.png" />';	
	}
	else if( rate == 3)
	{
		html += '<img src="images/full.png" /><img src="images/full.png" /><img src="images/full.png" /><img src="images/gray.png" /><img src="images/gray.png" />';	
	}
	else if( rate == 3.5)
	{
		html += '<img src="images/full.png" /><img src="images/full.png" /><img src="images/full.png" /><img src="images/half.png" /><img src="images/gray.png" />';	
	}
	else if( rate == 4)
	{
		html += '<img src="images/full.png" /><img src="images/full.png" /><img src="images/full.png" /><img src="images/full.png" /><img src="images/gray.png" />';	
	}
	else if( rate == 4.5)
	{
		html += '<img src="images/full.png" /><img src="images/full.png" /><img src="images/full.png" /><img src="images/full.png" /><img src="images/half.png" />';	
	}
	else if( rate == 5)
	{
		html += '<img src="images/full.png" /><img src="images/full.png" /><img src="images/full.png" /><img src="images/full.png" /><img src="images/full.png" />';	
	}
	html += '</div>';
	html = '';
	return html;
}
function getProperty(property_id) {
	    var propertylatlng = null;
		clearOverlays();
		gmarkers = [];
		var str ="";
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "mode=searchData&loadVal=0",	
			async: false,
			dataType: "json",
			success: function(msg){
				var html = '';
				for(i=0;i<msg.length;i++)
				{
					pnt = new google.maps.LatLng(msg[i]['normalData'].lat,msg[i]['normalData'].lang);
					html = '<table width="100%" border="0" cellspacing="3" cellpadding="0">';
					if(msg[i]['images'].length>0)
					{
						html +='<tr><td><img src="proparty_image/thumb/'+msg[i]['images'][0]+'" /></td></tr>';
					}
					html +='<tr><td width="12%"><strong>'+ msg[i]['normalData'].property_title +'</strong></td></tr><tr><td>'+CURRENCY+' '+ formatWithComma(msg[i]['normalData'].price) +'</td></tr><tr><td>'+ formatWithComma(msg[i]['normalData'].prop_size) +' - Meter sqaure</td></tr><tr><td>'+ msg[i]['normalData'].bedrooms;
					if(msg[i]['normalData'].bedrooms>1)
						html += ' bedrooms';
					else
						html += ' bedroom';
					//html += '</td></tr><tr><td>'+starHtml(msg[i]['normalData'].rating)+'</tr></td></table>';
					var ratingID = 'star-rating';
					html += '</td></tr><tr><td><div id="' + ratingID + '" class="rating"></div></tr></td></table>';
		
					
					if( msg[i]['normalData'].property_id == property_id)
					{
						propertylatlng = pnt;
						createMarker(pnt, html,'images/marker-selected.png',msg[i]['normalData'].property_id, {ID:ratingID, curvalue:msg[i]['normalData'].rating, propertyID: msg[i]['normalData'].property_id});
					}
					else
					{
						createMarker(pnt, html,'images/marker.png',msg[i]['normalData'].property_id, {ID:ratingID, curvalue:msg[i]['normalData'].rating, propertyID: msg[i]['normalData'].property_id});
					}
				}
				markerClusterer = new MarkerClusterer(map, gmarkers, {
					maxZoom: 11,
					gridSize: 50
				});
				map.setCenter(propertylatlng);
				map.setZoom(12);
			}
		});
	}
	function createMarkerShow(latlng,content, ikon, property_id,pid)
	{
		if( ikon != "")
		{
			var marker = new google.maps.Marker({
				position: latlng,
				icon: ikon
			});
		}
		else
		{
			var marker = new google.maps.Marker({
				position: latlng
			});
		}
		google.maps.event.addListener(marker, 'click', function () {
		  infoBubbleWindow.close();
		 
		  this.setIcon('images/the-marker-selected.png');
		  map.panTo(latlng);
		   if(theSelectedMarker!=null)
			   theSelectedMarker.setIcon('images/marker-selected.png');
		   theSelectedMarker = marker;
		  openDetailsPopup(property_id);
		});
		google.maps.event.addListener(marker, 'mouseover', function () {
		  infoBubbleWindow.open(map, this);
		  infoBubbleWindow.setContent(content);
		});
		google.maps.event.addListener(marker, 'mouseout', function () {
			infoBubbleWindow.close();
		});
		if( pid == property_id)
		{
			 infoBubbleWindow.open(map, this);	
		}
		gmarkers.push(marker);	
	}
