// Funcs.js
// Last updated: 6/25/2009 RAK

var isStnd = document.getElementById ? true : false; // Standards compliant
var pre_w3c = false;
var ie5 = false;
var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isMac = false; 
var isSafari = false;
var adjusted = false;
var newWindow = null;

// X Variables
var xVersion='3.14.1';
var xOp7=false;
var xOp5or6=false;
var xIE4Up=false;
var xNN4=false;
var 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.layers) {xNN4=true;}
else {xIE4Up=document.all && xUA.indexOf('msie')!=-1 && parseInt(navigator.appVersion)>=4;}

var DHTML = (document.getElementById || document.all || xNN4); // Any of the above

w3c();

/* -- FUNCTIONS -- */

function w3c()  { 
	if(navigator.appVersion.indexOf( "Macintosh" ) != -1){
		isMac = true;
	}
	if(navigator.appVersion.indexOf( "Safari" ) != -1){
		isSafari = true;
	}
	if (navigator.appName.indexOf("Netscape") != -1) {
		var version = navigator.appVersion
		var subVersion = version.substring(0,3)
		var subVersion_p = parseFloat(subVersion)
		if (subVersion_p < 5)  {
			pre_w3c = true;
			isNS = true;
    	}  
  	}   
	if (navigator.appName.indexOf("Explorer") != -1) {
		var version = navigator.appVersion
		var subVersion = version.substring(22,25)
		var subVersion_p = parseFloat(subVersion)
		if (subVersion_p < 5)  {
			pre_w3c = true;
			isNS = false;
		}
		if ((subVersion_p == 5) || (subVersion_p == 5.5))  {
			ie5 = true;
		}
	}
}  

function adjustLayoutx(){
	
}
function adjustLayout(){

	var pageName = getPageFileName();
	
	//var corimg = xHeight("banner") + 8;
	
	//var col1height = xHeight("sidebar1");
	
	if((pageName == "") || (pageName == "index.php")){
		var col1height = 0;
	} else {
		var col1height = xHeight("sidebar1");
	}
	
	var col2height = xHeight("rgtCol");
	var col3height = xHeight("innerContent");
	var maxheight = Math.max(col3height, Math.max(col2height, col1height));


	// Adjust Site map
	//var margin = 18 + 20; // top padding of sitemap lists + top padding of maincontent
	//var ul1height = xHeight("l");
	//var ul2height = xHeight("r");
	//var maxulheight = Math.max(ul1height, ul2height) + margin;

	// TODO: MacIE5 still doesn't extend divs properly. Look for OK fix.
	//var addmargin = 0;
	//if(ie5){
//var addmargin = 25; 
	//}
	//var cheight = xHeight("main") + addmargin;
	
	//cheight = Math.max(cheight, maxcolheight);

	//var lheight = xHeight("lftBar");
	//var rheight = xHeight("rgtBar");

	// Assign the maximum height to all columns
	//var maxheight = 
	//Math.max(Math.max(cheight, Math.max(lheight, rheight)), maxulheight); 
	
	//if((cheight < rheight) || (rheight < lheight)){
		//xHeight("maincontenthome", maxheight);
		// 5/3/04 RAK: Apply "main" height adjustment only on pages that need it
		// Note: Adjust Legal Alerts with font-style formatting applied to more than
		// one line results in layer "main" 'slipping' to the bottom of the page.
		// 5/6/04 RAK: .flow images in news items don't render without the adjustment,
		// so implement fileNameContains("news")
		// 8/18/2004 RAK: See fix on fileNameContains() in response to att formatting
		// search_res with one item was obscured without main height adjustment
		// (pageName == "links.php") || 
		//if(fileNameContains("search_res") || fileNameContains("news")){
		//	xHeight("main", maxheight); 
		//}

		// Note: Win IE6 - Don't change dimensions of  elements with padding 
		// or margin, as these values will be erased with this operation.
	//}

	// WinIE6: Stretch columns vertically
	//if((pageName == "yc.php") || (pageName == "yc.htm") || (pageName == "home.php") || (pageName == "la_search_res_no_aop.php")){
	//if(fileNameContains("yc.php") || fileNameContains("yc.htm") || fileNameContains("yc2.htm") || fileNameContains("home.php") /*||  fileNameContains("la_search_res_no_aop.php")  ||  fileNameContains("search_res_adv") || fileNameContains("article.php")*/){
		//xHeight("yccontent", maxheight); 
		//xHeight("lftBar", maxheight);
		// New formatting
		//xHeight("content", maxheight); 
	//} else {
		//xHeight("innerContent", maxheight);
		xHeight("rgtCol", maxheight);
		
		//xHeight("lftBar", maxheight);
	//}
	
} 

// GetPageFileName returns name of current file
function getPageFileName(){
	var f = document.location.href;
	var ix  = f.lastIndexOf('/');
	if ( ix > -1 ) f = f.substring( ix + 1 );
	return f;
	//var page  = parseInt( ( f.substring( f.indexOf('Page') + 4 ) ), 10 );
}

// Functions from the x DHTML library
// X v3.14.1, Cross-Browser DHTML Library from Cross-Browser.com
// Copyright (c) 2002,2003 Michael Foster (mike@cross-browser.com)
// This library is distributed under the terms of the LGPL (gnu.org)

function xHeight(e,uH) {
  if(!(e=xGetElementById(e))) return 0;
  if (xNum(uH)) { // v3.13.1
    if (uH<0) uH = 0;
    else uH=Math.round(uH);
  }
  else uH=0;
  var css=xDef(e.style);
  if(css && xDef(e.offsetHeight) && xStr(e.style.height)) {
    if(uH) xSetCH(e, uH);
    uH=e.offsetHeight;
  }
  else if(css && xDef(e.style.pixelHeight)) {
    if(uH) e.style.pixelHeight=uH;
    uH=e.style.pixelHeight;
  }
  else if(xDef(e.clip) && xDef(e.clip.bottom)) {
    if(uH) e.clip.bottom=uH;
    uH=e.clip.bottom;
  }
  return uH;
}

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 if(document.layers) e=xLayer(e);
  else e=null;
  return e;
}

/*
	Utility:getObj creates crossbrowser layer object.
	Requires: layer name
	Returns: self with obj and style reference
	Error checking: TODO check presence of 'objName' layer
*/
function getObj(objName) { 
	if (isStnd) { 
		this.obj = document.getElementById(objName); 
		this.style = document.getElementById(objName).style; 
	} else if (isIE) { 
		alert("Document.all");
		this.obj = document.all[objName]; 
		this.style = document.all[objName].style; 
	} else if (xNN4) { 
		//this.obj = document.layers[objName]; 
		//this.style = document.layers[objName]; 
		this.obj = xLayer(objName);
		this.style = xLayer(objName);
	} else {
		// Other browsers
		
	}
} 

function xLayer(id,root) { // only for nn4
  var i,layer,found=null;
  if (!root) root=window;
  for(i=0; i<root.document.layers.length; i++) {
    layer=root.document.layers[i];
    if(layer.id==id) return layer;
    if(layer.document.layers.length) found=xLayer(id,layer);
    if(found) return found;
  }
  return null;
}

function xNum(n) {
  return typeof(n)=='number';
}

function xDef() {
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}
function xStr(s) {
  return typeof(s)=='string';
}

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 xGetCS(ele,sP){return parseInt(document.defaultView.getComputedStyle(ele,'').getPropertyValue(sP));}

// Event:
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 if(e.captureEvents) {
    if(useCapture||(eventType.indexOf('mousemove')!=-1)) { e.captureEvents(eval('Event.'+eventType.toUpperCase())); }
    eval(eh);
  }
  else eval(eh);
}

// Window:
function xClientWidth() {
  var w=0;
  if(xOp5or6) w=window.innerWidth;
  else if(!window.opera && document.documentElement && document.documentElement.clientWidth) // v3.12
    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) // v3.12
    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() { // window resize event simulation
  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() { // window scroll event simulation
  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 xScrollLeft(e) {
  var offset=0;
  if (!(e=xGetElementById(e))) {
    if(xDef(window.pageXOffset)) offset=window.pageXOffset;
    else if(document.documentElement && document.documentElement.scrollLeft) offset=document.documentElement.scrollLeft;
    else if(document.body && xDef(document.body.scrollLeft)) offset=document.body.scrollLeft;
  }
  else { if (xNum(e.scrollLeft)) offset = e.scrollLeft; }
  return offset;
}
function xScrollTop(e) {
  var offset=0;
  if (!(e=xGetElementById(e))) {
    if(xDef(window.pageYOffset)) offset=window.pageYOffset;
    else if(document.documentElement && document.documentElement.scrollTop) offset=document.documentElement.scrollTop;
    else if(document.body && xDef(document.body.scrollTop)) offset=document.body.scrollTop;
  }
  else { if (xNum(e.scrollTop)) offset = e.scrollTop; }
  return offset;
}
