function getFeed()
{
	var xmlHttp;
	try {
		// firefox, opera 8.0+, safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// internet explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("browser does not support AJAX.  i should have a fallback.");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
	  		parseFeed(xmlHttp.responseXML);
	  	}
	}
	xmlHttp.open("GET","feed.php",true);
	xmlHttp.setRequestHeader("Cache-Control", "no-cache");
	xmlHttp.setRequestHeader("Pragma", "no-cache");
	xmlHttp.send(null);
}

function getFirstElementValue(docNode, elementName)
{
	return docNode.getElementsByTagName(elementName)[0].firstChild.nodeValue;
}

function parseFeed(xml)
{
	var root = xml.getElementsByTagName('rss')[0];
	var channels = root.getElementsByTagName("channel");
	var items = channels[0].getElementsByTagName("item");

	var feed_div = document.getElementById('feed');

	for(idx=0; idx<items.length; idx++) {
		var item = items[idx];
		var title = getFirstElementValue(item, 'title');
		var link = getFirstElementValue(item, 'link');
		var date = getFirstElementValue(item, 'pubDate');
		var via = link.split('/')[2].replace(/.com$/,'').replace(/^www\./,'');

		var item_div = document.createElement('div');
		feed_div.appendChild(item_div);
		var id = document.createAttribute('class');
		id.value = 'item';
		item_div.setAttributeNode(id);
		
		item_div.innerHTML += '<div class="title">' + title.link(link) + '</div>';
		item_div.innerHTML += '<div class="content">' + parseContent(item) + '</div>'
		item_div.innerHTML += '<div class="date">' + date + '</div>'
		item_div.innerHTML += '<div class="via">via ' + via + '</div>'
	}
}

function parseContent(item)
{
	var link = getFirstElementValue(item, 'link');
	if (link.indexOf('flickr.com') != -1) {
		return parseFlickr(item);
	} else if (link.indexOf('youtube.com') != -1) {
		return parseYoutube(item);
	} else if (link.indexOf('vimeo.com') != -1) {
		return parseVimeo(item);
	} else {
		return '';
	}
}

function parseFlickr(item)
{
	var desc = getFirstElementValue(item, 'description');
	var url = desc.match(/img src=.*\.jpg/).toString();
	url = url.substring(url.indexOf('http://'));
	return '<img src="' + url.replace('_m.jpg', '.jpg') + '"/>'
}

function parseYoutube(item)
{
	var link = getFirstElementValue(item, 'link');
	var id = link.match(/watch\?v=.*$/).toString().replace(/^watch\?v=/,'');
	var url = 'http://www.youtube.com/v/' + id + '&fs=1&rel=0&ap=%2526fmt=22'
	return embedFlashVideo(url, 640, 384);
}

function parseVimeo(item)
{
	var link = getFirstElementValue(item, 'link');
	var id = link.split('/')[3];
	var url = 'http://vimeo.com/moogaloop.swf?clip_id=' + id + '&server=vimeo.com&show_title=1&amp;show_byline=0&show_portrait=0&color=00ADEF&afullscreen=1';
	return embedFlashVideo(url);
}

function embedFlashVideo(url, width, height)
{
	width = typeof(width) == 'undefined' ? 640 : width;
	height = typeof(height) == 'undefined' ? 360 : height;
	sizeStr = 'width="' + width + '" height="' + height + '"';
	return '<object ' + sizeStr + '><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="' + url + '" /><embed src="' + url +'" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" ' + sizeStr + '></embed></object>';
}