/*
* Standard Javascript
* vim:ts=2
*/
var e = new Standard();

function Standard()
{
	this.ua = ua = navigator.userAgent;
	this.lang = (navigator.userLanguage || navigator.language);

	this.isIE = this.isWinIE = this.isMacIE = this.isGecko = this.isOpera = this.isSafari = false;
	if (ua.indexOf('Opera') != -1) {
		this.isOpera = true;
	} else if (ua.indexOf('MSIE') != -1) {
		this.isIE = true;
		if (ua.indexOf('Mac') != -1) {
			this.isMacIE = true;
		} else if (ua.indexOf('Win') != -1) {
			this.isWinIE = true;
		}
	} else if (ua.indexOf('Safari') != -1) {
		this.isSafari = true;
	} else if (ua.indexOf('Gecko') != -1) {
		this.isGecko = true;
	}
	this.contents = "/";
	this.cookiePath = "/";

	this.popsz = {w:480, h:270, adj:16};
	this.popup = false;
	this.popupURL = false;
	this.timerID = false;
}

/*
* Startup of Standard object
*/
Standard.prototype.onload = function()
{
	if ((this.oH1 = document.getElementsByTagName('H1').item(0))) {
		if (this.oH1.getAttribute('id')) {
			this.idTop = this.oH1.getAttribute('id');
		} else {
			this.idTop = '_standard_top_h1';
			this.oH1.setAttribute('id', this.idTop);
		}
	}
}

Standard.prototype.noframe = function()
{
	if (top.frames.length > 0) {
		top.window.location = self.window.location;
	}
}

Standard.prototype.onshowpopup = function()
{
	if (e.timerID != 0) {
		clearTimeout(e.timerID);
		e.timerID = 0;
	}
}

Standard.prototype.onhidepopup = function()
{
	e.timerID = setTimeout('e.hidepopup();', 500);
}

Standard.prototype.showpopup = function(url, fixedh)
{
	if (!$('container')) return;
	ev = getEvent();

	if (e.timerID != 0) {
		clearTimeout(e.timerID);
		e.timerID = 0;
	}
	if (!e.popup || e.popupURL != url) {
		e.hidepopup();
		if (!e.popup) {
			e.popup = document.createElement('div');
			e.popup.setAttribute('id', 'dialog');
			e.popup.innerHTML = '<div id="dialogin" class="roundw"><img src="' + e.contents + 'images/indicator.gif" style="margin:120px 4px;" /></div>';
			e.popup.onmouseover = e.onshowpopup;
			e.popup.onmouseout = e.onhidepopup;
			if (e.isIE || e.isOpera) {
				if (!ev.srcElement.onmouseout) {
					ev.srcElement.onmouseout = e.onhidepopup;
				}
				var body = (document.compatMode=='CSS1Compat') ? document.documentElement : document.body;
				var rect = getOffsetRect(ev.srcElement);
				e.popup.style.pixelLeft = rect.x;
				e.popup.style.pixelTop  = rect.y + rect.h;
				e.popup.style.width     = e.popsz.w;
				e.popup.style.minHeight = e.popsz.h;
				xscroll = body.clientWidth + body.scrollLeft;
				yscroll = e.isOpera ? body.clientHeight : (body.clientHeight + body.scrollTop);
				if (xscroll < rect.x + (e.popsz.w + e.popsz.adj)) {
					e.popup.style.pixelLeft = body.clientWidth - (e.popsz.w + e.popsz.adj);
				}
				if (yscroll < rect.y + rect.h + (e.popsz.h + e.popsz.adj)) {
					e.popup.style.pixelTop = rect.y - (e.popsz.h + e.popsz.adj);
					if (body.scrollTop > e.popup.style.pixelTop) {
						e.popup.style.pixelTop = body.scrollTop;
					}
				}
				if (fixedh != '') {
					e.popup.style.height = fixedh;
					e.popup.style.overflowY = 'scroll';
				}
			} else {
				if (!ev.srcElement.onmouseout) {
					ev.target.onmouseout = e.onhidepopup;
				}
				alert('Sorry not Implemented.');
			}
			/* Ajax action */
			$('container').appendChild(e.popup);
			if (url.indexOf('?') == -1) {
				new Ajax.Updater('dialogin', url, {method:'get', onSuccess:e.applyCorner});
			} else {
				var uri = url.substring(0, url.indexOf('?'));
				var str = url.substring(url.indexOf('?')+1);
				new Ajax.Updater('dialogin', uri, {method:'get', parameters:str, onSuccess:e.applyCorner});
			}
		}
		e.popupURL = url;
	}
}

Standard.prototype.hidepopup = function()
{
	e.timerID = 0;
	if(e.popup) {
		e.popup.style.visibility = "hidden";
		e.popup.parentNode.removeChild(e.popup);
		e.popup = false;
	}
}

Standard.prototype.applyCorner = function()
{
}

/*
* Get event coordinates relative to current document
* @param	ev event
* @return	the coordinates as Object
* @access public
*/
function getClientRect(ev)
{
	if (e.isOpera) {
		this.x = ev.clientX + document.body.scrollLeft;
		this.y = ev.clientY + document.body.scrollTop;
	} else if (e.isIE) {
		this.x = ev.x + document.body.scrollLeft + document.documentElement.scrollLeft;
		this.y = ev.y + document.body.scrollTop + document.documentElement.scrollTop;
	} else {
		this.x = ev.pageX;
		this.y = ev.pageY;
	}
	return this;
}

/*
* Get event coordinates relative to the screen
* @param	ev event
* @return	the coordinates as Object
* @access public
*/
function getScreenRect(ev)
{
	if (e.isSafari) {
		// Safari doesn't seem to have location relative to current window.
		// Tricky but Safari's screenY counts form the bottom of the screen (or window)
		this.x = self.innerWidth - self.screenX - ev.screenX;
		this.y = self.innerHeight - self.screenY - ev.screenY;
		this.w = self.innerWidth;
		this.h = self.innerHeight;
	} else if (e.isOpera) {
		this.x = ev.clientX;
		this.y = ev.clientY;
		this.w = document.body.clientWidth;
		this.h = document.body.clientHeight;
	} else if (e.isIE) {
		this.x = ev.x;
		this.y = ev.y;
		this.w = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
		this.h = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	} else if (e.isGecko) {
		this.x = ev.clientX;
		this.y = ev.clientY;
		this.w = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
		this.h = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	} else {
		this.x = ev.clientX;
		this.y = ev.clientY;
		this.w = window.innerWidth;
		this.h = window.innerHeight;
	}
	return this;
}

/*
* Get event coordinates relative to the body
* @param	ev event
* @return	the coordinates as Object
* @access public
*/
function getOffsetRect(ev)
{
	p = ev;
	x = y = 0;
	while (p && p != 'BODY') {
		x += p.offsetLeft;
		y += p.offsetTop;
		p = p.offsetParent;
	}
	this.x = x;
	this.y = y;
	this.w = ev.offsetWidth;
	this.h = ev.offsetHeight;
	return this;
}

function getEvent()
{
	if (window.event) return window.event;
	var caller = arguments.callee.caller;
	while (caller) {
		var ob = caller.arguments[0];
		if(ob && ob.constructor == MouseEvent) return ob;
		caller = caller.caller;
	}
	return null;
}

/*
* Wrapping function
*/
function showPopup()
{
	if (!document.createElement) return;
	if (arguments.length == 0) return;

	url = arguments[0];
	if (arguments.length >= 2) {
		fixedh = arguments[1];
	} else {
		fixedh = '';
	}
	e.showpopup(url, fixedh);
}

function hidePopup()
{
	if (!document.createElement) return;
	e.hidepopup();
}

/*
* Cookie function
*/
function setCookie(name,data,expire,path)
{
	if (name && data) {
		if (expire) {
			dt = new Date();
			dt.setDate(dt.getDate() + eval(expire));
			dt = dt.toGMTString();
			_expire = ";expires=" + dt;
		} else {
			dt = new Date();
			dt.setTime(dt.getTime() + 24 * 60 * 60 * e.expireDate * 1000);
			dt = dt.toGMTString();
			_expire = ";expires=" + dt;
		}
		if (path) {
			_path = ";path=" + path;
		} else {
			_path = ";path=" + e.cookiePath;
		}
		document.cookie = escape(arg1) + "=" + escape(arg2) + _expire + _path;
	}
}

function getCookie(name)
{
	if (name) {
		data = document.cookie;
		name = escape(name) + "=";
		nlen = name.length;
		dlen = data.length;
		i = 0;
		while (i < dlen) {
			var nloc = i + nlen;
			if (data.substring(i, nloc) == arg) {
				elen = data.indexOf(";", nloc);
				if (elen == -1) {
					elen = data.length;
				}
				return unescape(data.substring(nloc,elen));
			}
			i = data.indexOf(" ", i) + 1;
			if (i == 0) break;
		}
	}
	return false;
}

function delCookie(name)
{
	if (getCookie(name)) {
		document.cookie = name + "=" + '; expires=Thu, 01-Jan-70 00:00:01 GMT;path=' + e.cookiePath;
	}
}

/* default startup */
e.noframe();

/* default startup - Hook onload */
var __default_onload_save = window.onload;

window.onload = function()
{
	if (__default_onload_save) __default_onload_save();
	e.onload();
}
