// Q:  Why do you keep all your code in a separate "secret" javascript
//     file?  

// A1: We keep all of our function definitions here so that when
//     we want to add new functionality, we can do it here without people
//     needing/having to change their web pages.
// A2: Also, it takes some of the load off of your web server.

// Q:  What if I don't trust you guys, and instead just paste all this
//     code directly into my pages myself?
// A:  Go right ahead.

// Here's where create a mess of variables.
var sextant_ct_hours = 24*365*100; var sextant_bargraph_height = 10; 
var sextant_bargraph_width = 40; var sextant_bargraph_other_html_options = "border=0";
var sextant_bargraph_bg = "FFFFCC"; var sextant_bargraph_fg = "0000FF";
var sextant_bargraph_min = 0; var sextant_bargraph_max = 7; 
var sextant_bargraph_map = "linear"; var sextant_ct_days = 365*100;
var sextant_from_url = document.URL;

var sextant_clickthru_logger_url = "http://sextant.civilution.com:8095/sx/clickthru-logger/"
var sextant_pageload_logger_url = "http://sextant.civilution.com:8095/sx/pageload-logger/"
var sextant_grapher_url = "http://sextant.civilution.com:8095/sx/comments-grapher/"
var sextant_discussion_url = "http://sextant.civilution.com:8095/sx/discussion-entry/"

var sextant_image_number_list = new Array()
var sextant_image_src_list = new Array()

// And now the functions
function num2hex(n) {
  if (n < 10) return String.fromCharCode(n+48)
  else return String.fromCharCode(n+55)
}

function urlencode(s) {
  answer = ""
  for (i=0; i < s.length; i++) {
    char_here = s.charAt(i)
    unicode_here = s.charCodeAt(i)
    if ( (unicode_here > 47) && (unicode_here < 58) ) answer = answer + char_here
    else if ( (unicode_here > 64) && (unicode_here < 91)) answer = answer + char_here
    else if ( (unicode_here > 96) && (unicode_here < 123)) answer = answer + char_here
    // else it's not an alphanumeric, so encode it
    else {
      right_digit = unicode_here & 0x000f
      left_digit = (unicode_here & 0x00f0) >> 4
      right_hex = num2hex(right_digit)
      left_hex = num2hex(left_digit)
      answer = answer + "%" + left_hex + right_hex
    }
  }
  return answer
}

function sextant_wait(milsecs) {
  var startdate = new Date()
  var starttime = startdate.getTime()

  var loopdate = new Date()
  var looptime = loopdate.getTime()
  while (looptime - starttime < milsecs) {
    var loopdate = new Date()
    var looptime = loopdate.getTime()
  }
}

function add_url_param(url_so_far, param_name, param_val) {
  return url_so_far + param_name + param_val + param_name + "/"
}

var have_gotten_offgraphcache = 0
var offgraphcache = new Image()

function sx_loglink() {
  var this_from_url = sextant_from_url
  var this_to_url = document.links[document.links.length - 1].href

  var this_real_from_url = document.URL
  if (this_real_from_url == this_from_url) {
	this_real_from_url = "(same)"
  }

  var encoded_this_from_url = urlencode(this_from_url)
  var encoded_this_to_url = urlencode(this_to_url)
  var encoded_this_real_from_url = urlencode(this_real_from_url)

  var msg_src = sextant_clickthru_logger_url
  msg_src = add_url_param(msg_src, "from_url", encoded_this_from_url)
  msg_src = add_url_param(msg_src, "to_url", encoded_this_to_url)
  msg_src = add_url_param(msg_src, "realfromurl", encoded_this_real_from_url)

  var f_string = ""
  f_string = f_string + "var loaddate = new Date(); var loadtime = loaddate.getTime();"
  f_string = f_string + "var message_img = new Image;"
  f_string = f_string + "message_img.src = '" + msg_src
  f_string = f_string + "' + 'end' + loadtime;" 
  f_string = f_string + "sextant_wait(300);"

  // Enable that new onclick function.
  document.links[document.links.length - 1].onclick = new Function("none", f_string)
}

function sx_internalforumlink(linktext, faketitle) {
  var this_from_url = sextant_from_url
  var this_to_url = document.links[document.links.length - 1].href

  var this_real_from_url = document.URL
  if (this_real_from_url == this_from_url) {
	this_real_from_url = "(same)"
  }
	
  // First, we're going to construct the target url for the link to the internal forum.
  var encoded_this_from_url = urlencode(this_from_url)
  var encoded_faketitle = urlencode(faketitle)
  var encoded_this_real_from_url = urlencode(this_real_from_url)

  var discuss_url = sextant_discussion_url
  discuss_url = add_url_param(discuss_url, "from_url", encoded_this_from_url)
  discuss_url = add_url_param(discuss_url, "to_url", encoded_faketitle)
  discuss_url = add_url_param(discuss_url, "realfromurl", encoded_this_real_from_url)
  discuss_url = add_url_param(discuss_url, "internal_p", "t")

  // Okay, now we've got our target url in link_url.  Great.  Now we write the
  // link onto the page.
  document.write('<a href = "' + discuss_url + '">')
  document.write(linktext)
  document.write('</a>')
}


function sx_graphlink() {
  // The supported optional "manual-override" parameters now are from_url and to_url,
  // ie. graphlink_for_latest_link('from_url = "before-archive.html" to_url = "target.html"')
  // It's possible to send in an optional paramter that's the "to" url for the bargraph.
  //
  // Normally, you don't need to set either of these.  The from_url defaults
  // to what's set above in the parameters section, and the to_url defaults to
  // to href in the most recent link definition.

  var this_from_url = sextant_from_url
  var this_to_url = document.links[document.links.length - 1].href

  var this_real_from_url = document.URL
  if (this_real_from_url == this_from_url) {
	this_real_from_url = "(same)"
  }

  // THIS IS MY BIG PROBLEM.
  if (have_gotten_offgraphcache == 0) {
    offgraphcache.src = img_src_for_new_bargraph("notthere", "notthere", "notthere")
    have_gotten_offgraphcache = 1
  }

  var encoded_this_from_url = urlencode(this_from_url)
  var encoded_this_to_url = urlencode(this_to_url)
  var encoded_this_real_from_url = urlencode(this_real_from_url)

  var msg_src = sextant_clickthru_logger_url
  msg_src = add_url_param(msg_src, "from_url", encoded_this_from_url)
  msg_src = add_url_param(msg_src, "to_url", encoded_this_to_url)
  msg_src = add_url_param(msg_src, "realfromurl", encoded_this_real_from_url)

  var f_string = ""
  f_string = f_string + "var loaddate = new Date(); var loadtime = loaddate.getTime();"
  f_string = f_string + "var message_img = new Image;"
  f_string = f_string + "message_img.src = '" + msg_src
  f_string = f_string + "' + 'end' + loadtime;" 
  f_string = f_string + "sextant_wait(300);"

  // Enable that new onclick function.
  document.links[document.links.length - 1].onclick = new Function("none", f_string)

  var discuss_url = sextant_discussion_url
  discuss_url = add_url_param(discuss_url, "from_url", encoded_this_from_url)
  discuss_url = add_url_param(discuss_url, "to_url", encoded_this_to_url)
  discuss_url = add_url_param(discuss_url, "realfromurl", encoded_this_real_from_url)

  document.write('<a href = "' + discuss_url + '">')
  document.write('<img width=' + sextant_bargraph_width + ' ')
  document.write('height=' + sextant_bargraph_height + ' ')
  document.write('src="' + offgraphcache.src + '" ')
  document.write(sextant_bargraph_other_html_options + '>')
  document.write('</a>')

  var next_image_index = sextant_image_number_list.length
  sextant_image_number_list[next_image_index] = document.images.length - 1
  sextant_image_src_list[next_image_index] = img_src_for_new_bargraph(encoded_this_from_url, encoded_this_to_url, encoded_this_real_from_url)
}

function send_pageload_message() {
  var this_from_url = sextant_from_url;
  var encoded_this_from_url = urlencode(this_from_url)

  var msg_src = sextant_pageload_logger_url  
  msg_src = add_url_param(msg_src, "url", encoded_this_from_url)

  var pageload_img = new Image()
  pageload_img.src = msg_src
}

function img_src_for_new_bargraph(encoded_from_url, encoded_to_url, encoded_real_from_url) {
  var sdate = new Date()
  var gmils = sdate.getTime()
  var new_src = sextant_grapher_url 
  new_src = add_url_param(new_src, "from_url", encoded_from_url)
  new_src = add_url_param(new_src, "to_url", encoded_to_url)
  new_src = add_url_param(new_src, "realfromurl", encoded_real_from_url)
  new_src = add_url_param(new_src, "wd", sextant_bargraph_width)
  new_src = add_url_param(new_src, "ht", sextant_bargraph_height)
  new_src = add_url_param(new_src, "rand", gmils)
  new_src = add_url_param(new_src, "bg", sextant_bargraph_bg)
  new_src = add_url_param(new_src, "fg", sextant_bargraph_fg)
  new_src = add_url_param(new_src, "min", sextant_bargraph_min)
  new_src = add_url_param(new_src, "max", sextant_bargraph_max)
  new_src = add_url_param(new_src, "map", sextant_bargraph_map)

  return new_src
}

// Netscape needs both the onload and onresize functions specifically defined,
//    which is what's going on right here.
// IE, on the other hand, seems to only need the onload function to be defined.
// When we define the onresize function for IE by putting
//    'onResize="sextant_onresize()"' in the body tag, it just wrecks those IE's
//    that have dynamic-insta-resize turned on.  So we don't do it.
function sextant_onload() {
   for (i=0; i < sextant_image_number_list.length; i++) {
      document.images[sextant_image_number_list[i]].src = sextant_image_src_list[i]
   }
   send_pageload_message()
}

function sextant_onresize() {
   for (i=0; i < sextant_image_number_list.length; i++) {
      document.images[sextant_image_number_list[i]].src = sextant_image_src_list[i]
   }
   // send_pageload_message()
}


window.onLoad = sextant_onload;
window.onResize = sextant_onresize;
