// Frames-per-second for transitions.
var gResolution = 30;

function find_pos (obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function tbanner (pDivId, pHeight, pWidth, pIn, pInLength, pOut, pOutLength, pDuration, pNextId, pDelay) {
	if (typeof(pDivId) != "undefined") {
		this.cDiv = document.getElementById(pDivId);			
		this.cId = pDivId;
	}
	if (typeof(pHeight) != "undefined") {
		this.cHeight = pHeight;
	} else {
		this.cHeight = 90;
	}
	if (typeof(pWidth) != "undefined") {
		this.cWidth = pWidth;
	} else {
		this.cWidth = 400;
	}
	if (typeof(pIn) != "undefined") {
		if (pIn == "random") {
			this.cInRand = true;
		} else {
			this.cIn = pIn;
			this.cInRand = false;
		}
	} else {
		this.cIn = 'fade';
	}
	if (typeof(pInLength) != "undefined") {
		this.cInLength = pInLength * gResolution;
		if (this.cInLength < 1) {
			this.cInLength = 1;
		}
	} else {
		this.cInLength = gResolution;
	}
	if (typeof(pOut) != "undefined") {
		if (pOut == "random") {
			this.cOutRand = true;
		} else { 
			this.cOut = pOut;
			this.cOutRand = false;
		}
	} else {
		this.cOut = 'fade';
	}
	if (typeof(pOutLength) != "undefined") {
		this.cOutLength = pOutLength * gResolution;
		if (this.cOutLength < 1) {
			this.cOutLength = 1;
		}
	} else {
		this.cOutLength = gResolution;
	}
	if (typeof(pDuration) != "undefined") {
		this.cDuration = pDuration * 1000;
	} else {
		this.cDuration = 5000;
	}
	this.cNextId = pNextId;
	if (typeof(pDelay) != "undefined") {
		this.cDelay = pDelay * 1000;
	} else {
		this.cDelay = 1000;
	}
	
	// Get supported opacity format.
	if (typeof(this.cDiv.style.opacity) != "undefined") {
		this.cOpacityType = "w3c";
	}
	else if (typeof(this.cDiv.style.MozOpacity) != "undefined") {
		this.cOpacityType = "moz";
	}
	else if (typeof(this.cDiv.style.KhtmlOpacity) != "undefined") {
		this.cOpacityType = "khtml";
	}
	else if (typeof(this.cDiv.filters) == "object") {
		this.cOpacityType = "ie";
		// Create the appropriate filter object.
		this.cDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100.0);";

	}
	else {
		this.cOpacityType = "none";
	}

	// Set up bounding box.
	this.cBBox = this.cDiv.parentNode.appendChild(document.createElement("div"));
	this.cBBox.appendChild(this.cDiv);
	this.cBBox.style.position = "relative";
	this.cBBox.style.width = this.cWidth + 'px';
	this.cBBox.style.height = this.cHeight + 'px';
	this.cBBox.style.overflow = "hidden";
	this.cBBox.style.visibility = "hidden";
	this.cBBox.style.display = "none";
	this.cDiv.style.position = "absolute";
	this.cDiv.style.top = "0px";
	this.cDiv.style.left = "0px";
	this.cDiv.style.width = this.cWidth + 'px';
	this.cDiv.style.height = this.cHeight + 'px';

	this.cCounter = 0;
	this.cTimerId = 0;
	
	// Hide div.
	this.cDiv.style.display = "none";
	this.cDiv.style.visibility = "hidden";
	
	this.transIn = function () {
		if (this.cCounter == 0) {
			this.resetDiv();
			if (this.cInRand) {
				this.cIn = this.pickRandomTransition();
			}
		}
		switch (this.cIn) {
			case "fade":
				lOpacity = (1.0 / this.cInLength) * this.cCounter;
				switch(this.cOpacityType)
				{
					case "ie":
						this.cDiv.filters[0].opacity = (lOpacity * 100.0);
						break;
					case "khtml":
						this.cDiv.style.KhtmlOpacity = lOpacity;
						break;
					case "moz": 
						this.cDiv.style.MozOpacity = (lOpacity == 1 ? 0.9999999 : lOpacity);
						break;
					default:
						this.cDiv.style.opacity = (lOpacity == 1 ? 0.9999999 : lOpacity);
				}
				break;
			case "scroll":
			case "scroll-left":
				this.cDiv.style.left = -this.cDiv.offsetWidth + ((this.cDiv.offsetWidth / this.cInLength) * this.cCounter) + "px";
				break;
			case "scroll-right":
				this.cDiv.style.left = this.cDiv.offsetWidth - ((this.cDiv.offsetWidth / this.cInLength) * this.cCounter) + "px";
				break;
			case "scroll-down":
				this.cDiv.style.top = -this.cDiv.offsetHeight + ((this.cDiv.offsetHeight / this.cInLength) * this.cCounter) + "px";
				break;
			case "scroll-up":
				this.cDiv.style.top = this.cDiv.offsetHeight - ((this.cDiv.offsetHeight / this.cInLength) * this.cCounter) + "px";
				break;
			case "wipe":
			case "wipe-left":
				this.cDiv.style.clip = "rect(0px " + ((this.cDiv.offsetWidth / this.cInLength) * this.cCounter) + "px " + this.cDiv.offsetHeight + "px 0px)";
				break;
			case "wipe-right":
				this.cDiv.style.clip = "rect(0px " + this.cDiv.offsetWidth + "px " + this.cDiv.offsetHeight + "px " + (this.cDiv.offsetWidth - ((this.cDiv.offsetWidth / this.cInLength) * this.cCounter)) + "px)";
				break;
			case "wipe-down":
				this.cDiv.style.clip = "rect(0px " + this.cDiv.offsetWidth + "px " + ((this.cDiv.offsetHeight / this.cInLength) * this.cCounter) + "px 0px)";
				break;
			case "wipe-up":
				this.cDiv.style.clip = "rect(" + (this.cDiv.offsetHeight - ((this.cDiv.offsetHeight / this.cInLength) * this.cCounter)) + "px " + this.cDiv.offsetWidth + "px " + this.cDiv.offsetHeight + "px 0px)";
				break;
		}
		if (this.cCounter > 0) {
			this.cDiv.style.visibility = "visible";
			this.cBBox.style.visibility = "visible";
		}

		if (this.cTimerId == 0) {
			this.cTimerId = setInterval(this.cId + '.transIn()', 1000 / gResolution);
			this.cCounter++;			
		} else if (this.cCounter >= this.cInLength) {
			clearInterval(this.cTimerId);
			this.cTimerId = 0;
			this.cCounter = 0;
			setTimeout(this.cId + '.transOut()', this.cDuration);
		} else { 
			this.cCounter++;		
		}
	}
	
	this.transOut = function () {	
		if (this.cCounter == 0) {
			if (this.cOutRand) {
				this.cOut = this.pickRandomTransition();
			}
		}
		switch (this.cOut) {
			case "fade":
				lOpacity = 1.0 - ((1.0 / this.cOutLength) * this.cCounter);
				switch(this.cOpacityType)
				{
					case "ie":
						this.cDiv.filters[0].opacity = (lOpacity * 100.0);
						break;
					case "khtml":
						this.cDiv.style.KhtmlOpacity = lOpacity;
						break;
					case "moz": 
						this.cDiv.style.MozOpacity = (lOpacity == 1 ? 0.9999999 : lOpacity);
						break;
					default:
						this.cDiv.style.opacity = (lOpacity == 1 ? 0.9999999 : lOpacity);
				}
				break;
			case "scroll":
			case "scroll-left":
				this.cDiv.style.left = ((this.cDiv.offsetWidth / this.cOutLength) * this.cCounter) + "px";
				break;
			case "scroll-right":
				this.cDiv.style.left = -((this.cDiv.offsetWidth / this.cOutLength) * this.cCounter) + "px";
				break;
			case "scroll-down":
				this.cDiv.style.top = ((this.cDiv.offsetHeight / this.cOutLength) * this.cCounter) + "px";
				break;
			case "scroll-up":
				this.cDiv.style.top = -((this.cDiv.offsetHeight / this.cOutLength) * this.cCounter) + "px";
				break;
			case "wipe":
			case "wipe-left":
				this.cDiv.style.clip = "rect(0px " + this.cDiv.offsetWidth + "px " + this.cDiv.offsetHeight + "px " + ((this.cDiv.offsetWidth / this.cOutLength) * this.cCounter) + "px)";
				break;
			case "wipe-right":
				this.cDiv.style.clip = "rect(0px " + (this.cDiv.offsetWidth - ((this.cDiv.offsetWidth / this.cOutLength) * this.cCounter)) + "px " + this.cDiv.offsetHeight + "px 0px)";
				break;
			case "wipe-down":
				this.cDiv.style.clip = "rect(" + ((this.cDiv.offsetHeight / this.cOutLength) * this.cCounter) + "px " + this.cDiv.offsetWidth + "px " + this.cDiv.offsetHeight + "px 0px)";
				break;
			case "wipe-up":
				this.cDiv.style.clip = "rect(0px " + this.cDiv.offsetWidth + "px " + (this.cDiv.offsetHeight - ((this.cDiv.offsetHeight / this.cOutLength) * this.cCounter)) + "px 0px)";
				break;				
		}

		if (this.cTimerId == 0) {
			this.cTimerId = setInterval(this.cId + '.transOut()', 1000 / gResolution);			
			this.cCounter++;			
		} else if (this.cCounter >= this.cOutLength) {
			this.cDiv.style.display = "none";
			this.cDiv.style.visibility = "hidden";
			this.cBBox.style.display = "none";
			this.cBBox.style.visibility = "hidden";			
			clearInterval(this.cTimerId);
			this.cTimerId = 0;
			this.cCounter = 0;
			if (typeof(this.cNextId) != "undefined") {
				setTimeout(this.cNextId + '.transIn()', this.cDelay);
			}
		} else {
			this.cCounter++;		
		}
	}	

	this.resetDiv = function () {
		this.cDiv.style.display = "block";
		this.cBBox.style.display = "block";
		this.cDiv.style.top = "0px";
		this.cDiv.style.left = "0px";	
		this.cDiv.style.clip = "rect(0px " + this.cDiv.offsetWidth + "px " + this.cDiv.offsetHeight + "px 0px)";
		switch(this.cOpacityType)
		{			
			case "ie":
				this.cDiv.filters[0].opacity = 100.0;
				break;
			case "khtml":
				this.cDiv.style.KhtmlOpacity = 1.0;
				break;
			case "moz": 
				this.cDiv.style.MozOpacity = 1.0;
				break;
			default:
				this.cDiv.style.opacity = 1.0;
		}
	}

	this.pickRandomTransition = function () {
		lRand = Math.floor(Math.random() * 12) + 1;
		switch (lRand) {
			case 1:
			case 2:
			case 3:
			case 4:
				lTrans = "fade";
				break;
			case 5:
				lTrans = "scroll-left";
				break;
			case 6:
				lTrans = "scroll-right";
				break;
			case 7:
				lTrans = "scroll-up";
				break;
			case 8:
				lTrans = "scroll-down";
				break;
			case 9:
				lTrans = "wipe-left";
				break;
			case 10:
				lTrans = "wipe-right";
				break;
			case 11:
				lTrans = "wipe-up";
				break;
			case 12:
				lTrans = "wipe-down";
				break;
		}
		return lTrans;
	}
}

