/**
  * RPB Client-Side Functionality
  * Copyright 2007 Research to Prevent Blindness
  * 
  * Produced by The Fund for the City of New York <csnyder@fcny.org>
  */


/**
  * Depends on MochiKit 1.3+
  */

var rpb = { "dirty":false };

// introspect drops a handy property dump into a popup window
rpb.introspect = function( obj ) {
  if ( !obj ) return;
  var result = "introspection of " + String(obj) + " " + obj.id + "<pre>{\n";
  if ( 1 || obj.id ) {
    for (var ix in obj) {
        if ( ix != 'selectionStart' && ix != 'selectionEnd' ) {
          result +=  '.' + ix + " = " + obj[ix] + "\n";
        }
        else {
          result +=  '.' + ix + " = unprintable by introspect() \n";
        }
    }
  }
  
  result = result + "}\n</pre>";
  inspector = window.open("","inspector", "width=420,height=640,resizable,scrollbars");
  if ( !inspector ) {
    alert("Popup windows are blocked!" );
  }
  else {
    inspector.document.writeln( result );
  }
}
introspect = rpb.introspect;

// escapes HTML entities
rpb.h  = function( mystring ) {
  var div = document.createElement( "div" );
  var text = document.createTextNode( mystring );
  div.appendChild( text );
  return div.innerHTML;
}
h = rpb.h;

/**
  * These items are handled by core3.js now
  *
// document dirty/clean for onbeforeunload
rpb.markDirty = function( ) {
  document.dirty = true;
}
rpb.markClean = function( ) {
  document.dirty = false;
}
rpb.checkDirty = function( e ) {
  if ( document.dirty )  {
    return "If you leave this page you will lose the changes you have made.";
  } 
}
window.onbeforeunload = rpb.checkDirty;

// form input double-click and dirty
rpb.initFormFields = function() {
  var inputs = getElementsByTagAndClassName('input',null);
  for( i=0; i<inputs.length; i++) {
    if ( inputs[i].type=='text' ) {
      connect( inputs[i], 'ondblclick', function( e ){ e.target().select() } );
    }
    else if ( inputs[i].type=='submit' || inputs[i].type=='button' ) {
      connect( inputs[i], 'onclick', rpb, 'markClean' );
    }
    connect( inputs[i], 'onchange', rpb, 'markDirty' );
  }
  var tas = getElementsByTagAndClassName('textarea',null);
  for( i=0; i<tas.length; i++) {
    connect( tas[i], 'onchange', rpb, 'markDirty' );
  }
}
connect( window,'ondomload',rpb,'initFormFields' );
 *
**/

// selectron otherselect check
rpb.otherselect = function(obj) {
  var otherid = obj.id+"_other";
  if (obj.value==-2) {
    $( otherid ).style.width = ( obj.offsetWidth - 10 ) + "px";
    $( otherid ).style.display = "block";
    $( otherid ).focus();
  }
  else {
    $( otherid ).style.display = "none";
  }
}

// fill an element with contents of http request
rpb.httpfill = function(url, id) {
  d = doSimpleXMLHttpRequest( url );
  d.addBoth( bind( function(xhr) {
    if ( xhr.readyState==4 ) {
      $(id).innerHTML = xhr.responseText;
    }
  }, id ) );
}
httpfill = rpb.httpfill;
httpFill = rpb.httpfill;


connect( window, 'ondomload', function() {
  var elements = getElementsByTagAndClassName( null, "3d" );
  log("Found 3d elements",elements);
  for(i=0;i<elements.length;i++) {
    elements[i].status = "up"
    connect(elements[i], "onmousedown", function( e ) {
      var el = e.target();
      log("mousedown on",el,"with src",el.src);
      el.src = el.src.substr(0, el.src.length-4) + "-down.gif";
      el.status = "down";
    } );
    connect(elements[i], "onmouseup", function( e ) {
      var el = e.target();
      el.src = el.src.substr(0, el.src.length-9) + ".gif";
      el.status = "up";
    } );
    connect(elements[i], "onmouseout", function( e ) {
      var el = e.target();
      if ( el.status=="down" ) {
        el.src = el.src.substr(0, el.src.length-9) + ".gif";
        el.status = "up";
      }
    } );
  }
} );

// eyemagination popup source code
function isUndefined(v) {
    var undef;
    return v===undef;
}
function raw_popup(url, target, features) {
    if (isUndefined(features)) features = '';
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}
function link_popup(src, features) {
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

// EOF