

	// ==UserScript==
	// @name                embed
	// @namespace	        http://www.leftfootmedia.nl
	// @description	        Replaces images that link to youtube video's with embedded video's
	// @include				http://www.leftfootmedia.nl/*

	// ==/UserScript==
	
	//Debug option: checking if script started
	//alert('Hello world!');

// Add jQuery, written by Joan Piedra http://joanpiedra.com/jquery/greasemonkey/
	var GM_JQ = document.createElement('script');
    GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
    GM_JQ.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(GM_JQ);

// Check if jQuery's loaded
    function GM_wait() {
        if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
    else { $ = unsafeWindow.jQuery; letsJQuery(); }
    }
    GM_wait();

// All your GM code must be inside this function
    function letsJQuery() {
        //Debug option: check if jQuery works.
		//alert($);
		
		var amount = $("a:has(img)").length;
		//Debug option: check the amount of links on the page
		//alert ("amount of links" + amount);

		for(var x=0; x <= amount; x++){
		//Debug option: Loop number
		//alert("loop nummer: "+x);
		
		//Get content of link
		var link = $("a:has(img):eq("+x+")").attr("href");
		
		//Debug option: Checking link of each A element on page (warining!!! long loop)
		//alert(link + " link");
		
		//Get substring to see is link goes to YouTube video
		var check = link.substr(0,29);
		
		//Debug option: Check the substring of link elements (warining!!! long loop)
		//alert (check + " check");
		
		//If link goes to youtube, replace current element
		if(check == "http://www.youtube.com/watch?"){ 
			var height = $("a:has(img):eq("+x+")").children().attr("height");
			var width = $("a:has(img):eq("+x+")").children().attr("width");
		//Debug option: Check the image height and width
		//alert ("height:" + height + " width:" + width);
		
			var begin_video = link.indexOf('=') +1;
			//alert (link.indexOf('='));
			
			var end_video = link.indexOf('&');
			//alert (end_video);
			
			if(end_video == -1){
				end_video = link.length;
			}
				
			var video = link.substr(begin_video,end_video);
			//alert("video_code" + video);
		
		//Replace current code with youtube embed
		$("a:has(img):eq("+x+")").replaceWith("<object width='"+ width +"' height='"+ height +"'><param name='movie' value='http://www.youtube.com/v/"+video+"&hl=nl&fs=1&'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/"+video+"&hl=nl&fs=1&' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='"+ width +"' height='"+ height +"'></embed></object>");
		}
		 	
	 	
	 
		}
		
    }