//	Global variable oh noes!
//	All of the following code is very crappy but I'm in a real hurry here
var currentTestimonial = 1;
var currentScreenshot = 1;

$(function() {
	//	Initialize the rotating screenshots and testimonials
	window.setInterval(rotateScreenshots, 5000);
	window.setInterval(rotateTestimonials, 7000);
});

function rotateScreenshots() {
	$('#site-previews-big li:nth-child(' + currentScreenshot + ')').fadeOut('slow');
	if (currentScreenshot == $('#site-previews-big li').length) {
		currentScreenshot = 1;
	} else {
		currentScreenshot += 1;
	}
	$('#site-previews-big li:nth-child(' + currentScreenshot + ')').fadeIn('slow');
}


function rotateTestimonials() {
	$('#testimonials div div:nth-child(' + currentTestimonial + ')').fadeOut('slow', function() {
		if (currentTestimonial == $('#testimonials div div').length) {
			currentTestimonial = 1;
		} else {
			currentTestimonial += 1;
		}
		$('#testimonials div div:nth-child(' + currentTestimonial + ')').fadeIn('slow');
	});
}

//	Very useful functions for converting URLs, usernames, and hashtags in Twitter's feed into links
//	http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript
String.prototype.parseURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) {
		return url.link(url);
	});
};
String.prototype.parseUsername = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
		var username = u.replace("@","")
		return u.link("http://twitter.com/"+username);
	});
};
String.prototype.parseHashtag = function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
		var tag = t.replace("#","%23")
		return t.link("http://search.twitter.com/search?q="+tag);
	});
};

// blog and twitter feed on home page - only call on home page
if(document.getElementById("home")) {
google.load("feeds", "1");
function initialize() {
	//	Recent Blog Posts
	var blogFeed = new google.feeds.Feed("http://feeds.feedburner.com/vidcasterblog");
	blogFeed.setResultFormat(google.feeds.Feed.XML_FORMAT);
	blogFeed.load(function(result) {
		if (!result.error) {
			var blogItems = result.xmlDocument.getElementsByTagName("item");
			var blogContainer = document.getElementById("blog-posts-container");
			var blogArticles = document.createElement("ol");
			for (var i = 0; i < 3; i++) {
				var pubDate = new Date(blogItems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue);
				var months = new Array(12);
				months[0] = "January";
				months[1] = "February";
				months[2] = "March";
				months[3] = "April";
				months[4] = "May";
				months[5] = "June";
				months[6] = "July";
				months[7] = "August";
				months[8] = "September";
				months[9] = "October";
				months[10] = "November";
				months[11] = "December";
				var month = months[pubDate.getMonth()];
				var date = pubDate.getDate();
				var dateString = month + " " + date;
				var li = document.createElement("li");
				var a = document.createElement("a");
				var strong = document.createElement("strong");
				var span = document.createElement("span");
				a.setAttribute("href", blogItems[i].getElementsByTagName("link")[0].firstChild.nodeValue);
				strong.appendChild(document.createTextNode(blogItems[i].getElementsByTagName("title")[0].firstChild.nodeValue));
				span.appendChild(document.createTextNode(dateString));
				a.appendChild(strong);
				a.appendChild(span);
				li.appendChild(a);
				blogArticles.appendChild(li);
			}
			blogContainer.appendChild(blogArticles);
		}
	});

	//	Recent Tweets
	var twitterFeed = new google.feeds.Feed("http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=vidcaster");
	twitterFeed.setResultFormat(google.feeds.Feed.XML_FORMAT);
	twitterFeed.load(function(result) {
		if (!result.error) {
			//	Get the first item in the feed
			var twitterNewestItem = result.xmlDocument.getElementsByTagName("item")[0];
			//	Grab the text from that item
			var tweetText = twitterNewestItem.getElementsByTagName("title")[0].firstChild.nodeValue;
			//	That text starts with "vidcaster: " which we'll remove here
			tweetText = tweetText.substr(11);
			//	We'll also want to detect any URLs in the string and wrap them in links
			tweetText = tweetText.parseURL().parseUsername().parseHashtag();
			//	Get the container for where the text goes
			var twitterContainer = document.getElementById("tweet-container");
			//	Create a new blockquote to hold the text
			var twitterTextArea = document.createElement("blockquote");
			//	Append the tweet text to the blockquote
			twitterTextArea.innerHTML = tweetText;
			//	Then append the blockquote to the container to add it to the page
			twitterContainer.appendChild(twitterTextArea);
		}
	});
}
google.setOnLoadCallback(initialize);
}
