// floatbox.js - JavaScript
// Copyright 2004 Sofrosune. All rights reserved.
// Author: Sofrosune; www.sofrosune.net
// No part of this program may be reproduced or transmitted in any form or 
// by any means without permission from the author, Sofrosune.
//
// Date: April 1, 2005.
// Version: 1.00; April 1, 2005.
// Version: 1.01; April 6, 2005. (Added testing (y - top) > doc_height)
// Version: 1.02; May 22, 2005. (Changed for testing to (y - top) > page_height)
// Version: 1.02b; September 23, 2005. (fixed doc_width, doc_height)

// Usage:
/**
<head>
	<script type="text/javascript" src="../scripts/floatbox.js"></script>
</head>
<body ONLOAD="floatbox_init('floatbox_id',-160,-40);">
<div id="floatbox_id" style="position:absolute; top:0px; left:800px; width:100px; visibility:hidden;">[ Back to Top ]</div>
<script type="text/javascript">floatbox_init('floatbox_id',-160,-40);</script>
</body>

You should initialize floatbox library using either method of those followings;
(1) call floatbox_init function on onload event.
(2) call floatbox_init function on demand.
*/

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// floatbox_init
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

function floatbox_init(id,offset_x,offset_y,margin_top,margin_bottom) {
	if (margin_top == undefined) { margin_top = 200; }
	if (margin_bottom == undefined) { margin_bottom = 20; }
	var func = "floatbox(\"id\",offset_x,offset_y,margin_top,margin_bottom)";
	func = func.replace("id",id).replace("offset_x",offset_x).replace("offset_y",offset_y).replace("margin_top",margin_top).replace("margin_bottom",margin_bottom);
	setInterval(func,100);
}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// floatbox
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Usage:
//	setInterval("floatbox('id',-160,-40,200,20)",100);
//	options:
//		id - div id
//		offset_x - from left for plus; from right for minus;
//		offset_y - from top for plus; from bottom for minus;
//		margin_top - threshold at top for visibility
//		margin_bottom - threshold at bottom for visibility

function floatbox(id,offset_x,offset_y,margin_top,margin_bottom) {

	var is_ie = ((document.all) ? true:false);
	var is_w3c = ((document.getElementById) ? true:false);
	var is_ns = ((document.layers) ? true:false);
	var is_gecko2004 = false;

	var obj = null;
	var style = null;
	var page_left = 0;
	var page_top = 0;
	var page_right = 0;
	var page_bottom = 0;
	var page_width = 0;
	var page_height = 0;
	var doc_width = 0;
	var doc_height = 0;
	var scroll_width = 0;
	var scroll_height = 0;

	if (is_ie) {
		obj = document.all[id];
		style = obj.style;
		page_left = document.body.scrollLeft;
		page_top = document.body.scrollTop;
		page_right = page_left + document.body.clientWidth;
		page_bottom = page_top + document.body.clientHeight;
		page_width = page_right - page_left;
		page_height = page_bottom - page_top;
		doc_width = document.body.clientWidth; //document.body.offsetWidth;
		doc_height = document.body.clientHeight; //document.body.offsetHeight;
		scroll_width = document.body.scrollWidth;
		scroll_height = document.body.scrollHeight;
	} else if (is_w3c) {
		obj = document.getElementById(id);
		style = obj.style;
		page_left = window.pageXOffset;
		page_top = window.pageYOffset;
		page_right = page_left + window.innerWidth ;
		page_bottom = page_top + window.innerHeight;
		page_width = page_right - page_left;
		page_height = page_bottom - page_top;
		doc_width = window.innerWidth; //document.body.offsetWidth;
		doc_height = window.innerHeight; //document.body.offsetHeight;
		scroll_width = document.body.scrollWidth;
		scroll_height = document.body.scrollHeight;
	} else if (is_ns) {
		obj = document[id];
		style = obj;
		page_left = window.pageXOffset;
		page_top = window.pageYOffset;
		page_right = page_left + window.innerWidth ;
		page_bottom = page_top + window.innerHeight;
		page_width = page_right - page_left;
		page_height = page_bottom - page_top;
		doc_width = window.innerWidth;
		doc_height = window.innerHeight;
	//	scroll_width = document.body.scrollWidth;
	//	scroll_height = document.body.scrollHeight;
		scroll_width = page_right + offset_z;
		scroll_height = page_bottom + offset_z;
	}

	var x = (offset_x > 0 ? page_left : page_right) + offset_x;
	var y = (offset_y > 0 ? page_top : page_bottom) + offset_y;
	var left = parseInt(style.left); // replace("px","")
	var top = parseInt(style.top); // replace("px","")
//	if ((y - top) > doc_height) { top = page_bottom; } // mod on April 6, 2005
	if ((y - top) > page_height) { top = page_bottom; } // mon on May 22, 2005
	style.left = left + (x - left) / 4.0; // x
	style.top = top + (y - top) / 4.0; // y
	if ((y - top) < offset_y) { style.top = y - offset_y; }

	if ((page_top < margin_top) || (page_bottom > (scroll_height - margin_bottom))) {
		style.visibility = "hidden";
//	} else if ((page_left < offset_z) || (page_right > (scroll_width - offset_z))) {
//		style.visibility = "hidden";
	} else {
		style.visibility = "visible";
	//	obj.innerHTML = "("+x+","+y+")-("+left+","+top+")";
	//	obj.innerHTML = "("+y+","+top+")-<br>("+page_height+","+is_ie+","+is_w3c+")";
	//	obj.innerHTML = "("+page_left+","+page_top+")-("+page_right+","+page_bottom+")";
	//	obj.innerHTML = "("+scroll_width+","+scroll_height+")";
	}
}

// end of javascript
