<?php
// (c) 2009 Michael A. Peters - all rights reserved
class embedMedia {
   
// default dimensions are what I like for mp3 playback.
   
public $width='384';
   public 
$height='24';
   public 
$cbarheight='24';
   public 
$autoplay false;
   public 
$autobuffer false;
   public 
$controls true;
   public 
$type 'audio';
   public 
$poster '';
   public 
$fullscreen false;
   
// change these to your environment (extend class)
   
public $fplayer  '/swf/flowplayer-3.1.5.swf';
   public 
$faplayer '/swf/flowplayer.audio-3.1.2.swf';
   
// leave if you don't have commercial
   
public $fpkey    '';
   
// path for download wrapper
   
public $dnlWrap  '';
   
// trigger windows media
   
public $wmpClassID 'clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6';
   
// style sheet hook
   
public $h5class '';
   public 
$fpclass '';
   public 
$wmclass '';
   public 
$fbclass '';

   public function 
embedMedia($dom,$id,$mpg,$ogg="",$wma="") {
      
$this->dom $dom;
      
$this->id  $id;
      
$this->mpg $mpg;
      
$this->ogg $ogg;
      
$this->wma $wma;
      }
      
   public function 
html5() {
      
$fallback $this->hyperLink();
      
$html     $this->html5media();
      
$flash    $this->flowPlay();
      
$flash->appendChild($fallback);
      
$html->appendChild($flash);
      return(
$html);
      }
      
   public function 
flow() {
      
$fallback $this->hyperLink();
      
$html     $this->html5media();
      
$flash    $this->flowPlay();
      
$html->appendChild($fallback);
      
$flash->appendChild($html);
      return(
$flash);
      }
      
   public function 
auto() {
      if (
strlen($this->ogg) > 0) {
         
$return $this->html5();
         } else {
         
$return $this->flow();
         }
      return(
$return);
      }
      
   public function 
windowsMedia() {
      
// not tested - this is here in case your boss demands
      //  Windows Media Player be used as default media player.
      // Hopefully it works "as is" but no promises. Patches welcome
      //  if it be baroque.
      
$startNOIE $this->dom->createComment('[if !IE]>');
      
$endNOIE $this->dom->createComment('<![endif]');
      if (
strlen($this->wma) > 0) {
         
$wmFile $this->wma;
         } else {
         
$wmFile $this->mpg;
         }
      
$wmp $this->dom->createElement('object');
      
$wmp->setAttribute('id','wmp_' $this->id);
      
$wmp->setAttribute('width',$this->width);
      
$wmp->setAttribute('height',$this->height);
      
$wmp->setAttribute('classid',$this->wmpClassID);
      
$wmp->setAttribute('type','application/x-oleobject');
      if (
strlen($this->wmclass) > 0) {
         
$wmp->setAttribute('class',$this->wmclass);
         }
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','FileName');
      
$param->setAttribute('value',$wmFile);
      
$wmp->appendChild($param);
         
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','ShowControls');
      
$saram $this->dom->createElement('param');
      
$saram->setAttribute('name','ShowStatusBar');
      if (
$this->controls) {
         
$param->setAttribute('value','true');
         
$saram->setAttribute('value','true');
         } else {
         
$param->setAttribute('value','false');
         
$saram->setAttribute('value','false');
         }
      
$wmp->appendChild($param);
      
$wmp->appendChild($saram);
         
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','AutoStart');
      if (
$this->autoplay) {
         
$param->setAttribute('value','true');
         } else {
         
$param->setAttribute('value','false');
         }
      
$wmp->appendChild($param);
      
$wmp->appendChild($startNOIE);
      
$nonIE $this->auto();
      
$wmp->appendChild($nonIE);
      
$wmp->appendChild($endNOIE);
      return(
$wmp);
      }

   private function 
html5media() {
      if (
strcmp($this->type,'video') == 0) {
         
$mimeMPG 'video/mp4';
         
$mimeOGG 'video/ogg';
         
$mimeWMA 'video/x-ms-wmv';
         } else {
         
$mimeMPG 'audio/mpeg';
         
$mimeOGG 'audio/ogg';
         
$mimeWMA 'audio/x-ms-wma';
         }
      
$media $this->dom->createElement($this->type);
      
$media->setAttribute('id','h5_' $this->id);
      if (
$this->controls) {
         
$media->setAttribute('controls','controls');
         }
      if (
$this->autoplay) {
         
$media->setAttribute('autoplay','autoplay');
         }
      if (
$this->autobuffer) {
         
$media->setAttribute('autobuffer','autobuffer');
         }
      if (
strcmp($this->type,'video') == 0) {
         
$media->setAttribute('width',$this->width);
         
$media->setAttribute('height',$this->height);
         if (
strlen($this->poster) > 0) {
            
$media->setAttribute('poster',$this->poster);
            }
         } else {
         if (
strlen($this->h5class) == 0) {
            
$media->setAttribute('style','width: ' $this->width 'px;');
            }
         }
      if (
strlen($this->h5class) > 0) {
         
$media->setAttribute('class',$this->h5class);
         }

      
$source $this->dom->createElement('source');
      
$source->setAttribute('src',$this->mpg);
      
$source->setAttribute('type',$mimeMPG);
      
$media->appendChild($source);   
   
      if (
strlen($this->ogg) > 0) {
         
// add ogg source
         
$source $this->dom->createElement('source');
         
$source->setAttribute('src',$this->ogg);
         
$source->setAttribute('type',$mimeOGG);
         
$media->appendChild($source);
         }
      if (
strlen($this->wma) > 0) {
         
// add wma source
         
$source $this->dom->createElement('source');
         
$source->setAttribute('src',$this->wma);
         
$source->setAttribute('type',$mimeWMA);
         
$media->appendChild($source);
         }
      return(
$media);
      }
      
   private function 
flowPlay() {
      
$object $this->dom->createElement('object');
      
$object->setAttribute('id','fp_' $this->id);
      
$object->setAttribute('width',$this->width);
      if (
$this->height $this->cbarheight) {
          
$flowHeight $this->height $this->cbarheight;
          } else {
          
$flowHeight $this->height;
          }
      
$object->setAttribute('height',$flowHeight);
      
$object->setAttribute('data',$this->fplayer);
      
$object->setAttribute('type','application/x-shockwave-flash');
      if (
strlen($this->fpclass) > 0) {
         
$object->setAttribute('class',$this->fpclass);
         }
      
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','movie');
      
$param->setAttribute('value',$this->fplayer);
      
$object->appendChild($param);
      
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','allowfullscreen');
      
$param->setAttribute('value','true');
      
$object->appendChild($param);
      
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','allowscriptaccess');
      
$param->setAttribute('value','always');
      
$object->appendChild($param);
      
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','flashvars');
      
$param->setAttribute('value',$this->buildFlashvars());
      
$object->appendChild($param);
      return(
$object);
      }
      
   private function 
hyperLink() {
      
$fallback $this->dom->createElement('p');
      if (
strlen($this->fbclass) > 0) {
         
$fallback->setAttribute('class',$this->fbclass);
         }
      
$link $this->dom->createElement('a',basename($this->mpg));
      if (
strlen($this->dnlWrap) > 0) {
          
$hyperlink $this->dnlWrap basename($this->mpg);
          } else {
          
$hyperlink $this->mpg;
          }
      
$link->setAttribute('href',$hyperlink);
      
$fallback->appendChild($link);
      if (
strlen($this->ogg) > 0) {
         
$br $this->dom->createElement('br');
         
$fallback->appendChild($br);
         
$link $this->dom->createElement('a',basename($this->ogg));
         if (
strlen($this->dnlWrap) > 0) {
              
$hyperlink $this->dnlWrap basename($this->ogg);
              } else {
              
$hyperlink $this->ogg;
              }
         
$link->setAttribute('href',$hyperlink);
         
$fallback->appendChild($link);
         }
      if (
strlen($this->wma) > 0) {
         
$br $this->dom->createElement('br');
         
$fallback->appendChild($br);
         
$link $this->dom->createElement('a',basename($this->wma));
         if (
strlen($this->dnlWrap) > 0) {
              
$hyperlink $this->dnlWrap basename($this->wma);
              } else {
              
$hyperlink $this->wma;
              }
         
$link->setAttribute('href',$hyperlink);
         
$fallback->appendChild($link);
         }
      return(
$fallback);
      }
      
   private function 
buildFlashvars() {
      
$string "config=";
      if (
strlen($this->fpkey) > 0) {
         
$json['key']=$this->fpkey;
         }
      
// build the playlist
      
if (strlen($this->poster) > 0) {
         
$poster['url']=$this->poster;
         
$playlist[]=$poster;
         }
      
$multimedia['url']=$this->mpg;
      if (! 
$this->autoplay) {
         
$multimedia['autoPlay']=false;
         }
      if (
$this->autobuffer) {
         
$multimedia['autoBuffering']=true;
         }
      
$playlist[]=$multimedia;
      
$json['playlist']=$playlist;
      
// plugins
      
$controls['height']=$this->cbarheight;
      if (
strcmp($this->type,'audio') == 0) {
         
$audio['url'] = $this->faplayer;
         
$plugins['audio']=$audio;
         }
      if (! 
$this->fullscreen) {
         
$controls['fullscreen']=false;
         }
      if (isset(
$controls)) {
         
$plugins['controls']=$controls;
         }
      if (isset(
$plugins)) {
         
$json['plugins']=$plugins;
         }
      
// end of plugins
      
$string .= json_encode($json);
      
$string preg_replace('/"/','\'',$string);
      
$string preg_replace('/\\\\/','',$string);
      
//die($string);
      
return($string);
      }
   } 
// end of class
?>