// 
// You may use this code for free on any web page provided that 
// these comment lines and the following credit remain in the code.
// Floating Div from http://www.javascript-fx.com
//	 
// Adapted by zhangxun2001@hotmail.com 
// 2006/08/16 adjust dcw
// 2005/11 add floatBar()
// 2005/01 
// 
// To Do:
//		- different ids
//

var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
var floatbar_Switch = true;

// ***********
//
// Public
//
// ***********

floatBar(); // must put before set rcol style
//floatbar_run();
floatbar_priv_adjustSize("rightcol", "pagecell1");


function floatbar_run(offs) {
	floatbar_priv_adjustSize("rightcol2", "pagebody");
	
	var pnv = JSFX_FloatDiv("sideBar", 580 , 45, offs); // sx, sy(top)
	if (pnv) 
		pnv.floatIt();
}

//XX merge
function floatBar() {
	var pnv;
	var defDiv = "pageNav";
	
	if ($(defDiv)) {
		pnv = JSFX_FloatDiv(defDiv, 580 , 55, 27); // old 580 useful ?
	}
	if (pnv) 
		pnv.floatIt();
}


// ***********
//
// Private
//	
// ***********

function floatbarOpen() {
	floatbar_Switch = true;
}

function floatbarClose() {
	floatbar_Switch = false;
}
	

function floatbar_priv_adjustSize(n1, n2) {
	var rcol = d.getElementById(n1);
	var col2 = d.getElementById(n2);
	
	//!! set size
	var scrollh = col2.scrollHeight;
	rcol.style.height = scrollh;
	rcol.style.width = "178px"; //xx
}
	//trace("floatbar", "scrollHeight", scrollh);
/*	trace("floatbar", "col2.scrollHeight", $('col2').scrollHeight);
	trace("floatbar", "col2.style.height", $('col2').runtimeStyle.height);
	trace("floatbar", "posHeight", col2.style.posHeight);
	trace("floatbar", "pixelHeight", col2.style.pixelHeight);
	trace("floatbar", "top", col2.style.top);
	trace("floatbar", "height", col2.style.height);
	trace("floatbar", "clientHeight", col2.clientHeight);
	trace("floatbar", "scrollHeight", col2.scrollHeight);
	trace("floatbar", "scrollTop", col2.scrollTop);
	trace("floatbar", "clientTop", col2.clientTop);
	trace("floatbar", "height", col2.height);
	trace("floatbar", "screenX", window.screenX);
	trace("floatbar", "body.offsetHeight", d.body.offsetHeight);
	trace("floatbar", "body.clientHeight", d.body.clientHeight);
	trace("floatbar", "body.scrollHeight", d.body.scrollHeight);
*/	
/* screen.width > 1024 ? "17%" : screen.width > 900 ? "179px" : "23.3%"; */


function JSFX_FloatDiv(id, sx, sy, os)
{
	var val = sy; //!! temp holder, keep here
	var el = d.getElementById ? d.getElementById(id) : d.all ? d.all[id] : d.layers[id];
	//var px = d.layers ? "" : "px"; // pixel
	var pxs = "px";
    var dcw = d.body.clientWidth ;
	var offs = os;
	
	//trace("JSFX_FloatDiv", "target", id);
	
	if (el == null) return null;
	
	window[id + "_obj"] = el;
	if (d.layers) el.style = el;
	
    dcw -= dcw < 800 ? 36 : dcw < 1000 ? 44 : 50; // 800 ~ 1000 -> 44
	// old: dcw < 800 ? 36 : dcw < 1000 ? 46 : 50;
	el.cx = el.sx = dcw - el.clientWidth ? dcw - el.clientWidth : 0; // adjust start pos	
	el.cy = el.sy = sy; // start pos
	
	// set points
	el.sP=function(x,y) {
		this.style.left = x + pxs;
		this.style.top = y + pxs;
		//this.style.display = "block";
	};

	el.floatIt=function() {
		var pX, pY;		
		
		//traceRun("JSFX_FloatDiv.floatIt");
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
			d.documentElement && d.documentElement.clientWidth ? 
			d.documentElement.clientWidth : d.body.clientWidth;
		pY = ns ? pageYOffset : d.documentElement && d.documentElement.scrollTop ? 
			d.documentElement.scrollTop : d.body.scrollTop;
		
		if (this.sy < 0) 
			pY += ns ? innerHeight : d.documentElement && d.documentElement.clientHeight ? 
				d.documentElement.clientHeight : d.body.clientHeight;
				
		this.cx += (pX + this.sx - this.cx)/8;

	    val += (pY + this.sy - val)/8;
		if (val > 300) // ?300
	      this.cy += (pY + this.sy - this.cy)/8 - offs; // ? - 27
 		else
		   this.cy = this.sy ;
		     		   
		this.sP(this.cx, this.cy);
		
		if (!floatbar_Switch) return;
		setTimeout(this.id + "_obj.floatIt()", 30); //
	}
	
	//traceRun("JSFX_FloatDiv", "To return");
	return el;
} 
	
