var GMapOverlay = function (id) {
	this.overlay = new Overlay(id, {	width: "300px",
										draggable: true,
										visible: false,
										zindex: 500,
										fixedcenter: true,
										constraintoviewport: true
	});

	this.overlay.scriptUrl = 'http://' + conf.dynamicUrl + '/gmap/getProfiles';
	
	this.overlay.onClose(function () {
		try { GMap.showMask(false); } catch (e) {}
	});
	
	this.show = function (anchorShow, text, anchorHide, fn) {
		if(text.location_id) text = this.formatResult(text); 
		this.overlay.show(null, this.overlay.generateOverlay(text), null, fn);
	};
	
	this.loadProfiles = function (city) {
		var params = { location_id: city.id, name: city.name };
		this.overlay.getContentByAjax(Utilities.createActionString(params), null, null, this);
	};
	
	this.formatResult = function (result) {
		var html = '<h3>A ' + result.name + ' puoi trovare...</h3>';
		html += this.createProfiles(result.profiles, result.location_id);
		return html;
	};
	
	this.createProfiles = function (profiles, locationId) {
		var html = '';
		
		if(profiles.length == 0) html += '<i>Nessun profilo</i>';
		else
			for(var index in profiles) {
				if(html) html += '<div class="n_line"></div>';
				html += this.createProfile(profiles[index]);
			}
		
		html += '<p/><center><a href="http://www.theclub.pvt/search/gmap/' + locationId + '/1" title="Guarda altri profili">Guarda altri profili</a></center>';
		return html;
	};
	
	this.createProfile = function (user) {
		return '<div class="microProfile">' +
			'<div id="g' + user.profile_id + '" class="thumb stats_gmap">' +
				'<a href="' + Utilities.getUserUrl(user.nick) + '" class="' + ((user.gender == "M") ? "male" : "female") + '"><span></span>' +
					'<img src="' + user.avatar + '" alt="" />' +
				'</a>' +
			'</div>' +
			'<span class="nickname">' + RtfUtilities.createNick(user.nick) + '</span><br/>' +
			user.age + ' anni' +
		'</div>';
	};
};

OverlayConfiguration.gmap = new GMapOverlay('textOverlay');
OverlayManager.gmap = function (args) {
	OverlayConfiguration.gmap.loadProfiles(args[0]);
};