var required_files = {};

function require_once(file, target) {
  if (!required_files[file]) {
    if (target == null) {
      document.write("<script type=\"text/javascript\" src=\"" + file + "\"></script>");
    }
    required_files[file] = true;
  }
}

var onload_functions = [];

window.onload = function() {
  for (i = 0; i < onload_functions.length; i++) { onload_functions[i](); }
};

// much code from
// http://www.quirksmode.org/viewport/compatibility.html

document.getWidthHeight = function() {
    var x,y;
    var test1 = this.body.scrollHeight;
    var test2 = this.body.offsetHeight
    if (test1 > test2) // all but Explorer Mac
    {
      x = this.body.scrollWidth;
      y = this.body.scrollHeight;
    }
    else // Explorer Mac;
        //would also work in Explorer 6 Strict, Mozilla and Safari
    {
      x = this.body.offsetWidth;
      y = this.body.offsetHeight;
    }  
    
    return [x, y];
  };

window.getWidthHeight = function() { 
    var clientWidth, clientHeight;
    if (self.innerHeight) // all except Explorer
    {
      clientWidth = self.innerWidth;
      clientHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
      // Explorer 6 Strict Mode
    {
      clientWidth = document.documentElement.clientWidth;
      clientHeight = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
      clientWidth = document.body.clientWidth;
      clientHeight = document.body.clientHeight;
    }
    
    return [ clientWidth, clientHeight ];
};

window.getScrollOffset = function() {
    var x,y;
    if (self.pageYOffset) // all except Explorer
    {
      x = self.pageXOffset;
      y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
      // Explorer 6 Strict
    {
      x = document.documentElement.scrollLeft;
      y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
      x = document.body.scrollLeft;
      y = document.body.scrollTop;
    }  
    
    return [x, y];
  };

function flip_archives() {
  $('archive-list').style.display = ($('archive-list').style.display == "") ? "none" : "";
}

function add_onload_function(code) { onload_functions.push(code); }

add_onload_function(function() {
  $('content').set_optimal_position = function() {
    var width = window.getWidthHeight()[0];
    
    $('content').className = (width < 990) ? "left" : "";
    $('intro').className = (width < 990) ? "wider-intro" : "";
    $('footer-contents').className = (width < 990) ? "footer-no-right-border" : "";
  }

  if (read_cookie("display") == "wide") {
    force_wide_display();  
  } else {
    force_normal_display();
  }
});

function set_window_onresize() {
  window.onresize = $('content').set_optimal_position;
}

function force_wide_display() {
  window.onresize = null;
  
  $('content').className = "full-width";
  $('intro').className = "full-width";
  $('footer-contents').className = "full-width footer-full-width";
  $('display-toggle').innerHTML = "View in normal format";
  $('display-toggle').onclick = function() {
    return force_normal_display();
  };
  
  $('content').style.minHeight = "";
  
  set_cookie("display","wide");
  
  return false;
}

function force_normal_display() {
  set_window_onresize();
  
  $('content').set_optimal_position();
  $('display-toggle').innerHTML = "View in wide format";
  $('display-toggle').onclick = function() {
    return force_wide_display();
  }
  
  $('content').style.minHeight = $('intro').offsetHeight + "px";
  
  set_cookie("display","normal");
  
  return false;
}

function read_cookie(which) {
  var cookies = document.cookie.split("; ");
  for (i = 0; i < cookies.length; i++) {
    parts = cookies[i].split("=");
    if (parts[0] == which) { return parts[1]; }
  }
  return null;
}

function set_cookie(which, value) {
  document.cookie = which + "=" + value;
}

// require everything else we'll need

require_once("/javascripts/FlashEmbed.js");
require_once("/javascripts/Gallery.js");