var TooltipWidget = new Class(
{
	Implements : Options,
	
	options : {
		fxOptions : { 'duration' : 600, 'link' : 'cancel' },
		markers : '.marker',
		tooltips : '.tooltipwrapper',
		areas : '.masterplanarea',
		tooltipSuffix : '-tooltip',
		areaSuffix : '-area'
	},
	
	initialize : function(options)
	{
		this.setOptions(options);
		this.initTooltips();
		this.initAreas();
		this.initMarkers();
	},
	
	initTooltips : function()
	{
		var that = this;
		this.tooltips = $$(this.options.tooltips);
		this.tooltips.set('tween',this.options.fxOptions).fade('hide');
		this.tooltips.closeButtons = this.tooltips.getElement('.tooltipclose');
		this.tooltips.closeButtons.addEvent('click',function()
		{
			var tooltip = this.getParent(that.options.tooltips),
				area = $(tooltip.get('id').replace(that.options.tooltipSuffix,that.options.areaSuffix));
				
			tooltip.fade('out');
			area.fade('out');
		});
	},
	
	initAreas : function()
	{
		this.areas = $$(this.options.areas);
		this.areas.set('tween',this.options.fxOptions).fade('hide');
		
		
	},
	
	initMarkers : function()
	{
		var that = this;
		
		this.markerClones = [];
		this.markers = $$(this.options.markers);
		this.markers.each(function(marker)
		{
			var tooltip = this.tooltips.filter(function(tip)
			{
				return tip.get('id') == marker.get('id') + this.options.tooltipSuffix;
			},this).shift();
			
			var area = this.areas.filter(function(area)
			{
				return area.get('id') == marker.get('id') + this.options.areaSuffix;
			},this).shift();
			
			var clone = marker.clone()
				.inject(marker,'after')
				.setStyles({'z-index': tooltip.getStyle('z-index').toInt()})
				.position({'relativeTo' : marker}).store('owner',marker);
			
			marker.store('clone',clone);
			marker.store('tooltip',tooltip)
			marker.store('area',area);
			this.markerClones.push(clone);
		},this)
		
		this.markerClones = $$(this.markerClones);
		
		this.markers.retrieve('clone').addEvents(
		{
			'mouseenter' : function()
			{
				that.tooltips.set('tween',that.options.fxOptions).fade('hide');
				that.areas.set('tween',that.options.fxOptions).fade('hide');
				this.retrieve('owner').retrieve('tooltip').set('tween',that.options.fxOptions).fade('in');
				this.retrieve('owner').retrieve('area').set('tween',that.options.fxOptions).fade('in');
			}
		});
	}
});

window.addEvent('domready',function()
{
	var toolTipWidget = new TooltipWidget();
});
