function gebi(id) {
	return document.getElementById(id);
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight_Alt()
{
	var ih = window.innerHeight ? window.innerHeight : 0;
	var dech = document.documentElement ? document.documentElement.clientHeight : 0;
	var db = document.body ? document.body.clientHeight : 0;

	// Most browsers
	if (ih != 0) return ih;
    // IE6 doesn't have window.innerHeight
	if (dech != 0) return dech;
	// IE6 without doctype doesn't have document.documentElement...
	return db;
}

function f_clientHeight() {
	return f_clientHeight_Alt();
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_filterResultsAlt(n_win, n_docel, n_body) {

	return n_body;
}

function _NotifyScript()
{
	this.marginX = 10;
	this.marginY = 10;

	this.resizeIframe = function()
	{
		this.iframe.style.width = (this.iWidth) + 'px';
		this.iframe.style.height = (this.iHeight) + 'px';
		this.iframeDiv.style.width = (this.iWidth) + 'px';
		this.iframeDiv.style.height = (this.dHeight) + 'px';
	}

	this.updatePopupPosition = function()
	{
			var l = document.body.scrollLeft + f_clientWidth() - this.iframe.offsetWidth - this.marginX;
			var t = f_scrollTop() + f_clientHeight() - this.iframe.offsetHeight + this.iHeight - this.dHeight - this.marginY;

			this.iframeDiv.style.height = this.dHeight + 'px';
			this.iframeDiv.style.top = (t) + 'px';
			this.iframeDiv.style.left = (l) + 'px';
	}

	this.doPopup = function(t) {
		setTimeout(
			(function(_this, t){
				return function() {

					t.dHeight = 0;
					t.updatePopupPosition();

					t.popupTimer = setInterval((function(t) {return function()
					{
						t.dHeight+=4;

						if (t.dHeight >= t.iHeight + 1 ) {
							clearInterval(t.popupTimer);
							t.dHeight = t.iHeight + 1;
						}
						t.updatePopupPosition();
					}})(t), 10);

				};
			})(this, t),
			3000
		);
	}

	this.popupNotify = function()
	{
		this.iframeDiv.innerHTML = '<iframe src="' + this.toolUrl + '" id="iframe" scrolling="no" frameborder="0" allowtransparency="true" style="width:0;height:0; overflow:hidden;" onload="_notifyScript.doPopup(_notifyScript)"></iframe>';
		this.iframe = gebi('iframe');
		this.resizeIframe();
	}

	this.hideNotify = function()
	{
		this.popupTimer = setInterval((function(t) {return function()
		{
			t.dHeight-=4;

			if (t.dHeight <= 1) {
				clearInterval(t.popupTimer);
				t.dHeight = 1;
			}
			t.updatePopupPosition();
		}})(this), 10)
	}


	this.scroll = function() {this.updatePopupPosition();}
	this.resize = function() {this.updatePopupPosition();}

	this.start = function()
	{
		// Initial Values
		this.dHeight = 0;

		this.iframeDiv = document.createElement('div');
		this.iframeDiv.style.position = 'absolute';
		this.iframeDiv.style.zIndex = 10000;
		this.iframeDiv.style.padding = '1px 0 0 0';
		this.iframeDiv.style.overflow = 'hidden';
		this.iframeDiv.style.height = "1px";
		
		document.getElementsByTagName('body')[0].appendChild(this.iframeDiv);

		switch (this.mode) {
			case 'simple':
				this.popupNotify();
				break;

			case 'timer':
				setTimeout((function(t){return function ()
				{
					t.popupNotify();
				}})(this), 7000);
				break;

			case 'replaceonce':
				this.popupNotify();
				setTimeout((function(t){return function ()
				{
					t.hideNotify();
					setTimeout((function(t){return function ()
					{
						t.popupNotify();
					}})(t), 2000);
				}})(this), 18000);
				break;

			case 'replaceforever':
				this.popupNotify();
				setInterval((function(t){return function ()
				{
					t.hideNotify();
					setTimeout((function(t){return function ()
					{
						t.popupNotify();
					}})(t), 2000);
				}})(this), 18000);
				break;
		}
	}


}

var _notifyScript = new _NotifyScript();

// Defaults
_notifyScript.toolUrl = notifyScript_toolUrl;
_notifyScript.mode    = notifyScript_mode;
_notifyScript.iWidth  = parseInt(notifyScript_iWidth);
_notifyScript.iHeight = parseInt(notifyScript_iHeight);
_notifyScript.marginX = parseInt(notifyScript_marginX);
_notifyScript.marginY = parseInt(notifyScript_marginY);

if (window.addEventListener)
{
	window.addEventListener('load', function() {_notifyScript.start();}, false);
	window.addEventListener('scroll', function() {_notifyScript.scroll();}, false);
	window.addEventListener('resize', function() {_notifyScript.scroll();}, false);
}
else if (window.attachEvent)
{
	window.attachEvent('onload', function() {_notifyScript.start();});
	window.attachEvent('onscroll', function() {_notifyScript.scroll();});
	window.attachEvent('onresize', function() {_notifyScript.scroll();});
}
