function extractIdentifier(elementID)
{
	return elementID.split('-')[1];
}

function getUniqueValue()
{
    return new Date().getTime();
}

var channelID = 0;

function WebDisplay() {
}

WebDisplay.prototype = {
	
	init: function()
	{
		$$('.zone').each( function(zone) {
		    // get the ordered lists of bulletins for this zone...
		    // and start the loop once the ajax call has completed.
		    this.getLatestBulletins(extractIdentifier(zone.id));
		    
		}, this);

		$$('.alerts').each( function(channel) {
		    channelID = extractIdentifier(channel.id);
		    this.getLatestAlerts(channelID);
		}, this);

		$('FullAlertZone-' + channelID).setOpacity(0.0);
	    this.updateFullAlerts(channelID);

		// set up a load event for an image load to trigger it to fade in
		$$('.nextBulletin').each( function(b) {
			$(b.id).setOpacity(0.0);
			var zoneID = extractIdentifier(b.id);
			var zoneType = b.id.split('-')[0];
			var nextID = b.id;
			var prevID = zoneType + '-' + zoneID + '-Current';
			b.observe(
				'load',
				function(e) {
					var el = b;
					$(nextID).style.display = 'inline';
					var prevUrl = $(prevID).src;
					var nextUrl = $(nextID).src;
					// fade new image up
					new Effect.Opacity($(nextID), {
						duration: 1.0, 
						from: 0.0, to: 1.0,
						afterFinish: function(obj) {
							// should be able to switch images now without a glitch, since it is cached
							$(prevID).src = nextUrl;
							// reset for next time
							$(nextID).style.display = 'none';
							$(nextID).setOpacity(0.0);
							
							// show full alert zone
							if(prevID.split('-')[0] == 'FullAlertZone' && $('FullAlertZone-' + zoneID).style.display != 'inline')
							{
								$('FullAlertZone-' + zoneID).setOpacity(0.0);
								$('FullAlertZone-' + zoneID).style.display = 'inline';
								new Effect.Opacity($('FullAlertZone-' + zoneID), {
										duration: 1.0, 
										from: 0.0, to: 1.0
									});
							}
						}
					});
				}
			);
		}, this);
	}, 
	
	displayBulletin: function(zoneID, url, fullAlert)
	{
		var id = 'ZoneID-' + zoneID + '-Next';
		if(fullAlert)
			id = 'FullAlertZone-' + zoneID + '-Next';
		// start loading next image in 'Next' container, when it
		// is loaded, the event will fire (above)
		$(id).style.display = 'none';
		$(id).src = url;
	},
	
	getLatestAlerts: function(channelID)
	{
	    new Ajax.Updater($('Alerts-' + channelID), 'WebDisplay.aspx', {
	        method: 'get',
	        parameters: { op: 'getLatestAlerts', ChannelID: channelID, cache: getUniqueValue() },
	        onComplete: function() {
				var delay = 15;
		        // start the loop.
				setTimeout("this.webdisplay.getLatestAlerts(" + channelID + ");", (1000 * delay));
	        }
	    });
	},
	
	getLatestBulletins: function(zoneID)
	{
		// refresh bulletin list from server
	    new Ajax.Updater($('ZoneID-' + zoneID + '-Bulletins'), 'WebDisplay.aspx', {
	        method: 'get',
	        parameters: { op: 'getLatestBulletins', ZoneID: zoneID, cache: getUniqueValue() },
	        onComplete: function() {
		        // start the loop.
		        this.webdisplay.updateBulletin(zoneID);
	        }
	    });
	},
	
	updateBulletin: function(zoneID) 
    {
		var delay = 15;
		
		var zoneString = 'CurrentPages-' + zoneID;
		var divString = 'ZoneID-' + zoneID + '-Bulletins';
		var indexAttrib = 'index';
		
		// detect alert mode, and adjust where we look for certain things
        var alerts = $$('#AlertPages-' + zoneID + ' li');
        if(alerts.length > 0)
        {
			zoneString = 'AlertPages-' + zoneID;
			indexAttrib = 'alertIndex';
		}
		
        var bulletins = $$('#' + zoneString + ' li');
        var count = bulletins.length;
		
		var index = $(divString).getAttribute(indexAttrib);
		if(!index)
			index = 0;
		
        // if in full alert mode, don't update standard bulletin zone(s)
        var fullAlerts = $$('#FullAlertPages li');
        if(fullAlerts.length > 0)
        {
			delay = 15;
		    setTimeout("this.webdisplay.updateBulletin(" + zoneID + ");", (1000 * delay));
		}
		// if we have an empty zone
		else if(count == 0)
		{
			index = 0;
			// delay and check again
			delay = 15;
			setTimeout("this.webdisplay.getLatestBulletins(" + zoneID + ");", (1000 * delay));
		}
		else if(index >= count)
		{
			// we're at the end of the bulletin list, reset index and try again
			index = 0;
			delay = 0;
			setTimeout("this.webdisplay.getLatestBulletins(" + zoneID + ");", (1000 * delay));
		}
		else
		{
			// get bulletin
			var bulletin = bulletins[index];
			delay = bulletin.getAttribute('dwelltime');
			if(!delay)
				delay = 5;

			// display this bulletin			
			this.displayBulletin(zoneID, bulletin.firstDescendant().src); // initiate the load of the image
		    setTimeout("this.webdisplay.updateBulletin(" + zoneID + ");", (1000 * delay)); // update again after the dwell time

			// increment for next time
			index++;
		}
		// update index for next time
		$(divString).setAttribute(indexAttrib, index);
    },
    
    updateFullAlerts: function(channelID) 
    {
		var delay = 15;
		var zoneString = 'FullAlertZone-' + channelID;
        var bulletins = $$('#FullAlertPages li');
        var count = bulletins.length;
		
		var index = $(zoneString).getAttribute('index');
		if(!index)
			index = 0;
		
		if(count == 0)
		{
			// no full alerts
			index = 0;
			setTimeout("this.webdisplay.updateFullAlerts(" + channelID + ");", (1000 * delay));
			// hide full alert zone
			if($(zoneString).style.display != 'none')
			{
				$(zoneString).setOpacity(1.0);
				new Effect.Opacity($(zoneString), {
						duration: 1.0, 
						from: 1.0, to: 0.0,
						afterFinish: function(obj) {
							$(zoneString).style.display = 'none';
						}
					});
			}
		}
		else if(index >= count)
		{
			// we're at the end of the bulletin list or an empty list, reset index and try again
			index = 0;
			delay = 0;
			setTimeout("this.webdisplay.updateFullAlerts(" + channelID + ");", (1000 * delay));
		}
		else
		{
			// get bulletin
			var bulletin = bulletins[index];
			delay = bulletin.getAttribute('dwelltime');
			if(!delay)
				delay = 5;

			// display this bulletin (if not in full alert mode yet, the display bulletin will show the zone after loading the image)
			this.displayBulletin(channelID, bulletin.firstDescendant().src, true); // initiate the load of the image
		    setTimeout("this.webdisplay.updateFullAlerts(" + channelID + ");", (1000 * delay)); // update again after the dwell time

			// increment for next time
			index++;
		}
		// update index for next time
		$(zoneString).setAttribute('index', index);
    }
}
