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
		var activeCount = 0;
		for(var i=0,moduleElm;moduleElm=moduleList[i];i++)
		{
			moduleWindowElm = getElementsByClassName("module-window",moduleElm,"div")[0];
			moduleHeaderElm = getElementsByClassName("module-footer",moduleElm,"div")[0];
			moduleBlockList = getElementsByClassName("content-block",moduleElm,"div");
			tabSetElm = getElementsByClassName("tab-set",moduleElm,"ul");
			if(moduleBlockList.length>1)
			{
				if(tabSetElm.length)
				{
					activeCount = modules.attachTabSet(moduleBlockList,tabSetElm[0]);
				} 
				else
				{
					moduleNavElm = modules.createNavElm(moduleBlockList);					
					moduleHeaderElm.appendChild(moduleNavElm);
					activeCount = moduleNavElm.activeNav.index;
					
				}
				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 - activeCount)) +"px"
						blockElm.style.position = "absolute";
						blockElm.style.top = "0";
					}
				}
				if(!hasClassName(moduleWindowElm,"H"))
				{
					moduleBlockList[0].parentNode.style.top = (moduleBlockList[0].offsetHeight * (-activeCount)) +"px"
				}
			}
		}
	},
	createNavElm : function(moduleBlockList)
	{
		var moduleNavElm = document.createElement("div");
		moduleNavElm.className ="module-nav";
		moduleNavElm.blockIndex = 0;
		moduleNavElm.moduleBlockList = moduleBlockList;
		
		imgElm = document.createElement("img");
		imgElm.className = "archive-button";
		imgElm.src = "/images/btn_archive_items.gif"
		moduleNavElm.appendChild(imgElm);
		
		aElm = document.createElement("a");
		aElm.className = "prev";
		aElm.href="#";
		aElm.innerHTML = "[";
		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 = i+1; //"<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 || hasClassName(moduleBlockElm,"active")) 
			{
				moduleNavElm.activeNav = aElm;
			}
			moduleNavElm.appendChild(aElm);
		}
		moduleNavElm.activeNav.className = "active";
		
		aElm = document.createElement("a");
		aElm.className = "next";
		aElm.href="#";
		aElm.innerHTML = "]";
		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 activeCount = 0;
		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.style.backgroundPosition = "0px "+-25*this.index+"px";
				moduleNavElm.activeNav= this;
				return false;
			}
			
			if(aElm.parentNode.className == "active")
			{
				activeCount = i;
				moduleNavElm.style.backgroundPosition = "0px "+-25*activeCount+"px";
				moduleNavElm.activeNav = aElm;
			}
		}
		if(moduleNavElm.activeNav == undefined)
		{
			aElm = aList[0];
			aElm.parentNode.className = "active";
			moduleNavElm.activeNav = aElm;
		}
		return activeCount;
	},
	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);

// ===============================================================


// =================================================
	
function easeInOut(t, b, c, d, s) {

	// 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;
}
