/* IE 5.0 does not support the push method, so here goes */
if(Array.prototype.push == null) {
	Array.prototype.push = function(item) {
		this[this.length] = item;
		return this.length;
	};
};

var easy = {
  addEvent: function( obj, type, fn ){
    if (obj.addEventListener) obj.addEventListener( type, fn, false );
    else if (obj.attachEvent) {
      obj["e"+type+fn] = fn;
      obj[type+fn] = function() {
        obj["e"+type+fn]( window.event );
      };
      obj.attachEvent( "on"+type, obj[type+fn] );
    }
  },
  removeEvent: function ( obj, type, fn ) {
    if (obj.removeEventListener) obj.removeEventListener( type, fn, false );
    else if (obj.detachEvent) {
      obj.detachEvent( "on"+type, obj[type+fn] );
      obj[type+fn] = null;
      obj["e"+type+fn] = null;
    }
  },
  // Shaun Inman's smooth scroll
  scrollLoop:      false,
  scrollInterval:  null,
  getWindowHeight: function(){
    if( document.all ){
      return ( document.documentElement.clientHeight ) ? document.documentElement.clientHeight
                                                       : document.body.clientHeight;
    } else {
      return window.innerHeight;
    }
  },
  getScrollLeft:   function(){
    if( document.all ){
      return ( document.documentElement.scrollLeft ) ? document.documentElement.scrollLeft 
                                                     : document.body.scrollLeft;
    } else {
      return window.pageXOffset;
    }
  },
  getScrollTop:    function(){
    if( document.all ){
      return ( document.documentElement.scrollTop ) ? document.documentElement.scrollTop 
                                                    : document.body.scrollTop;
    } else {
      return window.pageYOffset;
    }
  },
  getElementYpos:  function( el ){
    var y = 0;
    while( el.offsetParent ){
      y += el.offsetTop;
      el = el.offsetParent;
    }
    return y;
  },
  to:              function( id ){
    if( this.scrollLoop ){
      clearInterval( this.scrollInterval );
      this.scrollLoop = false;
      this.scrollInterval = null;
    }
    var container = $( 'content' );
    var documentHeight = this.getElementYpos( container ) + container.offsetHeight;
    var windowHeight = this.getWindowHeight();
    var ypos = this.getElementYpos( document.getElementById( id ) );
    if( ypos > documentHeight - windowHeight ) ypos = documentHeight - windowHeight;
    this.scrollTo( 0, ypos );
  },
  scrollTo:        function( x, y ){
    if( this.scrollLoop ){
      var left = this.getScrollLeft();
      var top = this.getScrollTop();
      if( Math.abs( left-x ) <= 1 && Math.abs(top-y) <= 1 ){
        window.scrollTo( x, y );
        clearInterval( this.scrollInterval );
        this.scrollLoop = false;
        this.scrollInterval = null;
      } else {
        window.scrollTo(left+(x-left)/2, top+(y-top)/2);
      }
    } else {
      this.scrollInterval = setInterval( 'easy.scrollTo('+x+','+y+')', 100 );
      this.scrollLoop = true;
    }
  }
};

var trace;
if( typeof( jsTrace ) != 'undefined' ){
  trace = function( msg ){
    jsTrace.send( msg );
  };
} else {
  trace = function( ){ };
}

// sIFR replace statements (for the pretty fonts)
function runMyIFR() {
  if ( sIFR != null &&
       sIFR.replaceElement != null){
    var headings = new Array( '#content h1', '#home caption' );
    for( var i=0; i < headings.length; i++){
      sIFR.replaceElement( headings[i],
                           '/fonts/Edwardian-Script.swf',
                           '#9cb26f', '#ffffff', '#ffffff', '#ffffff',
                           0, 0, 0, 0,
                           '', '', 'transparent' );
    }
    sIFR.replaceElement('#home #content h2',
                        '/fonts/Edwardian-Script.swf',
                        '#9cb26f', '#ffffff', '#ffffff', '#ffffff',
                        0, 0, 0, 0,
                        '', '', 'transparent' );
    sIFR.replaceElement('#extras h3',
                        '/fonts/Edwardian-Script.swf',
                        '#4c5987', '#ffffff', '#ffffff', '#ffffff',
                        0, 0, 0, 0,
                        '', '', 'transparent' );
  }
}
easy.addEvent( window, 'load', runMyIFR );

var listings = {
  table:     null,
  deceased:  null,
  container: null,
  listing:   null,
  loader:    null,
  closer:    null,
  timer:     null,
  uri:       null,
  init:      function(){
    if( !document.getElementById ||
        !document.getElementsByTagName ||
        !$( 'listings' ) ||
        !$( 'deceased' ) ||
        !$( 'container' ) ) return;
    listings.table     = $( 'listings' );
    listings.listing   = $( 'deceased' );
    listings.container = $( 'container' );
    listings.uri       = window.location.protocol+'//'+window.location.host+'/deceased.php';
    listings.makeLoader();
    listings.makeCloser();
    listings.assign();
  },
  makeLoader:function(){
    listings.loader = document.createElement( 'div' );
    var img = document.createElement( 'img' );
        img.setAttribute( 'src', '/images/loading.gif' );
        img.setAttribute( 'alt', 'Loading content, please wait\u2026' );
        img.style.position = 'absolute';
        img.style.top      = '50%';
        img.style.left     = '50%';
        img.style.margin   = '-16px 0 0 -16px;';
    listings.loader.appendChild( img );
  },
  makeCloser:function(){
    listings.closer = document.createElement( 'input' );
    listings.closer.className = 'close';
    listings.closer.setAttribute( 'type', 'button' );
    listings.closer.setAttribute( 'value', 'Close' );
    easy.addEvent( listings.closer, 'click', listings.hide );
  }, 
  assign:    function(){
    var i, j, rows, rowcount, tds, cellcount, a, id;
    rows = listings.table.getElementsByTagName( 'tbody' )[0].getElementsByTagName( 'tr' );
    rowcount = rows.length;
    for( i=0; i < rowcount; i++ ){
      tds = rows[i].getElementsByTagName( 'td' );
      a = tds[0].getElementsByTagName( 'a' )[0];
      a.setAttribute( 'd_id', a.getAttribute( 'href' ).replace( /.*\?w=/, '') );
      a.setAttribute( 'href', '#_' + a.getAttribute( 'd_id' ) );
      easy.addEvent( a, 'click', function(){ listings.show( this.getAttribute( 'd_id' ) ); } );
      cellcount = tds.length;
      for( j=0; j < cellcount; j++ ){
        tds[j].setAttribute( 'd_id', a.getAttribute( 'd_id' ) );
        easy.addEvent( tds[j], 'click', function(){
                                          listings.show( this.getAttribute( 'd_id' ) );
                                        } );
      }
    }
  },
  show:      function( id ){
    trace( 'Populating the deceased' );
    listings.deceased = id;
    var div = listings.listing;
    //div.style.marginTop = '-'+(div.scrollHeight/2)+'px';
    listings.listing.heightFX = new fx.Height( listings.listing, { duration:400 } );
    trace( 'Showing the deceased' );
    listings.listing.heightFX.custom( 0, listings.container.scrollHeight );
    listings.loading();
  },
  hide:      function(){
    trace( 'Hiding the help panel' );
    listings.listing.heightFX.toggle();
  },
  loading:   function(){
    listings.listing.innerHTML = '';
    listings.listing.appendChild( listings.loader );
    listings.listing.appendChild( listings.closer );
    document.getElementsByTagName( 'body' )[0].style.cursor = 'wait';
    listings.timer = setTimeout( 'listings.insert()', 500 );
  },
  insert:   function(){
    clearTimeout( listings.timer );
    var myAjax = new Ajax.Updater( listings.listing,
                                   listings.uri,
                                   { method: 'get',
                                     parameters: 'a=1&w=' + listings.deceased,
                                     onComplete: function(){
                                         listings.listing.appendChild( listings.closer );
                                       } } );
    document.getElementsByTagName( 'body' )[0].style.cursor = 'default';
  }
};
easy.addEvent( window, 'load', listings.init );

function tooltip(){
	if (!document.getElementById('tooltip')) return;
	var tip = document.getElementById('tooltip')
	tip.onclick = function() { window.open(tip.getAttribute('href'),"width=300,height=250,status=yes,resizable=yes,scrollbars=yes"); return false; }
}
easy.addEvent( window, 'load', tooltip );

var styleSwitcher = {
  body:        false,
  form:        false,
  size:        false,
  contrast:    false,
  init:        function(){
    styleSwitcher.body = document.getElementsByTagName( 'body' )[0];
    styleSwitcher.form = $( 'form-switcher' );
    styleSwitcher.size = 'normal';
    styleSwitcher.contrast = 'normal';
    styleSwitcher.getCookie();
    styleSwitcher.handleForm();
  },
  handleForm:  function(){
    var form = styleSwitcher.form;
    var size = styleSwitcher.size;
    var ctst = styleSwitcher.contrast;
    var i;
    var sFlds = $( 'form-size' ).getElementsByTagName( 'input' );
    for( i=sFlds.length-1; i>=0; i-- ){
      easy.addEvent( sFlds[i], 'click', styleSwitcher.switchStyle );
      /* if( sFlds[i].getAttribute( 'value' ) == size ){
        sFlds[i].setAttribute( 'checked', 'checked' );
      } */
    }
    var cFlds = $( 'form-contrast' ).getElementsByTagName( 'input' );
    for( i=cFlds.length-1; i>=0; i-- ){
      easy.addEvent( cFlds[i], 'click', styleSwitcher.switchStyle );
      if( cFlds[i].getAttribute( 'value' ) == ctst ){
        cFlds[i].setAttribute( 'checked', 'checked' );
      }
    }
    var inpts = form.getElementsByTagName( 'input' );
    inpts[ inpts.length-1 ].parentNode.removeChild( inpts[ inpts.length-1 ] );
  },
  switchStyle: function(){
    var type = this.getAttribute( 'name' );
    var val  = this.getAttribute( 'value' );
    var currClass = styleSwitcher.body.className;
    var ptrn;
    if( type == 'size' ){
      ptrn = /[\s]*large(r|st)/;
      styleSwitcher.size = val;
    }else if( type == 'contrast' ){
      ptrn = /[\s]*darke(r|st)/;
      styleSwitcher.contrast = val;
    }
    if( currClass.match( ptrn ) ){
      if( val == 'normal' ){
        currClass = currClass.replace( ptrn, '' );
      } else {
        currClass = currClass.replace( ptrn, ' '+val );
      }
    } else {
      if( val != 'normal' ) currClass = currClass + ' ' + val;
    }
    styleSwitcher.body.className = currClass;
    styleSwitcher.setCookie();
  },
    // cookie handlers
  setCookie:   function(){
    var str = 'size:'+styleSwitcher.size+'&contrast:'+styleSwitcher.contrast
    document.cookie = 'styleSwitcher=' + escape( str );
    trace( document.cookie );
  },
  getCookie:   function(){
    if( !document.cookie ) return;
    var all_cookies = document.cookie;
    var found_at = all_cookies.indexOf('styleSwitcher=');
    if( found_at != -1 ){
      var start = found_at + 'styleSwitcher='.length;
      var end   = all_cookies.indexOf( ';', start );
      var value = ( end != -1 ) ? all_cookies.substring( start, end )
                                : all_cookies.substring( start );
      value = unescape( value );
      var vals = value.split( '&' );
      for( var i=vals.length-1; i>=0; i-- ){
        var pair = vals[i].split( ':' );
        if( pair[0] == 'size' ) styleSwitcher.size = pair[1];
        if( pair[0] == 'contrast' ) styleSwitcher.contrast = pair[1];
      }
    }
  }
};
easy.addEvent( window, 'load', function(){
                                 if( $( 'form-switcher' ) ) styleSwitcher.init();
                               } );