var Banners = null;
//--------------------------------------------------------------------------------------------


function initBanners(sid)
{
	Banners = new Array();
	var arr = $A(document.getElementsByClassName('gameAdvertisement'));
	var w, b, bd, maxHeight;
	for (var i = 0; i < arr.length; i++)
	{
		b = new OddBanner(arr[i], sid);
		Banners.push(b);
	}
}
//--------------------------------------------------------------------------------------------
var OddBanner = Class.create(
{
	initialize: function(element, sid, wrapper)
	{
		this.container = $(element);
		if (this.container != null)
		{
			try
			{
				this.id = this.container.id;
				this.sessionID = sid;
				this.timer = null;
				this.wrapper = wrapper;
				var tmp = document.getElementByClassName('tid', this.container);
				if (!tmp) return;
				this.tippID = tmp.value;
				this.oddsContainer = document.getElementByClassName('odds', this.container);
				if (!this.oddsContainer) return;
				this.oddItems = document.getElementsByClassName('oddItem', this.oddsContainer);
				this.content = document.getElementByClassName('box_content', this.container);
				this.progress = document.getElementByClassName('progress', this.container);
				this.refreshOdds();
			}
			catch (ex)
			{
				signal('Exception in constructor of OddBanner: ' + ex.message);
			}
		}
	},
	refreshOdds: function()
	{
		try
		{
			this.container.addClassName('updating');
			var queryString = 'cmd=update_banner&tid=' + this.tippID + '&sid=' + this.sessionID + '&ajax=1';
			new Ajax.Request('content.aspx', {
				method: 'post',
				parameters: queryString,
				onComplete: this.refreshComplete.bind(this),
				onTimeout: this.refreshFailure.bind(this),
				onFailure: this.refreshFailure.bind(this)
			});
		}
		catch (ex)
		{
			signal('OddBanner.Refresh: ' + ex.message);
		}
	},
	refreshComplete: function(request)
	{
		try
		{
			var response = request.responseText;
			var arr = $A(response.split('|'));
			var tid = parseInt(arr[0]);
			var ht = $H([]);
			var id, value, odd, tmp, oddItem, link, count;
			if (tid != this.tippID)
			{
				signal('TippID was not correct');
				return;
			}
			if (arr.length < 2)
			{
				signal('Not enough data received');
				return;
			}
			//PARSE INPUT ODDS INTO HT
			count = 0;
			for (var i = 1; i < arr.length; i = i + 2)
			{
				id = arr[i];
				value = parseFloat(arr[i + 1]).toFixed(2);
				if (value != null && !isNaN(value))
				{
					//signal(id + ' -- ' + value);
					tmp = new Object();
					tmp[id] = value;
					ht = ht.merge(tmp);
					count++;
				}
			}
			this.container.removeClassName('updating');
			if (count == 0)
			{
				this.oddsContainer.innerHTML = arr[1];
				this.container.addClassName('hidden');
			}
			else
			{
				this.container.removeClassName('hidden');
				//LOOP THROUGH BANNER ODDS
				for (var u = 0; u < this.oddItems.length; u++)
				{
					oddItem = this.oddItems[u];
					link = oddItem.getElementsByTagName('a')[0];
					var ov = document.getElementsByClassName('oddValue', link)[0];
					id = link.id.substring(2);
					value = ht[id];
					//<span class="oddValue">1.65</span><span class="oddName">Polen</span>
					if (value != null) Element.update(ov, value);
					else
					{
						Element.update(ov, 'N/A');
						oddItem.addClassName('hidden');
					}
				}
			}
			OnExecResize();
		}
		catch (ex)
		{
			signal('Banner.refreshComplete: ' + ex.message);
		}
	},
	adjustHeight: function()
	{
		var wh = this.wrapper.getHeight();
		var h = this.container.getHeight();
		//signal(this.container.getAttribute("name") + ' : container=' + h + ', wrapper=' + wh);
		if (h < wh) Element.setStyle(this.content, {
			'height': +(this.content.getHeight() + (wh - h)) + 'px'
		});
		//signal('after: ' + this.container.getAttribute("name") + ' : container=' + h + ', wrapper=' + wh);
	},
	refreshFailure: function(request)
	{
		var response = request.responseText;
		signal('Failure: ' + response);
	}
});

