
var loadEvent = {
	fnList : [],
	timer : null,
	domLoadElementId : "dom-load",
	add : function(fn)
	{
		loadEvent.fnList.push(fn)
		if(loadEvent.domLoaded == undefined)
		{
			loadEvent.domLoaded=false;
			loadEvent.domLoad();
			window.onload = function()
			{
				if(document.getElementById(loadEvent.domLoadElementId) == undefined)
				{
					window.defaultStatus = "Error: domLoadElementId[\""+loadEvent.domLoadElementId+"\"] is not embeded.";
				}
			}
		}
	},
	domLoad : function()
	{
		loadEvent.timer = setInterval(function() { // doesnt work in IE/Mac
		if((document.getElementsByTagName("body")[0] != null || document.body != null) && document.getElementById(loadEvent.domLoadElementId)) {
			loadEvent.runFnList();
			clearInterval(loadEvent.timer);
		}
		}, 250);
		if (typeof document.addEventListener != "undefined") {
			document.addEventListener("DOMContentLoaded", function() { loadEvent.runFnList(); clearInterval(loadEvent.timer); } , null); // Mozilla only
		}
	},
	runFnList : function()
	{
		if(loadEvent.domLoaded) return // for Mozilla, only execute once
		loadEvent.domLoaded = true; 
		for(var i=0,fn;fn=loadEvent.fnList[i];i++)
		{
			fn();
		}
	}
}

var modules = {
	
	// initModules
	init : function()
	{
		var moduleList = getElementsByClassName("module",document,"div");
		var moduleContentElm
		var moduleBlocks
		var tabSetList
		for(var i=0,moduleElm;moduleElm=moduleList[i];i++)
		{
			moduleWindowElm = getElementsByClassName("module-window",moduleElm,"div")[0];
			moduleHeaderElm = getElementsByClassName("module-header",moduleElm,"div")[0];
			moduleBlockList = getElementsByClassName("content-block",moduleElm,"div");
			tabSetElm = getElementsByClassName("tab-set",moduleElm,"ul");
			if(moduleBlockList.length>1)
			{
				if(tabSetElm.length)
				{
					modules.attachTabSet(moduleBlockList,tabSetElm[0]);		
				} 
				else
				{
					moduleNavElm = modules.createNavElm(moduleBlockList);					
					moduleHeaderElm.appendChild(moduleNavElm);
				}
				for(var j=0,blockElm;blockElm=moduleBlockList[j];j++)
				{
					blockElm.style.height = moduleWindowElm.style.height;
					if(hasClassName(moduleWindowElm,"H"))
					{
						blockElm.style.left = (blockElm.offsetWidth * j) +"px"
						blockElm.style.position = "absolute";
						blockElm.style.top = "0";
					}
				}
			}
		}
	},
	createNavElm : function(moduleBlockList)
	{
		var moduleNavElm = document.createElement("div");
		moduleNavElm.className ="module-nav";
		moduleNavElm.blockIndex = 0;
		moduleNavElm.moduleBlockList = moduleBlockList;
		
		aElm = document.createElement("a");
		aElm.className = "prev";
		aElm.href="#";
		//aElm.innerHTML = "<img src=\"img/transparent.gif\" alt=\"\" />";
		aElm.onclick = function()
		{
			var moduleNavElm = this.parentNode
			if(moduleNavElm.activeNav.index>0) 	moduleNavElm.getElementsByTagName("a")[moduleNavElm.activeNav.index].onclick();
			else moduleNavElm.getElementsByTagName("a")[moduleNavElm.moduleBlockList.length].onclick();
			return false;
		}
		moduleNavElm.appendChild(aElm);
		
		for(var i=0,aElm,moduleBlockElm;moduleBlockElm=moduleBlockList[i];i++)
		{
			aElm = document.createElement("a");
			aElm.href="#";
			//aElm.innerHTML = "<img src=\"img/transparent.gif\" alt=\"\" />";
			aElm.index = i;
			aElm.onclick = function()
			{
				var moduleNavElm = this.parentNode;
				modules.slideContent(moduleNavElm.moduleBlockList[this.index]);
				moduleNavElm.activeNav.className = "";
				this.className = "active";
				moduleNavElm.activeNav= this;
				return false;
			}
			if(i==0) {
				aElm.className = "active"
				moduleNavElm.activeNav = aElm;
			}
			moduleNavElm.appendChild(aElm);
		}
		
		aElm = document.createElement("a");
		aElm.className = "next";
		aElm.href="#";
		//aElm.innerHTML = "<img src=\"img/transparent.gif\" alt=\"\" />";
		aElm.onclick = function()
		{
			var moduleNavElm = this.parentNode
			if(moduleNavElm.activeNav.index<moduleNavElm.moduleBlockList.length-1) moduleNavElm.getElementsByTagName("a")[moduleNavElm.activeNav.index+2].onclick();
			else moduleNavElm.getElementsByTagName("a")[1].onclick();
			return false;
		}
		moduleNavElm.appendChild(aElm);
		
		return moduleNavElm;
	},
	attachTabSet : function(moduleBlockElm,moduleNavElm){
		var aList = moduleNavElm.getElementsByTagName("a");
		moduleNavElm.moduleBlockList = moduleBlockList;
		for(var i=0,aElm,moduleBlockElm;moduleBlockElm=moduleBlockList[i];i++)
		{
			aElm = aList[i];
			aElm.index = i;
			aElm.onclick = function()
			{
				var moduleNavElm = this.parentNode.parentNode; 
				modules.slideContent(moduleNavElm.moduleBlockList[this.index]);
				moduleNavElm.activeNav.parentNode.className = "";
				this.parentNode.className = "active";
				moduleNavElm.activeNav= this;
				return false;
			}
			if(i==0) {
				aElm.parentNode.className = "active"
				moduleNavElm.activeNav = aElm;
			}
		}
	},
	slideContent : function(moduleBlockElm)
	{
		var moduleContent = moduleBlockElm.parentNode;
		var moduleWindowElm = moduleContent.parentNode;
		moduleContent.id= moduleContent.id || giveUniqueID();
		moduleContent.t = 0;
		moduleContent.b = hasClassName(moduleWindowElm,"H") ? moduleContent.offsetLeft : moduleContent.offsetTop;
		moduleContent.c = hasClassName(moduleWindowElm,"H") ? -moduleBlockElm.offsetLeft : -moduleBlockElm.offsetTop;
		moduleContent.d = 30;
		setTimeout("modules.moveContent('"+moduleContent.id+"')",20);
	},
	moveContent : function(id)
	{
		var mC = document.getElementById(id);
		var moduleWindowElm = mC.parentNode;
		var LT = Math.round(easeInOut(mC.t,mC.b,(mC.c-mC.b),mC.d))+"px";
		if(++mC.t<=mC.d) setTimeout("modules.moveContent('"+id+"')",20);
		else LT = mC.c+"px"
		if(hasClassName(moduleWindowElm,"H")) mC.style.left = LT;
		else mC.style.top = LT;
	}
}

loadEvent.add(modules.init);

// ===============================================================

var friendFeatures = {
	contextMenuElm : null,
	contextMenuInviteId : "context-menu-invite",
	contextMenuAvatarId : "context-menu-avatar",
	init : function()
	{
		
		var inviteElmList = getElementsByClassName("invite",document,"tr");
		var avatarElmList = getElementsByClassName("avatar",document,"img");
		if(inviteElmList.length || avatarElmList.length) friendFeatures.createContextMenu();
		
		// invite
		if(document.getElementById(friendFeatures.contextMenuInviteId))
		{
			for(var i=0,trElm;trElm=inviteElmList[i];i++)
			{
				trElm.onmouseover = function()
				{
					clearTimeout(this.timeID);
					var reqUsername = this.getElementsByTagName("a")[0].innerHTML;
					friendFeatures.contextMenuElm.className = "context-menu invite-menu"
					friendFeatures.contextMenuElm.innerHTML = document.getElementById(friendFeatures.contextMenuInviteId).innerHTML.replace(/(\%req-username\%)/g,reqUsername);
					friendFeatures.contextMenuElm.callee = this;
					this.timeID = setTimeout("friendFeatures.showHideMenu(true)",500);
					this.onmouseout = function()
					{
						clearTimeout(this.timeID);
						this.timeID = setTimeout("friendFeatures.showHideMenu(false)",10);
					}
				}
			}
		}
		// avatar
		if(document.getElementById(friendFeatures.contextMenuAvatarId))
		{
			for(var i=0,imgElm;imgElm=avatarElmList[i];i++)
			{
				imgElm.onmouseover = function()
				{
					clearTimeout(this.timeID);
				
					var contextMenuContent_ul = document.getElementById(friendFeatures.contextMenuAvatarId).cloneNode(true);
					friendFeatures.contextMenuElm.className = "context-menu avatar-menu"
					friendFeatures.contextMenuElm.innerHTML = document.getElementById(friendFeatures.contextMenuAvatarId).innerHTML;
					if(hasClassName(this,"friend")) friendFeatures.contextMenuElm.getElementsByTagName("ul")[0].className="isFriend";
					friendFeatures.contextMenuElm.callee = this;
					this.timeID = setTimeout("friendFeatures.showHideMenu(true)",500);
					this.onmouseout = function()
					{
						clearTimeout(this.timeID);
						this.timeID = setTimeout("friendFeatures.showHideMenu(false)",10);
					}
				}
			}
		}	
		
	},
	createContextMenu : function()
	{
		friendFeatures.contextMenuElm = document.createElement("div");
		var bodyElm = document.getElementsByTagName("body")[0]
		friendFeatures.contextMenuElm.className ="context-menu";
		bodyElm.appendChild(friendFeatures.contextMenuElm);
	},
	showHideMenu : function(show)
	{
		if(show)
		{
			var offset = getGlobalOffset(friendFeatures.contextMenuElm.callee);
			friendFeatures.contextMenuElm.style.left=offset.left+5+"px";
			friendFeatures.contextMenuElm.style.top=offset.top+25+"px";
			friendFeatures.contextMenuElm.style.visibility="visible";
			friendFeatures.contextMenuElm.onmouseover = function()
			{
				clearTimeout(this.callee.timeID);
				this.onmouseout = function()
				{
					clearTimeout(this.callee.timeID);
					this.callee.timeID = setTimeout("friendFeatures.showHideMenu(false)",10);
				}
			}
		}
		else
		{
			friendFeatures.contextMenuElm.style.visibility="hidden";
		}
	},
	get : function(aElm)
	{
		userName = aElm.parentNode.parentNode.parentNode.callee.name;
		aElm.href += userName;
		return true;
	}
}

loadEvent.add(friendFeatures.init);

function getGlobalOffset(elm)
{
	var offset = {left:0,top:0};
	if (elm.offsetParent)
	{
		while (elm.offsetParent)
		{
			offset.left += elm.offsetLeft;
			offset.top += elm.offsetTop;
			elm = elm.offsetParent;
		}
	}
	return offset;
}


// ===============================================================

var dialog = {
	viewport : {},
	page : {},
	open : function(aElm,id)
	{
		var dialogBaseElm = document.getElementById("dialog-base");
		var dialogIframeElm = document.getElementById("dialog-iframe");
		if(dialogBaseElm == undefined)
		{
			dialogBaseElm = document.createElement("div");
			dialogBaseElm.id = "dialog-base";
			if(document.all)
			{
				dialogIframeElm = document.createElement("iframe");
				dialogIframeElm.id = "dialog-iframe";
			}
		}
		else
		{
			var dialogWindowElm = document.getElementById(dialogBaseElm.windowId);
			dialogWindowElm.style.display = "none";
			dialogWindowElm.style.visibility = "hidden";
		}
				
		var bodyElm = document.getElementsByTagName("body")[0]
		bodyElm.appendChild(dialogBaseElm);
		if(dialogIframeElm) 
		{
			bodyElm.appendChild(dialogIframeElm);
		}

		dialogBaseElm.windowId = id;
		dialog.resizeBase();
		
		var viewport = dialog.viewport;
		var page = dialog.page;
		
		dialogBaseElm.onclick = function()
		{
			var bodyElm = document.getElementsByTagName("body")[0];
			var dialogWindowElm = document.getElementById(this.windowId);
			dialogWindowElm.style.display = "none";
			dialogWindowElm.style.visibility = "hidden";
			
			if(this.iframe) 
			{
				bodyElm.removeChild(this.iframe);
			}
			bodyElm.removeChild(this);
		}
		
		dialogWindowElm = document.getElementById(id);
		dialogWindowElm.style.top = dialogWindowElm.style.left = "50%";
		dialogWindowElm.style.display = "block";
		dialogWindowElm.style.marginTop = Math.round(-dialogWindowElm.offsetHeight/2)+viewport.t+"px";
		dialogWindowElm.style.marginLeft = Math.round((-dialogWindowElm.offsetWidth)/2)+viewport.l+"px";
		dialogWindowElm.style.visibility = "visible";
		
		cancelBtn = getElementsByClassName("cancel",dialogWindowElm,"a")[0];
		cancelBtn.dialogBaseElm = dialogBaseElm;
		
		
		
		cancelBtn.onclick = function()
		{
			this.dialogBaseElm.onclick();
		}
		
		window.onresize = dialog.resizeBase;
		
		if(aElm) return false;
	},
	resizeBase : function()
	{
		var dialogBaseElm = document.getElementById("dialog-base");
		var dialogIframeElm = document.getElementById("dialog-iframe");
		var bodyElm = document.getElementsByTagName("body")[0];
		var viewport = dialog.viewport;
		var page = dialog.page;
		if (self.innerHeight) {	// all except Explorer
			viewport.t = self.pageYOffset;
			viewport.l = self.pageXOffset;
			viewport.w = self.innerWidth;
			viewport.h = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			viewport.t = document.documentElement.scrollTop;
			viewport.l = document.documentElement.scrollLeft;
			viewport.w = document.documentElement.clientWidth;
			viewport.h = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			viewport.t = document.body.scrollTop;
			viewport.l = document.body.scrollLeft;
			viewport.w = bodyElm.clientWidth;
			viewport.h = bodyElm.clientHeight;
		}	

		if (window.innerHeight && window.scrollMaxY) {	
			page.t = bodyElm.scrollTop
			page.w = bodyElm.scrollWidth;
			page.h = window.innerHeight + window.scrollMaxY;
		} else if (bodyElm.scrollHeight > bodyElm.offsetHeight){ // all but Explorer Mac
			page.w = bodyElm.scrollWidth;
			page.h = bodyElm.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			page.w = bodyElm.offsetWidth;
			page.h = bodyElm.offsetHeight;
		}

		//dialogBaseElm.style.width = ((page.w < viewport.w) ? viewport.w : page.w ) +"px";
		dialogBaseElm.style.height = ((page.h < viewport.h) ? viewport.h : page.h ) +"px";
		
		if(dialogIframeElm)
		{
			dialogBaseElm.iframe = dialogIframeElm;
			dialogIframeElm.className="dialog-base";
			bodyElm.appendChild(dialogIframeElm);
			dialogIframeElm.style.width =  dialogBaseElm.style.width;
			dialogIframeElm.style.height = dialogBaseElm.style.height;
		}
	}
}


// =================================================
	
function easeInOut(t, b, c, d, s) {

	// CIRCULAR EASING: sqrt(1-t^2)
	/*if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
	return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;

	// SINUSOIDAL EASING: sin(t)
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;*/

	// QUINTIC EASING: t^5
	if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
	return c/2*((t-=2)*t*t*t*t + 2) + b;
	
	// QUARTIC EASING: t^4
	if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
	return -c/2 * ((t-=2)*t*t*t - 2) + b;

	// QUADRATIC EASING: t^2
	if ((t/=d/2) < 1) return c/2*t*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;
};



// ====================================================================

uniqueId = 0;
function giveUniqueID()
{
	return "BRUniq"+ ++uniqueId
}


function getElementsByClassName(className,nodeElm,elmType)
{
	var elementList = [];
	var elementsByClassName = [];
	if(elmType) elementList = nodeElm.getElementsByTagName(elmType);
	else elementList = document.all || document.getElementsByTagName("*");
	for(var i=0,elm;elm=elementList[i];i++)
	{
		if(hasClassName(elm,className))
		{
			elementsByClassName.push(elm);
		}
	}
	return elementsByClassName;
}


function hasClassName(elm,className)
{
	var re = RegExp("(^|\\s)"+className+"(\\s|$)");
	return re.test(elm.className);
}

function getNextSibling(elm){
	nextElm=elm.nextSibling;
	while(nextElm.nodeType!=1){
		nextElm = nextElm.nextSibling;
	}
	return nextElm;
}

function getPreviousSibling(elm){
	previousElm=elm.previousSibling;
	while(previousElm.nodeType!=1){
		previousElm = previousElm.nextSibling;
	}
	return previousElm;
}

