// x_core.js, X v3.15.2, Cross-Browser.com DHTML Library
// Copyright (c) 2004 Michael Foster, Licensed LGPL (gnu.org)

// This .js only contains the functions we currently need

var xVersion='3.15.2',xNN4,xOp7,xOp5or6,xIE4Up,xIE4,xIE5,xMac,xUA=navigator.userAgent.toLowerCase();
if (window.opera){
  xOp7=(xUA.indexOf('opera 7')!=-1 || xUA.indexOf('opera/7')!=-1);
  if (!xOp7) xOp5or6=(xUA.indexOf('opera 5')!=-1 || xUA.indexOf('opera/5')!=-1 || xUA.indexOf('opera 6')!=-1 || xUA.indexOf('opera/6')!=-1);
}
else if (document.all && xUA.indexOf('msie')!=-1) {
  xIE4Up=parseInt(navigator.appVersion)>=4;
  xIE4=xUA.indexOf('msie 4')!=-1;
  xIE5=xUA.indexOf('msie 5')!=-1;
}
else if (document.layers) {xNN4=true;}
xMac=xUA.indexOf('mac')!=-1;

function xGetElementById(e)
{
  if (typeof(e) != 'string') 
	return e;
  
  if (document.getElementById) 
  {
    e = document.getElementById(e);
  }
  else if (document.all) 
  {
	e=document.all[e];
  }
  else e=null;

  return e;
}

function xHeight(e,uH) {
  if(!(e=xGetElementById(e))) return 0;
  if (xNum(uH)) {
    if (uH<0) uH = 0;
    else uH=Math.round(uH);
  }
  else uH=-1;
  var css=xDef(e.style);
  if(css && xDef(e.offsetHeight) && xStr(e.style.height)) {
    if(uH>=0) xSetCH(e, uH);
    uH=e.offsetHeight;
  }
  else if(css && xDef(e.style.pixelHeight)) {
    if(uH>=0) e.style.pixelHeight=uH;
    uH=e.style.pixelHeight;
  }
  return uH;
}

function xHeightAuto(e) {
    if(!(e=xGetElementById(e))) return 0;
    e.style.height = "auto";
}

function xGetCS(ele,sP){return parseInt(document.defaultView.getComputedStyle(ele,'').getPropertyValue(sP));}

function xSetCH(ele,uH){
  var pt=0,pb=0,bt=0,bb=0;
  if(xDef(document.defaultView) && xDef(document.defaultView.getComputedStyle)){
    pt=xGetCS(ele,'padding-top');
    pb=xGetCS(ele,'padding-bottom');
    bt=xGetCS(ele,'border-top-width');
    bb=xGetCS(ele,'border-bottom-width');
  }
  else if(xDef(ele.currentStyle,document.compatMode)){
    if(document.compatMode=='CSS1Compat'){
      pt=parseInt(ele.currentStyle.paddingTop);
      pb=parseInt(ele.currentStyle.paddingBottom);
      bt=parseInt(ele.currentStyle.borderTopWidth);
      bb=parseInt(ele.currentStyle.borderBottomWidth);
    }
  }
  else if(xDef(ele.offsetHeight,ele.style.height)){ // ?
    ele.style.height=uH+'px';
    pt=ele.offsetHeight-uH;
  }
  if(isNaN(pt)) pt=0; if(isNaN(pb)) pb=0; if(isNaN(bt)) bt=0; if(isNaN(bb)) bb=0;
  var cssH=uH-(pt+pb+bt+bb);
  if(isNaN(cssH)||cssH<0) return;
  else ele.style.height=cssH+'px';
}

function xStr(s) {
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='string') return false;}
  return true;
}

function xDef() {
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}

function xNum(n) {
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='number') return false;}
  return true;
}

function xClientWidth() {
  var w=0;
  if(xOp5or6) w=window.innerWidth;
  else if(!window.opera && document.documentElement && document.documentElement.clientWidth)
    w=document.documentElement.clientWidth;
  else if(document.body && document.body.clientWidth)
    w=document.body.clientWidth;
  else if(xDef(window.innerWidth,window.innerHeight,document.height)) {
    w=window.innerWidth;
    if(document.height>window.innerHeight) w-=16;
  }
  return w;
}
function xClientHeight() {
  var h=0;
  if(xOp5or6) h=window.innerHeight;
  else if(!window.opera && document.documentElement && document.documentElement.clientHeight)
    h=document.documentElement.clientHeight;
  else if(document.body && document.body.clientHeight)
    h=document.body.clientHeight;
  else if(xDef(window.innerWidth,window.innerHeight,document.width)) {
    h=window.innerHeight;
    if(document.width>window.innerWidth) h-=16;
  }
  return h;
}

function xResizeEvent() {
  if (window.xREL) setTimeout('xResizeEvent()', 250);
  var cw = xClientWidth(), ch = xClientHeight();
  if (window.xPCW != cw || window.xPCH != ch) { window.xPCW = cw; window.xPCH = ch; if (window.xREL) window.xREL(); }
}

function xScrollEvent() {
  if (window.xSEL) setTimeout('xScrollEvent()', 250);
  var sl = xScrollLeft(), st = xScrollTop();
  if (window.xPSL != sl || window.xPST != st) { window.xPSL = sl; window.xPST = st; if (window.xSEL) window.xSEL(); }
}

function xAddEventListener(e,eventType,eventListener,useCapture) {
  if(!(e=xGetElementById(e))) return;
  eventType=eventType.toLowerCase();
  if((!xIE4Up && !xOp7) && e==window) {
	
    if(eventType=='resize') { window.xPCW=xClientWidth(); window.xPCH=xClientHeight(); window.xREL=eventListener; xResizeEvent(); return; }
    if(eventType=='scroll') { window.xPSL=xScrollLeft(); window.xPST=xScrollTop(); window.xSEL=eventListener; xScrollEvent(); return; }
  }
  var eh='e.on'+eventType+'=eventListener';
  if(e.addEventListener) e.addEventListener(eventType,eventListener,useCapture);
  else if(e.attachEvent) e.attachEvent('on'+eventType,eventListener);
  else eval(eh);
}

var lastRightHeight = 0;
var resizeTimer = null;
var isAdjustingLayout = false;

function adjustLayout()
{
    isAdjustingLayout = true;
	// Inspired from: http://www.sitepoint.com/article/exploring-limits-css-layout/2
	
	// Setup the ids:
	var menuID = "menu";
	var contentID = "content";
	var rightID = "rightpanecontent"
	// alert('adjusting');

    // Setup the content id:
	if (xGetElementById(contentID) == null)
		contentID = "Content";

	// Setup the right panes id:
	if (xGetElementById(rightID) == null)
		rightID = "RightPaneContent";
    
	// Get natural heights:
	var menuHeight = xHeight(menuID);
	var contentHeight = xHeight(contentID);
	var rightHeight = xHeight(rightID);
	
	// Find the maximum content height:
	var maxContentHeight = Math.max(menuHeight, Math.max(contentHeight, rightHeight));
	
	// The page should collapse agian, if the content becomes smaller (the menu collapse), therefore we
	// ignore the right pane height, if the right pane height is equal the height we have set last time.
	if (lastRightHeight == rightHeight)
	    maxContentHeight = Math.max(menuHeight, contentHeight);
	
	/*
	var debug = "Menu: ID = " + menuID + ", height: " + menuHeight;
	debug = debug + "\nContent: ID = " + contentID + ", height: " + contentHeight;
	debug = debug + "\nRight: ID = " + rightID + ", height: " + rightHeight;
	debug = debug + "\nMax: " + maxContentHeight;
    alert("debug: \n" + debug);
    */
    	
	// If the right pane height is different from the
	// maximum content height, then set the heights to be equal.
	if (rightHeight != maxContentHeight)
	{
	    // Set the right pane height.
        xHeight(rightID, maxContentHeight);
        
        // Save the last set right pane height.
        lastRightHeight = maxContentHeight;
    }
    
    if (resizeTimer) clearTimeout(resizeTimer);
    // Reallow adjusting the layout again in 500 milliseconds.
    // This allows us to avoid the case where IE causes window.resize
    // to be called right after adjustLayout finishes, thus causing an 
    // infinite loop.
    setTimeout(function() { isAdjustingLayout = false; }, 500);
}

window.onload = function()
{
    adjustLayout();

	xAddEventListener(window, "resize", 
	    function() {
	        if (isAdjustingLayout) return;
	        if (resizeTimer) clearTimeout(resizeTimer);
	        resizeTimer = setTimeout(adjustLayout, 100);
	    }
	    , false);
}

