YHA.Page.Hostels.MapPopup = Base.extend
({
	constructor: function(lat, lng, description, addMarker)
	{
		this.lat = lat;
		this.lng = lng;
		this.chromeHeight = 136;
		this.description = description;
		
		this.addMarker = addMarker;
		
		Event.observe(window, 'load', this.windowOnLoad.bind(this));
		Event.observe(window, 'resize', this.windowOnResize.bind(this));
	},
	
	windowOnLoad: function()
	{
		this.hostelMap = new YHA.Map
		(
			element = $('map'), 
			lat = this.lat, 
			lng = this.lng
		);
		
		
		if (window.opener && window.opener.page.hostelMap)
		{
			this.hostelMap.mirror(window.opener.page.hostelMap.map);
		}
		
	
			this.resizeMap();
	
		this.observers = 
		{
			btCenterMapOnClick: this.btCenterMapOnClick.bindAsEventListener(this),
			hostelMarkerOnClick: this.hostelMarkerOnClick.bindAsEventListener(this),
			btCloseOnClick: this.btCloseOnClick.bindAsEventListener(this),
			btPrintMapOnClick: this.btPrintMapOnClick.bindAsEventListener(this)
		}
	
		if (this.addMarker)
		{
			this.hostelMarker = this.hostelMap.addCenterMarker();
			this.openInfoWindow();
			
			GEvent.addListener
			(
				this.hostelMarker, 
				"click", 
				this.observers.hostelMarkerOnClick
			);
		}
		
		
		if (this.observers.btCenterMapOnClick)
			Event.observe($('btCenterMap'), 'click', this.observers.btCenterMapOnClick);
		
		Event.observe($('btPrintMap'), 'click', this.observers.btPrintMapOnClick);
		Event.observe($('btClose'), 'click', this.observers.btCloseOnClick);
	
		new RedSquare.PopupBuilder('popup', 'map', 'width=1024,height=768,left=c,top=c,resizable');
		new RedSquare.PopupBuilder('popupprint', 'print', 'width=800,height=600,left=c,top=c,resizable,scrollbars,menubar');
	},
	
	windowOnResize: function(event)
	{
		this.resizeMap();		
	},
	
	hostelMarkerOnClick: function()
	{
		this.openInfoWindow();
	},
	
	btCenterMapOnClick: function(event)
	{
		this.hostelMap.panCenter();	
	
		Event.stop(event);
		Event.element(event).blur();	
	},
	
	btCloseOnClick: function()
	{
		window.close();	
	},
	
	btPrintMapOnClick: function(event)
	{
		window.print();	
	},

	openInfoWindow: function()
	{
		this.hostelMarker.openInfoWindow(this.description);
	},
	
	resizeMap: function()
	{
		if (RedSquare.Window.getHeight() != 0)
		{	
		
			$('frame').style.height = ( RedSquare.Window.getHeight() - this.chromeHeight  ) + 'px';	
			$('map').style.height = ( RedSquare.Window.getHeight() - this.chromeHeight  - 4 ) + 'px';
		
		this.hostelMap.map.checkResize();
		
		}
	}
});
