var boxes = [];
var preI = []; // Preload image objects

var mObj;
document.onmousemove = function(e) {
  mObj = e?e:event;
  document.onmousemove = null;
}

var isIE = (navigator.appName.indexOf('Microsoft')+1?1:0);
var is5 = (isIE && (navigator.appVersion.indexOf('MSIE 5')+1));
var isMac = (navigator.appVersion.toLowerCase().indexOf('mac')+1?1:0);
var isWin = (navigator.appVersion.toLowerCase().indexOf('win')+1);

if (isWin && is5) {
  var ie5WidthFixApplied = 0;
  addStyleSheet('ie5');
}

function addStyleSheet(href) {
  var newCSS = document.createElement('link');
  newCSS.rel = 'stylesheet';
  newCSS.type = 'text/css';
  newCSS.href = 'css/'+href+'.css';
  document.getElementsByTagName('head')[0].appendChild(newCSS);
}

function disableAllStyleSheets() {
  var a;
  for (var i=0; a=document.getElementsByTagName('link')[i]; i++) {
    if (a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
      a.disabled = true;
    }
  }
}

function setActiveStyleSheet(title) {
  var a;
  for (var i=0; (a=document.getElementsByTagName('link')[i]); i++) {
    if (a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
      a.disabled = true;
      if (a.getAttribute('title') == title || (a.getAttribute('title').indexOf('default')!=-1)) a.disabled = false;
    }
  }
  if (isIE && isWin) {
    // hack for background color change / display bug (limited just to IE6 actually?)
    window.resizeBy(0,1);
    window.resizeBy(0,-1);
  }
}

function setActiveFontStyle(title) {
  var a;
  for (var i=0; (a=document.getElementsByTagName('link')[i]); i++) {
    if (a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
      if (a.getAttribute('title').indexOf('font')+1) {
        a.disabled = true;
      }
      if (a.getAttribute('title') == title) a.disabled = false;
    }
  }
}

function getElementsByClassName(className) {
  var tmp = document.all?document.all:document.getElementsByTagName('*');  
  var elements = [];
  for (var i=0; i<tmp.length; i++) {
    if (tmp[i].className.indexOf(className)+1) elements[elements.length] = tmp[i];
  }
  return elements;
}

function preload() {
  for (var i=0; i<arguments.length; i++) {
    preI[preI.length] = new Image();
    preI[preI.length-1].src = 'image/'+arguments[i]+(arguments[i].indexOf('.')+1?'':'.gif');
  }
}

function init() {
  var useBoxes = (!isMac || (isMac && !isIE)); // IE5:Mac doesn't like the box objects (?)
  getArkanoidHighscores();
  getRandomArkanoidLevel();
  
  if (useBoxes) {
    var divs = document.getElementsByTagName('div');
    var boxCount = 0;
    for (var i=0; i<divs.length; i++) {
      if (divs[i].className.indexOf('box')+1) {
        boxes[boxes.length] = new Box(divs[i]);
        if (isIE) boxes[boxes.length-1].resizeHandler(); // fix for IE with odd widths
        boxCount++;
      }
    }
    webPad = new WebPad(boxes[3]);
  } else {
    webPad = new WebPad(document.getElementById('box3').getElementsByTagName('div')[0]);
  }
  if (isIE && is5) ie5DividerFix();
  if (isIE && isWin) {
    window.onresize = resizeHandler;
    setTimeout('resizeHandler()',20);
  }
  oneSixtyPXImage = new OneSixtyPXImage(document.getElementById('oneSixtyContainer'));

  // rpc-js implementation calls
  document.getElementById('data').focus();
  if (document.domain.indexOf('schillmania.com')+1) {
    setTimeout("rpcConnect()",2500);
  }

}

function ie5DividerFix() {
  // add &nbsp; around divider | content
  var dividers = getElementsByClassName('divider');
  for (var i=0; i<dividers.length; i++) {
    dividers[i].innerHTML = '&nbsp;'+dividers[i].innerHTML+'&nbsp;';
  }
}

function resizeHandler() {
  for (var i=0; i<boxes.length; i++) {
    boxes[i].resizeHandler();
  }
}

function reflowAllBoxes() {
  for (var i=0; i<boxes.length; i++) {
    boxes[i].reflow();
  }
}

function doMail() {
  window.location.href = String.fromCharCode(109)+'ai'+'lt'+'o'+String.fromCharCode(58)+'scott_s03'+String.fromCharCode(64)+document.domain;
}

function loadJS(jsURL,onComplete,noRandomize) {
  var oJS = document.getElementById('jsData');
  var head = document.getElementsByTagName('head').item(0);
  if (oJS) head.removeChild(oJS);
  oJS = document.createElement('script');
  oJS.type = 'text/javascript';
  oJS.id = 'jsData';
  oJS.onJSComplete = (onComplete?onComplete:webPad.refreshComplete);
  oJS.onJSReadyStateChange = function() {
    if (isIE) {
      if (this.readyState == 'loaded' || this.readyState == 'complete') {
        this.onreadystatechange = function() { return false; }
        this.onJSComplete();
      }
    } else {
      this.onload = function() { return false; }
      this.onJSComplete();
    }
  }
  if (isIE) {
    oJS.onreadystatechange = oJS.onJSReadyStateChange;
  } else {
    oJS.onload = oJS.onJSReadyStateChange;
  }
  var argSeparator = jsURL.indexOf('?')+1?'&':'?';
  oJS.src = jsURL+(!noRandomize?argSeparator+'r='+parseInt(Math.random()*65535):'');
  head.appendChild(oJS);
}

window.onload = init;
