//window.addEvent( 'domready', function(e) {itlnsetup();itlnloadheadlinelist();});

var ITLatestNews = new Class({

	options: {
		duration       : 120000, // 2 Minutes
		itemstodisplay : 7,
		rssurl 				 : '/feeds/rss2/breaking/index.rss',
		highlightcolour: '#ffdddd'
	},

	initialize: function(container, options) {
		this.setOptions(options);

		if (!$( container )) {
			return;
		}

		this.container = container;

		var lastlist = {};

		// seed the history list with the existings links
		$( container ).getElements( 'a' ).each( function(a) {
			lastlist[ a.href ] = 1;
		});

		this.lastlist = lastlist;

		if( $( container ) ) {
			var div1 = new Element( 'div', { 'id': 'itlnwrapper' } );
			var div2 = new Element( 'div', { 'id': 'itlnloading' } );
			var ul = $( container ).clone();
			div2.injectInside( div1 );
			ul.injectInside( div1 );
			$( container ).replaceWith( div1 );
		
			this.itlnwrapper = div1;
			this.itlnloading = div2;
			this.itlnlist    = ul;
		}

	},

	start: function() {
		this.timer = this.load.periodical( this.options.duration, this );
	},

	stop: function() {
		$clear( this.timer );
	},

	load: function() {
	  var gwidth  = this.itlnwrapper.getStyle( 'width' ).toInt();
	  var gheight = this.itlnwrapper.getStyle( 'height' ).toInt();

	  // Center the loading icon
	  var lwidth  = this.itlnloading.getStyle( 'width' ).toInt();
	  var lheight = this.itlnloading.getStyle( 'height' ).toInt();

		this.itlnloading.setStyle( 'top', (gheight/2) - (lheight/2));
		this.itlnloading.setStyle( 'left', (gwidth/2) - (lwidth/2));

		var itemstodisplay = this.options.itemstodisplay;
		var highlightcolour = this.options.highlightcolour;
		var container = $(this.container);
		var lastlist = this.lastlist;

		var alist = container.getElements( 'li a' );

	  var request = new XHR({
	    method: 'get',
	    onSuccess: function(txt, xml ) {

				var xmldoc = xml.documentElement;
				var itemlist = xmldoc.getElementsByTagName('item');
				var currlist = {};

				for(var i=0; i<itemlist.length; i++) {

					var el = itemlist[ i ];
					var record = {};

					for(var j=0;j<el.childNodes.length;j++) {
						if ( el.childNodes[ j ].nodeType == 1 &&
							el.childNodes[ j ].childNodes.length >= 1 ) {
								record[ el.childNodes[ j ].nodeName ] = 
									el.childNodes[ j ].childNodes[ 0 ].nodeValue;
						}
					}

					record.ts = record.pubDate.substring( 11, 16 );

					if (i > itemstodisplay) { break; }

					var a;
					if (i < alist.length) {
						a = alist[ i ];
					} else {
						a = new Element( 'a' ).appendText( ' ' );
						var li = new Element( 'li' );
						a.injectInside( li );
						li.injectInside( container );
						li = null;
					}

					a.href = record.link;
					//a.title = record.description; // Disabled by GW as per request from HL 23/04/2009
					var span = a.getElement( 'span' );
					if ( ! span ) { span = new Element( 'span' ).injectInside( a ); }

					a.firstChild.nodeValue = record.ts;
					span.innerHTML = record.title;

					if ( ! lastlist[ record.link ] == 1) { // It's a new article
						lastlist[ record.link ] = 1;
						//a.setStyle( 'background-color', highlightcolour ); // Disabled by GW as per request from PL 29/04/2009
					} else {
						a.setStyle( 'background-color', 'white');
					}
					currlist[ record.link ] = 1;

					a = null;
					span = null;
				}

				lastlist = currlist;		
				alist = [];

	  		new Fx.Style($('itlnloading'), 'opacity').start( 0 ).chain( function() {
				  new Fx.Style($('itlnlist'), 'opacity').start( 1 );
				});
			}
		
		});

		this.itlnloading.setStyle( 'display', 'block' );

		var rssurl = this.options.rssurl;
		new Fx.Style($('itlnlist'), 'opacity' ).start( 0.1 ).chain( function() {
			new Fx.Style($('itlnloading'), 'opacity').start( 1 ).chain( function() {
				request.send( rssurl + "?ts=" + (new Date()).getTime() );
			});
		});

	}

});
ITLatestNews.implement(new Options);
