var mapaObj;
var overlays = new Array();
var markers = new Array();

function loadMapa(lat, lng, zoom) {
	
	//Setup map
    var latlng = new google.maps.LatLng(lat, lng);
    var myOptions = {
      zoom: parseInt(zoom),
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    mapObj = new google.maps.Map($('#mapaContent').get(0), myOptions);
    
    google.maps.event.addListener(mapObj, 'click', function() {
    	hideOverlays()
    });
    
    activateList();
   	
}

function hideOverlays() {
	for(i = 0; i < overlays.length; i++)
		overlays[i].hide()
}

function Label(opt_options, position, data) {
	// Initialization
	this.setValues(opt_options);
	
	// Label specific	
	var div = this.div_ = document.createElement('div');
	div.style.cssText = 'position: absolute; display: none; width: 334px; height: 110px; background-image: url(images/balloon.png);padding:10px 20px;z-index:100000';
	
	var html = '<a href="javascript:detalle(\''+data.url+'\')" style="z-index:10000;position:absolute;width:84px;height:14px;text-indent:-9999px;margin: 51px 0 0 268px; left:0">mas info</a>';
	html += '<img src="'+data.foto+'" width="82" height="71" style="float:left;margin-right:10px;" />';
	html += '<p style="margin:2px 0;font-weight:bold;color:#FFF;font-size:15px;font-family:Helvetica,Arial,sans-serif">'+data.nombre+'</p>';
	html += '<p style="margin:2px 0;color:#c8c8c8;font-size:13px;font-family:Helvetica,Arial,sans-serif">'+data.direccion+'</p>';
	
	if(data.tipo == 'Piso' || data.tipo == 'Casa')
		var dorm = data.dormitorios+' dormitorio'+(data.dormitorios > 1 ? 's' : '')+' - ';
	else
		var dorm = '';
	
	html += '<p style="margin:2px 0;color:#c8c8c8;font-size:13px;font-family:Helvetica,Arial,sans-serif">'+dorm+data.superficie+'</p>';
	html += '<p style="margin:2px 0;color:#FFF;font-size:13px;font-family:Helvetica,Arial,sans-serif">'+(data.precio == 0 ? 'A consultar' : data.precio+' euros')+'</p>';
	
	div.innerHTML = html;
	
	div.onclick = function() {
		this.style.display = 'none'
	}
	
	this._position = position;
	this._zoom = data.zoom;
	
};

Label.prototype = new google.maps.OverlayView;

Label.prototype.onAdd = function() {
	var pane = this.getPanes().floatPane;
	pane.appendChild(this.div_);

};

Label.prototype.draw = function() {
	
	var div = this.div_;
	
	var projection = this.getProjection();
	position = projection.fromLatLngToDivPixel(this._position);
	div.style.left = (position.x - 185) + 'px';
	div.style.top = (position.y - 145) + 'px';
	
};

Label.prototype.remove = function() {
	
	this.div_.parentNode.removeChild(this.div_);
	
};

Label.prototype.hide = function() {
	
	this.div_.style.display = 'none';
	
};

Label.prototype.show = function() {
	
	this.div_.style.display = 'block';
	this.getMap().setCenter(this._position)
	this.getMap().setZoom(this._zoom)
	
};
