var neCategories = new Array();
neCategories["TYPE_TIMEX"] = new Array(
	"TYPE_ERA",
	"TYPE_REL_TIME",
	"TYPE_MONTHOFYEAR",
	"TYPE_MONTHNAME",
	"TYPE_DAY_NAME",
	"TYPE_YEAR_RANGE",
	"TYPE_REL_DATE",
	"TYPE_HOLIDAY_NAME",
	"TYPE_DAYOFYEAR_RANGE",
	"TYPE_DAYOFYEAR",
	"TYPE_SEASON",
	"TYPE_DATE_GENERAL",
	"TYPE_TIMEX",
	"TYPE_TIME",
	"TYPE_DECADE",
	"TYPE_YEAR",
	"TYPE_DATE");
neCategories["TYPE_VALUE_SIMPLE"] = new Array(
	"TYPE_OTHER_COUNT",
	"TYPE_MEASURE_TOKEN",
	"TYPE_VALUE_SIMPLE",
	"TYPE_UNIT",
	"TYPE_LENGTH",
	"TYPE_MEASUREMENT",
	"TYPE_PERSON_COUNT",
	"TYPE_DEGREE",
	"TYPE_NUMERIC",
	"TYPE_AGE",
	"TYPE_ORDINAL_RATIO",
	"TYPE_FTP",
	"TYPE_EMAIL",
	"TYPE_URL",
	"TYPE_PERCENT",
	"TYPE_VOLUME",
	"TYPE_TEMPERATURE",
	"TYPE_ORDINAL_NUM",
	"TYPE_NUMBER",
	"TYPE_MONEY_RANGE",
	"TYPE_COMPUTER",
	"TYPE_PHONE_NUMBER",
	"TYPE_DURATION",
	"TYPE_CONTACT_INFO",
	"TYPE_RATIO",
	"TYPE_HTTP",
	"TYPE_SPEED",
	"TYPE_VELOCITY",
	"TYPE_MONEY",
	"TYPE_COLOR",
	"TYPE_FRACTION",
	"TYPE_RATE",
	"TYPE_AREA",
	"TYPE_PASSPORT_NUM",
	"TYPE_POWER",
	"TYPE_MASS",
	"TYPE_NUMBER_RANGE",
	"TYPE_CURRENCY");
neCategories["TYPE_ORGANIZATION"] = new Array(
	"TYPE_MILITARY_ORG",
	"TYPE_RELIGIOUS_ORG",
	"TYPE_BANK_ORG",
	"TYPE_MEDIA_ORG",
	"TYPE_SPORTS_ORG",
	"TYPE_AIRLINE_ORG",
	"TYPE_SCIENCE_ORG",
	"TYPE_PUBLICATION_ORG",
	"TYPE_NON_GOVT_ORG",
	"TYPE_ORGANIZATION",
	"TYPE_POLITICAL_ORG",
	"TYPE_MEDICAL_ORG",
	"TYPE_TERRORIST_ORG",
	"TYPE_UNION_ORG",
	"TYPE_ARTS_ORG",
	"TYPE_EDUCATIONAL_ORG",
	"TYPE_ENTERTAINMENT_ORG",
	"TYPE_COMMERCIAL_ORG",
	"TYPE_GOVERNMENT_ORG");
neCategories["TYPE_ARTIFACT_THING"] = new Array(
	"TYPE_ARTS_FACILITY",
	"TYPE_MONUMENT_FACILITY",
	"TYPE_SHARP_WEAPON",
	"TYPE_PATH_FACILITY",
	"TYPE_BIOLOGICAL_WEAPON",
	"TYPE_WEAPON",
	"TYPE_SHOOTING_WEAPON",
	"TYPE_PLANT_FACILITY",
	"TYPE_ARTIFACT_THING",
	"TYPE_MEDICAL_FACILITY",
	"TYPE_POWERPLANT_FACILITY",
	"TYPE_NUCLEAR_WEAPON",
	"TYPE_FACILITY",
	"TYPE_BUILDING_FACILITY",
	"TYPE_BRIDGE_FACILITY",
	"TYPE_DEVICE",
	"TYPE_AIRPORT_FACILITY",
	"TYPE_HOUSING_FACILITY",
	"TYPE_PROJECTILE_WEAPON",
	"TYPE_ROADWAY_FACILITY",
	"TYPE_SANCTUARY_FACILITY",
	"TYPE_MISSLE_WEAPON",
	"TYPE_EXPLODING_WEAPON",
	"TYPE_STADIUM_FACILITY",
	"TYPE_BLUNT_WEAPON",
	"TYPE_PORT_FACILITY",
	"TYPE_BOMB_WEAPON",
	"TYPE_CHEMICAL_WEAPON",
	"TYPE_PRISON_FACILITY",
	"TYPE_SUBAREA_FACILITY",
	"TYPE_MISSLE_DEFENSE_WEAPON");
neCategories["TYPE_PERSON"] = new Array(
	"TYPE_PERSON",
	"TYPE_PERSON_ALIAS",
	"TYPE_ARABIC_NAME",
	"TYPE_PERSON_GROUP",
	"TYPE_PERSON_NAME");
neCategories["TYPE_LOCATION"] = new Array(
	"TYPE_STAR",
	"TYPE_MOUNTAIN",
	"TYPE_ADDRESS",
	"TYPE_REGION",
	"TYPE_SPACE_LOCATION",
	"TYPE_RIVER",
	"TYPE_HEMISPHERE",
	"TYPE_PARK",
	"TYPE_REEF",
	"TYPE_SUB_CITY",
	"TYPE_SEA",
	"TYPE_CITY",
	"TYPE_CANYON",
	"TYPE_PLACE",
	"TYPE_BOUNDARY",
	"TYPE_CONTINENT",
	"TYPE_BEACH",
	"TYPE_LAKE",
	"TYPE_BAY",
	"TYPE_WATER_BODY",
	"TYPE_MOON",
	"TYPE_PLANET",
	"TYPE_GULF",
	"TYPE_INTERNATIONAL_REGION",
	"TYPE_LOCATION",
	"TYPE_LAND_REGION",
	"TYPE_CANAL",
	"TYPE_OCEAN",
	"TYPE_PENINSULA",
	"TYPE_ISLAND");
neCategories["TYPE_GPE"] = new Array(
	"TYPE_DISTRICT",
	"TYPE_POPULATION_CENTER",
	"TYPE_SUB_CITY",
	"TYPE_COUNTRY",
	"TYPE_CITY",
	"TYPE_COUNTY",
	"TYPE_GPE_CLUSTER",
	"TYPE_COMPOUND_LOCATION",
	"TYPE_STATE",
	"TYPE_GPE",
	"TYPE_GPE_SPECIAL");

var neStyles = new Array();
neStyles["TYPE_TIMEX"] = new Array( "TYPE_TIMEX", "normal" );
neStyles["TYPE_VALUE_SIMPLE"] = new Array( "TYPE_VALUE_SIMPLE", "normal" );
neStyles["TYPE_ORGANIZATION"] = new Array( "TYPE_ORGANIZATION", "normal" );
neStyles["TYPE_ARTIFACT_THING"] = new Array( "TYPE_ARTIFACT_THING", "normal" );
neStyles["TYPE_PERSON"] = new Array( "TYPE_PERSON", "normal" );
neStyles["TYPE_LOCATION"] = new Array( "TYPE_LOCATION", "normal" );
neStyles["TYPE_GPE"] = new Array( "TYPE_GPE", "normal" );

function loadURL(urlPath) {
  var textarea = document.getElementById("urlEntered");  
  textarea.value = urlPath;
  textarea.select();
  textarea.focus();
}

function clearTextArea(object) {
  var y = document.getElementById(object);  
  y.value = "";
  //y.outerHTML = "<form jwcid=\"@Form\" style=\"margin: 5px; margin-left: 0px;\" id=\"doNERForm\"></form>";
  //y.innerHTML = "";
}

function clearAllForms() {
  for(var i=0; i < document.forms.length; ++i) {
    //document.forms[i].reset();
    for(var j=0; j < document.forms[i].elements.length; ++j) {
      if(document.forms[i].elements[j].type.match("text") ||
          document.forms[i].elements[j].type.match("hidden")) {
        document.forms[i].elements[j].value = "";
      }
      //XXXelse if(document.forms[i].elements[j].type.match("file")) {
      //XXX  document.forms[i].reset();        
      //XXX}
    }
  }
}


function clearDiv(object) {
  var y = document.getElementById(object);
  if(y != null) {
    y.value = "";
    y.innerHTML = "";
  }
}

function disableButton(object) {
  var y = document.getElementById(object);
  y.disabled = true;
}

function enableButton(object) {
  var y = document.getElementById(object);
  y.disabled = false;
}

function printForm(object) {
  var y = document.getElementById(object);
  var attribs = y.attributes;
  alert("method=" + y.method);
  alert("method=" + y.action);
  alert("enctype=" + y.enctype); 
  //<form jwcid="@Form" style="margin: 5px; margin-left: 0px;" id="doNERForm">
  //y.outerHTML = "<form jwcid=\"@Form\" style=\"margin: 5px; margin-left: 0px;\" id=\"doNERForm\"></form>"
  alert("Type="+document.getElementById("passingType").value);
  alert("TextArea innerHTML="+document.getElementById("rawTextArea").innerHTML);
  alert("TextArea value="+document.getElementById("rawTextArea").value);
  var x = document.getElementById("rawTextArea");
  x.innerHTML = x.value;
  alert("TextArea innerHTML="+document.getElementById("rawTextArea").innerHTML);  
    
  return true;
}

function updateForUserInput(objectIDType, objectIDSource, str) { 
  var y = document.getElementById(objectIDSource);
  y.value="";
  y = document.getElementById(objectIDType);
  y.value=str;
}

/*
   Varibles in NERVisit:
   inputType(Hidden, encode=false)
   address(Hidden, encode=false)
   rawText
   NERText
 */
function changeAttribute( objectID, attrib, value){
  var myObject = document.getElementById(objectID);
  var attribs = myObject.attributes;
  var desiredAttrib = attribs.getNamedItem(attrib);
  desiredAttrib.value = value;
}

function changeText( element ){
  if( element.swap ){
    element.swap = false;
    element.checked = false;
    document.getElementById('original').style.display='block';
    document.getElementById('transliterated').style.display='none';
  }
  else{
    element.swap = true;
    document.getElementById('original').style.display='none';
    document.getElementById('transliterated').style.display='block';
  }
}

// toggles color highlighting to next alternative in valueCollection for
// all elements with name entityType (state is stored in activatorId)
// typical call: changeColor( 'TYPE_TEMPORALBox', 'TYPE_DAT' );
function changeColor( activatorId, entityType ){
  // store state on activatorObject (eg the "button") 
  // for each entity type in an map called activatorObject.toggler
  
  // Get the valid styles for this ne span name type
  var stylesCollection = neStyles[ entityType ];
  // Get tye activator object
  var activatorObject = document.getElementById( activatorId );
  // create the state variable on the activator object

  //alert("activatorId: "+activatorId+" entityType: "+entityType);

  if( activatorObject.toggler == null ){
    activatorObject.toggler = 0;
  }
  // change the state
  activatorObject.toggler = (activatorObject.toggler + 1) % stylesCollection.length;
  
  // get the collection of spans that have the name "entityType"
  for( var i=0; i < neCategories[entityType].length; ++i ){
    var myCollection = getElementsByTagAndName("div", neCategories[entityType][i]);
    for( var j=0; j < myCollection.length; ++j ){
      // Change the className for each span to the new state.
      myCollection[j].className = stylesCollection[ activatorObject.toggler ];
    }
  }
}

/** Show/hide subtree */
function flip( id ){
  var obj = document.getElementById(id);
  if( obj.vis ){ 
    obj.className='hidedrop';
    obj.vis=false;
  }
  else{
    obj.className='drop';
    obj.vis=true;
  }
  return false;
}

/** Expand the entity tree. */
function expandAll( name ){
  var objs = getElementsByTagAndName("div", name);
  for( i=0; i<objs.length; ++i ){
    objs[i].className='drop';
    objs[i].vis=true;
  }
}

/** Collapse the entity tree. */
function collapseAll( name ){
  var objs = getElementsByTagAndName("div", name);
  for( i=0; i<objs.length; ++i ){
    objs[i].className='hidedrop';
    objs[i].vis=false;
  }
}

/** linear interpret */
function lerp( low, high, t ){
  return (high - low) * t + low;
}

/** Change a color value. */
function reColor( value ){
  return Math.floor(lerp( value, 0xff, 0.75 )).toString(16);
}

/** Brighten a color by by 75% of the distance to FFFFFF */
function brighten( color ){
  var rend = color.indexOf(",", 0 );
  var gend = color.indexOf(",", rend + 1 );
  var bend = color.indexOf( ")", gend + 1 );
  var r = parseInt( color.substr( 4, rend ) );
  var g = parseInt( color.substr( rend + 1, gend ) );
  var b = parseInt( color.substr( gend + 1, bend ) );
  return '#' + reColor(r) + reColor(g) + reColor(b);
}

/** Get the current style for a specific property. */
function getStyle(el,styleProp){
  if(el.currentStyle){
    return el.currentStyle[styleProp];
  }
  else if(window.getComputedStyle){
    return window.getComputedStyle(el,null).getPropertyValue(styleProp);
  }
  return "";
}

/** highlight the specified object. */
function highlight( obj ){
 if (navigator.appName == 'Microsoft Internet Explorer') {
   obj.style.backgroundColor = "#ffff66";
 }
 else {
   var color = getStyle( obj, 'color' );
   obj.style.backgroundColor = brighten( color );
 }
}

/** highlight all elements with the provided name. */
function highlightAll( name ){
  var objs = getElementsByTagAndName("font", name);
  for( i=0; i<objs.length; ++i ){
    highlight( objs[i] );
  }
}

/** un-highlight an elment. */
function unhighlight( obj ){
  obj.style.backgroundColor='transparent';
}

/** un-highlight all of the entities with the provided name. */
function unhighlightAll( name ){
  var objs = getElementsByTagAndName("font", name);
  for( i=0; i<objs.length; ++i ){
    unhighlight( objs[i] );
  }
}

/** State information about what entities are highlighted. */
var highlightstatename = null;
var highlightobjstate = null;

/** Set the highlight of an entity */
function setHighlight( obj, name ){
  if( highlightobjstate != obj ){
    if( highlightobjstate != null ){
      highlightobjstate.style.backgroundColor = 'transparent';
    }
  } 
  highlightobjstate = obj;
  if( highlightstatename != name ){
    if( highlightstatename != null ){
      unhighlightAll( highlightstatename );
    }
    if( name != null ){
      highlightAll( name );
    }
  }
  highlightstatename = name;
}

/** Jump to an entity in the document view. */
function jumpToEntity( aObj, name ){
  aObj.style.backgroundColor='#CCCCCC';
  var objs = getElementsByTagAndName("font", name);
  if( aObj.index == undefined ){
    aObj.index = 0;
  }
  else{
    do {
      aObj.index = (aObj.index + 1) % objs.length;
      // there are two annotated divs, one for each langauge,
      // only scroll through the entities on the visible div
    }while( (objs[aObj.index]).parentNode.style.display == 'none' );
  }
  objs[aObj.index].scrollIntoView(true);
  setInfo( objs[aObj.index] );
}

/** 
 * Highlight all entities of this type in the document and jump to the current
 * entitie while also setting the details panel.
 */
function fireEntity( obj, name ){
  setHighlight( obj, name );
  jumpToEntity( obj, name );
}

/** Highlight and set the info for a single entity. */
function viewSingle( obj ){
  setHighlight( obj, null );
  highlight( obj );
  setInfo( obj );
}

/** This method replaces all instances of _ with ' ' */
function cleanEType( str ){
  var pos = 0;
  //str = str.substring(5);
  while( (pos = str.indexOf( "_", pos )) > -1 ){
    ++pos;
    str = str.replace("_"," ");
  }
  return str; 
}

/** This method uses information on the obj to set info
 * text in the details panel.
 */
function setInfo( obj ){
  var infoBox = document.getElementById( 'info' );
  if( obj.title != undefined ){
    infoBox.innerHTML = "&nbsp;" + obj.innerHTML + '<br>&nbsp;TYPE: ' + cleanEType(obj.title);
    var extra = obj.getAttribute( "extra" );
    var prettyLat = obj.getAttribute( "prettyLat" );
    var prettyLong = obj.getAttribute( "prettyLong" );
    if( extra != null ){
      if( obj.className == "TYPE_LOCATION" ){
        infoBox.innerHTML += '<br/>&nbsp;Latitude:&nbsp;&nbsp;' + prettyLat;
        infoBox.innerHTML += '<br/>&nbsp;Longitude:&nbsp;' + prettyLong;
        infoBox.innerHTML += '<div id="map" class="map"> </div>';
        var pos = extra.indexOf(",");
        var lat = extra.substring(0, pos);
        var lon = extra.substring(pos+1);
        gmapLoad();
        plotPoint( parseFloat(lat), parseFloat(lon) );
      }
      else if( obj.className = "TYPE_DATE" ){
        infoBox.innerHTML += "<br>" + extra;
      }
    }
  }
  else{
    infoBox.innerHTML = "";
  }
}

/** This method build the xml string for downloading the entitie types and offsets.
 * All of the required information is part of the font wrappers in the document.
 */
function writeXML(){
  var obj = document.getElementById('original');
  document.open("text/plain");
  document.writeln("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  document.write("<Entities>\n");
  for( var i=0; i<obj.childNodes.length; ++i ){
    if( obj.childNodes[i].nodeName == 'FONT' ){
      document.write('  <Entity type="TYPE');
      var eType = obj.childNodes[i].title.split(' ');
      for( var j=0; j<eType.length; ++j ){
        document.write( '_' + eType[j] );
      }
      document.write('" start="');
      document.write( obj.childNodes[i].getAttribute('s') );
      document.write('" end="');
      document.write( obj.childNodes[i].getAttribute('e') );
      document.write('">');
      document.write( obj.childNodes[i].innerHTML );
      document.write("</Entity>\n");
    }
  }
  document.write("</Entities>");
  document.close();
  return true;
}

function getElementsByTagAndName(tag, name) {
  var elem = document.getElementsByTagName(tag);
  var arr = new Array();
  for(i=0, j=0; i < elem.length; i++) {
    att = elem[i].getAttribute("name");
    if(att == name) {
      arr[j] = elem[i];
      j++;
    }
  }
  return arr;
}

/** Right-Click-Menu handling */
/*function rcMenu(evnt){
  var obj = document.getElementById( 'rmenu' );
  if( evnt.button == 2 ){
    obj.style.display='block';
    obj.style.top = evnt.clientY + 'px';
    obj.style.left = evnt.clientX + 'px';
  }
  else{
    obj.style.display = 'none';
  }
  return false;
}*/

/** Catch all mouse clicks. */
/*document.onclick=rcMenu;*/
