$(function() {
	if (GBrowserIsCompatible()) {
		var f = _firmData;
	
	   	var map = new GMap2(document.getElementById("map"));
	   	map.addControl(new GLargeMapControl());
	   	map.addControl(new GMapTypeControl());
	   	map.setCenter(new GLatLng(f.gpsy, f.gpsx), f.zoom);
	   	
	   	var imap = iMap(map);
	   	imap.addMarker(f, 1);
	   	if(f.cat){
	   		imap.request({category: f.cat});
	   	}
	}
	
   	$(document).unload(GUnload);
});

var iMap = function(map){
	
	var icon = new GIcon();
	icon.image = "/pics/maps/small-poi-free.png";
	icon.shadow = "/pics/maps/firm-poi-shadow.png";
	icon.iconSize = new GSize(37, 34);
	icon.shadowSize = new GSize(37, 34);
	icon.iconAnchor = new GPoint(10, 34);
	icon.infoWindowAnchor = new GPoint(10, 34);
	
	var 
	orderOfCreation = function(){
		return 1;
	},
	createMarker = function(point,html,profile) {
		//premium marker
		var premium = false;
		if(!_firmData.cat){
			premium = true;
		}
		var marker = profile&&!premium ? new GMarker(point, {icon: icon, zIndexProcess:orderOfCreation})
			: new GMarker(point);
		GEvent.addListener(marker, "mouseover", function() {
			marker.openInfoWindowHtml(html);
		});
	  return marker;
	},
	
	getMarkerHtml = function(firm){
		var html = [];
		html.push('<b>'+ firm.title +'</b>');
		html.push('<br/>'+ firm.address);
		html.push('<br/>'+ firm.postal +' '+ firm.city);
		
		if(firm.phones){
			html.push('<br/>'+ firm.label.phones +' '+ firm.phones);
		}	     			
		if(firm.mobiles){
		  	html.push('<br/>'+ firm.label.mobiles +' '+ firm.mobiles);
		}
	  return html.join("");
	};

	return {
	map  : map,
	markers : [],
	request : function(params){
		var bounds = this.map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var center = this.map.getCenter();

		var o = {
			centerx: center.lng(),
			centery: center.lat(),
			maxy: northEast.lat(),
			miny: southWest.lat(),
			maxx: northEast.lng(),
			minx: southWest.lng(),
			zoom: this.map.getZoom()
		};
		this.load($.extend(o,params));
		
	},
	load : function(options){
		var	url = "/mapa/firmy.html?";
		for (k in options) {
			if (options.hasOwnProperty(k)) {
				url += k + "=" + options[k] + "&";
			}
		}
		for (k in this.params) {
			if( !this.isEmpty(this.params[k]) )
				url += k + "=" + this.params[k] + "&";			
		}
		var me=this;
		$.ajax({
			url: url,
			cache: false,
			dataType: "json",
			success: function(data) {				
				me.spreadMarkers(data);			
			}
		});
	},
	spreadMarkers : function(data){
		for(k in data){
			if(this.profile == data[k].n) continue;
			this.addMarker({
				gpsx: data[k].x,
				gpsy: data[k].y,
				title: data[k].n,
				address: data[k].a,
				city: data[k].c,
				postal: data[k].k
			});
		}
	},
	addMarker : function(firm, profile){
		var point = new GLatLng(firm.gpsy,firm.gpsx);
		var marker = !profile ? createMarker(point, getMarkerHtml(firm))
			: createMarker(point, getMarkerHtml(firm), 1);
	    this.map.addOverlay(marker);
	    if(profile){
	    	this.profile = firm;
	    	marker.openInfoWindowHtml(getMarkerHtml(firm));
	    }
	},
	isEmpty: function(v,allowBlank){
		return v===null||v===undefined||(!allowBlank?v==="":false)
	}
	} 
};