//
// (C) 2006 Lexiteria, all rights reserved.
//

function makeembedplayer(soundFile) {
   var newObject = document.createElement("embed");
   newObject.setAttribute("type", "audio/mp3");
   newObject.setAttribute("src", soundFile);
   newObject.style.width="200px";
   newObject.style.height="20px";
   newObject.style.verticalAlign="middle";
   return newObject;
}

// 	<a style="display: block; text-decoration: underline; font-size: x-small;" href="http://www.alphadictionary.com/sounds/abide.mp3">Click Here if Plugin Doesn't Work.</a>

function makebackuplink(soundFile) {
   var anchor = document.createElement("a");
   anchor.style.display="block";
   anchor.style.textDecoration="underline";
   anchor.style.fontSize="x-small";
   anchor.setAttribute("href", soundFile);
   anchor.appendChild(document.createTextNode("Not Working?  Click Here."));
   return anchor;
}

//<object data="http://www.alphadictionary.com/podcasts/2006-06june/abide.mp3" type="audio/mpeg" style="border: 1px solid black; display: none; height: 20px; width: 200px; vertical-align: middle; margin: 0px auto;"></object>

function makeplayer(soundFile) {
   var newObject = document.createElement("object");
	/* newObject.setAttribute("id", id); */
   newObject.setAttribute("type", "audio/mpeg");
   newObject.setAttribute("data", soundFile);
   newObject.style.width="200px";
   newObject.style.height="20px";
   newObject.style.verticalAlign="middle";
   newObject.style.marginLeft="auto";
   newObject.style.marginRight="auto";
   newObject.style.display="block";
   return newObject;
}

function makesafariplayer(soundFile) {
//   var container = document.createElement("div");
//   container.style.backgroundColor="blue";
//   container.style.width="200px";
//   container.style.height="50px";

   var newObject = document.createElement("object");
   newObject.setAttribute("classid", "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B");
   newObject.setAttribute("codebase", "http://www.apple.com/qtactivex/qtplugin.cab");
   var params = {};
   params["src"] = "/podcasts/QTPlaceholder.mov";
   params["qtsrc"] = soundFile;
   params["controller"] = "true";
   params["autoplay"] = "false";
   params["scale"] = "tofit";
   params["volume"] = "100";
   params["loop"] = "false";
   for(var param in params) {
      var paramtag = document.createElement("param");
      paramtag.setAttribute("name", param);
      paramtag.setAttribute("value", params[param]);
      newObject.appendChild(paramtag);
   }
   newObject.style.margin="0px";
   newObject.style.backgroundColor="black";
   newObject.style.display="block";
   newObject.style.width="200px";
   newObject.style.height="20px";

//   container.appendChild(document.createTextNode("BEGIN"));
//   container.appendChild(newObject);
//   container.appendChild(document.createTextNode("END"));
//   return container;
   return newObject;
}

function showplayer(player, link, display, removelink) {

   if(removelink) {
      link.style.display='none';		
   }
   
   player.style.display=display;
   
   var playerobject = player.getElementsByTagName('object')[0];
   if(playerobject != null) { 
      playerobject.style.display=display; 
   }
   
   playerobject = player.getElementsByTagName('embed')[0];
   if(playerobject != null) { 
      playerobject.style.display=display; 
   }
   
}

function sethearit(link, player, soundFile, display, removelink) {
   if(!link || !player) { return; }

   link.removeAttribute('href');
   link.playertarget = player;

   if (navigator.userAgent.toLowerCase().indexOf("safari") != -1) {       

      link.onclick = function () {	 
	 var player = this.playertarget;
	 if(player == null) return;
	 if(player.getElementsByTagName('object').length > 0) return;
	 var safariplayer = makesafariplayer(soundFile);
	 player.insertBefore(makebackuplink(soundFile), player.firstChild);
	 player.insertBefore(safariplayer, player.firstChild);
	 showplayer(player, this, display, removelink);
      };

   } else if ((navigator.appName == "Microsoft Internet Explorer") &&
	      (navigator.userAgent.toLowerCase().indexOf("opera") == -1)) {

      link.onclick = function () {
	 var player = this.playertarget;
	 if(player == null) return;
	 if(player.getElementsByTagName('embed').length > 0) return;
	 player.insertBefore(makebackuplink(soundFile), player.firstChild);
	 player.insertBefore(makeembedplayer(soundFile), player.firstChild);
	 showplayer(player, this, display, removelink);
      };

   } else {

      link.onclick = function() {
	 var player = this.playertarget;
	 player.insertBefore(makebackuplink(soundFile), player.firstChild);
	 player.insertBefore(makeplayer(soundFile), player.firstChild);
	 showplayer(player, this, display, removelink);
      };

   }
};

