// xScrollTop, xGetElementById, xDef, xNum Copyright 2001-2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

var cHeight, scrollTimer
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 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 xClientHeight()
{
  var h=0;
  if(document.compatMode == 'CSS1Compat' && !window.opera && document.documentElement && document.documentElement.clientHeight)
    h=document.documentElement.clientHeight;
  else if(document.body && document.body.clientHeight && !(navigator.userAgent.indexOf("Safari")!=-1) )
    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 xClientWidth()
{
  var w=0;
  if(document.compatMode == 'CSS1Compat' && !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 xNum()
{
  for(var i=0; i<arguments.length; ++i){if(isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;}
  return true;
}

function xDef()
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}

function floatButton(cHeight) {
	try {
		var e = xGetElementById('topButton')
		var temp = cHeight/20 + xScrollTop()
		e.style.top = parseInt(temp) + 'px'
	}
	catch (err){
		return
	}
}

function initButton(ty,scrollPos,cHeight) {
	try {
		var e = xGetElementById('topButton')
		var f = xGetElementById('topText')
		if (ty !=1) {
			nowScrollPos = xScrollTop()
			//alert("Init scrolPos: "+scrollPos+"\nNow: "+ nowScrollPos+"\nHeight: "+cHeight+"\n"+ parseFloat((scrollPos+cHeight-nowScrollPos)/cHeight)  )
			if (  parseFloat((scrollPos+cHeight-nowScrollPos)/cHeight)  < 1.2 ) return
			scrollTimer = setInterval('floatButton(cHeight)', 1000); //Needed for FF since window.onscroll is quirky
			if (pwidth > 100) pwidth = (pwidth/self.screen.width*100) // incase pwidth is still in pixels
			var temp = xClientWidth()*(100 - pwidth)/200 - 30
			e.style.left = parseInt(temp) + 'px'
			var barHeight = (cHeight)*.9
			f.style.height = parseInt(barHeight) +  'px'
			e.style.visibility = 'visible';
		}
		else {
			clearInterval(scrollTimer)
			e.style.visibility = 'hidden'
			window.location.hash='bottom'
		}
		}
	catch (err) {
		return
	}
}

