//=======================================
//== Common =============================
//=======================================
var documentLoadedTimestamp;
var millisecondsSinceDocumentLoaded;

/// This function is called by the <body> at the onLoad event
/// It should contain all initialization of the javascript clock
/// the parameter timestamp is the number of seconds since the nebulaEpoch
function initializeClock( timestamp ){
  documentLoadedTimestamp = timestamp;
  millisecondsSinceDocumentLoaded = 0;
  // initialize the stardate clock
  renderClock();
  setInterval("updateClock()", 605 );
}
//---------------------------------------
/// Stardate clock code
function updateClock(){
  millisecondsSinceDocumentLoaded = millisecondsSinceDocumentLoaded + 605;
  renderClock();
}
//---------------------------------------
/// Stardate clock code
function renderClock(){
  var time = documentLoadedTimestamp + millisecondsSinceDocumentLoaded/1000;
  var cronons = Math.round( time / 604800 );
  var millicronons = Math.round( (time / 604.8) % 1000 );
  var microcronons = Math.round( (time / 0.6048) % 1000 );
  var stardate = cronons +"."+ millicronons +"."+ microcronons;
  document.getElementById("clock").firstChild.nodeValue = stardate;
}
//---------------------------------------
/// Opens a browser popup window
function popupWindow(url,w,h){
  layout = 'height='+h+',width='+w+',left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes';
  popupWindow = window.open(url,'popUpWindow',layout);
}
//---------------------------------------
/// command is formatted as a "|"-separated list of commands
/// mode:radar|mine:127|message:murtlest,Hello there!
function execute(command){
  document.controlform.command.value = command;
  document.controlform.submit();
}
//---------------------------------------
function encodeCommand(str){
  str = str.replace(/#/g, "#s");
  str = str.replace(/,/g, "#c");
  str = str.replace(/:/g, "#k");
  str = str.replace(/\|/g,"#p");
  str = str.replace(/\;/g,"#e");
  return str;
}
//---------------------------------------
/// Returns the character of a pressed key. Accepts a triggering keyEvent object as parameter.
function pressedKey(e){
  var keynum = (window.event ? e.keyCode : e.which) ;//IE or FF
  return String.fromCharCode(keynum);
}
//---------------------------------------
function autoComplete(e){
  if( /\t/.test( pressedKey(e) ) ){
    
    var string = document.customCommandForm.customCommand.value;
    var commands = [
      "add_player: ",
      "c",
      "close_msg: ",
      "create_",
      "entity_position: ",
      "give_",
      "give_energy: ",
      "give_item: ",
      "install_subsystem: ",
      "jettison_cargo: ",
      "log",
      "login: ",
      "logout",
      "m",
      "mode: ",
      "mode: admin_",
      "mode: admin_info",
      "mode: admin_map",
      "mode: admin_scenario",
      "mine: ",
      "my_",
      "my_avatar: ",
      "my_password: ",
      "player_",
      "player_avatar: ",
      "player_info: ",
      "player_p",
      "player_password: ",
      "player_privilege: ",
      "remove_player: ",
      "quest_",
      "quest_add_",
      "quest_add_goal_",
      "quest_add_event_",
      "send_msg: ",
      "travel: ",
      "uninstall_subsystem: "
    ];
    
    for( i in commands ){
      var pattern = new RegExp( "^"+string );
      if( pattern.test( commands[i] ) ){
        document.customCommandForm.customCommand.value = commands[i];
        break;
      }
    }
    
    return false;
  }
  return true;
}

//=======================================
//== Radar ==============================
//=======================================
var selectedEntity = -1;
//---------------------------------------
function mouseOverEntity(id){
  toggleDiv("highlight-"+id,1);
}
//---------------------------------------
function mouseOutEntity(id){
  if(id != selectedEntity)
    toggleDiv("highlight-"+id,0);
}
//---------------------------------------
function clickEntity(id){
  if(selectedEntity != -1){
    toggleDiv("highlight-"+selectedEntity,0);
    toggleDiv("hud-"+selectedEntity,0);
    toggleDiv("buttons-"+selectedEntity,0);
  }
  
  if(selectedEntity == id){
    selectedEntity = -1;
    toggleDiv("highlight-"+id,1);
  } 
  else {
    selectedEntity = id;
    toggleDiv("highlight-"+id,1);
    toggleDiv("hud-"+id,1);
    toggleDiv("buttons-"+id,1);
  }
}
//---------------------------------------
function toggleDiv(divId, iState){
  //http://www.geocities.com/technofundo/tech/js/showhide.html
  if(document.layers){ // NN4
    document.layers[divId].visibility = iState ? "show" : "hide";
  }
  else if(document.getElementById){ //IE5 + NN6
    var obj = document.getElementById(divId);
    obj.style.visibility = iState ? "visible" : "hidden";
  }
  else if(document.all){ // IE 4
    document.all[divId].style.visibility = iState ? "visible" : "hidden";
  }
}

//=======================================
//== Vessel =============================
//=======================================
var selectedItem = -1;
//---------------------------------------
/// Shows the context menu associated with an item in the Vessel screen.
function clickItem(id){
  if(selectedItem != -1){
    toggleDiv("item-context-menu-"+selectedItem,0);
  }
  toggleDiv("item-context-menu-"+id,1);
  selectedItem = id;
}
//---------------------------------------
/// Hides the context menu associated with an item in the Vessel screen.
function closeItem(id){
  toggleDiv("item-context-menu-"+id,0);
  selectedItem = -1;
}

//=======================================
//== Actions ============================
//=======================================
//---------------------------------------
function placeBid(id,name,price){
  if( confirm("Press OK to place a bid of "+price+" credits on the "+name+".") ){
    document.controlform.action.value = "place_bid";
    document.controlform.id.value = id;
    document.controlform.submit();
  }
}
//---------------------------------------
function buyout(id,name,price){
  if( confirm("Press OK to immediately buy the "+name+" for "+price+" credits.") ){
    document.controlform.action.value = "buyout";
    document.controlform.id.value = id;
    document.controlform.submit();
  }
}
//---------------------------------------