
_olfs = new Array(); // Array of onload functions
function onloadAdd(func) { _olfs[_olfs.length] = func; }
function onloadExec() { for (var i = 0; i < _olfs.length; i++) eval(_olfs[i]); }

window.onload = function() { onloadExec(); }

//
// Navigation
//

NavButton = function(n, c) {
  var ref = this
  this.itemName = n;
  this.container = c;
  this.main = document.getElementById('nav_' + this.itemName);
  this.subNav = document.getElementById('subNav_' + this.itemName);

  if (!this.main || !this.subNav) return;

  // if the page has no nav bar this.main will be empty
  if (this.main != null) {
    if (this.main.className == 'selected') this.container.defaultSelected = this;

    this.main.onmouseover = function () {
       clearTimeout(ref.container.timer);
       ref.container.active = true;
       ref.select();
    }

    this.main.onmouseout = function () {
       ref.container.active = false;
       // Adjust timeout here
       ref.container.timer = setTimeout(function() { if (ref.container.active == false) { ref.container.returnToDefault(); } }, 1500 );
    }

    this.select = function() {
       clearTimeout(ref.container.timer);
       ref.container.active = true;
       ref.container.turnAllOff();
       ref.main.className = 'selected';
    }
  }
}

// Swaps the background image for the top nav line and turns on that items subnavigation
Navigation = function () {
  var ref = this;
  this.defaultSelected = null;
  (document.getElementById('welcomeText') != null) ? this.welcomeText = document.getElementById('welcomeText'): this.welcomeText = false;

  //Add new nav nodes here
  this.navButtons = new Array();
  this.navButtons.push(new NavButton('phones', this));
  this.navButtons.push(new NavButton('rates', this));
  this.navButtons.push(new NavButton('stuff', this));
  this.navButtons.push(new NavButton('life', this));
  this.navButtons.push(new NavButton('help', this));
  this.navButtons.push(new NavButton('myaccount', this));

  this.timer = null
  this.active = new Boolean();

  this.turnAllOff = function () {
    for (var i = 0; i < ref.navButtons.length; i++) ref.navButtons[i].main.className = 'unselected';
  }

  this.returnToDefault = function () {
    ref.turnAllOff();
    if (ref.defaultSelected != null) ref.defaultSelected.main.className = 'selected';
  }
}

//
// Image Mouseovers
//

// This array must be maintained
var APPLY_MOUSEOVER_TO = new Array();
APPLY_MOUSEOVER_TO.push('bhv_button');
APPLY_MOUSEOVER_TO.push('bhv_clown');
APPLY_MOUSEOVER_TO.push('bhv_phonePreviewImage');

ImageMouseover = function(i) {
  var ref = this;
  this.imgobj = i;

  this.outSrc = this.imgobj.src;
  this.overSrc = this.imgobj.src.substr(0, this.imgobj.src.lastIndexOf(".")) + '_on' + this.imgobj.src.substr(this.imgobj.src.lastIndexOf("."), 4);

  this.doOver = function () { ref.imgobj.src = ref.overSrc; }
  this.doOut = function () { ref.imgobj.src = ref.outSrc; }
  this.removeMouseovers = function () { ref.imgobj.onmouseover = null; ref.imgobj.onmouseout = null; }
  this.addMouseovers = function ()  { ref.imgobj.onmouseover = this.doOver; ref.imgobj.onmouseout = this.doOut; }

  this.addMouseovers();
}

// This function looks for all elements that have one of the APPLY_MOUSEOVER values specified at the top.
// If one is found it's mouseover functionality is added. Assumes an "on" image like this: imagename_on.ext
ImageMouseovers = function() {
  var images = document.getElementsByTagName('IMG');
  var inputs = document.getElementsByTagName('INPUT');

  for (var i = 0; i<images.length; i++) {
    for (var j =0; j < APPLY_MOUSEOVER_TO.length; j++) {
      if (images[i].className.indexOf(APPLY_MOUSEOVER_TO[j]) > -1) images[i].obj = new ImageMouseover(images[i]);
    }
  }
  for (var i = 0; i<inputs.length; i++) {
    for (var j =0; j < APPLY_MOUSEOVER_TO.length; j++) {
      if (inputs[i].className.indexOf(APPLY_MOUSEOVER_TO[j]) > -1) inputs[i].obj = new ImageMouseover(inputs[i]);
    }
  }
}

//
// Popups
//

function popupAccessory(url) {
  var win = window.open(url, "Accessory", "resizable=no,scrollbars=yes,location=no,toolbar=no,menubar=no,status=no,width=612,height=390");
}

function ShipPop(URL) {
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=554,height=600,left =0,top =0');");
}

function displayMap(mapUrl) {
  var newWindow = window.open(mapUrl, "map", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=410');
}

function previewAnimation(url) {
  var win = window.open(url, "previewAnimation", "resizable=no,scrollbars=no,location=no,toolbar=no,menubar=no,status=no,width=128,height=132");
}

function popupPicture(url) {
  var win = window.open(url, "ViewPicture", "resizable=yes,scrollbars=yes,location=no,toolbar=no,menubar=yes,status=no,width=700,height=640");
}

//
// Other initialization stuff
//

function init() {
  var nav = new Navigation();
  var iMouseovers = new ImageMouseovers();
}
onloadAdd("init()");

