/**
* Autor: Jorge Casas [jorgecasas@iternova.net]
* Version: 1.0.5
* Fecha modificación: 20090701
*/

var categoryID;
var codeID;
var lat;
var lng;
//ICONO 1 - LOCALIZACION EXACTA
var iconApartamento1 = new GIcon(); 
iconApartamento1.image = 'modules/apartamentos/images/apartamentos_icon1.png';
//iconApartamento1.shadow = 'images/googlemarkshadow.png';
iconApartamento1.iconSize = new GSize(36, 36);
//iconApartamento1.shadowSize = new GSize(22, 20);
iconApartamento1.iconAnchor = new GPoint(18, 36);
iconApartamento1.infoWindowAnchor = new GPoint(12,8);

//ICONO 2 - LOCALIZACION APROXIMADA
var iconApartamento2 = new GIcon(); 
iconApartamento2.image = 'modules/apartamentos/images/apartamentos_icon2.png';
//iconApartamento2.shadow = 'images/googlemarkshadow.png';
iconApartamento2.iconSize = new GSize(36, 36);
//iconApartamento2.shadowSize = new GSize(22, 20);
iconApartamento2.iconAnchor = new GPoint(18, 36);
iconApartamento2.infoWindowAnchor = new GPoint(12,8);

//RESTO DE PARAMETROS NECESARIOS - ASOCIACION DE ICONOS UTILIZADOS
var customIcons = [];
customIcons['puntoApartamento1'] = iconApartamento1;
customIcons['puntoApartamento2'] = iconApartamento2;

var markerGroups = { 'puntoApartamento1':[],'puntoApartamento2':[]};				
var visibilityGroups = { 'puntoApartamento1':true,'puntoApartamento2':true};

var groups= new Array( 'puntoApartamento1','puntoApartamento2');				
var map;
var gdir;
var tooltip; 
var gmarkers = [];
var bubbles = [];
var refreshId = 0;

function createMarker(point, name, html, type, i) {
	var marker = new GMarker(point, customIcons[type]);
  	marker.tooltip = '<div class=\'tooltip\'>'+name+'</div>';
	markerGroups[type].push(marker);
	GEvent.addListener(marker, 'click', function(){marker.openInfoWindowHtml(html,{maxWidth:250});});
	GEvent.addListener(marker,'mouseover', function(){showTooltip(marker);});
    GEvent.addListener(marker,'mouseout', function(){tooltip.style.visibility='hidden';});   
  	bubbles[i] = name;
  	gmarkers[i] = marker;  
  	return marker;
}
//This function displays the tooltip, it can be called from an icon mousover or a side_bar mouseover
function showTooltip(marker) {
	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
	pos.apply(tooltip);
	tooltip.style.visibility='visible';
} 

function apartamentosRefreshMarkers(lat,lng,getall,advanced){
    var request = GXmlHttp.create();
    if (getall!=null){
    	var urlAjaxJS='index.php?type=publicactions&zone=apartamentos&action=searchjsall&categoryID=-1&codeID='+codeID;
    }else{
    	if (advanced!=null){
    		var urlAjaxJS='index.php?type=publicactions&zone=apartamentos&action=searchjs&categoryID=-1&codeID='+codeID+apartamentosSearchGETAdvanced();
    	}else{
    		var urlAjaxJS='index.php?type=publicactions&zone=apartamentos&action=searchjs&categoryID=-1&codeID='+codeID+apartamentosSearchGETSimple();
    	}
    }
	request.open('GET', urlAjaxJS, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			map.clearOverlays();
			
			//alert(jscript);
			var points;
			var vertexpoints;
			var vertexpolygon;
			var jscript = request.responseText;
			
			eval(jscript);
			//create each point from the list
			if (points.length>0) var setcenter=true;
			else setcenter=false;
			var mapcenter=null;
			for (i in points) {
				if(!isNaN(i)){
					var point = new GLatLng(points[i].lat,points[i].lng);
					var marker = createMarker(point, points[i].title, points[i].text, points[i].type, i);
					if (getall==null) {//si vemos todos no se centra
						mapcenter=new GLatLng(points[i].lat,points[i].lng);
					}
					//no añadimos marker al visor, solo al cluster (array markers)
					//map.addOverlay(marker);
					markers.push(marker);
					if (visibilityGroups[points[i].type]){
						gmarkers[i].show();
				   	} else {
				   		gmarkers[i].hide();
				   	}
				}
			}
			//no mostramos area de busqueda
			//map.addOverlay(vertexpolygon);
			if (mapcenter!=null){
				map.setCenter(mapcenter);
			}
			var markerCluster = new MarkerClusterer(map, markers);
		}
	}
	request.send(null);
}
