


function NewsFeedManager ( data )
{
	
	NewsFeedManager.prototype.data = 			[];
	NewsFeedManager.prototype.schedule =		false;		
	NewsFeedManager.prototype.node = 			false;
	NewsFeedManager.prototype.loadNode = 		false;
	NewsFeedManager.prototype.options = 		[];	
	
	function NewsFeedManager( data )
	{
		this.data = data;
		this.parent = document.getElementById(data.parent);
		window.NewsFeedSchedule = this;
		this.display();
		this.getFeed();
		this.startSchedule();
	}
	
	NewsFeedManager.prototype.getFeed = function() 
	{
		args = [];
		validRequest = false;
		for ( var i in this.options )
		{
			if ( this.options[i] == true )
			{
				validRequest = true;
				args[args.length] = i;
			}
			
		}
		if ( !validRequest )
		{ 
			this.selectAfeed();
			return ; 
		}
		
		args = args.concat();
		
		new Ajax.Request(this.data.location + "?event_id=" + this.data.eventId + "&args=" + args, 
		{
  			method: 'get',
  			onSuccess: this.loadFeed
		});			
	};
	
	NewsFeedManager.prototype.selectAfeed = function()
	{
		message = document.createElement("DIV");
		message.className = "innerFeed";
		message.appendChild(document.createTextNode("Please Select A Feed"));
		window.NewsFeedSchedule.loadNode.parentNode.replaceChild(message,window.NewsFeedSchedule.loadNode);
		window.NewsFeedSchedule.loadNode = message;		
		
	};
	
	
	NewsFeedManager.prototype.changeFeed = function( feed ) 
	{
		this.options[feed] = ( this.options[feed] == true ) ? false : true ;
		this.getFeed();
	};
	
	NewsFeedManager.prototype.loadFeed = function( response, json )
	{		
		data = eval('(' + response.responseText + ')');
		newsFeed = new NewsFeed( data );
		window.NewsFeedSchedule.loadNode.parentNode.replaceChild(newsFeed.node,window.NewsFeedSchedule.loadNode);
		window.NewsFeedSchedule.loadNode = newsFeed.node;		
	};

	NewsFeedManager.prototype.startSchedule = function () 
		{ window.setInterval("window.NewsFeedSchedule.getFeed()",this.data.refreshRate); };
	
	NewsFeedManager.prototype.display = function () 
	{
				
		title = document.createElement("DIV");
		title.className = "borderBoxTitle";
		title.appendChild(document.createTextNode("MLG"));				
		live = document.createElement("I");
		live.appendChild(document.createTextNode(" LIVE "));
		title.appendChild(live);
		title.appendChild(document.createTextNode("NEWSFEED"));
		//this.parent.appendChild(title);
		
		options = document.createElement("TABLE");
		options.className = "checkBoxes";
		options.cellPadding = 3;
		options.cellSpacing = 3;
		options.width = "100%";
		
		optBody = document.createElement("TBODY");
		options.appendChild(optBody);
		
		var x = 0;
		row = document.createElement("TR");
		for ( var i in this.data.options )
		{
			if ( x == 2 )
			{
				optBody.appendChild(row);
				row = document.createElement("TR");
				x = 0;
			}
			cell = document.createElement("TD");
			option = document.createElement("INPUT");
			option.type = "CHECKBOX";
			option.checked = true;
			option.defaultChecked = true;
			option.value = i;
			option.obj = this;
			option.onclick = function() { this.obj.changeFeed(this.value); };
			this.options[i] = true; 
			cell.appendChild(option);
			cell.appendChild(document.createTextNode(this.data.options[i]));
			
			row.appendChild(cell);
			x++;
		}
		if ( x == 2 || x == 1 )
			{ optBody.appendChild(row); }
			
		this.parent.appendChild(options);	
			
		this.loadNode = document.createElement("DIV");
		this.loadNode.appendChild(document.createTextNode("Loading"));
		this.parent.appendChild(this.loadNode);		
	};
		
	return new NewsFeedManager ( data );
}



function NewsFeed ( data )
{
	
	NewsFeed.prototype.data = 			[];
	NewsFeed.prototype.node =			false;
	
	function NewsFeed ( data )
	{
		this.data = data;
		this.display();
	}
	
	NewsFeed.prototype.display = function () 
	{
		this.node = document.createElement("DIV");
		this.node.className = "feed";
		
		for ( var i in this.data )
		{
			container = document.createElement("DIV");
			container.className = "innerFeed";
			link = document.createElement("A");
			link.href = this.data[i].url;
			link.target = "_blank";
			link.appendChild(document.createTextNode(this.data[i].title));
			container.appendChild(link);
			this.node.appendChild(container);
		}

				
	};
	
	
 	return new NewsFeed( data );	
}