/*****************************************************/
/*     interos Standard Document Header - Script     */
/*****************************************************/
/*         URIs:  http://interos.com/js/home.js      */
/*                http://www.interos.com/js/home.js  */
/*  Description:  JavaScript for interos             */
/*       Author:  Brent Angeline                     */
/*       Script:  JavaScript                         */
/*****************************************************/

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

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

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() {
  if (width('mainPanel') > 372) {
    return (8 + 12 + width('mainPanel') + 18 + 8);
  } else {
    return (8 + 12 + 372 + 18 + 8);
  }
}

function contentHeight() {
  return (height('navigation') + 16 + 12 + height('mainPanel') + 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();
  var i = div();
  var nteros = div();

  style('body').marginLeft = 0;
  style('body').marginRight = 0;

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

  style('title').height = '60px';
  content('title', '');

  style('mainPanel').border = 0;
  style('mainPanel').background = 
                     '#bfbfbf url(/images/panel/login.gif) repeat-x';
  if (width('mainPanel') < 372) {
    style('mainPanel').width = '372px';
  } else {
    style('mainPanel').width = width('mainPanel') + 'px';
  }
  style('mainPanel').left = x('mainPanel') + 'px'
  style('mainPanel').top = ((maxHeight - totalHeight) / 2) + 
                           height('navigation') + 16 + 12 + 'px';
  style('mainPanel').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('mainPanel') - 12 + 'px';
  style('bottomLeft').top = y('mainPanel') + height('mainPanel') + 'px';

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

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

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

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

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

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

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

  style('bottomLeft').background = 'url(/images/panel/bottom_left.gif)';
  style('left').background = 'url(/images/panel/left.gif)';
  style('topLeft').background = 'url(/images/panel/login/top_left.gif)';
  style('top').background = 'url(/images/panel/login/top.gif)';
  style('topRight').background = 'url(/images/panel/login/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)';

  i.setAttribute('id', 'i');
  id('body').appendChild(i);
  style('i').zIndex = 2;
  style('i').width = '52px';
  style('i').height = '60px';
  style('i').left = x('mainPanel') + ((width('mainPanel') - 336) / 2) + 'px';
  style('i').top = y('mainPanel') + 7 + 'px';
  style('i').position = 'absolute';
  style('i').background = 'url(/images/login/i.gif)';

  nteros.setAttribute('id', 'nteros');
  id('body').appendChild(nteros);
  style('nteros').zIndex = 2;
  style('nteros').width = '284px';
  style('nteros').height = '60px';
  style('nteros').left = x('mainPanel') + ((width('mainPanel') - 336) / 2) + 
                         52 + 'px';
  style('nteros').top = y('mainPanel') + 7 + 'px';
  style('nteros').position = 'absolute';
  style('nteros').background = 'url(/images/login/nteros.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('title').height = '';
  content('title', '');

  style('mainPanel').border = '';
  style('mainPanel').width = '';
  style('mainPanel').left = '';
  style('mainPanel').top = '';
  style('mainPanel').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('mainPanel').left = ((maxWidth - width('mainPanel')) / 2) + 'px';
  style('mainPanel').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('mainPanel') - 12 + 'px';
  style('bottomLeft').top = y('mainPanel') + height('mainPanel') + 'px';

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

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

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

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

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

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

  style('bottom').left = x('mainPanel') + 'px';
  style('bottom').top = y('mainPanel') + height('mainPanel') + '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.com                       */
/***********************************************************************/
