function detectLinks(obj){
	content = $(obj).html();
	//URLs starting with http://, https://, or ftp://
	var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
	var replacedText = content.replace(replacePattern1, '<a onclick="clickLinkVideo=true" class="url-link" 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 onclick="clickLinkVideo=true" class="url-link" href="http://$2" target="_blank">$2</a>');
	
	//unlink youtube
	var replacePattern3 = /<a\s.*href=['"](\b(https?|ftp|file):\/\/(www.)?youtube.com\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])['"].*>.*<\/a>/ig;
	replacedText = replacedText.replace(replacePattern3,"$1");

	//unlink vimeo
	var replacePattern4 = /<a\s.*href=['"](\b(https?|ftp|file):\/\/(www.)?vimeo.com\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])['"].*>.*<\/a>/ig;
	replacedText = replacedText.replace(replacePattern4,"$1");
	
	return replacedText;
}

function showYouTubeVideo(link, videoId){
	var html = '<object width="425" height="344">';
	html += '<param name="movie" value="http://www.youtube.com/v/' + videoId + '&hl=en_US&fs=1"></param>';
	html += '<param name="allowFullScreen" value="true"></param>';
	html += '<param name="allowscriptaccess" value="always"></param>';
	html += '<embed src="http://www.youtube.com/v/' + videoId + '&hl=en_US&fs=1" type="application/x-shockwave-flash" width="425" height="344" allowscriptaccess="always" allowfullscreen="true"></embed>';
	html += '</object>';
	
	$(link).parents('.item-content').find('.item-content-media').html(html);
}

function detectYouTubeUrl(obj){
	content = $(obj).html();
	results = content.match("http://(www.)?youtube.com/watch[\\?&]v=([^&#. <]*)");
	
	if(results != null){
		showYouTubeVideo(obj, results[2]);
		//content = content.replace(/(http:\/\/(www.)?youtube.com\/watch[\\?&]v=([^&#. <]*))/g, '<a href="javascript:;" title="Show YouTube video" class="youtube-link" onclick="clickLinkVideo=true;showYouTubeVideo(this, \'$3\')">$1</a>');
	}
	
	return content;
}

function showVimeoVideo(link, videoId){
	var html = '<object width="425" height="344">';
	html += '<param name="allowfullscreen" value="true" />';
	html += '<param name="allowscriptaccess" value="always" />';
	html += '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + videoId + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" />';
	html += '<embed src="http://vimeo.com/moogaloop.swf?clip_id=' + videoId + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed>';
	html += '</object>';
	
	$(link).parents('.item-content').find('.item-content-media').html(html);
}

function detectVimeoUrl(obj){
	content = $(obj).html();
	
	results = content.match("http://(www.)?vimeo.com/([^&#. <]*)");
	
	if(results != null){
		showVimeoVideo(obj, results[2]);
		//content = content.replace(/(http:\/\/(www.)?vimeo.com\/([^&#. <]*))/g, '<a href="javascript:;" title="Show Vimeo video" class="youtube-link" onclick="clickLinkVideo=true;showVimeoVideo(this, \'$3\')">$1</a>');
	}
	
	return content;
}

function filterItemContent(obj){
	content = detectLinks(obj);
	content = detectYouTubeUrl(obj);
	content = detectVimeoUrl(obj);
	return content;
}
