﻿// (c) Copyright Visit Technology Group

Type.registerNamespace("CitybreakOnline.Event");

CitybreakOnline.Event.WhatsOn = function(element)
{
    CitybreakOnline.Event.WhatsOn.initializeBase(this, [element]);

	this._servicePath = null;
	this._organizationId = null;
	this._languageId = null;
	this._dates = null;
	this._excludeCategoryIds = null;
	this._title = null;

	this._eventGroups = null;
	this._page = -1;
	this._interval = 5000;

	this._timer = null;

	this._body = null;
	this._pagers = null;
}

CitybreakOnline.Event.WhatsOn.prototype =
{
	initialize: function()
	{
	    CitybreakOnline.Event.WhatsOn.callBaseMethod(this, 'initialize');

		this.start();
	},
	dispose: function()
	{
	    CitybreakOnline.Event.WhatsOn.callBaseMethod(this, 'dispose');

		if (this._pagers != null)
			for (var i = 0; i < this._pagers.length; i++)
				$clearHandlers(this._pagers[i]);
	},

	get_servicePath: function()
	{
		return this._servicePath;
	},
	set_servicePath: function(value)
	{
		this._servicePath = value;
	},

	get_organizationId: function()
	{
		return this._organizationId;
	},
	set_organizationId: function(value)
	{
		this._organizationId = value;
	},

	get_languageId: function()
	{
		return this._languageId;
	},
	set_languageId: function(value)
	{
		this._languageId = value;
	},

	get_dates: function()
	{
		return this._dates;
	},
	set_dates: function(value)
	{
		if (value != null)
		{
			var dt = [];
			for (var i = 0; i < value.length; i++)
				dt[i] = new Date(value[i]);
			this._dates = dt;
		}
		else
			this._dates = null;
	},

	get_excludeCategoryIds: function()
	{
		return this._excludeCategoryIds;
	},
	set_excludeCategoryIds: function(value)
	{
		this._excludeCategoryIds = value;
	},

	get_title: function()
	{
		return this._title;
	},
	set_title: function(value)
	{
		this._title = value;
	},

	start: function()
	{
		if (this.get_dates() == null || this.get_organizationId() == null || this.get_languageId() == null)
			return;

		Sys.Net.WebServiceProxy.invoke(
			this.get_servicePath(),
			'Search',
			false,
			{
				'dates': this.get_dates(),
				'organizationid': this.get_organizationId(),
				'languageid': this.get_languageId(),
				'excludecategoryids': this.get_excludeCategoryIds()
			},
			Function.createDelegate(this, this.Search_Succeeded),
			Function.createDelegate(this, this.Search_Failed));
	},

	stop: function()
	{
		if (this._timer != null)
			window.clearInterval(this._timer);
	},

	Search_Succeeded: function(e)
	{
		if (e == null || e.length == 0)
			return;

		this._eventGroups = e;

		this._setupLayout();

		this._timer_tick();
	},

	Search_Failed: function(e)
	{
		this.get_element().innerHTML = 'What\'s On failed (' + e.get_statusCode() + '): ' + e.get_message() + '<br />' + e.get_stackTrace();
	},

	_setupLayout: function()
	{
		var t = document.createElement('table');
		t.cellPadding = 0;
		t.cellSpacing = 0;
		t.className = 'cntCrnBoxGrey tableFull';
		t.style.marginTop = '30px';

		var tb = document.createElement('tbody');
		t.appendChild(tb);

		var tr = document.createElement('tr');
		tb.appendChild(tr);

		var td = document.createElement('td');
		td.className = 'bgTopL';
		tr.appendChild(td);

		td = document.createElement('td');
		td.className = 'Content';
		tr.appendChild(td);

		var div = document.createElement('div');
		div.className = 'cntBasketWhatson';
		td.appendChild(div);

		var h2 = document.createElement('h2');
		h2.appendChild(document.createTextNode(this.get_title()));
		div.appendChild(h2);

		div.appendChild(this._createPager());

		this._body = document.createElement('div');
		div.appendChild(this._body);

		td = document.createElement('td');
		td.className = 'bgTopR';
		tr.appendChild(td);

		tr = document.createElement('tr');
		tb.appendChild(tr);

		td = document.createElement('td');
		td.className = 'bgBotL';
		tr.appendChild(td);

		td = document.createElement('td');
		td.className = 'bgBot';
		td.innerHTML = '&nbsp;';
		tr.appendChild(td);

		td = document.createElement('td');
		td.className = 'bgBotR';
		tr.appendChild(td);

		tb.appendChild(tr);

		this.get_element().appendChild(t);
	},

	_createPager: function()
	{
		var div = document.createElement('div');
		div.className = 'Pager';

		this._pagers = [];

		var pages = Math.ceil(this._eventGroups.length / 4);

		if (pages > 1)
		{
			for (var i = pages - 1; i >= 0; i--)
			{
				var a = document.createElement('a');
				a.href = '#';
				a.appendChild(document.createTextNode(i + 1));
				div.appendChild(a);

				$addHandler(a, 'click', Function.createDelegate(this, Function.createCallback(this._pagers_click, a)));

				this._pagers[i] = a;
			}
		}

		return div;
	},

	_timer_tick: function()
	{
		this._page++;
		if (this._page * 4 >= this._eventGroups.length)
			this._page = 0;

		this._refreshPager();

		this._layout();

		if (this._timer != null)
			window.clearInterval(this._timer);
		this._timer = window.setInterval(Function.createDelegate(this, this._timer_tick), this._interval);
	},

	_layout: function()
	{
		var idx = this._page * 4;

		var len = this._eventGroups.length;

		var eg1 = idx < len ? this._eventGroups[idx++] : null;
		var eg2 = idx < len ? this._eventGroups[idx++] : null;
		var eg3 = idx < len ? this._eventGroups[idx++] : null;
		var eg4 = idx < len ? this._eventGroups[idx++] : null;

		var html = '<table cellpadding="0" cellspacing="0">';
		html += '<tr valign="top">';
		if (eg1 != null)
			html += this._createEvent(eg1);
		if (eg2 != null)
		{
			html += '<td class="Sep"><div></div></td>';
			html += this._createEvent(eg2);
		}
		html += '</tr>';
		if (eg3 != null)
		{
			html += '<tr valign="top">';
			html += this._createEvent(eg3);
			if (eg4 != null)
			{
				html += '<td class="Sep"><div></div></td>';
				html += this._createEvent(eg4);
			}
			html += '</tr>';
		}
		html += '</table>';

		this._body.innerHTML = html;
	},

	_createEvent: function(eg)
	{
		var intro = eg.Introduction;
		if (intro.length > 80)
			intro = intro.substring(0, 80) + '...';

		return '<td class="Push"><img src="' + eg.ImageUrl + '" /><a href="' + eg.Url + '" target="_blank" class="link"><span class="Date">' + this._getDateString(eg.Dates) + '</span><br/><h3>' + eg.Name + '</h3><br /><span class="Text">' + intro + '</span></a></td>';
	},

	_getDateString: function(dates)
	{
		var groups = [];

		var group = [];
		for (var i = 0; i < dates.length; i++)
		{
			if (group.length == 0 || dates[i] - group[group.length - 1] <= 86400000)
			{
				group.push(dates[i]);
			}
			else
			{
				groups.push(group);
				group = [];
			}
		}
		if (group.length != 0)
			groups.push(group);

		var ret = '';
		for (var i = 0; i < groups.length; i++)
		{
			if (ret != '')
				ret += ', ';

			if (groups[i].length == 1)
			{
				ret += groups[i][0].localeFormat('dd MMM');
			}
			else
			{
				var f = groups[i][0];
				var l = groups[i][groups[i].length - 1];

				if (f.getMonth() != l.getMonth())
					ret += f.localeFormat('dd MMM') + '-' + l.localeFormat('dd MMM');
				else
					ret += f.localeFormat('dd') + '-' + l.localeFormat('dd MMM');
			}
		}

		return ret;
	},

	_pagers_click: function(e, sender)
	{
		e.preventDefault();

		this.stop();

		for (var i = 0; i < this._pagers.length; i++)
		{
			if (this._pagers[i] == sender)
			{
				this._page = i;

				this._refreshPager();

				this._layout();
			}
		}
	},

	_refreshPager: function()
	{
		for (var i = 0; i < this._pagers.length; i++)
			this._pagers[i].className = i == this._page ? 'bgLight Act' : 'btnMarker';
	}
}

CitybreakOnline.Event.WhatsOn.registerClass('CitybreakOnline.Event.WhatsOn', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

