///////////////////////////////////////////////////////////////////
//// 
//// website utilities - njscuba.net / A.R. Galiano ( argaliano@optonline.com )
////
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//// pre-formatted embedded video player or placeholder
//// plays audio: mp3, wav, wma, mid
//// plays video: wmv, mpg, avi, mov

var PlayerCount = 1 ;

function media_player( vurl, options )
  { 
  //// defaults  
  
  var autostart     = 0   ;
  var stretch       = 1   ;
  var showcontrols  = 1   ;
  var loop          = 0   ;
  var aspectratio   = 4/3 ;
  var vwidth        = 320 ; //// 320 native
  var awidth        = 450 ;
  var width               ;
  var height              ;
  var controlheight = 45  ;
  var type          = "unknown/unknown" ;
  var pid           = "" ;
  var fn            = new String( window.location ) ;
  var home          = "http://jhenryband.com/media/" ;

  if ( fn.indexOf( "jhenryband.com" ) < 0 )
       { home = "d:/seonj/jhenry2/media/" ; }

  plid = "Player" + PlayerCount++ ;
  ////alert( plid ) ;

  //// options override defaults
  if( typeof( options ) != "undefined" )
    {
    if ( options.indexOf( "loop"       ) >= 0 ) { loop         = 100000 ; }
    if ( options.indexOf( "autostart"  ) >= 0 ) { autostart    = 1 ; }
    if ( options.indexOf( "stretch"    ) >= 0 ) { autosize     = 1  ; }
    if ( options.indexOf( "widescreen" ) >= 0 ) { aspectratio  = 16/9  ; vwidth = 480 ;}
    if ( options.indexOf( "nocontrols" ) >= 0 ) { showcontrols = 0 ; controlheight = 0 ; }
    if ( options.indexOf( "slideshow"  ) >= 0 ) { showcontrols = 0 ; controlheight = 0 ; autostart = 1 ; loop = 100000 ; }
    if ( options.indexOf( "wide"       ) >= 0 ) { awidth       = 550 ; }
    }

  //// get mime type
  if     ( typeof( vurl ) == "undefined" ) { vurl = "" ; }
  
  if     ( vurl.indexOf( ".mp3"  ) >= 0 )  { type = "audio/mpeg"      ; width = awidth ; height = 0 ; }
  else if( vurl.indexOf( ".m4a"  ) >= 0 )  { type = "audio/mpeg"      ; width = awidth ; height = 0 ; }
  else if( vurl.indexOf( ".wav"  ) >= 0 )  { type = "audio/x-wav"     ; width = awidth ; height = 0 ; }
  else if( vurl.indexOf( ".wma"  ) >= 0 )  { type = "audio/x-ms-wmv"  ; width = awidth ; height = 0 ; }
  else if( vurl.indexOf( ".mid"  ) >= 0 )  { type = "audio/x-midi"    ; width = awidth ; height = 0 ; }

  else if( vurl.indexOf( ".wmv"  ) >= 0 )  { type = "video/x-ms-wmv"  ; width = vwidth ; height = width / aspectratio ; }
  else if( vurl.indexOf( ".mpg"  ) >= 0 )  { type = "video/mpeg"      ; width = vwidth ; height = width / aspectratio ; }
  else if( vurl.indexOf( ".mpeg" ) >= 0 )  { type = "video/mpeg"      ; width = vwidth ; height = width / aspectratio ; }
  else if( vurl.indexOf( ".avi"  ) >= 0 )  { type = "video/avi"       ; width = vwidth ; height = width / aspectratio ; }
  else if( vurl.indexOf( ".mov"  ) >= 0 )  { type = "video/quicktime" ; width = vwidth ; height = width / aspectratio ; }

  type = "application/x-ms-wmp" ;

  vurl = home + vurl ;
  height += controlheight ;

  //document.writeln( '<textarea style="width:500px;height:500px;">' ) ; //// sanity check
  document.writeln( '<object ' ) ;
  document.writeln( 'id="' + plid + '" ' ) ;
  document.writeln( ' type="' + type + '" ' ) ;
  //document.writeln( ' data="' + vurl + '" ' ) ; //// preload! performance impact on slow conxns
  if( navigator.userAgent.indexOf("Firefox") == -1 ) document.writeln( ' classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ' ) ;
  document.writeln( ' width="' + width + '" height="' + height + '" ') ;
  document.writeln( ' >' ) ;
  document.writeln( '<param name="src"            value="' + vurl         + '" />' ) ;
  document.writeln( '<param name="autostart"      value="' + autostart    + '" />' ) ;
  document.writeln( '<param name="autoplay"       value="' + autostart    + '" />' ) ;
  document.writeln( '<param name="animationstart" value="' + autostart    + '" />' ) ;
  document.writeln( '<param name="loop"           value="' + loop         + '" />' ) ;
  document.writeln( '<param name="showcontrols"   value="' + showcontrols + '" />' ) ;
  document.writeln( '<param name="controller"     value="' + showcontrols + '" />' ) ;
  document.writeln( '<param name="autosize"       value="' + stretch      + '" />' ) ;
  document.writeln( '<param name="visible"        value="1" />' ) ;
  document.writeln( '<param name="showdisplay"    value="0" />' ) ;
  document.writeln( '<param name="showstatusbar"  value="0" />' ) ;
  document.writeln( '</object>' ) ;
  //// sanity checks:
  //document.writeln( '</textarea>' ) ;
  //alert ( navigator.userAgent ) ;
  //document.writeln( "<p><a href='" + vurl + "'>" + vurl + "</a></p>" ) ; //// debug
  
  }
