// Reduced Version of X, a Cross-Browser Javascript Library
// Copyright 2001-2006 Michael Foster (Cross-Browser.com) Distributed under the terms of the GNU LGPL

function xGetElementsByTagName(t,p)
{
	var list = null;
	t = t || '*';
	p = p || document;
	if (typeof p.getElementsByTagName != 'undefined') { // DOM1
		list = p.getElementsByTagName(t);
		if (t=='*' && (!list || !list.length)) list = p.all; // IE5 '*' bug
	}
	else { // IE4 object model
		if (t=='*') list = p.all;
		else if (p.all && p.all.tags) list = p.all.tags(t);
	}
	return list || new Array();
}

function xGetElementsByClassName(c,p,t,f)
{
	var r = new Array();
	var re = new RegExp("(^|\\s)"+c+"(\\s|$)");
//	var e = p.getElementsByTagName(t);
	var e = xGetElementsByTagName(t,p); // See xml comments.
	for (var i = 0; i < e.length; ++i) {
		if (re.test(e[i].className)) {
			r[r.length] = e[i];
			if (f) f(e[i]);
		}
	}
	return r;
}

function xRemoveEventListener(e,eT,eL,cap)
{
	if(!(e=xGetElementById(e)))return;
	eT=eT.toLowerCase();
	if(e.removeEventListener)e.removeEventListener(eT,eL,cap||false);
	else if(e.detachEvent)e.detachEvent('on'+eT,eL);
	else e['on'+eT]=null;
}

function xGetElementById(e)
{
	if(typeof(e)=='string') {
		if(document.getElementById) e=document.getElementById(e);
		else if(document.all) e=document.all[e];
		else e=null;
	}
	return e;
}

function xClientHeight()
{
	var v=0,d=document,w=window;
	if(d.compatMode == 'CSS1Compat' && !w.opera && d.documentElement && d.documentElement.clientHeight)
		{v=d.documentElement.clientHeight;}
	else if(d.body && d.body.clientHeight)
		{v=d.body.clientHeight;}
	else if(xDef(w.innerWidth,w.innerHeight,d.width)) {
		v=w.innerHeight;
		if(d.width>w.innerWidth) v-=16;
	}
	return v;
}

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

function xTop(e, iY)
{
	if(!(e=xGetElementById(e))) return 0;
	var css=xDef(e.style);
	if(css && xStr(e.style.top)) {
		if(xNum(iY)) e.style.top=iY+'px';
		else {
			iY=parseInt(e.style.top);
			if(isNaN(iY)) iY=xGetComputedStyle(e,'top',1);
			if(isNaN(iY)) iY=0;
		}
	}
	else if(css && xDef(e.style.pixelTop)) {
		if(xNum(iY)) e.style.pixelTop=iY;
		else iY=e.style.pixelTop;
	}
	return iY;
}

function xGetComputedStyle(oEle, sProp, bInt)
{
	var s, p = 'undefined';
	var dv = document.defaultView;
	if(dv && dv.getComputedStyle){
		s = dv.getComputedStyle(oEle,'');
		if (s) p = s.getPropertyValue(sProp);
	}
	else if(oEle.currentStyle) {
		// convert css property name to object property name for IE
		var i, c, a = sProp.split('-');
		sProp = a[0];
		for (i=1; i<a.length; ++i) {
			c = a[i].charAt(0);
			sProp += a[i].replace(c, c.toUpperCase());
		}
		p = oEle.currentStyle[sProp];
	}
	else return null;
	return bInt ? (parseInt(p) || 0) : p;
}

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

function xNum()
{
	for(var i=0; i<arguments.length; ++i){if(isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;}
	return true;
}

function xPageY(e)
{
	if (!(e=xGetElementById(e))) return 0;
	var y = 0;
	while (e) {
		if (xDef(e.offsetTop)) y += e.offsetTop;
		e = xDef(e.offsetParent) ? e.offsetParent : null;
	}
	return y;
}

function xVisibility(e, bShow)
{
	if(!(e=xGetElementById(e))) return null;
	if(e.style && xDef(e.style.visibility)) {
		if (xDef(bShow)) e.style.visibility = bShow ? 'visible' : 'hidden';
		return e.style.visibility;
	}
	return null;
}

function xAddClass(e, c)
{
	if ((e=xGetElementById(e))!=null) {
		var s = '';
		if (e.className.length && e.className.charAt(e.className.length - 1) != ' ') {
			s = ' ';
		}
		if (!xHasClass(e, c)) {
			e.className += s + c;
			return true;
		}
	}
	return false;
}

function xRemoveClass(e, c)
{
	if(!(e=xGetElementById(e))) return false;
	e.className = e.className.replace(new RegExp("(^|\\s)"+c+"(\\s|$)",'g'),
		function(str, p1, p2) { return (p1 == ' ' && p2 == ' ') ? ' ' : ''; }
	);
	return true;
}

function xHasClass(e, c)
{
	e = xGetElementById(e);
	if (!e || e.className=='') return false;
	var re = new RegExp("(^|\\s)"+c+"(\\s|$)");
	return re.test(e.className);
}

function xWidth(e,w)
{
	if(!(e=xGetElementById(e))) return 0;
	if (xNum(w)) {
		if (w<0) w = 0;
		else w=Math.round(w);
	}
	else w=-1;
	var css=xDef(e.style);
	if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
		w = xClientWidth();
	}
	else if(css && xDef(e.offsetWidth) && xStr(e.style.width)) {
		if(w>=0) {
			var pl=0,pr=0,bl=0,br=0;
			if (document.compatMode=='CSS1Compat') {
				var gcs = xGetComputedStyle;
				pl=gcs(e,'padding-left',1);
				if (pl !== null) {
					pr=gcs(e,'padding-right',1);
					bl=gcs(e,'border-left-width',1);
					br=gcs(e,'border-right-width',1);
				}
				// Should we try this as a last resort?
				// At this point getComputedStyle and currentStyle do not exist.
				else if(xDef(e.offsetWidth,e.style.width)){
					e.style.width=w+'px';
					pl=e.offsetWidth-w;
				}
			}
			w-=(pl+pr+bl+br);
			if(isNaN(w)||w<0) return;
			else e.style.width=w+'px';
		}
		w=e.offsetWidth;
	}
	else if(css && xDef(e.style.pixelWidth)) {
		if(w>=0) e.style.pixelWidth=w;
		w=e.style.pixelWidth;
	}
	return w;
}

function xHeight(e,h)
{
	if(!(e=xGetElementById(e))) return 0;
	if (xNum(h)) {
		if (h<0) h = 0;
		else h=Math.round(h);
	}
	else h=-1;
	var css=xDef(e.style);
	if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
		h = xClientHeight();
	}
	else if(css && xDef(e.offsetHeight) && xStr(e.style.height)) {
		if(h>=0) {
			var pt=0,pb=0,bt=0,bb=0;
			if (document.compatMode=='CSS1Compat') {
				var gcs = xGetComputedStyle;
				pt=gcs(e,'padding-top',1);
				if (pt !== null) {
					pb=gcs(e,'padding-bottom',1);
					bt=gcs(e,'border-top-width',1);
					bb=gcs(e,'border-bottom-width',1);
				}
				// Should we try this as a last resort?
				// At this point getComputedStyle and currentStyle do not exist.
				else if(xDef(e.offsetHeight,e.style.height)){
					e.style.height=h+'px';
					pt=e.offsetHeight-h;
				}
			}
			h-=(pt+pb+bt+bb);
			if(isNaN(h)||h<0) return;
			else e.style.height=h+'px';
		}
		h=e.offsetHeight;
	}
	else if(css && xDef(e.style.pixelHeight)) {
		if(h>=0) e.style.pixelHeight=h;
		h=e.style.pixelHeight;
	}
	return h;
}

function xOpacity(e, o)
{
	var set = xDef(o);
	//	if (set && o == 1) o = .9999; // FF1.0.2 but not needed in 1.5
	if(!(e=xGetElementById(e))) return 2; // error
	if (xStr(e.style.opacity)) { // CSS3
		if (set) e.style.opacity = o + '';
		else o = parseFloat(e.style.opacity);
	}
	else if (xStr(e.style.filter)) { // IE5.5+
		if (set) e.style.filter = 'alpha(opacity=' + (100 * o) + ')';
		else if (e.filters && e.filters.alpha) { o = e.filters.alpha.opacity / 100; }
	}
	else if (xStr(e.style.MozOpacity)) { // Gecko before CSS3 support
		if (set) e.style.MozOpacity = o + '';
		else o = parseFloat(e.style.MozOpacity);
	}
	else if (xStr(e.style.KhtmlOpacity)) { // Konquerer and Safari
		if (set) e.style.KhtmlOpacity = o + '';
		else o = parseFloat(e.style.KhtmlOpacity);
	}
	return isNaN(o) ? 1 : o; // if NaN, should this return an error instead of 1?
}

function xLeft(e, iX)
{
	if(!(e=xGetElementById(e))) return 0;
	var css=xDef(e.style);
	if (css && xStr(e.style.left)) {
		if(xNum(iX)) e.style.left=iX+'px';
		else {
			iX=parseInt(e.style.left);
			if(isNaN(iX)) iX=xGetComputedStyle(e,'left',1);
			if(isNaN(iX)) iX=0;
		}
	}
	else if(css && xDef(e.style.pixelLeft)) {
		if(xNum(iX)) e.style.pixelLeft=iX;
		else iX=e.style.pixelLeft;
	}
	return iX;
}

function xSlideTo(e, x, y, uTime)
{
	if (!(e=xGetElementById(e))) return;
	if (!e.timeout) e.timeout = 25;
	e.xTarget = x; e.yTarget = y; e.slideTime = uTime; e.stop = false;
	e.yA = e.yTarget - xTop(e); e.xA = e.xTarget - xLeft(e); // A = distance
	if (e.slideLinear) e.B = 1/e.slideTime;
	else e.B = Math.PI / (2 * e.slideTime); // B = period
	e.yD = xTop(e); e.xD = xLeft(e); // D = initial position
	var d = new Date(); e.C = d.getTime();
	if (!e.moving) _xSlideTo(e);
}
function _xSlideTo(e)
{
	if (!(e=xGetElementById(e))) return;
	var now, s, t, newY, newX;
	now = new Date();
	t = now.getTime() - e.C;
	if (e.stop) { e.moving = false; }
	else if (t < e.slideTime) {
		setTimeout("_xSlideTo('"+e.id+"')", e.timeout);

		s = e.B * t;
		if (!e.slideLinear) s = Math.sin(s);
//		if (e.slideLinear) s = e.B * t;
//		else s = Math.sin(e.B * t);

		newX = Math.round(e.xA * s + e.xD);
		newY = Math.round(e.yA * s + e.yD);
		xMoveTo(e, newX, newY);
		e.moving = true;
	}
	else {
		xMoveTo(e, e.xTarget, e.yTarget);
		e.moving = false;
		if (e.onslideend) e.onslideend();
	}
}

function xMoveTo(e,x,y)
{
	xLeft(e,x);
	xTop(e,y);
}

function xScrollLeft(e, bWin)
{
	var offset=0;
	if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
		var w = window;
		if (bWin && e) w = e;
		if(w.document.documentElement && w.document.documentElement.scrollLeft) offset=w.document.documentElement.scrollLeft;
		else if(w.document.body && xDef(w.document.body.scrollLeft)) offset=w.document.body.scrollLeft;
	}
	else {
		e = xGetElementById(e);
		if (e && xNum(e.scrollLeft)) offset = e.scrollLeft;
	}
	return offset;
}

function xClientWidth()
{
	var v=0,d=document,w=window;
	if((!d.compatMode || d.compatMode == 'CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientWidth)
		{v=d.documentElement.clientWidth;}
	else if(d.body && d.body.clientWidth)
		{v=d.body.clientWidth;}
	else if(xDef(w.innerWidth,w.innerHeight,d.height)) {
		v=w.innerWidth;
		if(d.height>w.innerHeight) v-=16;
	}
	return v;
}

function xScrollTop(e, bWin)
{
	var offset=0;
	if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
		var w = window;
		if (bWin && e) w = e;
		if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
		else if(w.document.body && xDef(w.document.body.scrollTop)) offset=w.document.body.scrollTop;
	}
	else {
		e = xGetElementById(e);
		if (e && xNum(e.scrollTop)) offset = e.scrollTop;
	}
	return offset;
}

function xAniOpacity(e, o, t, a, oe)
{
	if (!(e=xGetElementById(e))) return;
	var o0 = xOpacity(e); // start value
	var dx = o - o0; // displacement
	var fq = 1 / t; // frequency
	if (a) fq *= (Math.PI / 2);
	var t0 = new Date().getTime(); // start time
	var tmr = setInterval(
		function() {
			var et = new Date().getTime() - t0; // elapsed time
			if (et < t) {
				var f = et * fq; // constant velocity
				if (a == 1) f = Math.sin(f); // sine acceleration
				else if (a == 2) f = 1 - Math.cos(f); // cosine acceleration
				f = Math.abs(f);
				xOpacity(e, f * dx + o0); // instantaneous value
			}
			else {
				clearInterval(tmr);
				xOpacity(e, o); // target value
				if (typeof oe == 'function') oe(); // 'onEnd' handler
				else if (typeof oe == 'string') eval(oe);
			}
		}, 10 // timer resolution
	);
}

// "Extensions" and modifications to X by Damien Watson
// Distributed under the terms of the GNU LGPL
// Blame me for these, not Mike.

function xAddEventListener(e,eT,eL,cap)
{
	if(!(e=xGetElementById(e)))return;
	eT=eT.toLowerCase();
	if(e.addEventListener)e.addEventListener(eT,eL,cap||false);
	else if(e.attachEvent)e.attachEvent('on'+eT,eL);
	else e['on'+eT]=eL;
 	bom.events[bom.events.length] = [e, eT, eL, cap]; // addition to original
}

function xToggleClass(e, c){

	if(!(e=xGetElementById(e))) return false;
	if(xHasClass(e, c)){
		xRemoveClass(e, c);
	}else{
		xAddClass(e, c);
	}
	return true;
}

function xCenter(e){

	try{

// 		alert('width(body)' + document.body.offsetWidth + '; width(e)' + e.offsetWidth);
// 		alert('height(body)' + xClientHeight() + '; height(e)' + e.offsetHeight);

		xMoveTo(e,
			xScrollLeft() + (document.body.offsetWidth - e.offsetWidth)/2,
			xScrollTop() + (xClientHeight() - e.offsetHeight)/2
		);

	}catch(e){ }
}

// other functions by Damien Watson
// Distributed under the terms of the GNU LGPL

function ucfirst(s) {
	return s.charAt(0).toUpperCase() + s.substr(1, s.length-1);
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	WCH.js - Windowed Controls Hider v3.20
	www.aplus.co.yu/wch/
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	(c) Copyright 2003 and on, Aleksandar Vacic, www.aplus.co.yu
		This work is licensed under the Creative Commons Attribution License.
		To view a copy of this license, visit http://creativecommons.org/licenses/by/2.0/ or
		send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits: Mike Foster for x functions (cross-browser.com)
	Credits: Tim Connor for short and sweet way of dealing with IE5.0 - dynamic creation of style rule (www.infosauce.com)
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Based on idea presented by Joe King. Works with IE5.0+/Win
	IE 5.5+: place iFrame below the layer to hide windowed controls
	IE 5.0 : hide/show all elements that have "WCHhider" class
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

var WCH_Constructor = function() {
	//	exit point for anything but IE5.0+/Win
	if ( !(document.all && document.getElementById && !window.opera && navigator.userAgent.toLowerCase().indexOf("mac") == -1) ) {
		this.Apply = function() {};
		this.Discard = function() {};
		return;
	}
	
	//	private properties
	var _bIE55 = false;
	var _bIE6 = false;
	var _oRule = null;
	var _bSetup = true;
	var _oSelf = this;

	//	public: hides windowed controls
	this.Apply = function(vLayer, vContainer, bResize) {
		if (_bSetup) _Setup();

		if ( _bIE55 && (oIframe = _Hider(vLayer, vContainer, bResize)) ) {
			oIframe.style.visibility = "visible";
		} else if(_oRule != null) {
			_oRule.style.visibility = "hidden";
		}

	};

	//	public: shows windowed controls
	this.Discard = function(vLayer, vContainer) {
		if ( _bIE55 && (oIframe = _Hider(vLayer, vContainer, false)) ) {
			oIframe.style.visibility = "hidden";
		} else if(_oRule != null) {
			_oRule.style.visibility = "visible";
		}
	};

	//	private: returns iFrame reference for IE5.5+
	function _Hider(vLayer, vContainer, bResize) {
		var oLayer = _GetObj(vLayer);
		var oContainer = ( (oTmp = _GetObj(vContainer)) ? oTmp : document.getElementsByTagName("body")[0] );
		if (!oLayer || !oContainer) return;

		//	is it there already?
		//		1. first check does the layer has an ID at all. if not, assign one, using current timestamp, so we avoid duplicates
		if (oLayer.id == "")
			oLayer.id = "WCHid" + (new Date()).getTime();
		//		2. then try to locate the hiding iFrame
		var oIframe = document.getElementById("WCHhider" + oLayer.id);
		
		//	if not, create it
		if ( !oIframe ) {
			//	IE 6 has this property, IE 5 not. IE 5.5(even SP2) crashes when filter is applied, hence the check
			var sFilter = (_bIE6) ? "filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);" : "";
			//	get z-index of the object
			var zIndex = oLayer.style.zIndex;
			if ( zIndex == "" ) zIndex = oLayer.currentStyle.zIndex;
			zIndex = parseInt(zIndex);
			//	if no z-index, do nothing
			if ( isNaN(zIndex) ) return null;
			//	if z-index is below 2, do nothing (no room for Hider)
			if (zIndex < 2) return null;
			//	go one step below for Hider
			zIndex--;
			var sHiderID = "WCHhider" + oLayer.id;
			oContainer.insertAdjacentHTML("afterBegin", '<iframe class="WCHiframe" src="javascript:false;" id="' + sHiderID + '" scroll="no" frameborder="0" style="position:absolute;visibility:hidden;' + sFilter + 'border:0;top:0;left;0;width:0;height:0;background-color:#ccc;z-index:' + zIndex + ';"></iframe>');
			oIframe = document.getElementById(sHiderID);
			//	then do calculation
			_SetPos(oIframe, oLayer);
		} else if (bResize) {
			//	resize the iFrame if asked
			_SetPos(oIframe, oLayer);
		}
		return oIframe;
	};

	//	private: set size and position of the Hider
	function _SetPos(oIframe, oLayer) {
		//	fetch and set size
		oIframe.style.width = oLayer.offsetWidth + "px";
		oIframe.style.height = oLayer.offsetHeight + "px";
		//	move to specified position
		oIframe.style.left = oLayer.offsetLeft + "px";
		oIframe.style.top = oLayer.offsetTop + "px";
	};

	//	private: returns object reference
	function _GetObj(vObj) {
		var oObj = null;
		switch( typeof(vObj) ) {
			case "object":
				oObj = vObj;
				break;
			case "string":
				oObj = document.getElementById(vObj);
				break;
		}
		return oObj;
	};

	//	private: setup properties on first call to Apply
	function _Setup() {
		_bIE55 = (typeof(document.body.contentEditable) != "undefined");
		_bIE6 = (typeof(document.compatMode) != "undefined");

		if (!_bIE55) {
			if (document.styleSheets.length == 0)
				document.createStyleSheet();
			var oSheet = document.styleSheets[0];
			oSheet.addRule(".WCHhider", "visibility:visible");
			_oRule = oSheet.rules(oSheet.rules.length-1);
		}

		_bSetup = false;
	};
};

var WCH = new WCH_Constructor();