/* =============================================================================
* iTabLib.js
*
* JavaScript dynamic tabbed navigation function library
*
* Developed by Puna Tannehill
* Updated by Jake Kronika <jkronika@imagescape.com>
* For Imaginary Landscape, LLC <www.imagescape.com>
* Copyright (c) 2006
*
* Last updated 2007.08.03
*
* Reuse or modification without permission is prohibited.
* --------------------------------------------------------------------------- */

var iTab;

if (document.write) {
  document.write('<style type="text/css"><!--\n'
  + '  .iTabBlock,\n'
  + '  .iTabBlock.hidden,\n'
  + '  .itab-block.hidden { display: none; }\n'
  + '\n--></style>');
}

function iTabOnload() {
  iTab = new iTabModule(arguments);

  var preTabResize = window.onresize;

  window.onresize = function() {
    if (preTabResize) {
      preTabResize();
    }

    iTab.resizeBlocks();
  }

  var divs;
  if (document.getElementsByTagName) {
    divs = document.getElementsByTagName('div');

    for (var i = 0; i < iTab.blocks.length; i++) {
      iTab.blocks[i].style.display = 'block';
      iTab.blocks[i].style.border = '1px solid #f00'
    }
  }
}

var tabLoad = window.onload;
window.onload = function() {
  if (typeof(tabLoad) == 'function') {
    tabLoad();
  }

  iTabOnload('iTabBlock');
};

function iTabModule() {
    this.prefix = location.href.match(/\/[^\/]*\/?$/);

    if (this.prefix && this.prefix[0]) {
      this.prefix = this.prefix[0];
    } else {
      this.prefix = '';
    }

    this.prefix = this.prefix.replace(/[^a-z0-9]/ig, '');

    this.easels = new Array();
    this.blocks = new Array();
    this.navs = new Array();
    this.blkname = arguments[0][0];
    this.parsePage();
    if(this.blocks.length>0){
	this.parseBlocks(this.blocks);
	this.prepBlocks();
	this.prepNav();
	this.resizeBlocks();
	this.toggleLoad();
    }return;
}

iTabModule.prototype.parsePage = function() {
    var pardiv = document.body.getElementsByTagName('div');
    for (var a=0; a<pardiv.length; ++a) {
        if (pardiv[a].className == this.blkname) {
            pardiv[a].className = 'itab-block';
            this.blocks.push(pardiv[a]);
        }
    }
};

iTabModule.prototype.parseBlocks = function(divray) {
    var tmpray = divray;
    var sibblocks = new Array();
    this.easels.push(tmpray[0]);
    var neweas = this.easels[this.easels.length-1];
    neweas.blocks = new Array();
    var dad = tmpray[0].parentNode;
    for (var b=tmpray.length-1; b>-1; --b) {
        if (tmpray[b].parentNode == dad) {
            neweas.blocks.unshift(tmpray[b]);
            tmpray.splice(b,1);
        }
    }
    if (tmpray.length>0) { this.parseBlocks(tmpray); }
};

iTabModule.prototype.prepBlocks = function() {
    for (var eas in this.easels) {
        var easdiv = this.easels[eas].parentNode.insertBefore(document.createElement('div'),this.easels[eas]);
        easdiv.className = 'itab-easel';
        for (var blk in this.easels[eas].blocks) {
            var tmpdiv = this.easels[eas].blocks[blk].parentNode.removeChild(this.easels[eas].blocks[blk]);

            easdiv.appendChild(tmpdiv);

            if (!tmpdiv.className.match(/\bhidden\b/)) {
              tmpdiv.className += ' hidden';
            }

            tmpdiv.style.top = '0px';
        }
        easdiv.blocks = this.easels[eas].blocks;
        this.easels[eas] = easdiv;
    }
};

iTabModule.prototype.prepNav = function() {
    for (var eas in this.easels) {
        var navdiv = document.createElement('div');
        navdiv.className = 'itab-nav-container';
        var tbl = navdiv.appendChild(document.createElement('table'));
        tbl.className = 'itab-nav-table';
        tbl.cellSpacing = 0;
        tbl.cellPadding = 0;
        var tblbd = tbl.appendChild(document.createElement('tbody'));
        var tblrw = tblbd.appendChild(document.createElement('tr'));
        tblrw.valign = 'top';
        tblrw.align = 'center';

        this.easels[eas].navs = new Array();
        var rem = 100, blks = this.easels[eas].blocks.length;
        for (var blk in this.easels[eas].blocks) {
            var tblcl = tblrw.appendChild(document.createElement('td'));

            this.easels[eas].navs.push(tblcl);
            tblcl.mod = this;
            tblcl.easel = eas;
            tblcl.block = blk;
            this.easels[eas].blocks[blk].nav = tblcl;
            tblcl.onclick = this.toggle;
            tblcl.className = 'itab-nav';
            tblcl.iTabModule = this;
            var tbdiv = document.createElement('div');
            tbdiv.appendChild(document.createTextNode(this.easels[eas].blocks[blk].title));
            tblcl.appendChild(tbdiv);
            this.easels[eas].blocks[blk].title = '';
        }

        var tblcl = tblrw.appendChild(document.createElement('td'));
        tblcl.className = 'itab-nav-end';
        tblcl.innerHTML = '&#160;';
        tblcl.onclick = function() { return false; };
        tblcl.style.cursor = 'default';

        this.navs[eas] = this.easels[eas].parentNode.insertBefore(navdiv,this.easels[eas]);
        this.navs[eas].lastCell = tblcl;
    }
};

iTabModule.prototype.toggle = function() {
  var i = 0, bingo = false;
  
  for (var itm in this.mod.easels[this.easel].blocks) {
    var eas = this.mod.easels[this.easel],
        blk = eas.blocks[itm],
        nav = eas.navs[itm];
          
    if (itm == this.block) {
      blk.className = blk.className.replace(/\t*\bhidden\b/g, '');

      nav.className = 'itab-nav-select' +
        (i == 0 ? ' itab-nav-left-last'
          : (i == eas.navs.length - 1 ? ' itab-nav-right-last' : ''));

      setCookie('its.'+this.mod.prefix+'.'+this.easel, itm);
            
      bingo = true;
    } else {
      blk.className += ' hidden';

      if (bingo) {
        nav.className = 'itab-nav itab-nav-right';
      } else {
        nav.className = 'itab-nav itab-nav-left';
      }

      nav.className +=
        (i == 0 || i == eas.navs.length - 2 ? ' itab-nav-last' : '');
    }
    
    i++;
  }
};

iTabModule.prototype.toggleLoad = function() {
    for (var eas in this.easels) {
        var cookieIndex = getCookie('its.'+this.prefix+'.'+eas);
        if (cookieIndex == null) {
            cookieIndex = 0;
        } else {
            cookieIndex = parseInt(cookieIndex);
        }
	if (location.hash &&
            location.hash.substring(0, 5) == '#tab-') {
	    cookieIndex = parseInt(location.hash.substring(5));
	}
	
	var curr_nav = this.easels[eas].navs[cookieIndex],
	    curr_blk = this.easels[eas].blocks[cookieIndex];
	    
        if (typeof curr_nav != 'undefined') {
            curr_nav.onclick();
        }
        
        if (typeof curr_blk != 'undefined') {
          curr_blk.className = curr_blk.className.replace(/\t*\bhidden\b/g, '');
 
          if (curr_blk.blocks
              && curr_blk.blocks.length
              && curr_blk.blocks[0]
              && typeof curr_blk.blocks[0].nav != 'undefined') {
            curr_blk.blocks[0].nav.onclick();
          }
        }
    }
};

iTabModule.prototype.resizeBlocks = function() {
  if (!this.easels) {
    return;
  }

  for (var c = this.easels.length-1; c>-1; --c) {
    this.easels[c].style.height = '1px';
   	this.navs[c].lastCell.style.width='1px';
    this.navs[c].lastCell.style.width='99%';

    for (var blk in this.easels[c].blocks) {
      var block = this.easels[c].blocks[blk];

      // change display to correctly set offsetHeight
      block.style.visibility = 'hidden';
      block.style.display = 'block';

      if (this.easels[c].blocks[blk].offsetHeight > this.easels[c].offsetHeight) {
        this.easels[c].style.height = this.easels[c].blocks[blk].offsetHeight+'px';
      }

      // revert display to keep things hidden
      block.style.display = '';
      block.style.visibility = '';
    }

    if (this.easels[0].offsetHeight < this.easels[c].offsetHeight) {
      this.easels[0].style.height = this.easels[c].offsetHeight+'px';
    }
  }
};



function showProp(obj) {
    var tmp = new String();
    for (var prop in obj) {
        if (typeof obj[prop] == 'string') {
            tmp += '['+prop+'] == '+obj[prop]+"\n";
        }
    }
    return tmp;
}

/* Cookie handling routines */

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
