$(function() {
	carregarImperdiveis(1, null, function() {
		var coords = this;
		if(null != coords && coords.length > 0) {
			$("#divMapsImperdiveis #divMapa").gMap({
				isHome: true,
				mapOptions:
				{
					mapTypeId: google.maps.MapTypeId.SATELLITE,
					zoom:12,
					mapTypeControl: false,
					streetViewControl: false,
					center: new google.maps.LatLng(coords[0].latitude, coords[0].longitude)
				}
			});
			
			var $gmap = $("#divMapsImperdiveis #divMapa").data('gmap');

			google.maps.event.addListener($gmap, 'tilesloaded', function() {
				google.maps.event.clearListeners($gmap, 'tilesloaded');
				mostrarImperdiveis($gmap);
				google.maps.event.addListener($gmap, 'dragend', function() {
					mostrarImperdiveis($gmap);
				});
				
				google.maps.event.addListener($gmap, 'zoom_changed', function() {
					mostrarImperdiveis($gmap);
				});
			});
		}						  
	});

	if( parseInt($("input#single").val()) == 1 ) {
		carregar("loadid", null, true, function() {
			var current = this;
			if(null != current) {
				$("#divMapaInterna").gMap({
					isSingle: true,
					mapOptions:
					{
						mapTypeId: google.maps.MapTypeId.SATELLITE,
						mapTypeControl: true,
						streetViewControl: true,
						zoom:15,
						center: new google.maps.LatLng(current.latitude, current.longitude)
					}
				});
				
				var $gmapMain = $("#divMapaInterna").data('gmap');

				google.maps.event.addListener($gmapMain, 'tilesloaded', function() {
					google.maps.event.clearListeners($gmapMain, 'tilesloaded');
					mostrar($gmapMain, true);
					google.maps.event.addListener($gmapMain, 'dragend', function() {
						mostrar($gmapMain, false);
					});
					
					google.maps.event.addListener($gmapMain, 'zoom_changed', function() {
						mostrar($gmapMain, false);
					});
				});
			}
		});
	}
	
	$("#searchform input").bind("focus", function() {
		if(this.value == this.title) this.value = "";
	}).bind("blur", function() {
		if(this.value.replace(/ /g,'') == "") this.value = this.title;
	});
	$("#divMaisVotados .divVotar a, #divImperdiveis .divVotar a, #divPostagens .divVotar a").click(function(e) {
		e.preventDefault();
		var id = $(this).attr('rel');
		
		$.post(ajaxUrl +'/ajax-actions.php',
			{
				action: 'checkrate',
				postId: id
			},
			function(ret_check) {
				if(ret_check == true) {
					$("span",$(this).parent()).html('<img src="'+ ajaxUrl +'/images/ajax-loader.gif" alt="Loading" />')
					
					var $this = $(this);
					$.post(ajaxUrl +'/ajax-actions.php',
						{
							action: 'rate',
							postId: id
						},
						function(ret) {
							$(".divVotar span",$('.post-'+id)).html(ret);
							alert("Apreciado com sucesso.");
						}
					);
				}
				else { alert("Você já apreciou esse graffiti antes."); }
			}
		);
	});
});

function carregar(action, bounds, autoopen, fn) {
	var jsonData = {
		action: action,
		postid: autoopen ? $("input#post-id").val() : 0
	};

	if(null != bounds) {
		jsonData.leste = bounds.leste;
		jsonData.oeste = bounds.oeste;
		jsonData.norte = bounds.norte;
		jsonData.sul   = bounds.sul;
	}
	
	$.getJSON(ajaxUrl +'/ajax-actions.php', jsonData,
		function(current) {
			if(typeof(fn) === "function") fn.call(current);
		}
	);	
}

function mostrar($gmap, autoopen) {
	if($gmap.isLoadingMore == true) return;
	$gmap.isLoadingMore = true;
	var b = $gmap.getBounds();
	var bounds = {
		leste: b.getNorthEast().lng(),
		oeste: b.getSouthWest().lng(),
		sul: b.getSouthWest().lat(),
		norte: b.getNorthEast().lat()
	}
	
	carregar("loadsingle", bounds, autoopen, function() {
		$.fn.gMap.addMarker($gmap, this, {
			isSingle: true		
		});  
		$gmap.isLoadingMore = false;
	});
}

function carregarImperdiveis(qtde, bounds, fn) {
	var jsonData = {
		action: 'loadhome',
		qtde: qtde
	};
	
	if(null != bounds) {
		jsonData.leste = bounds.leste;
		jsonData.oeste = bounds.oeste;
		jsonData.norte = bounds.norte;
		jsonData.sul   = bounds.sul;
	}

	$.getJSON(ajaxUrl +'/ajax-actions.php', jsonData,
		function(coords) {
			if(typeof(fn) === "function") fn.call(coords);
		}
	)	
}

function mostrarImperdiveis($gmap) {
	if($gmap.isLoadingMore == true) return;
	$gmap.isLoadingMore = true;
	var b = $gmap.getBounds();
	var bounds = {
		leste: b.getNorthEast().lng(),
		oeste: b.getSouthWest().lng(),
		sul: b.getSouthWest().lat(),
		norte: b.getNorthEast().lat()
	}
	carregarImperdiveis(-1, bounds, function() {
		$.fn.gMap.addMarker($gmap, this, {
			isHome: true		
		}); 

		$gmap.isLoadingMore = false;
	});
}
