// ebmed youtube and google video links in a web page
// @author - Shawn Parker, shawn@topfroggraphics.com
//
// I accept any and all comments, criticisms and suggestions
// please feel free to contact me if you find anything about
// this script that could be added to, fixed or improved upon.
//
// @warranty - none
//
// @rights - do what you will. Really - its just a simple javascript.
//
// @use - simply include this script in the head of your document and it'll
//        take care of the rest.

function embed_yt_gv()
{
	// run it all
	//
	// @return bool
	this.embed_videos = function()
	{
		// be a stodgy bitch - only work for full DOM browsers
		if(!document.getElementById)
		{
			alert('<nelson>Ha Ha!</nelson>');
			return false;
		}

		// get page links
		var links = this.get_yt_gv_links();

		if(links != null)
		{
			//_this fixes variable scoping - not sure how, but it does
			var _this = this;
			
			// loop through and manipulate the links
			for(var i = 0; i < links.length; i++)
			{
				_this.link_router(links[i]);
			}
		}
		else
		{
			return false;
		}
	} // embed_videos


	// get all the links in a page
	//
	// @return obj - HTML link objects
	this.get_yt_gv_links = function()
	{
		var page_links = document.getElementsByTagName('a');
		var embed_links = new Array();
		var type;
		for(var i = 0; i < page_links.length; i++)
		{
			// look for:
			//		youtube.com/watch
			//		video.google.com/videoplay
			if( page_links[i].href.match(/youtube.com\/watch|video.google.com\/videoplay/gi) )
			{
				// assign a type to this object
				type = page_links[i].href.match(/google/gi) ? 'google' : 'youtube';
				page_links[i].type = type;
				// add it to the array
				embed_links.push(page_links[i]);
			}
		}
		if(embed_links.length > 0)
		{
			return embed_links;
		}
		else
		{
			return null;
		}

	} // get_yt_gv_links


	// route links to proper processor
	//
	// @param obj - html link
	this.link_router = function(link)
	{
		if(link.type == 'google')
		{
		 	this.replace_google_link(link);
		}
		else
		{
			this.replace_youtube_link(link);
		}

	} // link_router


	// replace individual google link
	//
	// @param object link
	this.replace_google_link = function(link)
	{
		var googleVars = this.get_key_value_pairs(this.get_query_string(link.href));
		var gembd = document.createElement('embed');
		with(gembd)
		{
			setAttribute('width','400px');
			setAttribute('height','326px');
			setAttribute('id','VideoPlayback');
			setAttribute('type','application/x-shockwave-flash');
			setAttribute('src',"http://video.google.com/googleplayer.swf?docId=" + googleVars['docid']);
		}
		link.parentNode.replaceChild(gembd,link);

	} // replace_google_link


	// replace individual youtube link
	//
	// @param object link
	this.replace_youtube_link = function(link)
	{
		var strippedLink = link.href.split(/&/gm);
		var processedLink = strippedLink[0].replace(/(\/watch\?)|(=)/gm, "/");

		// create object, user setAttribute just to make it non-IE compatible
		var obj = document.createElement('object');
		obj.setAttribute('width','425');
		obj.setAttribute('height','350');

		// create param
		var parm = document.createElement('param');
		parm.setAttribute('name','movie');
		parm.setAttribute('value',processedLink);

		// create embed
		var embd = document.createElement('embed');
		with(embd)
		{
			setAttribute('src',processedLink);
			setAttribute('type','application/x-shockwave-flash');
			setAttribute('width','425');
			setAttribute('height','350');
		}

		// put 'em all together
		obj.appendChild(parm);
		obj.appendChild(embd);
		link.parentNode.replaceChild(obj,link);

	} // replace_youtube_link

	// Get the query string of a url
	//
	// @param string - url
	// @return string - get vars
	this.get_query_string = function(uri)
	{
		return uri.substr(uri.indexOf('?')+1);

	} // get_query_string


	// get the key/value pairs of a GET string
	//
	// @param string - GET string
	// @return array - key/value pairs
	this.get_key_value_pairs = function(get_string)
	{
		// break string into pair strings
		var parts = get_string.split(/&/);

		if(parts.length > 0)
		{
			// array for our key/value pairs
			var get_vars = new Array();
			var temp;
			// get key/value pairs
			for(var i = 0; i < parts.length; i++)
			{
				temp = parts[i].split(/=/);
				get_vars[temp[0]] = temp[1];
			}

			return get_vars;
		}
		else
		{
			return false
		}

	} // get_key_value_pairs


	// run it automatically
	this.embed_videos();
}

// attach to page load
window.onload = function()
{
	var emb = new embed_yt_gv();
}