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

///////////////////////////////////////////////////////////////////
//// Global Settings: ( can be over-ridden on individual pages )

var ShowExtra     = 0  ;    //// 0 = off ; 1 = on
var FloatExtra    = 0  ;    //// 0 = off ; 1 = on

var check_browser = 0  ;    //// 0 = off ; 1 = on

var protection    = 0  ;    //// 0 = off ; 1 = on
var browser       = "??" ;  //// string "IE" | "NS"

var home = "http://njscuba.net/" ;
var athome = 0 ;

var Div1, Div2, Div3, Div4 ;
var startY, speed;

var PlayerCount = 1 ;
var nomore = 0 ;

//alert( "check" ) ;

//// location detection

var fn = new String( window.location ) ;
var dr ;

if (( fn.indexOf( "http://127.0.0.1" ) >= 0 
   || fn.indexOf( "localhost" )        >= 0 
   || fn.indexOf( "http://arg" )       >= 0 
   || fn.indexOf( "file://" )          >= 0 )
   && 1 )
  {
  check_browser = 1 ;
  announce_on   = 0 ;
  guest_book_on = 0 ;
  popups        = 0 ;
  protection    = 0 ;
  home          = "http://127.0.0.1/scuba/"  ;
  athome        = 1 ;

  //ga_hide_inline   = true ;  //// true | false -- hide ads
  //ga_hide_sidebar  = true ;  //// true | false -- hide ads

  ////alert("Home") ;
  }

///////////////////////////////////////////////////////////////////
//// set global variables at page load

function DoStuff1()
  {
  BrowserCheck() ; //// aborts page load if necessary
  //ProtectPage() ;
  }

  ///////////////////////////////////////////////////////////////////

function DoStuff2()
  {
  ManageDivs() ;
  Linkd() ;
  }

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

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        = 500 ;
  var width               ;
  var height              ;
  var controlheight = 45  ;
  var type          = "unknown/unknown" ;
  var root          = "/zzz_media/"

  if( athome ) { root = "../zzz_media/" ; }

  if ( browser == "NS" ) { controlheight = 66  ; }
  
  var 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 = 500 ; }
    if ( options.indexOf( "nocontrols" ) >= 0 ) { showcontrols = 0 ; controlheight = 0 ; }
    if ( options.indexOf( "slideshow" )  >= 0 ) { showcontrols = 0 ; controlheight = 0 ; autostart = 1 ; loop = 100000 ; }
    }

  //// 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 ; }

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

  height += controlheight ;
  vurl = home + "zzz_media/" + vurl ;

  //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="StretchToFit"   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
  
  }

///////////////////////////////////////////////////////////////////

function Counter( page )
  {
  //var str = new String( window.location.pathname ) ;
  var str = new String( document.location.href ) ;
  
  //alert( str ) ;

  //// add default filename if necessary
  //// better to just specify it in the file
  if( str.indexOf( "http://" ) >= 0 && str.indexOf( ".html" ) < 0 )
    { str = str + "/index.html" ; }

  //// sniff out file name 
  var temp = new Array();
  temp = str.split("/") ;
  fn = temp.pop() ;
  dr = temp.pop() ;
  if ( dr == "njscuba.net" )
    { dr = "" ; }
  //alert( "&n=[" + dr + "]" + fn  ) ;

  if( athome )
    {
    sPath = "http://njscuba.net/" + str.substring( 23, 100 ) ;
    document.write( "<a href='" + sPath + "' target='_blank' class='a_aqua'>" + "Online version" + "</a><br>" ) ;
    }
  else
    {
    //document.write( "<a target='_self' href='http://njscuba.net/cgi-bin/analyze.php'>" ) ;
    document.write( "<img alt='counters' src='http://69.89.31.132/~njscuban/cgi-bin/counter.pl?n=" ) ;
    if ( page )
      //// override or counter
      { document.write( page ) ; }
    else
      { document.write( "[" + dr + "]" + fn ) ; }
    if ( document.referrer )
      { document.write( "&r=" + escape( document.referrer ) ) ; }
    document.write( "'>" ) ;
    //document.write( "</a>" ) ;
    document.write( "\n" ) ;
    }
  }

///////////////////////////////////////////////////////////////////

function PageDate()
  {
  if ( Date.parse( document.lastModified ) != 0 )
    {
    var modiDate = new Date( document.lastModified ) ;
    var monthName = new Array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ) ;
    document.write( monthName[modiDate.getMonth()] + " " + modiDate.getDate() + ", " + modiDate.getFullYear() ) ;
    }
  }
  
///////////////////////////////////////////////////////////////////

function BrowserCheck()
  {
  //// acceptable browsers:
  //alert ( navigator.appName + " // " + navigator.appVersion  ) ;
  
  if( navigator.userAgent.indexOf(  "Chrome" , 0 ) >= 0 )
    {
    return 0 ;
    window.location.replace( "http://njscuba.net/util/error-other.html" ) ;
    return 1 ;
    } 

  if( navigator.userAgent.indexOf(  "Safari" , 0 ) >= 0 )
    {
    return 0 ;
    window.location.replace( "http://njscuba.net/util/error-other.html" ) ;
    return 1 ;
    } 
    
if( navigator.appName == "Microsoft Internet Explorer" && parseFloat(navigator.appVersion) >= 4.0 )
    {
    if(   ( navigator.userAgent.indexOf(  "MSIE 7" , 0 ) >= 0 )
       || ( navigator.userAgent.indexOf(  "MSIE 8" , 0 ) >= 0 )
       || ( navigator.userAgent.indexOf(  "MSIE 9" , 0 ) >= 0 ) ) //// future?
      {
      browser = "IE" ;
      return 0 ;
      }
    }
    
  if( navigator.appName == "Opera" && parseFloat(navigator.appVersion) >= 7.11 )
    {
    browser = "NS" ;
    return 0 ;
    }

  if( navigator.appName == "Netscape" && parseFloat(navigator.appVersion ) >= 5.0 ) 
    {
    browser = "NS" ;
    return 0 ;
    }

  if( navigator.appName == "Konqueror" && parseFloat(navigator.appVersion ) >= 5.0 ) 
    {
    browser = "NS" ;
    return 0 ;
    }

  if( navigator.appName == "Safari" && parseFloat(navigator.appVersion ) >= 5.0 ) 
    {
    browser = "NS" ;
    return 0 ;
    }

  if ( ! check_browser ) return 0 ;
  //// reject the rest:
  //alert( "This website requires MS Internet Explorer 6.0+ or Netscape 6.0+\n\nPlease upgrade your browser." ) ;
  window.location.replace( "http://njscuba.net/util/error-other.html" ) ;
  return 1 ;
  }

//////////////////////////////////////////////////////////////////
////Manage Layout by divisions

function ManageDivs()
  {

  //alert("start") ;

  startY  = 10   ;    //// vertical starting point for floating divs
  speed   = 10   ;    //// lower = faster, but with performance price

  Div1 = document.getElementById( "Menu" )   ;     
  if ( Div1 != null ) Div1.style.top = startY + "px" ;

  //// float divs only at suitable screen resolutions
  if ( window.screen.availHeight >= 700 ) 
    {
    if ( Div1 != null ) setInterval( "FloatDiv( Div1, startY, speed )" , speed ) ;
    }

  }

var nn=0;

function FloatDiv( Div, startY, speed ) //// Div assumed not null !
  {
  var pY ;
  if( window.document.documentElement.scrollTop ) { pY = parseInt( window.document.documentElement.scrollTop ) ; } //// most browsers
  else { pY = parseInt( window.document.body.scrollTop ) } //// chrome, safari, webkit browsers

  var dY = parseInt( ( pY + startY - parseInt( Div.style.top ) ) / 2.0 ) ;

  Div.style.top = parseInt( Div.style.top ) + dY + "px" ; 

  //// pull up the last pixel if necessary
  if ( parseInt(Div.style.top) - startY < 2 ) { Div.style.top = startY + "px" ; }

  }

///////////////////////////////////////////////////////////////////
//// Disable right-clicking
//// oddball browsers already blocked by BrowserCheck(), so this is simple ...

function click( e )
  {
  if ( browser == "IE" && event.button == 2 )
    {
    if ( confirm( "Reload page ?" ) ) location.reload() ; 
    ////this.location.href = "http://njscuba.net/index.html" ; 
    } 
  else if ( browser == "NS" && e.which == 3 )
    {
    document.onmousedown = new Function ( "return false" ) ;
    ////if ( confirm( "Reload page ?" ) ) location.reload() ; 
    this.location.href = "http://njscuba.net/index.html" ; 
    } 
  }

function ProtectPage()
  {
  if ( ! protection ) return 0 ;
  document.onmousedown = click ; 
  if ( browser == "IE" )
    {
    //// stops text selection - obnoxious ?
    document.onselectstart = new Function ( "return false" ) ;
    }
  else if ( browser == "NS" )
    {
    ////document.captureEvents( Event.MOUSEDOWN ) ;
    }
  }

///////////////////////////////////////////////////////////////////
//// makes a neat drop-down navigation list; self-contained

function MakeSelector()
  {
  fn = new String( window.location ) ;
  ////home = "http://njscuba.net/" ;
  if (( fn.indexOf( "http://127.0.0.1" ) >= 0 
     || fn.indexOf( "http://arg" )       >= 0 
     || fn.indexOf( "file://" )          >= 0 )
     && 1 )
    { home = "http://127.0.0.1/scuba/" ; }
  spacer = "<option>----------------------------------</option>" ;
  document.writeln( "\n\n<p align='center'><font face='Arial' size='2'>" ) ;  
  document.writeln( "<select name='Jumper' size='1' OnChange='JumpTo( this )'>" ) ; 
 
  document.writeln( "<option selected>New Jersey Scuba Diver</option>" ) ;
  document.writeln( spacer ) ;
  document.writeln( "<option value='" + home + "util/frame_.html'          >Home &amp; Search</option>" ) ;
  document.writeln( "<option value='" + home + "util/frame_sites.html'     >Dive Sites</option>"  ) ;
  document.writeln( "<option value='" + home + "util/frame_reefs.html'     >Artificial Reefs</option>"       ) ;
  document.writeln( "<option value='" + home + "util/frame_biology.html'   >Marine Biology</option>" ) ;
  document.writeln( "<option value='" + home + "util/frame_artifacts.html' >Artifacts &amp; Shipwrecks</option>" ) ;
  document.writeln( "<option value='" + home + "util/frame_gear.html'      >Gear &amp; Training</option>"  ) ;
  document.writeln( spacer ) ;
  document.writeln( "<option value='" + home + "index.html'               >Single Screen</option>" ) ;

  document.writeln( "</select></font></p>\n" ) ;
  }

function JumpTo( sel )
  {
  page = sel.options[sel.selectedIndex].value ;
  sel.selectedIndex = 0 ;
  ////alert( page ) ;
  if ( page.indexOf( "http://" ) >= 0 )
    {
    top.location.href = page ;
    }
  }

////  version for message board
function MakeSelector2()
  {
  fn = new String( window.location ) ;
  ////home = "http://njscuba.net/" ;
  spacer = "<option>---------------------------</option>" ;
  document.writeln( "\n\n<p align='center'><font face='Arial' size='2'>" ) ;  
  document.writeln( "<select name='Jumper' size='1' OnChange='JumpTo( this )'>" ) ; 
 
  document.writeln( "<option selected>Back to the main site ...</option>" ) ;
  document.writeln( spacer ) ;
  document.writeln( "<option value='" + home + "index.html'           >Home &amp; Search</option>" ) ;
  document.writeln( spacer ) ;
  document.writeln( "<option value='" + home + "sites/index.html'     >Dive Sites</option>"  ) ;
  document.writeln( "<option value='" + home + "reefs/index.html'     >Artificial Reefs</option>"       ) ;
  document.writeln( "<option value='" + home + "biology/index.html'   >Marine Biology</option>" ) ;
  document.writeln( "<option value='" + home + "artifacts/index.html' >Artifacts &amp; Shipwrecks</option>" ) ;
  document.writeln( "<option value='" + home + "gear/index.html'      >Gear &amp; Training</option>"  ) ;
  document.writeln( spacer ) ;
  document.writeln( "<option value='" + home + "util/contact.html'    >Contact</option>" ) ;
  document.writeln( "<option value='" + home + "guest_book.html'      >Guest Book</option>" ) ;
  document.writeln( "<option value='" + home + "/bb/idex.php'         >Message Board</option>" ) ;
  document.writeln( "<option value='" + home + "util/whats_new.html'  >What's New</option>" ) ;
  document.writeln( "<option value='" + home + "cgi-bin/shuffle.pl'   >Shuffle</option>" ) ;

  document.writeln( "</select></font></p>\n" ) ;
  }

///////////////////////////////////////////////////////////////////
//// check page against w3c

function w3c()
  {
  fn1 = "http://validator.w3.org/check?&charset=%28detect+automatically%29&doctype=Inline&group=0&uri=" + encodeURI( window.location ) ;
  var newWindow = window.open( fn1 , "w3c" ) ;
  newWindow.focus(); 
  return false;
  }

  
///////////////////////////////////////////////////////////////////
//// set link targets for all external links - subvert w3c idiots

function Linkd()
  {
  fn = new String( window.location ) ;
  fn = fn.toLowerCase() ;
  temp = new Array();
  temp = fn.split("/") ;
  base = temp[0] ;
  
  if ( base == "file:" ) {}
  else if ( base == "http:" ) 
    {
    base = base + "//" + temp[2] ;
    }
  else if( fn.indexOf ( ".pdf" ) != -1 ) {}
  else return ;
  if( base.indexOf( "master.com" ) != -1 ) return ;
  for ( i=0 ; i<document.links.length ; i++ )
    {
    if ( 
       ( ( document.links[i].href.indexOf( base ) == -1 )  && ( document.links[i].href.indexOf("javascript") == -1 ) ) || 
       ( document.links[i].href.indexOf(".pdf") != -1 ) ||
       ( document.links[i].href.indexOf(".jpg") != -1 ) ||
       ( document.links[i].href.indexOf(".gif") != -1 ) ||
       ( document.links[i].href.indexOf(".png") != -1 ) ||
       ( document.links[i].href.indexOf(".txt") != -1 )
       )
      {
      document.links[i].target = "njsd" ;
      }
    }
  }