/******************************************************/
/* File: iandevlin.js                                 */
/* Version: 0.1                                       */
/* Created: 03/01/2010                                */
/* Author: Ian Devlin (http://www.iandevlin.com)      */
/******************************************************/

var roman = new Array();
roman = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
var decimal = new Array();
decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];

/******************************************************/
/* decimalToRoman()                                   */
/* Converts the passed in decimal value to Roman      */
/* numerals                                           */
/******************************************************/
function decimalToRoman(value) {
	if (value <= 0 || value >= 4000) return value;
	var romanNumeral = "";
	for (var i=0; i < roman.length; i++) {
		while (value >= decimal[i]) {
			value -= decimal[i];
			romanNumeral += roman[i];
		}
	}
	return romanNumeral;
}

/******************************************************/
/* setTitleRollOver()                                 */
/* Adds the current year to the copyright message on  */
/* the titleImage title                               */
/******************************************************/
function setTitleRollOver() {
	document.getElementById('titleImage').title += " " +  decimalToRoman(new Date().getFullYear());
}

/******************************************************/
/* goto()                                             */
/* Goes to the requested URL                          */
/******************************************************/
 function goto(url) {
 	document.location.href=url;
 }
 /******************************************************/
/* toggleDisplay()                                     */
/* Uses the JQuery timers library to toggle the        */
/* visiblity of the different infocontainers           */
/*******************************************************/
 function toggleDisplay(id) {
	var item = '#' + id;
	if ($(item).is(':hidden')) {
		if($('.formmarker').is(':visible')) $('.formmarker').slideUp('slow');
		$(item).oneTime(1, function() { $(item).slideDown('slow'); });
	}
	else $(item).slideUp('slow');
}
/******************************************************/
/* slideUpInterval()                                  */
/* Uses the JQuery timers library to slide up the     */
/* specified div in the specified interval            */
/******************************************************/
function slideUpInterval(id,interval) {
	var item = '#' + id;
	$(item).oneTime(interval, function() { $(item).slideUp('slow'); });
}

/******************************************************/
/* Uses jQuery to serialise and send the email form   */
/******************************************************/
$(function() {	
	$("#sendEmail").click(function() {
		var dataString = $('form').serialize();
		$.ajax({
			type: "POST",
			url: "sendemail.php",
			data: dataString,
			cache: false,
			success: function(response) {
				if (response == "ok") {
					document.getElementById("emailcontainer").innerHTML = "Thank you";
					slideUpInterval('emailbox',100);
				}
				else {
					document.getElementById("formerror").style.display = "block";
					document.getElementById("formerror").innerHTML = response;
				}
			}
		});
		return false;
	});
});

/******************************************************/
/* When the document is loaded and ready              */
/******************************************************/
$(document).ready(function() {
	// Get the latest tweet
	getTweet();
	// Get the latest tweet every 5 minutes
	setInterval(getTweet, (60000 * 5));
	// Get the blog feed
	getFeed();
});

/******************************************************/
/* Hide and show the twitter bubble on hover          */
/******************************************************/
$(function() {
	$("#twitter").hover(function() {
		$("#twitter-bubble").fadeIn('slow');
	},
	function() {
		$("#twitter-bubble").fadeOut('slow');
	});
});

/******************************************************/
/* getTweet()                                         */
/* Get the latest tweet                               */
/******************************************************/
function getTweet() {
	$("#tweet").getTwitter({
		userName: "iandevlin",
		numTweets: 1,
		maxNumTweetsToTry: 6,
		loaderText: "loading...",
		slideIn: false,
		showHeading: false,
		showProfileLink: false,
		showTimestamp: true,
		showReplyTweets: false
	});
}

/******************************************************/
/* getBlogFeed()                                      */
/* Get the blog feed and enter some of the info into  */
/* blogposts                                          */
/******************************************************/
function getFeed() {
	$("#blogposts").empty();
	var html = "";
	$.get('http://www.iandevlin.com/blog/feed', function(f) {
		var post_count = 0;
		$(f).find('item').each(function() {
			post_count++;
			if (post_count > 2) return;
			var $item = $(this);
			var title = $item.find('title').text();
			var link = $item.find('link').text();
			var description = $item.find('description').text();			
			// Remove the related posts bit
			var loc = description.indexOf('Related posts:');
			description = description.substring(0, loc);			
			var pubDate = $item.find('pubDate').text();
			// Format the date
			var d = new Date(pubDate);
			var dateTime = d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate();
			pubDate = formatDate(pubDate);
			// Define the HTML and append it to the div		
			html += "<section class='bentry'><header><h2>" + title + "</h2></header>";
			html += "<time datetime='" + dateTime + "'>" + pubDate + "</time>";
			html += "<p>" + description + "</p>";
			html += "<footer><a href='" + link + "'>read more &raquo;</a></footer></section>";
		});
		document.getElementById("blogposts").innerHTML = html;
	});
}

/******************************************************/
/* formatDate()                                       */
/******************************************************/
function formatDate(dateString) {
	var d_names = new Array("Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday");
	var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var d = new Date(dateString);
	var curr_day = d.getDay();
	var curr_date = d.getDate();
	var sup = "";
	if (curr_date == 1 || curr_date == 21 || curr_date ==31) sup = "st";
	else if (curr_date == 2 || curr_date == 22) sup = "nd";
	else if (curr_date == 3 || curr_date == 23) sup = "rd";
	else sup = "th";
	var curr_month = d.getMonth();
	var curr_year = d.getFullYear();
	return d_names[curr_day] + ", " + curr_date + "<sup>" + sup + "</sup> " + m_names[curr_month] + " " + curr_year;
}