// Get time for the log function
var ts = new Date().getTime();
var limit = 4;

// ii function
var ii = {
	// Check if Firebug is installed and output the log
	log: function(msg){if (window.console && window.console.firebug){console.log((new Date().getTime() - ts) + ': '+ msg);};},

	init: function(){
		ii.log('init()');
	},

	domready: function(){
		$("a.overlay").fancybox();
		
		ii.getContent();
		
		$('#news-nav').click(function(){
			ii.getContent('news');	
			$('#box-nav li').each(function(){
				$(this).removeClass('selected');						   
			})
			$(this).addClass('selected');
			return false;
		});
		
		$('#recentevents-nav').click(function(){
			ii.getContent('recentevents');
			$('#box-nav li').each(function(){
				$(this).removeClass('selected');						   
			})
			$(this).addClass('selected');
			return false;
		});		
		
		$('#upcomingevents-nav').click(function(){
			ii.getContent('upcomingevents');
			$('#box-nav li').each(function(){
				$(this).removeClass('selected');						   
			})
			$(this).addClass('selected');
			return false;
		});	
		$('#blog-nav').click(function(){
			ii.getContent('blog');	
			$('#box-nav li').each(function(){
				$(this).removeClass('selected');						   
			})
			$(this).addClass('selected');
			return false;
		});

		$('#twitter-nav').click(function(){
			ii.getContent('twitter');
			$('#box-nav li').each(function(){
				$(this).removeClass('selected');						   
			})
			$(this).addClass('selected');
			return false;
		});
	},
	
	getContent: function(t, l){
		if(!l)
			l = limit;
		var content = '';
		var count = 0;
		if(!t){t = 'news';}
		$.ajax({
			 type: "GET",
			 url: "xml/content.xml",
			 dataType: "xml",
			 success: function(xml) {
				items = $(xml).find('item');
				items.each(function(i){						  
												  
					date = $(this).find('date').text();
					type = $(this).find('type').text();
					desc = $(this).find('desc').text();
					
					wh = '';
					
					if(count == 0){
						wh = ' class="nobg"';
					}else if(count > l){
						content = content + '<a href="javascript:ii.getContent(\'' + t + '\', ' + (count + 5) + ');void(0)" id="more" class="button" style="margin:0 auto"><span>Load More</span></a>';
						return false;	
					}
					if(t == 'news' || t == type){
						content = content + '<li'+wh+'><span class="date">'+date+'</span><p>'+desc+'</p></li>';
						count++;
					}
				});
				$('#box-content').html(content);
				$('#box-content li a').each(function(){
					var aC = $(this).html() + ' <img src="i/ext.png" alt="External Website" />';
					$(this).html(aC);
				});
			 }
		 })		
	}
}
// Execute post DOM ready manipulation
$(document).ready(function(){ii.domready()});

// Execute pre DOM ready manipulation
ii.init();
