window.addEvent("domready", function () {
	
	var videoEmbed = '<object style="height: 390px; width: 640px"><param name="movie" value="http://www.youtube.com/v/{videoid}?version=3&feature=player_detailpage">';
	videoEmbed += '<param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always">';
	videoEmbed += '<embed src="http://www.youtube.com/v/{videoid}?version=3&feature=player_detailpage" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="360">';
	videoEmbed += '</object>';
	
	$$("a[href*=youtube.com/watch]").each(
		function (element, index, array) {
			var videoid = element.get("href").toString().split("?")[1].parseQueryString().v
			new Element("div").set("html", videoEmbed.replace(/({videoid})/g,videoid )).replaces(element)
			}
		
		);
	
	
	});

String.implement({

	parseQueryString: function(decodeKeys, decodeValues){
		if (decodeKeys == null) decodeKeys = true;
		if (decodeValues == null) decodeValues = true;

		var vars = this.split(/[&;]/),
			object = {};
		if (!vars.length) return object;

		vars.each(function(val){
			var index = val.indexOf('=') + 1,
				value = index ? val.substr(index) : '',
				keys = index ? val.substr(0, index - 1).match(/([^\]\[]+|(\B)(?=\]))/g) : [val],
				obj = object;
			if (!keys) return;
			if (decodeValues) value = decodeURIComponent(value);
			keys.each(function(key, i){
				if (decodeKeys) key = decodeURIComponent(key);
				var current = obj[key];

				if (i < keys.length - 1) obj = obj[key] = current || {};
				else if (typeOf(current) == 'array') current.push(value);
				else obj[key] = current != null ? [current, value] : value;
			});
		});

		return object;
	},

	cleanQueryString: function(method){
		return this.split('&').filter(function(val){
			var index = val.indexOf('='),
				key = index < 0 ? '' : val.substr(0, index),
				value = val.substr(index + 1);

			return method ? method.call(null, key, value) : (value || value === 0);
		}).join('&');
	}

});


