//*************************************************
//*		liveManager object
//*************************************************

function liveManager(channel, day, type)
{
	this.channel = channel;
	this.day = day;
	this.type = type;
	this.upDate = undefined;
}

liveManager.prototype =
{
	upDate:undefined,
	delay:30000,
	rssUrl:'http://circuitfrancobelge.lunarpark.be/live.rss?application=web',
	channel:undefined,
	day:undefined,
	type:1,
	upDates:{},
	request:null,
	count:0,
	channelUpdates:0,
	document:null,
	timer:null
}

liveManager.prototype.checkUpdate = function()
{
	if(!this.request || this.request.customs['requestType'] == liveRequestType.waiting)
	{
		this.SetIsSynchronizing(true);
		this.request = new Ajax('live', true, true);
		this.request.customs['liveManager'] = this;
		this.request.customs['requestType'] = liveRequestType.upDate;
		this.request.Get(this.rssUrl + '&upDate=true&channel=' + this.channel + '&day=' + this.day);
	}
}

liveManager.prototype.GetChannelDays = function()
{
	this.SetIsSynchronizing(true);
	this.request = new Ajax('live', true, true);
	this.request.customs['liveManager'] = this;
	this.request.customs['requestType'] = liveRequestType.getDays;
	this.request.Get(this.rssUrl + '&channelDays=true&channel=' + this.channel);
}

liveManager.prototype.compareUpdate = function(date, count)
{
	if(!this.upDate || date > this.upDate || count != this.count)
	{
		this.upDate = date;
		this.count = count;
		this.getRSSFeed();
		return true;
	}
	this.request.customs['requestType'] = liveRequestType.waiting;
	return false;
}

liveManager.prototype.getRSSFeed = function()
{
	this.SetIsSynchronizing(true);
	this.request = new Ajax('live', true, true);
	this.request.customs['liveManager'] = this;
	this.request.customs['requestType'] = liveRequestType.RSSFeed;
	this.request.Get(this.rssUrl + '&channel=' + this.channel + '&day=' + this.day);
}

liveManager.prototype.start = function()
{
	this.checkUpdate();
}

liveManager.prototype.startInterval = function()
{
	if(!this.timer)
	{
		var MySelf = this;
		this.timer = setInterval(function() { MySelf.checkUpdate(); }, this.delay);
	}
}

liveManager.prototype.stopInterval = function()
{
	if(this.timer)
	{
		clearInterval(this.timer);
		this.timer = null;
	}
}

liveManager.prototype.stop = function()
{
	this.request.Abort();
	this.request.customs['requestType'] = liveRequestType.waiting;
	this.upDate = undefined;
	this.channelUpdates = 0;
	this.stopInterval();
}

//*************************************************
//*		RSS functions
//*************************************************

liveManager.prototype.UpdateChannel = function(channel)
{
	try
	{
		document.getElementById('errorBlock').style.display = 'none';
		document.getElementById('synchroMessage').innerHTML = 'Loading';
		document.getElementById('synchroImage').src = 'images/import/move.gif';
		document.getElementById('synchroBlock').style.display = 'none';
		if(channel.SelectSingleNode('title'))
		{ document.getElementById('currentLiveTitle').innerHTML = channel.SelectSingleNode('title').NodeValue(); }
		else { document.getElementById('currentLiveTitle').innerHTML = 'FrancoBelge' }
		document.getElementById('liveUpdate').innerHTML = channel.SelectSingleNode('pubDate').NodeValue();
		document.getElementById('RSSLink').href = 'http://circuitfrancobelge.lunarpark.be/live.rss?channel=' + this.channel + '&day=' + this.day;
		with(document.getElementById('liveBlock'))
		{
			style.display = 'block';
			var items = channel.SelectNodes('item');
			if(items.length > 0)
			{
				var s = '<table cellspacing="0" cellpadding="0" border="0">';
				s += '<tr style="text-decoration:underline;"><td></td><td style="width:50px;">';
				s += (this.type == 1) ? 'heure' : 'temps';
				s += '</td><td>';
				s += (this.type == 1) ? 'commentaire' : 'détails';
				s += '</td></tr>';
				for(var i = 0; i < items.length; i++)
				{
					var guid = items[i].SelectSingleNode('guid').NodeValue().toInteger();
					var upDate = new Date(items[i].SelectSingleNode('upDate').NodeValue());
					
					s += '<tr ' + ((i%2 == 0) ? '' : 'class="bk"') + '>';
					
					if(this.channelUpdates != 0 && (!this.upDates[guid] || this.upDates[guid] < upDate))
					{
						s+= '<td class="il"><img src="images/exclam.gif" alt="Mise à jour : ' + upDate + '" title="Mise à jour : ' + upDate + '"/></td>';
					}
					else { s+= '<td></td>'; }
										
					s += '<td class="bl"';
					if(this.type == 2) { s += ' style="width:80px;"'; }
					s += '>';
					if(items[i].SelectSingleNode('title'))
					{ s += items[i].SelectSingleNode('title').NodeValue(); }
					else { s += '...'; }
					s += '</td>';
					if(supervisor.browser == EBrowser.Opera)
					{ s += '<td>' + items[i].SelectSingleNode('description').NodeValue() + '</td>'; }
					else
					{ s += channel.CreateElement('td', items[i].SelectSingleNode('description').NodeValue()).OuterXml(); }
					s += '</tr>';
					this.upDates[guid] = upDate;
				}
				s += '</table>';
				document.getElementById('liveBlock').innerHTML = s;
			}
			else
			{
				innerHTML = '<div style="margin-top:50px; color:#C53300;text-align:center;">Pas d\'&eacute;l&eacute;ments &agrave; afficher.</div>';
			}
			this.channelUpdates++;
			window.focus();
		}
	}
	catch(e){ this.DisplayError(e.message); }
}

liveManager.prototype.ChangeChannel = function(channelId)
{
	this.stop();
	this.channel = channelId;
	this.GetChannelDays();
}

liveManager.prototype.ChangeDay = function(dayData)
{
	this.stop();
	this.day = dayData.substring(0, dayData.indexOf(':', 0));
	this.type = dayData.substring(dayData.indexOf(':', 0) + 1, dayData.length);
	this.start();
}

liveManager.prototype.SetIsSynchronizing = function(status)
{
	this.Lock(status);
	if(status)
	{
		with(document.getElementById('status'))
		{
			style.color = '#C53300';
			innerHTML = 'synchronizing';
		}
		this.stopInterval();
	}
	else
	{
		with(document.getElementById('status'))
		{
			style.color = '#5B9218';
			innerHTML = 'synchronized';
		}
		this.startInterval();
	}
}

liveManager.prototype.DisplayError = function(message)
{
	this.stop();
	document.getElementById('errorMessage').innerHTML = 'An error occured, the application have been stopped: ' + message;
	document.getElementById('errorBlock').style.display = 'block';
	document.getElementById('synchroMessage').innerHTML = 'Stopped';
	document.getElementById('synchroImage').src = 'images/icon_err.gif';
	document.getElementById('liveBlock').style.display = 'none';
	document.getElementById('synchroBlock').style.display = 'block';
	document.getElementById('liveBlock').style.display = 'none';
	with(document.getElementById('status'))
	{
		style.color = '#C53300';
		innerHTML = 'stopped';
	}
	this.Lock(false);
}

liveManager.prototype.Lock = function(state)
{
	document.getElementById('channel').disabled = document.getElementById('day').disabled = state;
}

//*************************************************
//*		live request types enum
//*************************************************

var liveRequestType =
{
	upDate:1,
	RSSFeed:2,
	waiting:3,
	getDays:4
}

//*************************************************
//*		Ajax events
//*************************************************

function Ajax_OnCompleted_live(sender)
{
	var manager = sender.customs['liveManager'];
	manager.document = new XmlDom2(null, 'live');
	manager.document.LoadXML(sender.Text());
	//alert(sender.Text());
	//alert(manager.document.OuterXml());
	
	if(manager.document.DocumentElement().NodeName() == 'error')
	{
		manager.DisplayError(manager.document.DocumentElement().NodeValue());
	}
	else
	{
		switch(manager.request.customs['requestType'])
		{
			case liveRequestType.upDate:
				if(!manager.compareUpdate(new Date(manager.document.DocumentElement().SelectSingleNode("day/upDate").NodeValue()),
					manager.document.DocumentElement().SelectSingleNode("day/count").NodeValue().toInteger()))
				{ manager.SetIsSynchronizing(false); }
				break;

			case liveRequestType.RSSFeed:
				manager.UpdateChannel(manager.document.DocumentElement().SelectSingleNode('channel'));
				manager.request.customs['requestType'] = liveRequestType.waiting;
				manager.SetIsSynchronizing(false);
				break;
				
			case liveRequestType.getDays:
				with(document.getElementById('day'))
				{
					options.length = 0;
					var days = manager.document.DocumentElement().SelectNodes('day');
					for(var i = 0; i < days.length; i++)
					{
						var day_id = days[i].GetAttribute('id');
						var day_type = days[i].GetAttribute('type');
						options[options.length] = new Option(days[i].SelectSingleNode('description').NodeValue(), day_id + ':' + day_type);
						if(i == 0)
						{
							manager.day = day_id;
							manager.type = day_type;
						}
					}
					selectedIndex = 0;
				}
				manager.request.customs['requestType'] = liveRequestType.waiting;
				manager.start();
				break;
		}
	}
}

function Ajax_OnGetFailed_live(sender, e)
{
	sender.customs['liveManager'].DisplayError(e.message);
}