/***********************VIDEO FUNCTIONS******************************************/

$(function(){ //Function to load video in a new overlay div and play it.
	$("a.FLVvideo").click(function(event) {
		//Prevent Default Click function
		event.preventDefault();
	
		//Go and start video	
		try {
			var EmbedLink = "http://www.youtube.com/embed/" + this.id + "?rel=0&hd=1&autoplay=1";
			var rel = $(this).attr("rel");
			var rel_array = rel.split(";");
			var container = null;
			
			for(x in rel_array){
				var value = rel_array[x].toLowerCase();
				if(value.substr(0,7) == "height:"){
					var Vheight = value.substr(7,value.length);
				}
				if(value.substr(0,6) == "width:"){
					var Vwidth = value.substr(6,value.length);
				}
				if(value.length > 9 && value.substr(0,10) == "container:"){
					container = value.substr(10, value.length);
				}
			}
			var success = $(this).StartEmbed(Vheight, Vwidth, EmbedLink, container);
			if(!success)
				window.open(this.href);
		}
		catch(e){
			window.open(this.href);
			return;
		}
	});
});

jQuery.fn.extend({
	StartEmbed: function(Vheight, Vwidth, VideoLink, container) { // Embed the video
		$("#updateFlashText").remove();
		$("#videoFrame").remove();
		$(".closebutton").remove();
		$("img:hidden").show();
		$(this).find("img").hide();
	
		//Show updateFlashText if no HTML5 supported and flashversion is lower than 10 (YouTube error).		
		var flash = getFlashVersion();
		var video = !!document.createElement('video').canPlayType
		var updateFlashText = "";
		if(!video && flash.split(",")[0] < 10)
			updateFlashText = "<div id='updateFlashText' style='font-weight:bold; font-size:.8em;margin-bottom:10px;'>Kunt u het filmpje niet zien? Update uw flash speler, of <a href='http://get.adobe.com/flashplayer/' title='download flash'>klik hier</a> om een flash speler te installeren.</div>"
		
		var closeButton = "<div class='closebutton' style='text-align:right'><a href='#' title='close'>Klik hier om deze video weer te laten verdwijnen</a></div>";
		var theContainer = "<div id='videoFrame' class='hidden' style='width:" + Vwidth + ";height:" + Vheight + ";margin-bottom:15px;'>";
		var theVideo = "<iframe class='youtube-player' type='text/html' width='" + Vwidth + "' height='" + Vheight + "' src=" + VideoLink + " frameborder='0'></iframe>";

		var theHtml = closeButton + theContainer + theVideo + "</div>" + updateFlashText;
		
		if(container == null)
			$(this).after(theHtml);
		else 
			$("#" + container).append(theHtml);

		$("#videoFrame").fadeIn('fast');
		$(".closebutton").click(function(event) {
			 event.preventDefault();
			$(this).parents().find("img").show();
			$(".closebutton").remove();
			$("#videoFrame").remove();
			$("#updateFlashText").remove();
		});
		
		return true;
	}
});


function getFlashVersion(){ //Check the Flash Version
  // ie
  try {
    try {
      // avoid fp6 minor version lookup issues
      // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
      var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
      try { axo.AllowScriptAccess = 'always'; }
      catch(e) { return '6,0,0'; }
    } catch(e) {}
    return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
  // other browsers
  } catch(e) {
    try {
      if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
        return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
      }
    } catch(e) {}
  }
  return '0,0,0';
}

/***********************END OF VIDEO FUNCTIONS************************************/
        
        
    


