if (typeof(console) == 'undefined' || typeof(console.log) == 'undefined') {
	var console = {
		'log': function(msg) {
			// do nothing
		}
	};
}

/**
 * @author sweston
 */

	var stream_items = 1;
	var stream_timeout = 5000;
	var stream_timer = null;
	
	$('document').ready(function(){
		
		//stream_timer = setInterval(getStream, stream_timeout);

		$(".page").click(getFeatures);
	});
	
	function getFeatures(){
			
			var page = $(this).text();	
			
			if ($(this).text() == '>')
				page = parseInt($("#focused_page").val()) + 1;
			else if ($(this).text() == '<')
				page = parseInt($("#focused_page").val()) - 1;
			else if ($(this).text() == '...Last ›')
				page = (parseInt($("#last-link > a").attr('href').replace('/', '')) / 4) + 1;
			else if ($(this).text() == '‹ First...')
				page = 1;
			
			
			
			$("#focused_page").val(page);
					
			$.getJSON(siteUrl("base/ajax_features/") + ((page - 1) * 4), function(data){
				
				$(".features-box").empty();
				$.each(data.items, function(i, item){

					var injection = '<div class="picture"><div><em><a href="' + siteUrl(base_url + item.category_id) + '">' + item.category + '</a></em>' + '<span><a href="' + siteUrl("post/")  + item.post_id + '/' + item.url_title + '">' + item.title + '</a></span>' + '<strong><a href="' + siteUrl("post/")  + item.post_id + '/' + item.url_title + '">' + item.comment_count + '</a></strong></div><a href="' + siteUrl('post/' + item.post_id + '/' + item.url_title ) + '"><img src="' + item.path + '" alt="" width="261" height="198"/></a></div><div class="txt"><em>' + item.fuzzy + '</em><p>' + item.excerpt + '</p><a href="' + siteUrl("post/") + item.post_id + '/' + item.url_title + '">View and Discuss</a></div>';
																																																																																																																																											
					$("#box_" + i).append(injection);
					
				});
				
				$("#pagination").empty();
				$("#pagination").append(data.pagination);
				
				$(".page").unbind('click');
				$(".page").click(getFeatures);
				});
			
			return false;
		
	}
		
	function getStream(){
		
		var stream_key = $("#stream-key").text().split('-');
		
		var type = stream_key[0];
		var item_id = stream_key[1];
		
		$.getJSON(siteUrl("base/ajax_stream/") + type + "/" + item_id, function(data){
			
			$.each(data.items, function(i, item){
				
				type = item.type;
				item_id = item.item_id;
				var focused_type = type;
				
				if (focused_type == 'collective')
					focused_type = 'question';
				else
					focused_type = 'thread';
			
				var injection = '<li style="display:none"><strong>' + item.fuzzy + '</strong><div><p><a href="' + siteUrl() + focused_type + '/' + item.item_id + '/' + item.url_title + '">' + item.title + '</a></p><div><a href="' + siteUrl() + focused_type + '/' + item.item_id + '/' + item.url_title + '" ';
				
				if (focused_type=='question')
					injection += ' class="question"'
						
				injection += '"><span>' + focused_type.toUpperCase() + '</span></a><p>by <a href="'+ siteUrl("people/") + item.username + '">' + item.username + '</a><p></div></div><span class="img"><a href="' + siteUrl('people/' + item.username)  + '"><img src="' + item.path + '" alt="" width="48" height="48" /></a></span></li>';
				
				$("#consciousness_stream li:first-child").before(injection);
				$("#consciousness_stream li:first-child").slideDown("slow");
				$("#consciousness_stream li:last-child").slideUp("slow");
				$("#consciousness_stream li:last-child").remove();
				$("#stream-key").text(type + '-' + item_id);
				
			
			});
			
			stream_timer = setInterval(getStream, stream_timeout);
		});
	}
	
	
