///////////////////////////////////////////////////////////////////////
// forum youtube tag 
// creator : Bryan Kwon
// date : 2009-08-13
///////////////////////////////////////////////////////////////////////


$(document).ready(function () {	 // onload
	$('var').each(function() {
			var tmp = $.trim($(this).html());
			$(this).replaceWith( embedYoutubeSrc(tmp) );

	});
});


function embedYoutubeSrc(url){

			var regularURL = new RegExp("http:\/\/\w{0,3}\.youtube\.com");
			var tmp = url;
			var embedSrc = "";
				
			if (regularURL.test(tmp)) {	
				if (tmp.indexOf("?") > 23) {
					var theMark = tmp.indexOf("?");
					tmp = tmp.substring(theMark+1);
					params = tmp.split("&");
					youtubecode = "";
					for (i = 0; i < params.length; i++) {
						if (/^v=/.test(params[i])) {
							youtubecode = params[i].substring(2);
						}
					}
					if (youtubecode != "") {
						embedSrc = "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+youtubecode+"&hl=en&fs=1&\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/"+youtubecode+"&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object><br>";
					}
				}
				else if (tmp.indexOf("v") == 20 || tmp.indexOf("v") == 23) {
					var theMark = tmp.indexOf("v");
					tmp = tmp.substring(theMark+2);
					params = tmp.split("&");
					youtubecode = "";
					youtubecode = params[0];
					
					if (youtubecode != "") {
						embedSrc = "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+youtubecode+"&hl=en&fs=1&\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/"+youtubecode+"&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object><br>";
					}
					
				} 
			}
			return (embedSrc==""?url:embedSrc);
}
