
var onDomReady = function( callback ) {
  var alreadyrunflag = 0;

  if( document.addEventListener )
  {
    document.addEventListener("DOMContentLoaded", function(){
      alreadyrunflag = 1;
      callback();
    }, false );
  }
  else if (document.all && !window.opera)
  {
    document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
    var contentloadtag = document.getElementById("contentloadtag");
    contentloadtag.onreadystatechange = function(){
      if( this.readyState=="complete" ) {
        alreadyrunflag = 1;
        callback();
      }// end if()
    };
  }// end if()

  if(/Safari/i.test(navigator.userAgent)){
    var _timer = setInterval(function(){
      if( /loaded|complete/.test( document.readyState ) )
      {
        clearInterval(_timer);
        alreadyrunflag = 1;
        callback();
      }// end if()
    }, 10);
  }// end if()

  window.onload = function() {
    setTimeout(function(){
      if( ! alreadyrunflag )
        callback();
    }, 0);
  };
};// end onDomReady()

var $ = function(id){return document.getElementById(id)};
var $t = function(tag){return document.getElementsByTagName(tag)};
var $c = function(cls){
  var out = [ ];
  var items = $t("*");
  for( var i = 0; i < items.length; i++ )
    if( items[i].className && items[i].className == cls )
      out.push(items[i]);
  return out;
};
var $cr = function(reg){
  var out = [ ];
  var items = $t("*");
  for( var i = 0; i < items.length; i++ )
    if( items[i].className && reg.test( items[i].className ) )
      out.push(items[i]);
  return out;
};

function getPosition( who )
{
  var o = who;
  var l = o.offsetLeft;
  var t = o.offsetTop;
  while( o = o.offsetParent )
    l += o.offsetLeft;
  o = who;
  while( o = o.offsetParent )
    t += o.offsetTop;
  return [l,t];
}// end getPosition( x )

function show_menu(who, menu)
{
  if( menu_timeout )
    clearTimeout(menu_timeout);

  menu.style.display = "block";
  var pos = getPosition(who);
  var left = pos[0] + who.clientWidth + 2;
  var top  = pos[1];
  menu.style.position = "absolute";
  menu.style.left = left + "px";
  menu.style.top = top + "px";
}// end show_menu(who, menu)


function hide_menu(menu)
{
  menu.style.display = "none";
}// end hide_menu(menu)


function show_lightbox()
{
  black.style.display = "block";
  white.style.display = "block";
  try {
    window.frames['lightbox_iframe'].document.body.innerHTML = "";
  }
  catch(e) {
    // Do nothing:
  }
  
  setTimeout(function() {
    $('lightbox_iframe').src = "/forms/lightbox.asp?record_type_id=" + record_type_id + "&state_id=" + state_id + "&county_id=" + county_id;
    $('lightbox_iframe').scrolling = "no";
    $('lightbox_iframe').frameBorder = "0";
  }, 10);
}// end show_lightbox()


function hide_lightbox()
{
  black.style.display = "none";
  white.style.display = "none";
}// end hide_lightbox()


function reset_county_options()
{
  var record_type_id = $("record_type_id").options[ $("record_type_id").selectedIndex ].value;
  var state_id = $("state_id").options[ $("state_id").selectedIndex ].value;
  
  if( record_type_id == 8 )
  {
    $("county_id").innerHTML = "";
    $("county_id").options[0] = new Option("[ Loading Counties... ]", "");
    var httpOb = new XMLHttpRequest();
    httpOb.open("GET", "/handlers/rp.json.counties?state_id=" + state_id, true);
    httpOb.onreadystatechange = function() {
      if( httpOb.readyState == 4 )
      {
        var c = $("county_id");
        c.innerHTML = "";
        var info = eval(httpOb.responseText);
        for( var i = 0; i < info.length; i++ )
        {
          c.options[ i ] = new Option( info[i].label, info[i].value );
        }// end for()
      }// end if()
    };
    httpOb.send( null );
  }
  else
  {
    $("county_id").innerHTML = "";
    $("county_id").options[0] = new Option("-- All Counties --", "");
  }// end if()
}// end reset_county_options()


/******************** Meat and Potatoes **********************/

var menu_timeout = null;
var black, white, iframe;

onDomReady(function() {
  // Prepare lightbox:
  black = document.createElement("DIV");
  black.id = "lightbox";
  black.className = "black_overlay";
  white = document.createElement("DIV");
  white.className = "white_content";
  white.id = "lightbox_insides";
  $('right').appendChild( black );
  $('right').appendChild( white );
  
  iframe = document.createElement("IFRAME");
  iframe.id = "lightbox_iframe";
  iframe.name = "lightbox_iframe";
  white.appendChild( iframe );

  // Death popup menu:
  $('nav_death_records').onmouseover = function() {
    try {
      hide_menu($('census_popup'));
    }
    catch(e) {
      // Do nothing:
    }
    clearTimeout( menu_timeout );
    show_menu(this, $('cemetery_popup'));
  };
  $('nav_death_records').onmouseout = function() {
    menu_timeout = setTimeout(function() {
      hide_menu( $('cemetery_popup') );
    }, 1000);
  };
  
  $('cemetery_popup_a').onmouseover = function() {
    clearTimeout( menu_timeout );
  };
  $('cemetery_popup_a').onmouseout = function() {
  	menu_timeout = setTimeout(function() {
      hide_menu($('cemetery_popup'));
    }, 500);
  };
  
  $('obituary_popup_a').onmouseover = function() {
    clearTimeout( menu_timeout );
  };
  $('obituary_popup_a').onmouseout = function() {
  	menu_timeout = setTimeout(function() {
      hide_menu($('cemetery_popup'));
    }, 500);
  };
  
  // Genealogy popup menu:
  $('nav_genealogy_records').onmouseover = function() {
    try {
      hide_menu($('cemetery_popup'));
    }
    catch(e) {
      // Do nothing:
    }
    clearTimeout( menu_timeout );
    show_menu(this, $('census_popup'));
  };
  $('nav_genealogy_records').onmouseout = function() {
    menu_timeout = setTimeout(function() {
      hide_menu( $('census_popup') );
    }, 1000);
  };

  $('census_popup_a').onmouseover = function() {
    clearTimeout( menu_timeout );
  };
  $('census_popup_a').onmouseout = function() {
  	menu_timeout = setTimeout(function() {
      hide_menu($('census_popup'));
    }, 500);
  };
  
  // Setup county_id auto-populator:
  if( $("county_id") )
  {
    $("record_type_id").onchange = $("state_id").onchange = reset_county_options;
  }// end if()
});



