RegisterNamespace("Arland");

Arland.OddBrowser = Class.create(Arland.Control, {
	initialize: function($super, id)
	{
		$super(id);

		this.initObservers();

		this.initClickHandlers();
	}
	,
	initObservers: function()
	{
		this.boundOnClearOdd = this.onClearOdd.bind(this);
		this.boundOnClearAllOdds = this.onClearAllOdds.bind(this);
		this.boundOnAddOdd = this.onAddOdd.bind(this);

		top.MainNotificationCenter.observe("ClearOdd", this.boundOnClearOdd);
		top.MainNotificationCenter.observe("ClearAllOdds", this.boundOnClearAllOdds);
		top.MainNotificationCenter.observe("AddOdd", this.boundOnAddOdd);

		Event.observe(window, "unload", (function() {
			top.MainNotificationCenter.unobserve("ClearOdd", this.boundOnClearOdd);
			top.MainNotificationCenter.unobserve("ClearAllOdds", this.boundOnClearAllOdds);
			top.MainNotificationCenter.unobserve("AddOdd", this.boundOnAddOdd);
		}).bind(this));
	}
	,
	initClickHandlers: function()
	{
		var quickTippsEl = $(this.id + "_quicktipps");
		this.quickTipps = quickTippsEl ? quickTippsEl.value : null;
		var specialCountLinks = this.container.select(".specialCountLink");
		var oddb = this;

		specialCountLinks.each(function(specialCountLink) {
			Event.observe(specialCountLink, "click", function(e) {
				Event.stop(e);
				oddb.toggleSpecialTippList(parseInt(specialCountLink.id.replace(oddb.id + "_specialCountLink_", "")));
			}.bindAsEventListener());
		});

		var defaultOdds = this.container.select(".odd");
		defaultOdds.each(function(defaultOdd) {
			Event.observe(defaultOdd, "click", function(e) {
				var gameId = parseInt(defaultOdd.parentNode.id.replace(oddb.id + "_game_", ""));
				var oddId = parseInt(defaultOdd.id.replace(oddb.id + "_odd_", ""));
				oddb.addOdd(oddId, gameId);
				Event.stop(e);
			}.bindAsEventListener());
		});
	}
	,
	addOdd: function(oddId, gameId)
	{
		var odd = $(this.id + "_odd_" + oddId);
		var add = true;
		if(odd)
		{
			if(odd.hasClassName("selected_odd"))
			{
				odd.removeClassName("selected_odd");
				odd.addClassName("odd");
				add = false;
			}
			else
			{
				odd.removeClassName("odd");
				odd.addClassName("selected_odd");
			}
		}
		if(add)
			top.MainNotificationCenter.notify("AddOdd", {oddId: oddId});
		else
			top.MainNotificationCenter.notify("ClearOdd", {oddId: oddId});
		
		var url = "betslip.aspx?cmd=" + (add ? "add" : "del") + "_odd&odd=" + oddId + "&game=" + gameId + "&sid=" + sessionID;
		top.betslip_frame.location = url;
	}
	,
	onAddOdd: function(context)
	{
		Console.Log("onAddOdd context.oddId = " + context.oddId);

		var odd = $(this.id + "_odd_" + context.oddId);
		if(odd)
		{
			odd.addClassName("selected_odd");
			odd.removeClassName("odd");
		}
	}
	,
	onClearOdd: function(context)
	{
		var odd = $(this.id + "_odd_" + context.oddId);
		if(odd)
		{
			odd.addClassName("odd");
			odd.removeClassName("selected_odd");
		}
	}
	,
	onClearAllOdds: function(context)
	{
		var defaultOdds = this.container.select(".selected_odd");
		defaultOdds.each(function(defaultOdd) {
			defaultOdd.addClassName("odd");
			defaultOdd.removeClassName("selected_odd");
		});
	}
	,
	toggleSpecialTippList: function(gameId)
	{
		var game = $(this.id + "_game_" + gameId);
		var specialTippTypes = $(this.id + "_specialtipptypes_" + gameId);

		var specialTippTypesContainer = specialTippTypes.down(".specialtipps_container");

		if(game.hasClassName("details"))
		{
			game.removeClassName("details");
			specialTippTypes.hide();
			OnExecResize();
		}
		else
		{
			game.addClassName("details");
			if(specialTippTypes.hasClassName("empty"))
			{
				var params= 'sid=' + sessionID + '&cmd=load_specials' + '&game=' + gameId + '&showAll=0&id=' + this.id;
				if(this.quickTipps)
					params += '&qt=' + this.quickTipps;

				var req = new Arland.Updater({success: specialTippTypesContainer}, 'content.aspx', {
						method: 'get', 
						parameters: params, 
						onFailure: this.onAjaxFailure.bind(this), 
						onComplete: function() { OnExecResize(); }
				});

				specialTippTypes.removeClassName("empty");
				OnExecResize();
			}
			else
			{
				specialTippTypes.addClassName("details");
				specialTippTypes.show();
				OnExecResize();
			}
		}
	}
});


Arland.NextGamesBrowser = Class.create(Arland.OddBrowser, {
	initialize: function($super, id)
	{
		$super(id);
		filterForm = this.container.down(".filterBox form");

		if(filterForm)
		{
			ngb = this;
			Event.observe(filterForm, "submit", (function(e) {
				Event.stop(e);
				ngb.refreshNextGames();
			}).bindAsEventListener());
		}
	},
	refreshNextGames: function()
	{
		var nextGamesContent = this.container.down('.box_content');
		var nextGamesTable = $(this.id + "_table");

		var ol = new Arland.OverlaySpinner("spinnerOverlay", nextGamesContent);
		ol.show(Translator.Instance.GetItem(58));

		var params = Form.serialize(document.forms["ng_timespan"]);
		new Arland.Updater(nextGamesTable, "content.aspx", {
			parameters: params,
			onComplete: (function() { 
				this.initClickHandlers();
				ol.hide();
				OnExecResize();
			}).bind(this)
		});
		return false;
	}
})
