var rmMarkerIcons = {};

function rmCreateMarker(point, md) {
	var marker = new GMarker(
		point,
		{
			icon: rmMarkerIcons[md['category_id']]['gicon']
		}
	);
	
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml('<b>'+md['name']+'</b><br/><i>'+rmMarkerIcons[md['category_id']]['name']+'</i><br/><br/>');
	});
	
	return marker;
}

function rmCreateStructureMarker(structure) {
	var marker = new GMarker(
		new GLatLng(structure['lat'], structure['lng']),
		{
			icon: rmMarkerIcons[structure['category_id']]['gicon']
		}
	);

	GEvent.addListener(marker, "click", function() {
		new Request.JSON({
			url: structure['url'],
			onSuccess: function(responseJSON, responseText){
					var obj = $H(responseJSON);
					var html = '';
					if ( $defined(obj['Ambient']) ){
						obj['Ambient'].each(function(ambient,i,hash){
							html += '<img src="/works/file/'+ambient['code']+'t.jpg" width="100" height="50" /> ';
						});
					}
			//		marker.openInfoWindowHtml('<b>'+structure['name']+'</b><br/><i>'+rmMarkerIcons[structure['category_id']]['name']+'</i><br/><br/><iframe hspace="0" vspace="0" width="600" height="400" frameborder="0" style="border:none; padding:0; margin:0; overflow: hidden; " src="'+structure['url']+'"> </iframe>');
					marker.openInfoWindowHtml('<b>'+structure['name']+'</b><br/><i>'+rmMarkerIcons[structure['category_id']]['name']+'</i><br/><br/>'+html);
			}
		}).get();
	});
	
	return marker;
}

function rmCreateAmbientMarker(ambient) {
	var iconOptions = {};
	iconOptions.width = 16;
	iconOptions.height = 16;
	iconOptions.primaryColor = "#FF9900";
	iconOptions.label = "";
	iconOptions.labelSize = 9;
	iconOptions.labelColor = "#000000";
	iconOptions.shape = "circle";
	var icon = MapIconMaker.createFlatIcon(iconOptions);

	var marker = new GMarker(
		 new GLatLng(ambient['Ambient']['lat'], ambient['Ambient']['lng']),
		 {
			 icon: icon
		 }
	);
	var title = (ambient['Structure']['id']) ? ambient['Structure']['name']+' - '+ambient['Ambient']['name'] : ambient['Workgroup']['name']+' - '+ambient['Ambient']['name'];
	GEvent.addListener(marker, "click", function() {
		var iframe = new IFrame({
			src: '/player/index.php#type=flash&resolution=sd&id='+ambient['Ambient']['code'],
			frameborder: 0,
			hspace: 0,
			vspace: 0,
			styles: {
				width: 600,
				height: 400,
				border: 'none'
			}
		});
		var div = new Element('div').adopt(iframe);
		var iframe_id = iframe.get('id');
		var html = '';
		html += '<b>'+title+'</b><br/><br/>';
		html += div.get('html');
		html += '<br /><br />'
		html += '<b>Flash:</b> ';		
		html += '<a href="/player/?'+(Date.now()+1)+'#type=flash&resolution=sd&fullscreen=false&id='+ambient['Ambient']['code']+'" target="'+iframe_id+'">SD</a> - ';
		html += '<a href="/player/?'+(Date.now()+2)+'#type=flash&resolution=hd&fullscreen=true&id='+ambient['Ambient']['code']+'" target="'+iframe_id+'">HQ</a> ';
		html += ' | ';		
		html += '<b>Java:</b> ';		
		html += '<a href="/player/?'+(Date.now()+3)+'#type=java&resolution=sd&fullscreen=false&id='+ambient['Ambient']['code']+'" target="'+iframe_id+'">SD</a> - ';
		html += '<a href="/player/?'+(Date.now()+4)+'#type=java&resolution=hd&fullscreen=true&id='+ambient['Ambient']['code']+'" target="'+iframe_id+'">HQ</a> ';
		marker.openInfoWindowHtml(html);
	});
	
	return marker;
}


// rmFadeMessage
function rmFadeMessage(text) {
	this.messageDiv = new Element(
		'div',
		{
			'id': 'rmFadeMessage',
			'styles': {
				'position': 'absolute',
				'background-color':'#000000',
				'color': '#cccccc',
				'text-align':'center',
				'padding': '1em',
				'opacity': '.95'
			},
			'text': text
		}
	);
}

rmFadeMessage.prototype = new GControl();
rmFadeMessage.prototype.initialize = function(map) {
	map.getContainer().appendChild(this.messageDiv);
	this.messageDiv.fade.delay(5000, this.messageDiv, 'out');
	return this.messageDiv;
}
rmFadeMessage.prototype.getDefaultPosition = function() {
	var d = this.messageDiv.getCoordinates();	
	var cc = $(map.getContainer()).getCoordinates();
	var left = (cc.width - d.width) / 2;	
	var top = (cc.height - d.height) / 2;

	
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(left,top));
}

// rmTopBarControl
function rmTopBarControl() {
}
// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
rmTopBarControl.prototype = new GControl();
rmTopBarControl.prototype.initialize = function(map) {
	var container = new Element(
		'div',
		{
			'id': 'rmTopBarControl',
			'styles': {
				'background':'url("http://maps.realtour.it/img/rm_topbar_control_bg.png")',
				'background-repeat':'repeat-x',
				'background-position':'top center',
				'height': '110px',
				'text-align':'center',
				'width': '100%',			
				'margin': '0 auto',
				'padding': 0
			}
		}
	);

	var inner = new Element(
		'div',
		{
			'styles': {
				'background':'url("http://maps.realtour.it/img/rm_topbar_control_center_bg.png")',
				'background-repeat':'no-repeat',
				'background-position':'top center',
				'height': '99px',
				'width': '974px',			
				'margin': '0 auto',
				'text-align':'left',
				'padding': 0
			}
		}
	).inject(container);
	
	
	map.getContainer().appendChild(container);
	return container;
}

rmTopBarControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}

// rmBottomBarControl
function rmBottomBarControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
rmBottomBarControl.prototype = new GControl();
rmBottomBarControl.prototype.initialize = function(map) {
	var container = new Element(
		'div',
		{
			'id': 'rmBottomBarControl',
			'styles': {
				'background':'url("http://maps.realtour.it/img/rm_bottombar_control_bg.png")',
				'background-repeat':'repeat-x',
				'background-position':'top center',
				'height': '33px',
				'text-align':'center',
				'width': '100%',			
				'margin': '0 auto',
				'padding': 0,
				'line-height' : '33px'
			}
		}
	);

	var inner = new Element(
		'div',
		{	
			'text' : "REALTOUR - VIA DEL TREBBO, 2 - 40128 - BOLOGNA (BO) - EMAIL: INFO@REALTOUR.IT - TEL: 051.6257210 - CELL: 392.2524478 - P.IVA: 02158081204 - SKYPE: REALTOURESTATE",
			'styles': {
				'font-family': 'Arial, Helvetica, sans-serif',
				'font-size': '9px',
				'font-weight': 'normal',
				'color': '#656565'
			}
		}
	).inject(container);
	
	map.getContainer().appendChild(container);
	return container;
}

rmBottomBarControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(0, 0));
}
