var LatestNewsTabs = {
	
	latestNewsItems: null,
	currentlyShowing: "", 
	
	init: function() {
		this.latestNewsItems  = new Array();
		this.currentlyShowing = "";
		LatestNewsTabs.addRolloverEffects();
		LatestNewsTabs.showInitialNewsItem();
		LatestNewsTabs.verticalAlign();
		
	},
	
	addRolloverEffects: function() {
		$$('.latest_news_tab').each(function(el) {	
			
			var latestNewsID = "latestNews_" + el.id;
			latestNewsItems.push(latestNewsID);
			$(el.id).addClass('latest_news_tab_unselected');
			
			el.addEvents({
				
				mouseover: function() {
					
					// hide and deselct currently showing
					if(currentlyShowing != "") {
						$(currentlyShowing).setStyle('display', 'none');
						
						var newsItemAndTabID = currentlyShowing.split("_");
						// newsItemAndTabID[1] = id of the news item tab
						LatestNewsTabs.deselectNewsItem(newsItemAndTabID[1]);
						$(el.id).removeClass('latest_news_tab_unselected');
						$(el.id).addClass('latest_news_tab_selected');
					}
					
					// set heading
					var heading = $('latest_news_title');
					heading.set('html', el.get('html').trim().substr(0, 46));
					
					$(el.parentNode.parentNode).setStyle('background-position','-164px 0');
					
					//show this news item
					$(latestNewsID).setStyle('display', 'inline');
					
					// set this news item as current one
					currentlyShowing = latestNewsID;
				},
				
				mouseout: function() {
					$(el.id).addClass('latest_news_tab_unselected');
					$(el.id).removeClass('latest_news_tab_selected');
					//$(el.parentNode.parentNode).setStyle('background-position','1px 0');
				}
				
			});
			
			
		});
	},
	
	showInitialNewsItem: function() {
	
		// get an initial news item to display
		var id = latestNewsItems.pop();
		$(id).setStyle('display', 'inline');
		
		// show correct background image 
		// for selected news item
		var newsItemAndTabID = id.split("_");
		// newsItemAndTabID[1] = id of the news item tab
		LatestNewsTabs.showInitialNewsItemSelected(newsItemAndTabID[1])
		
		// set heading
		var heading = $('latest_news_title');
		heading.set('html', $(newsItemAndTabID[1]).get('html').trim().substr(0, 46));

		// set currently showing news item
		currentlyShowing = id;
	},
	
	// show initial news item on page load
	showInitialNewsItemSelected: function(id) {
		var initialNews = $(id);
		$(initialNews.parentNode.parentNode).setStyle('background-position','-164px 0');
	},
	
	// deselect news tab
	deselectNewsItem: function(id) {
		var initialNews = $(id);
		$(initialNews.parentNode.parentNode).setStyle('background-position','1px 0');
	},
	
	// vertical align the text on the tabs
	verticalAlign: function() {
        $$('#latest_news_list div p').each(function(el) {
	        var elHeight = el.getSize().y;
	        var parentHeight = el.getParent().getSize().y;
	        el.setStyle('margin-top', (parentHeight - elHeight)/2 + 'px');
		});

	}
};

window.addEvent('domready', LatestNewsTabs.init);
