EventSelector = Class.create ({
	initialize: function (featured, data) {
		this.featured = featured;
		this.data = data;
		this.index = 0;
		Event.observe (window, 'load', this.onLoad.bindAsEventListener (this));
		Event.observe ($(document.body), 'mouseover', this.onBodyMouseOver.bindAsEventListener (this));
		
	},

	onLoad: function () {
		this.arrow = $('event-selector-arrow');
		this.splashScreen = $('event-selector').select ('.splash')[0];
		this.list = [];
		this.data.each (function (item) {
			var i = new EventItem(this, item, this.index);
			this.list.push (i);
			if (this.featured == item.Code) {
				this.select (i);
			}
			this.index++;
		}, this);
	},
	
	select: function (item) {
		if (this.selected) {
			this.selected.obj.style.display = 'none';
			this.arrow.style.display = 'none'
		}
		if (item) {
			item.obj.style.display = 'block';
			this.arrow.style.top = (item.index*54+55)+'px';
			this.arrow.style.display = 'block';
			this.buttonHover (item);
		}
		this.selected = item;
	},
	
	buttonHover: function (item) {
		if (this.hoverred) {
			this.hoverred.button.removeClassName ('event-button-'+this.hoverred.data.Code+'-selected');
		}
		if (item) {
			item.button.addClassName ('event-button-'+item.data.Code+'-selected');
			this.splash (item);
		} else {
			if (this.selected) {
				this.moveArrow (this.selected);
				this.reset ();
				this.selected.obj.style.display = 'block';
				this.selected.button.addClassName ('event-button-'+this.selected.data.Code+'-selected');
			}
		}
		this.hoverred = item;
	},
	
	onBodyMouseOver: function (event) {
		var element = Event.element (event);
		if (! element.hasClassName ('event-button')) {
			this.buttonHover (null);
		}
	},
	
	splash: function (item) {
		this.reset ();
		item.obj.style.display = 'block';
		this.moveArrow (item);
		this.splashScreen.style.display = 'block';
		jQuery (jQuery (this.splashScreen)).fadeOut (1000);
	},
	
	moveArrow: function (toItem) {
		if (toItem) {
			var top_ = (toItem.index*54+55);
			jQuery (jQuery(this.arrow)).animate ({top: top_+'px'}, 100);
		} 
	},
	
	reset: function () {
		$('event-selector').select ('.event-bg').each (function (e) {
			e.style.display = 'none';
		}, this);
	},
	
	makeSelected: function (newItem) {
		if (this.selected) {
			//alert (this.selected.data.Code);
			this.selected.button.removeClassName ('event-button-'+this.selected.data.Code+'-selected');
		}
		this.select (newItem);
	}
});

EventItem = Class.create ({
	initialize: function (parent, item, index) {
		this.data = item;
		this.index = index;
		this.parent = parent; this.item = item;
		this.obj = $$('.event-bg-'+item.Code)[0];
		this.button = $('event-selector-buttons').select ('.event-button-'+item.Code)[0];
		this.button.observe ('mouseover', this.onButtonHover.bindAsEventListener(this));
		this.button.observe ('click', this.onButtonClick.bindAsEventListener(this));
		this.obj.observe ('click', this.onClick.bindAsEventListener(this));

	},
	
	onClick: function () {
		if (this.data.link)
			document.location = this.data.link;
	},
	
	onButtonHover: function (event) {
		this.parent.buttonHover (this);
	},
	
	onButtonClick: function (event) {
		this.parent.makeSelected (this);
		if (this.data.link)
			document.location = this.data.link;
	}
});
		Event.observe (window, 'load', function (e) {
			var sub = $(document.body).select ('.sub-menu')[0];
			sub.select ('li').each (function (li) {
				li.observe ('click', function (ev) {
					var element = Event.element (ev);
					if (element.tagName == 'LI') {
						var a = element.select('a')[0];
						document.location = a.href;
					}
				});
			});
		});
