/*
	Script by R. Walker Dec 2007
	Feel free to rip it - but please give credit where credit is due
	and tell me if you make it better
	
	See/duplicate my pipe - http://pipes.yahoo.com/pipes/pipe.info?_id=xu54deA53BGhypt_y6ky6g
*/

/* TO DO
- experiment with content sliding
- add perm URLs
*/

// config
var stream_url = 'http://pipes.yahoo.com/pipes/pipe.run?_id=xu54deA53BGhypt_y6ky6g&_render=json' + '&_callback=feed';
//var stream_url = 'pipe.run';

var maxallcount = 20;

function showContent(ele) {
	$('.section').css('display', 'none');
	$('#menu li').removeClass('selected');
	$(ele).parent('li').addClass('selected').blur();
	$('#' + $(ele).text() ).css('display', 'block');
	return false;
}

function toggle(elem) {
	$(elem).toggleClass('show');
}

function toggleAll() {
	$('#content ol li').each(function() {
		toggle(this);
	});
}

function cleanDate(date) {
	// remove the time zone
	var endpos = date.lastIndexOf(' +');
	date = date.substring(0, endpos);

	// get the time
	endpos = date.lastIndexOf(' ');
	var time = date.substring(endpos, date.length);
	var date = date.substring(0, endpos);

	// remove date id equal to today
	var datenow = new Date();
	var datenow_dateyear = datenow.getDate() + ' ' + datenow.getFullYear();

	var timestamp = new Date(date);
	var timestamp_dateyear = timestamp.getDate() + ' ' + timestamp.getFullYear();
	if (datenow_dateyear == timestamp_dateyear) {
		date = '';
	}
	
	return date + '<strong>' + time + '</strong>';
}

function parseJSON(i, item) {
	var itemTitle = item.title;
	var itemLink = item.link;
	var itemDescription = item.description;
	var itemDate = item.pubDate;

	if (itemTitle) {
		// get service and strip it
		var service = itemTitle.slice(0, itemTitle.indexOf(':') );
		itemTitle = itemTitle.slice( itemTitle.indexOf(':') + 1 );
		if ( !$('#' + service).is('div') ) {
			$('#content').append('<div id="' + service + '" class="section"><ol class="stream"></ol></div>');
			$('#menu').append('<li class="menu_' + service + '"><a href="#' + service + '" onclick="return showContent(this)">' + service + '</a></li>');
		}
		var output = '<li onclick="toggle(this)" class="' + service + '">';
		if (itemLink) {
			output += '<a href="' + itemLink + '"><strong>' + itemTitle + '</strong></a>';
		} else {
			output += itemTitle;
		}
		if (itemDate) {
			output += '<div class="date">';
			output += cleanDate(itemDate);
			output += '</div>';
		}
		if (itemDescription) {
			output += '<div class="description">';
			output += itemDescription;
			output += '</div>';
		}
		output += '</li>';
	} else { // no title
		output += 'ERROR OCCURRED';
	}
	
	$('#' + service).find('ol').append(output);
	if (i < maxallcount) {
		$('#latest').find('ol').append(output);
	}
}

function feed(json) {
		$.each(json.value.items, function(i, item) {
	 		parseJSON(i, item);
		});
		// Show only one section block
		showContent( $('#menu a').get(0) );
}

function destoryHolders() {
	$('#menu').add('#content div').add('#toggleall').remove();
	//$('#content div').add('#toggleall').remove();
}

function createHolders() { // creates the block which the menu & other stuff is loaded in to
	$('<ol id="menu"><li class="menu_latest"><a href="#latest" onclick="destoryHolders(); createHolders(); return showContent(this)">latest</a></ol>').appendTo('#sidebar');
	$('<div id="latest" class="section"><ol class="stream"></ol></div>').appendTo('#content');
	$('<p id="toggleall"><a href="#toggleAll" onclick="toggleAll()">Toggle All</a></p>').appendTo('#sidebar');

	getFeed(stream_url);
}

// Remove's place holder in the initial HTML
function removeDegrade() {
	$('.noscript').remove();
}

function getFeed(feed) {
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	newScript.src = feed;
	// has a call_back to 'feed'
	document.getElementsByTagName('head')[0].appendChild(newScript);
}

$(function() {
	createHolders();
	removeDegrade();
});
