$(document).observe('dom:loaded', function() {
	// activate mouseover on tv_channels
	if (this.body.id == 'events_page') {
		WS.Events.activateTVs();
		WS.Events.default_quick_jump();
	}
});

Object.extend(WS.Events, {
	activateTVs: function() {
		$$('a.tv_channels').invoke('setModal', {
			position: 'mouse',
			offsetTop: 20,
			zIndex: 10000,
			containerClassName: 'tv_channels'
		});
	},

	split_code: function(code) {
		return code.split('-');
	},

	_qje: null, // quick jump elements cache
	quick_jump: function(element) {
		element = $(element);
		if (!element) return;

		var value = element.getValue();
		if (element.id == 'qjs') { // sports select
			WS.Events._qje = (WS.Events._qje || $(element.form).select('select')).invoke('hide'); // hide all league selects first
			element.show();
			// show league select for selected sport
			$('qj_' + value).show();
		} else if (value) { // leagues select
			// redirect to given league
			WS.redirectTo('events/league/' + value);
		}
	},

	default_quick_jump: function() {
		var sport = ($$('div.sport') || []).first();
		if (sport) {
			var element = ($$('#qjs option[value=' + sport.id + ']') || []).first();
			if (element) {
				element.selected = true;
				WS.Events.quick_jump(element.parentNode);
			}
		}
	}
});

// WS.Events.Options = {
// 	toggle: function(element) {
// 		var parts = element.id.split('_');
// 		$$('div.league.' + parts[1] + ' table tr.option' + (parts[2] != 'all' ? '.e' + parts[2] : '')).invoke('toggle');
// 	}
// };

// behaviours
Event.addBehavior({
	'td.checkbox a:click': function(e) {
	  Basket.modifyBet(this);
		return false;
	},

	'a.tv_channels:click': function(e) {
		// dont respond to onclicks...
		return false;
	},

	// go back
	'a.back:click': function() {
		window.history.go(-1);
		return false;
	},
	// reload
	'a.reload:click': function() {
		window.location.reload();
		return false;
	},

	'#quick_jump select:change': function() {
		WS.Events.quick_jump(this);
	},

	'.checkbox a': function() {
		this.style.visibility = 'visible';
	}
});

Ajax.Responders.register({
	onComplete : function() { 
		WS.Events.activateTVs();
	}
});
