var Display = {
	Box : {
		_debug: false,
		_default_messages: '<li class="highlight">This is an error message...</li><li class="highlight">If you have the variable _location set check the file exists and is accessible</li><li class="highlight">See http://www.bbc.co.uk/northernireland/sitewide/tools/messagebox/ for details on how to add messages</li>',
		_location: null,
		_refreshInterval: 7000,
		_elements: [],
		_currentIndex: -1,
		_container :  null,
		_interval : 0,
		
		doUpdates : function () {
			var refreshBox = Display.Box.refresh;	
			refreshBox();
			clearInterval(Display.Box._interval);
			Display.Box._interval = setInterval(refreshBox, Display.Box._refreshInterval);			
		},
		
		getUpdates : function(){
			glow.net.get(Display.Box._location, {
			 	onLoad: function(response){
					Display.Box._elements = glow.dom.create(response.text());
					Display.Box.doUpdates();
				}, 
			 	onError: function(response){
					if (Display.Box._debug) {
						Display.Box._elements = glow.dom.create(Display.Box._default_messages);
						Display.Box.doUpdates();
					} else {
						$("#sms-holder").remove();
					}
				}
			 });
		},
		
		init: function(MessageInfo){
			$ = glow.dom.get;
			
			if (MessageInfo != null) {
				if (MessageInfo.Details._debug != null) {Display.Box._debug = MessageInfo.Details._debug;}
				if (MessageInfo.Details._location != null) {Display.Box._location = MessageInfo.Details._location;}
				if (MessageInfo.Details._refreshInterval != null) {Display.Box._refreshInterval = MessageInfo.Details._refreshInterval;}
			}
			
			Display.Box._container = $("#sms-holder-list");
			Display.Box._container.empty();
			Display.Box._container.prepend("<li>Fetching Messages</li>");
			Display.Box.getUpdates(glow);
			
		},
		refresh: function(){
			Display.Box._currentIndex++;
			if(Display.Box._currentIndex >= Display.Box._elements.length){
				Display.Box._currentIndex = -1;
				Display.Box.getUpdates();
				return;
			}
			
			//Display.Box._container.filter(function (i) {return this.style.removeAttribute('filter');});
			
			//Display.Box._container.removeAttr('filter');
			
			//console.log(Display.Box._container);
			
			var fadeOut = glow.anim.css(Display.Box._container, 2, {"opacity": {to:"0"}});
			var fadeIn = glow.anim.css(Display.Box._container, 2, {"opacity": {from: "0", to:"1"}});
			glow.events.addListener(fadeOut, "complete",function(){
				// switch and fade up
				Display.Box._container.empty().prepend(Display.Box._elements.item(Display.Box._currentIndex));
				fadeIn.start();
			});
			fadeOut.start();
		} // no comma at end
	}
}

