/* $() function from Prototype */
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}		

//define tumble function
function tumble() {
	//assign posts to variable
	posts = tumblr_api_read['posts'];
	//define month names
	var month_names = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	//iterate over posts
	for (i=0; i<posts.length; i++) {
		//define icon variable
		var icon = false;
		var content = "";
		//switch based on post type and assign content for list item
		switch (posts[i]['type']) {
			case 'regular':
				content = "<strong>" + posts[i]['regular-title'] + "</strong>: " + posts[i]['regular-body'];				
				break;
			case 'quote':
				content = posts[i]['quote-text'] + "<br /><em>" + posts[i]['quote-source'] + "</em>";				
				break;
			case 'photo':
				content = "<a href='" + posts[i]['url'] + "'><img src='" + posts[i]['photo-url-250'] + "' class='photo'/></a>";				
				content += "<br />" + posts[i]['photo-caption'];				
				break;
			case 'link':
				content = "<a href='" + posts[i]['url'] + "'><strong>" + posts[i]['link-text'] + "</strong></a>: " + posts[i]['link-description'];				
				break;
			case 'conversation':
				content = "Conversation";				
				break;
			case 'video':
				icon = 'video.png';
				content = "<a href='" + posts[i]['url'] + "'>" + posts[i]['video-caption'] + "</a>";				
				break;
			case 'audio':
				icon = 'music.png';
				content = posts[i]['audio-caption'];				
				break;
		}
	
		//parse date 
		var date = new Date(posts[i]['date']);
		date = month_names[date.getMonth()] + " " + date.getDate();
		//define item variable
		var item = "";
		//build item html
		item += "<li>";
		if (icon) { item += "<img src='images/icons/" + icon + "' alt='' /> "; }
		item += content;
		item += "<div class='meta'><a href='" + posts[i]['url'] + "'>Read this post</a> from " + date + "</div>";
		item += "</li>";
		//add list item
		$('tumblr_feed').innerHTML += item;
	}						
}