///////////////////////////////////////////////////////////////////
//// 
//// 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          = "/images/" ;

  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 = 450 ;}
    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( ".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 ; }

  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
  
  }

///////////////////////////////////////////////////////////////////
//// simple form validator

/*

USAGE:

This validator mis-uses the css class attribute to specify validation requirements.
The class attribute is both "safe" to use in WordPress, and easily accessible in JS.

Sample syntax:

--------------------------------------------------------

<form id="form-id" ... >
<input type="text"     class="valid-required" ... />
<input type="text"     class="valid-email" ... />
<input type="text"     class="valid-phone" ... />
<input type="textarea" class="valid-required" ... ></textarea>
<input type="select"   class="valid-required" ... >
<input type="submit"   onclick="validate('form-id')"... />
</form>

------------------------------------------------------------

The "classes" trigger validation as follows:

valid-required: non-null text value or selection
valid-email:    valid email address
valid-phone:    text min length 10 characters

If none of these classes is specified for a field, the validator simply skips it. These names
will hopefully not collide with any real css classes that may be in use.

The validate function has the form id as a parameter, allowing multiple forms on a page. This is a simple
text string. The function knows nothing about the fields, nor does it need to. All set-up is done in the
html, no programming is required. Error messages are generic, the user is guided with color highlighting.

*/

function validate( frm )
{
emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i ;

theform = document.forms[frm] ;

if ( typeof theform  == "undefined" )
  {  
  alert( "Error - form not found" ) ;
  return false ;
  }

for( i=0 ; i<theform.length ; i++ )
  {
  thefield = theform.elements[i] ;
  ////alert( thefield.type ) ;

  if ( thefield.type == "text" && thefield.className == "valid-required" && thefield.value.length < 1 )
    {
    alert( "Please enter a value" ) ;
    thefield.focus();
    thefield.style.background = "yellow" ;
    return false ;
    }

  if ( thefield.type == "text" && thefield.className == "valid-phone" && thefield.value.length < 10 )
    {
    alert( "Please enter a valid phone number" ) ;
    thefield.focus();
    thefield.style.background = "yellow" ;
    return false ;
    }

  if ( thefield.type == "text" && thefield.className == "valid-email" && ! emailfilter.test(thefield.value) )
    {
    alert( "Please enter a valid email address" ) ;
    thefield.focus();
    thefield.style.background = "yellow" ;
    return false ;
    }

  if( thefield.type == "textarea" && thefield.className == "valid-required" && thefield.value.length < 1 )
    {
    alert( "Please enter a value" ) ;
    thefield.focus();
    thefield.style.background = "yellow" ;
    return false;
    }

  if( thefield.type == "select-one" && thefield.className == "valid-required" && thefield.selectedIndex == 0 )
    {
    alert( "Please make a selection" ) ;
    thefield.focus();
    thefield.style.background = "yellow" ;
    return false;
    }

  if( thefield.type == "radio" && thefield.className == "valid-required" )
    {
    //// tricky, maybe someday
    }

  }

return true ;
}

//alert( "scripts.js" );