/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +--------------------------------------------------------+
// | Internet Explorer 5.5 & Firefox 1.0                    |
// +--------------------------------------------------------+
// | Copyright (c) 2007 Song Hyo-Jin                        |
// +--------------------------------------------------------+
// | This source file is BSD license.                       |
// +--------------------------------------------------------+
// | Author : Song Hyo-Jin <crosser at hanmail dot net>     |
// |                                  (MSN Messengerable)   |
// +--------------------------------------------------------+
//
// $Id: follow_banner.js, v 0.0.1 2007/03/13 19:00:00 crosser Exp $
//
// follow banner
String.prototype.toInt = function() {
	if(/^-/.test(this)) {
		return this.replace(/[^0-9]/g, '') * -1;
	} else {
		return this.replace(/[^0-9]/g, '') * 1;
	}
}

function getStatus(obj) {
	obj.clientWidth = document.documentElement.clientWidth || document.body.clientWidth;
	obj.clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
	obj.offsetWidth = document.documentElement.offsetWidth || document.body.offsetWidth;
	obj.offsetHeight = document.documentElement.offsetHeight || document.body.offsetHeight;
	obj.offsetLeft = document.documentElement.offsetLeft || document.body.offsetLeft;
	obj.offsetTop = document.documentElement.offsetTop || document.body.offsetTop;
	obj.scrollWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
	obj.scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
	obj.scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
	obj.scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
}

function follow_banner(tag, scrollType, x, y)
{
	this.tag = tag;
	this.x = false;
	this.y = false;
	getStatus(this);

	if(!scrollType) {
		scrollType = 1;
	}
	if(scrollType & 1) {
		if(!tag.style.top) {
			tag.style.top = "0px";
		}
		this.top = tag.style.top.toInt();
		this.y = true;
		if(y) {
			this.top = y;
			this.height = tag.style.height.toInt();
		}
	}
	if(scrollType & 2) {
		if(!tag.style.left) {
			tag.style.left = "0px";
		}
		this.left = tag.style.left.toInt();
		this.x = true;
		if(x) {
			this.left = x;
			this.width = tag.style.width.toInt();
		}
	}

	this.loop = function(fName) {
		if(fName) {
			this.fName = fName;
		}
		getStatus(this);
		var gab;
		if(this.y) {
			if(this.top >= 0) {
				var newTop = this.top + this.scrollTop;
			} else {
				var newTop = this.scrollTop + this.scrollHeight + this.top - this.height;
				if(document.all) {
					newTop -= 18;
				}
			}
			var oldTop = this.tag.style.top.toInt();
			if(oldTop != newTop) {
				gab = newTop - oldTop;
				gab = Math.ceil(gab * .1);
				if(gab == 0) {
					gab = -1;
				}
				this.tag.style.top = (oldTop + gab) + "px";
			}
		}
		if(this.x) {
			if(this.left >= 0) {
				var newLeft = this.left + this.scrollLeft;
			} else {
				var newLeft = this.scrollLeft + this.scrollWidth + this.left - this.width;
				if(document.all) {
					newLeft -= 18;
				}
			}
			var oldLeft = this.tag.style.left.toInt();
			if(oldLeft != newLeft) {
				gab = newLeft - oldLeft;
				gab = Math.ceil(gab * .1);
				if(gab == 0) {
					gab = -1;
				}
				this.tag.style.left = (oldLeft + gab) + "px";
			}
		}
		setTimeout(this.fName + ".loop();", 10);
	}
}
