
// This function was written by "Travis" and originally submitted here:
// http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links#answer-2166104
function linkify(inputText) {
	//URLs starting with http://, https://, or ftp://
	var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
	var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

	//URLs starting with www. (without // before it, or it'd re-link the ones done above)
	var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
	var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

	//Change email addresses to mailto:: links
	var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
	var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

	// URLs starting with # or @, indicating Twitter hashes.
	// This part graciously stolen from incore_twitter.
	replacedText = replacedText.replace(/([^\w])\@([\w\-]+)/gm,'$1<a href="http://twitter.com/$2" target="_blank">@$2</a>');
	replacedText = replacedText.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	
	return replacedText
}


function wildsideTwitterSetup(account, xmlInput, itemLimit, destination) {
	
	var xml;
	
	if (jQuery.browser.msie) {
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(xmlInput);
		xml = jQuery(xmlDoc.documentElement);
	}
	else
		xml = jQuery(xmlInput);
	
	
	xml.find('channel item').each(function(count){
		if (count < itemLimit) {
			var item = jQuery(this);
			
			var content = item.find('> description').text();
			var pattern = new RegExp("^(" + account + "\:)\s?", "i");
			content = content.replace(pattern, '');
			
			content = linkify(content);
			
			var dateString = item.find('> pubDate').text();
			var dateObj = new Date(dateString);
			
			var parts = {};
			parts.time = dateObj.getDate() + '/' + (dateObj.getMonth() + 1) + ' \'' + (dateObj.getFullYear().toString().substr(2));
			parts.text = content;
			
			var entry = jQuery('<div>');
			entry.addClass('entry');
			
			for (label in parts) {
				var elm = jQuery('<span>');
				elm.addClass(label);
				elm.html(parts[label]);
				entry.append(elm);
			}
			
			jQuery(destination).append(entry);
		}
		
	});
}
