
// ********************************************
//   Default Values:

DEFAULTIMAGEURL = "";

// Enter the url to your prefered flash player below
var flashPlayer_url = "http://www.davidmeade.com/mediaplayer/mediaplayer.swf";

// If you want your viewers to have to click the play button even after they clicked the image set autoStart =false, otherwise let the movies autostart. 
var autoStart = true;

// Choose when the script should build out the players.
//   buildPlayers = "onClick";   will build out a player when the user clicks a link to the media file.  (Recommended)
//   buildPlayers = "onLoad";   will build out a player for all media links as the page loads. (Note you should really set autoStart = false if you use this option)
var buildPlayers = "onClick";  // set to "onClick" or "onLoad"

// if you want this script to only take action on links that have rel=enclosure, set requireRelEnclosure = true, otherwise leave it at false and the script will give a player to all supported media links.
var requireRelEnclosure = false;

// to size the player based on the size of the still image set the next line to true.  To always set players to a specific size set it to false
 var useImageSize = true;

// If the above is true, set the next to lines to the default size for your players.
 var DEFAULTVIDEOHEIGHT = 240;
 var DEFAULTVIDEOWIDTH = 320;

// displayHelpText allows you to toggle the text message that appears beneath the media players
// this is the text that says something like "The media file should begin automatically.  If you are having problems playing the media file, you can download it here" 
 var displayHelpText = false;

// the line below should be set to false unless you're troubleshooting/hacking
 var debug = false;
 
// ok now stop - change nothing below this line ... or ... bad things happen ....  you have been warned.

 var origLink = new Array();

// ********************************************

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
     obj['e'+type+fn] = fn;
     obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
     obj.attachEvent( 'on'+type, obj[type+fn] );
   } else
     obj.addEventListener( type, fn, false );
}
 
function removeEvent( obj, type, fn ) {
   if ( obj.detachEvent ) {
     obj.detachEvent( 'on'+type, obj[type+fn] );
     obj[type+fn] = null;
   } else
     obj.removeEventListener( type, fn, false );
}

function file_name_only(str) { 
  if (str.match(/\?/)) {
	 endpos = Math.min(str.lastIndexOf('?'), str.length);	
	}
	else {
	 endpos = str.length;
	}

  var slash = '/'
  if (str.match(/\\/)) {
	  slash = '\\'
  }  
  return str.substring(str.lastIndexOf(slash) + 1, endpos)
}

function file_ext_only(str) {
	return str.substring(str.lastIndexOf('.') + 1, str.length)
} 

function getFirstRealChild(parent){
  realFristChild = parent.firstChild;
  while((realFristChild.nodeName.toLowerCase() == 'br') || ((realFristChild.nodeType==3) && (realFristChild.data.replace(/^[\s\n\r]*([^\s\n\r]*?)[\s\n\r]*$/,"") == ""))) {
        realFristChild = realFristChild.nextSibling;
  }
  return realFristChild;
}

function mediaLink() {
  if (debug) {alert('looking for links...'); }
  links = document.getElementsByTagName('a');
  for (i=0;i<links.length;i++) {
	  hasImage = false;
    base_name = file_name_only(links[i].href); 
    file_type = file_ext_only(base_name);
    
    filetypes = ".mov.mp4.m4v.wmv.avi.m4a.wav.au.ogg.mp3.m3u.swf.flv";
    
    if ((file_type) && (filetypes.match(file_type) == file_type)) {
    if (links[i].onclick == undefined || links[i].onclick == null) {
      if (debug) { alert('Found Media Link!\next='+file_type+'\nhref='+links[i].href); }	

      linksRealFristChild = getFirstRealChild(links[i]);
      if ((linksRealFristChild.nodeName.toLowerCase() == 'img') || (linksRealFristChild.nodeName.toLowerCase() == 'image')) {			    if (debug) { alert ('image found!'); }
		// has poster image
		hasImage = true;
		if (useImageSize == true) { 				
		  imageURL = linksRealFristChild.src;
          videoHeight = linksRealFristChild.height;
          videoWidth = linksRealFristChild.width;
		  if (debug) { alert ('width = ' + videoWidth + '\nheight = ' + videoHeight); }
		}				
	  }			
	  else {
	    // has no poster image
		// is text video link
		hasImge = false;
		imageURL = DEFAULTIMAGEURL;
        videoHeight = DEFAULTVIDEOHEIGHT;
        videoWidth = DEFAULTVIDEOWIDTH;			 
	  }
			
      if ( (!requireRelEnclosure) || ((requireRelEnclosure) && (links[i].rel == 'enclosure')) ) {
       if (buildPlayers == "onClick" ) {
        if (hasImage == true) {
  		  links[i].onclick =new Function("showplayer(this," + videoWidth + ", " + videoHeight + ", '" + imageURL + "'); return false;");		
        }
		else {
		  // needs a thickbox or popup event
		  links[i].onclick = new Function("window.open('http://davidmeade.com/media/popup.php?width=" + videoWidth + "&height=" + videoHeight + "&url='+this.href,'video','width=" + (videoWidth + 40) + ",height=" + (videoHeight + 65) + ",top=20,left=20,scrollbars=no,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=yes,left=0,top=0');return false;");	
		}
        if (debug) {alert('Setting new event function:\n\n' + links[i].onclick);}						 
       }
       else if ((buildPlayers == "onLoad" ) && (hasImage == true)) {
        showplayer(links[i], videoWidth, videoHeight, imageURL);
       } 
		 } // end if enclosure
	} // end if doesnt already have an onclick	
    } // end if is filetype
			
  } // end for each link
} // end function


function showplayer(linkobject, videoWidth, videoHeight, imageURL)
{		
 if (debug) {alert('in showplayer function!\nrel='+linkobject.href); }
 
 file_url = linkobject.href;

 base_name = file_name_only(file_url); 
 file_type = file_ext_only(base_name);
 if (debug) { alert ('basename = ' + base_name + '\nfile type = '+file_type); }

 var div = document.createElement("div");
 div.setAttribute("class", "media-window");
 var randnum=Math.floor(Math.random()*1001)
 var playerID = "media-window_" + randnum;
 div.setAttribute("id", playerID); 
 var object = document.createElement("object");
 var embed = document.createElement("embed");

 file_type_name = "media file";

switch (file_type)
{
case 'au':
case 'ogg':
case 'wav':
case 'm4a':
case 'audio/mp4':
case 'mp3':
case 'm3u':
case 'audio/mpeg':
    if (debug) { alert ("Running Audio branch for:\n"+file_url); }			
    object.setAttribute("width", videoWidth);
    object.setAttribute("height", "50");
    
    var param0 = document.createElement("param");
    param0.setAttribute("name", "src");
    param0.setAttribute("value", file_url);
    var param1 = document.createElement("param");
    param1.setAttribute("name", "fileName");
    param1.setAttribute("value", file_url);
    var param2 = document.createElement("param");
    param2.setAttribute("name", "animationatStart");
    param2.setAttribute("value", "true");
    var param3 = document.createElement("param");
    param3.setAttribute("name", "transparentatStart");
    param3.setAttribute("value", "true");
    var param4 = document.createElement("param");
    param4.setAttribute("name", "autoStart");
    param4.setAttribute("value", autoStart);
    var param5 = document.createElement("param");
    param5.setAttribute("name", "showControls");
    param5.setAttribute("value", "true");
    var param6 = document.createElement("param");
    param6.setAttribute("name", "loop");
    param6.setAttribute("value", "false");
    var param7 = document.createElement("param");
    param7.setAttribute("name", "controller");
    param7.setAttribute("value", "true");		
    object.appendChild(param0);
    object.appendChild(param1);
    object.appendChild(param2);
    object.appendChild(param3);
    object.appendChild(param4);
    object.appendChild(param5);
    object.appendChild(param6);
    object.appendChild(param7);			
    embed = document.createElement("embed");
    embed.setAttribute("src", file_url);
    embed.setAttribute("loop", "false");
    embed.setAttribute("autostart", autoStart);
    embed.setAttribute("showcontrols", "true");
    embed.setAttribute("controller", "true");			
    embed.setAttribute("width", videoWidth);
    embed.setAttribute("height", "50");
    type = "audio";
    file_type_name = "audio file";
  break
case 'wmv':
case 'video/x-ms-wmv':
		  if (debug) { alert ("Running WMP branch for:\n"+file_url); }			
			object.setAttribute("classid", "CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95");
			object.setAttribute("codebase", "http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701");
			object.setAttribute("standby", "Loading Microsoft Windows Media Player components...");
			object.setAttribute("type", "application/x-oleobject");
			object.setAttribute("width", videoWidth);
			object.setAttribute("height", videoHeight+45);
			var param1 = document.createElement("param");
			param1.setAttribute("name", "fileName");
			param1.setAttribute("value", file_url);
			var param2 = document.createElement("param");
			param2.setAttribute("name", "animationatStart");
			param2.setAttribute("value", "true");
			var param3 = document.createElement("param");
			param3.setAttribute("name", "transparentatStart");
			param3.setAttribute("value", "true");
			var param4 = document.createElement("param");
			param4.setAttribute("name", "autoStart");
			param4.setAttribute("value", autoStart);
			var param5 = document.createElement("param");
			param5.setAttribute("name", "showControls");
			param5.setAttribute("value", "true");
			var param6 = document.createElement("param");
			param6.setAttribute("name", "loop");
			param6.setAttribute("value", "false");		
			object.appendChild(param1);
			object.appendChild(param2);
			object.appendChild(param3);
			object.appendChild(param4);
			object.appendChild(param5);
			object.appendChild(param6);		
			embed = document.createElement("embed");
			embed.setAttribute("src", file_url);
			embed.setAttribute("type", "application/x-mplayer2");
			embed.setAttribute("loop", "false");
			embed.setAttribute("pluginspage", "http://microsoft.com/windows/mediaplayer/en/download/");
			embed.setAttribute("autostart", autoStart);
			embed.setAttribute("showcontrols", "true");		
			embed.setAttribute("width", videoWidth);
			embed.setAttribute("height", videoHeight+45);
			type = "video";		
 	   file_type_name = "Windows Media File";
  break
case 'flv':
case 'm4v':
case 'mp4':
case 'video/mp4':
		  if (debug) { alert ("Running Flash Video branch for:\n"+file_url); }
	
			heightWcontroller = videoHeight+15;
			flashvars = "file="+file_url+"&backcolor=0xffffff&image="+imageURL+"&autostart="+autoStart+"&link="+file_url+"&showdownload=true&width="+videoWidth+"&height"+heightWcontroller 
			
			object.setAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
			object.setAttribute("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9.0.124.0");

			object.setAttribute("width", videoWidth);
			object.setAttribute("height", heightWcontroller);

			var param1 = document.createElement("param");
			param1.setAttribute("name", "movie");
			param1.setAttribute("value", flashPlayer_url);
			var param2 = document.createElement("param");
			param2.setAttribute("name", "quality");
			param2.setAttribute("value", "high");
			var param3 = document.createElement("param");
			param3.setAttribute("name", "bgcolor");
			param3.setAttribute("value", "#ffffff");		
			var param4 = document.createElement("param");
			param4.setAttribute("name", "allowscriptaccess");
			param4.setAttribute("value", "always");						
			var param5 = document.createElement("param");
			param5.setAttribute("name", "allowfullscreen");
			param5.setAttribute("value", "true");		
			var param6 = document.createElement("param");
			param6.setAttribute("name", "flashvars");
			param6.setAttribute("value", flashvars);						
			object.appendChild(param1);
			object.appendChild(param2);
			object.appendChild(param3);
			object.appendChild(param4);	
			object.appendChild(param5);
			object.appendChild(param6);						
			
			embed = document.createElement("embed");
			embed.setAttribute("src", flashPlayer_url);
			embed.setAttribute("quality", "high");
			embed.setAttribute("bgcolor", "#ffffff");
			embed.setAttribute("type", "application/x-shockwave-flash");			
			embed.setAttribute("pluginspage", "http://www.macromedia.com/go/getflashplayer");
			embed.setAttribute("width", videoWidth);
			embed.setAttribute("height", heightWcontroller);
			embed.setAttribute("allowscriptaccess", "always");
			embed.setAttribute("allowfullscreen", "true");
			
			embed.setAttribute("flashvars", flashvars);
			
			type = "video";
	    file_type_name = "video";
  break	
	
case 'avi':	
case 'mov':
case 'video/quicktime':
		  if (debug) { alert ("Running quicktime branch for:\n"+file_url); }	
			object.setAttribute("classid", "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B");
			object.setAttribute("codebase", "http://www.apple.com/qtactivex/qtplugin.cab");
			object.setAttribute("width", videoWidth);
			object.setAttribute("height", videoHeight+15);

			var param1 = document.createElement("param");
			param1.setAttribute("name", "src");
			param1.setAttribute("value", file_url);
			var param2 = document.createElement("param");
			param2.setAttribute("name", "autoplay");
			param2.setAttribute("value", autoStart);
			var param3 = document.createElement("param");
			param3.setAttribute("name", "controller");
			param3.setAttribute("value", "true");			
			object.appendChild(param1);
			object.appendChild(param2);
			object.appendChild(param3);
			
			embed = document.createElement("embed");
			embed.setAttribute("src", file_url);
			embed.setAttribute("autoplay", autoStart);
			embed.setAttribute("controller", "true");
			embed.setAttribute("pluginspage", "http://www.apple.com/quicktime/download/");
			embed.setAttribute("width", videoWidth);
			embed.setAttribute("height", videoHeight+15);

			type = "video";
	    file_type_name = "QuickTime movie";
  break
default:
  file_type_name = "Unknown";
	type = "Unknown";
  error_message = "No embed code was found for this file type.  Please download the requested media file <a href=\"+file_url+\">here</a>";
  return true;
  break
} 
			
			
	origdiv = document.createElement("div");
	
	smalldiv = document.createElement("div");
	b  = document.createElement("b");
	i  = document.createElement("i");
	br  = document.createElement("br");
	br_two  = document.createElement("br");
	
	manualdownloadlink = 	document.createElement("a");
  manualdownloadlink.setAttribute("href", file_url);
  file_type_text = document.createTextNode(file_type_name);
  manualdownloadlink.appendChild(file_type_text);
	
	autostart_text = document.createTextNode("The "+file_type_name+" should begin automatically.");
	nonautostart_text = document.createTextNode("Click the play button when you are ready.");
  trouble_text = document.createTextNode("If you are having problems playing the "+type+", you can download it here: ");
	
  smalldiv.appendChild(b);
	if (autoStart) {
   b.appendChild(autostart_text);
	}
	else {
   b.appendChild(nonautostart_text);
	}

	closebtn = document.createElement("div");
  closebtn.style.width = videoWidth;
  closebtn.className = "vp_playerCloseButton";
	
  closebtn_text = document.createTextNode("[x] Close video player");
	
  closelink = document.createElement("a");
  closelink.setAttribute("href", "javascript:closePlayer2img('"+randnum+"');");
	closelink.className = "vp_playerCloseButton";
  closelink.appendChild(closebtn_text);
	
	closebtn.appendChild(closelink);
	
  smalldiv.appendChild(br);
  smalldiv.appendChild(i);
  smalldiv.appendChild(br_two);
  i.appendChild(trouble_text);
  smalldiv.appendChild(manualdownloadlink);
  smalldiv.setAttribute("className", "small");
  smalldiv.setAttribute("class", "small");	
  manualdownloadlink.setAttribute("className", "medialink");
  manualdownloadlink.setAttribute("class", "medialink");	
  
 
 div.appendChild(closebtn);
 
 try
  {
      object.appendChild(embed);       
		  div.appendChild(object);
  }
  catch(e)
  {
  div.appendChild(embed);	
  }	
	
	if (displayHelpText) {
  div.appendChild(smalldiv);
	}

	origLink[randnum] = linkobject;  
  linkobject.parentNode.replaceChild(div,linkobject);

  if (window.event) event.returnValue = false;
  return false;			

}

function closePlayer2img (playerID) {
  player = document.getElementById('media-window_'+playerID);
	player.innerHTML = "... working ...";
  player.parentNode.replaceChild(origLink[playerID], player);  
}

addEvent(window, 'load', mediaLink);