/* Floating Content Random 1.0 */
/* Author: Doan Van Tung */

var persistclose = 0; //set to 0 or 1. 1 means once the bar is manually closed, it will remain closed for browser session
var startX = 0; //set x offset of bar in pixels
var startY = 0; //set y offset of bar in pixels
var verticalpos="fromtop" //enter "fromtop" or "frombottom"
var adinterval;
var dvContainerID;
var dvContentID;
var counter = 0;
var timeShow = 10; // giay
var timeHide = 5; // giay
var isShow = true;
var wWidth;
var wHeight;
var cHeight;
var cWidth;
var isFireFox = (navigator.userAgent.indexOf("Firefox")!=-1);


function iecompattest(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function get_cookie(Name) {
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
	if (offset != -1) {
		offset += search.length
		end = document.cookie.indexOf(";", offset);
		if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

function closeContent(){
	if (persistclose)
		document.cookie="remainclosed=1";
	document.all(dvContainerID).style.visibility="hidden";
	window.clearInterval(adinterval);
}

function staticbar(){
	barheight= document.all(dvContainerID).offsetHeight;
	var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
	var d = document;
	function ml(id){
		var el=d.all(id);
		if (!persistclose || persistclose && get_cookie("remainclosed")=="")
			el.style.visibility="visible";
		if (d.layers)	el.style=el;
		el.sP = function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
		el.x = startX;
		if (verticalpos=="fromtop")
		el.y = startY;
		else{
		el.y = ns ? pageYOffset + innerHeight : iecompattest().scrollTop + iecompattest().clientHeight;
		el.y -= startY;
		}
		return el;
	}
	window.stayTopLeft = function() {
		if (verticalpos=="fromtop"){
		var pY = ns ? pageYOffset : iecompattest().scrollTop;
		ftlObj.y += (pY + startY - ftlObj.y)/8;
		}
		else{
		var pY = ns ? pageYOffset + innerHeight - barheight: iecompattest().scrollTop + iecompattest().clientHeight - barheight;
		ftlObj.y += (pY - startY - ftlObj.y)/8;
		}
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("stayTopLeft()", 10);
	}
	ftlObj = ml(dvContainerID);
	stayTopLeft();
}

function randNum(minVal, maxVal) {
	var rand_no = Math.random();
	rand_no = rand_no * maxVal + (minVal - 1);
	return Math.ceil(rand_no);			
}

function showContent() {
	var content = document.all(dvContentID);		
	var xPos = randNum(0, wWidth);
	var yPos = randNum(0, wHeight);
	
	if ((wWidth - xPos) < cWidth) xPos = wWidth - cWidth;
	if ((wHeight - yPos) < cHeight) yPos = wHeight - cHeight;
	content.sP = function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
	content.sP(xPos, yPos);
}

function doShowContent() {
	var content = document.all(dvContentID);		
	counter++;		
	
	if (isShow) {
		if (counter >= timeShow) {
			isShow = false;
			counter = 0;
			opacity(dvContentID, 100, 0, 1000);
		}			
	} else {
		if (counter >= timeHide) {
			isShow = true;
			counter = 0;				
			showContent();	
			opacity(dvContentID, 0, 100, 1000);		
		}
	}
}

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.all(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
} 	

function init_floatingRND(divContainerID, divContentID, tShow, tHide) {
	dvContainerID = divContainerID;	
	dvContentID = divContentID;
	timeShow = tShow;
	timeHide = tHide;
	wWidth = isFireFox? (innerWidth - 150) : (document.body.offsetWidth - 150);
	wHeight = isFireFox? (innerHeight - 150) : (document.body.offsetHeight - 150);

	cHeight = document.getElementById(dvContentID).offsetHeight;
	cWidth = document.getElementById(dvContentID).offsetWidth;


	if (window.addEventListener)
		window.addEventListener("load", staticbar, false)
	else if (window.attachEvent)
		window.attachEvent("onload", staticbar)
	else if (document.all)
		window.onload=staticbar;	

	adinterval = window.setInterval('doShowContent()', 1000);

	showContent();
	opacity(dvContentID, 0, 100, 1000);
}
