/******************************************************/
/*     interos Standard Document Header - Script      */
/******************************************************/
/*         URIs:  http://interos.org/js/default.js    */
/*                http://*.interos.org/js/default.js  */
/*  Description:  Default JavaScript for interos      */
/*       Author:  Brent Angeline                      */
/*       Script:  JavaScript                          */
/******************************************************/

var panel, maxWidth, maxHeight, totalWidth, totalHeight;
var mode = 'static';

function capable() {
  return (document.createElementNS || document.createElement) && 
         document.getElementById && width('navigation') && initialize();
}

function initialize() {
  if (id('mainPanel')) {
    panel = 'mainPanel';
    return 1;
  } else if (id('titlePanel')) {
    panel = 'titlePanel';
    return 1;
  } else {
    return 0;
  }
}

function div() {
  if (document.createElementNS) {
    return document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
  } else if (document.createElement) {
    return document.createElement('div');
  }
}

function id(e) {
  return document.getElementById(e);
}

function style(e) {
  return id(e).style;
}

function property(e, p) {
  return document.defaultView.getComputedStyle(id(e), null).getPropertyValue(p);
}

function width(e) {
  if (id(e).offsetWidth) {
    return id(e).offsetWidth;
  } else if (window.getComputedStyle) {
    return property(e, 'width').substring(0, property(e, 'width').indexOf('p'));
  } else {
    return 0;
  }
}

function height(e) {
  if (id(e).offsetHeight) {
    return id(e).offsetHeight;
  } else if (window.getComputedStyle) {
    return property(e, 'height').substring(0, 
                                           property(e, 'height').indexOf('p'));
  } else {
    return 0;
  }
}

function x(e) {
  var x, done;
  x = done = 0;
  e = id(e);
  if (e.offsetParent) {
    while (e.offsetParent) {
      x += e.offsetLeft;
      e = e.offsetParent;
    }
  } else if (e.x) {
    x = e.x;
  }
  return x;
}

function y(e) {
  var y, done;
  y = done = 0;
  e = id(e);
  if (e.offsetParent) {
    while (e.offsetParent) {
      y += e.offsetTop;
      e = e.offsetParent;
    }
  } else if (e.y) {
    y = e.y;
  }
  return y;
}

function content(e, string) {
  if (document.all) {
    document.all[e].innerHTML = string;
  } else if (id(e).innerHTML) {
    id(e).innerHTML = string;
  }
}

function clientWidth() {
  var width = 0;
  if (self.innerWidth) {
    width = self.innerWidth;
  } else if (document.documentElement && document.documentElement.clientWidth) {
    width = document.documentElement.clientWidth;
  } else if (document.body) {
    width = document.body.clientWidth;
  }
  return width;
}

function clientHeight() {
  var height = 0;
  if (self.innerHeight) {
    height = self.innerHeight;
  } else if (document.documentElement && 
             document.documentElement.clientHeight) {
    height = document.documentElement.clientHeight;
  } else if (document.body) {
    height = document.body.clientHeight;
  }
  return height;
}

function contentWidth() {
  return (12 + width(panel) + 18);
}

function contentHeight() {
  return (height('navigation') + 16 + 12 + height(panel) + 18 + 16 + 
          height('copyrightDates'));
}

function dynamicMode() {
  var bottomLeft = div();
  var left = div();
  var topLeft = div();
  var top = div();
  var topRight = div();
  var right = div();
  var bottomRight= div();
  var bottom = div();

  style('copyrightDates').width = width('copyrightDates') + 'px';
  style('copyrightDates').left = x('copyrightDates') + 'px';
  style('copyrightDates').top = ((maxHeight - totalHeight) / 2) + 
                                height('navigation') + 16 + 12 + 
                                height(panel) + 18 + 16 + 'px';
  style('copyrightDates').position = 'absolute';
  style('copyrightDates').margin = 0;

  if ((clientWidth() - contentWidth()) < 9) {
    style(panel).width = width(panel) - 24 + 'px';
  }
  if (panel == 'mainPanel') {
    style('body').marginLeft = '0';
    style('body').marginRight = '0';
  }
  style(panel).border = 0;
  style(panel).left = x(panel) + 'px';
  style(panel).top = ((maxHeight - totalHeight) / 2) + height('navigation') + 
                     16 + 12 + 'px';
  style(panel).position = 'absolute';

  style('navigation').width = width('navigation') + 'px';
  style('navigation').left = x('navigation') + 'px';
  style('navigation').top = ((maxHeight - totalHeight) / 2) - 16 + 'px';
  style('navigation').position = 'absolute';

  bottomLeft.setAttribute('id', 'bottomLeft');
  id('body').appendChild(bottomLeft);
  style('bottomLeft').left = x(panel) - 12 + 'px';
  style('bottomLeft').top = y(panel) + height(panel) + 'px';

  left.setAttribute('id', 'left');
  id('body').appendChild(left);
  style('left').height = height(panel) - 84 + 'px';
  style('left').left = x(panel) - 12 + 'px';
  style('left').top = y(panel) + 84 + 'px';

  topLeft.setAttribute('id', 'topLeft');
  id('body').appendChild(topLeft);
  style('topLeft').left = x(panel) - 12 + 'px';
  style('topLeft').top = y(panel) - 12 + 'px';

  top.setAttribute('id', 'top');
  id('body').appendChild(top);
  style('top').width = width(panel) + 'px';
  style('top').left = x(panel) + 'px';
  style('top').top = y(panel) - 12 + 'px';

  topRight.setAttribute('id', 'topRight');
  id('body').appendChild(topRight);
  style('topRight').left = x(panel) + width(panel) + 'px';
  style('topRight').top = y(panel) - 12 + 'px';

  right.setAttribute('id', 'right');
  id('body').appendChild(right);
  style('right').height = height(panel) - 84 + 'px';
  style('right').left = x(panel) + width(panel) + 'px';
  style('right').top = y(panel) + 84 + 'px';

  bottomRight.setAttribute('id', 'bottomRight');
  id('body').appendChild(bottomRight);
  style('bottomRight').left = x(panel) + width(panel) + 'px';
  style('bottomRight').top = y(panel) + height(panel) + 'px';

  bottom.setAttribute('id', 'bottom');
  id('body').appendChild(bottom);
  style('bottom').width = width(panel) + 'px';
  style('bottom').left = x(panel) + 'px';
  style('bottom').top = y(panel) + height(panel) + 'px';

  style('bottomLeft').background = 'url(/images/panel/bottom_left.gif)';
  style('left').background = 'url(/images/panel/left.gif)';
  style('topLeft').background = 'url(/images/panel/main/top_left.gif)';
  style('top').background = 'url(/images/panel/main/top.gif)';
  style('topRight').background = 'url(/images/panel/main/top_right.gif)';
  style('right').background = 'url(/images/panel/right.gif)';
  style('bottomRight').background = 'url(/images/panel/bottom_right.gif)';
  style('bottom').background = 'url(/images/panel/bottom.gif)';

  mode = 'dynamic';
}

function staticMode() {
  style('body').marginLeft = '19px';
  style('body').marginRight = '19px';

  style('copyrightDates').width = '';
  style('copyrightDates').left = '';
  style('copyrightDates').bottom = '';
  style('copyrightDates').position = '';
  style('copyrightDates').margin = '';

  style(panel).border = '';
  style(panel).width = '';
  style(panel).left = '';
  style(panel).top = '';
  style(panel).position = '';

  style('navigation').width = '';
  style('navigation').left = '';
  style('navigation').top = '';
  style('navigation').position = '';

  /* more code */

  mode = 'static';
}

function resize() {
  style('copyrightDates').left = ((maxWidth - width('copyrightDates')) / 2) + 
                                 'px';

  style(panel).left = ((maxWidth - width(panel)) / 2) + 'px';
  style(panel).top = ((maxHeight - totalHeight) / 2) + 
                     height('navigation') + 16 + 12 + 'px';

  style('navigation').left = ((maxWidth - width('navigation')) / 2) + 'px';
  style('navigation').top = ((maxHeight - totalHeight) / 2) - 16 + 'px';

  style('bottomLeft').left = x(panel) - 12 + 'px';
  style('bottomLeft').top = y(panel) + height(panel) + 'px';

  style('left').left = x(panel) - 12 + 'px';
  style('left').top = y(panel) + 53 - 12 + 'px';

  style('topLeft').left = x(panel) - 12 + 'px';
  style('topLeft').top = y(panel) - 12 + 'px';

  style('top').left = x(panel) + 'px';
  style('top').top = y(panel) - 12 + 'px';

  style('topRight').left = x(panel) + width(panel) + 'px';
  style('topRight').top = y(panel) - 12 + 'px';

  style('right').left = x(panel) + width(panel) + 'px';
  style('right').top = y(panel) + 53 - 12 + 'px';

  style('bottomRight').left = x(panel) + width(panel) + 'px';
  style('bottomRight').top = y(panel) + height(panel) + 'px';

  style('bottom').left = x(panel) + 'px';
  style('bottom').top = y(panel) + height(panel) + 'px';
}

function layout() {
  maxWidth = clientWidth();
  maxHeight = clientHeight();
  totalWidth = contentWidth();
  totalHeight = contentHeight();
  if ((totalWidth < maxWidth) && (totalHeight < maxHeight)) {
    if (mode != 'dynamic') {
      dynamicMode();
    } else {
      resize();
    }
  } else if (mode != 'static') {
    staticMode();
  }
}

function remember() {
  document.cookie = 'javascript=on; path=/; secure';
}

window.onresize = function() {
  /*
  if (capable()) {
    layout();
  }
  */
  window.location.replace(unescape(window.location.pathname));
}

window.onload = function() {
  if (capable()) {
    layout();
    remember();
  }
}

/***********************************************************************/
/*                  Copyright and Trademark Statement                  */
/***********************************************************************/
/*                                                                     */
/*          All original textual and graphical site content:           */
/*                                                                     */
/*  Copyright 2001-2009 Brent Angeline and interos LLC.  All rights    */
/*  reserved.  Reproduction in whole or in part without written        */
/*  permission is prohibited.  interos and the interos logos are       */
/*  trademarks of Brent Angeline and interos LLC.  All other company,  */
/*  product, and service names mentioned herein may be the properties  */
/*  of their respective owners.  Comments in the interos.org forums    */
/*  are the properties of their respective authors.  All software      */
/*  developed in the forums is open source and belongs to everyone.    */
/*                                                                     */
/***********************************************************************/
/*                      e-mail: info@interos.org                       */
/***********************************************************************/
