/* ********************************** */
/* *                                * */
/* *   Aurel Conseil - Javascript   * */
/* *   22.01.2007                   * */
/* *                                * */
/* *   Yann Lojewski                * */
/* *   © Neocles                    * */
/* *                                * */
/* ********************************** */



winEvL = new EventListener(window);
//winEvL.addEvent('onload', mainDivVAlign);
//winEvL.addEvent('onresize', mainDivVAlign);
winEvL.addEvent('onload', aurelNav);



// >>>>> [Aurel Navigation]

function aurelNav() {
	var aurelnav = new SubNav('menu', 'sublink', 'submenu', true);
}

// <<<<< [Aurel Navigation]



// >>>>> [Block Swapper]

function autoSwap(obj) {
	var baseid		= obj.id.replace(/off|on/, '');
	var expanded	= /on/.test(obj.id);
	var swapid		= expanded ? 'off': 'on';
	var tobj			= document.getElementById(baseid + swapid);
	
	tobj.style.display	= 'block';
	obj.style.display		= 'none';
}

// <<<<< [Block Swapper]



// >>>>> [Main div's vertical alignment]

function mainDivVAlign() {
	mainDiv = document.getElementById('container');
	if(!mainDiv) return;
	var wh = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	var nh = (wh/2) - (mainDiv.scrollHeight/2);
	mainDiv.style.marginTop =  nh > 0 ? nh + 'px': 0;
}

// <<<<< [Main div's vertical alignment]



// >>>>> [Sub navigation object]
// >> @containerElId (!) : the box which contains all the navigation tree
// >> @triggerClsName (!) : classname of elements which display sub navigations links
// >> @subnavClsName (!) : the sub navigations boxes's classname
// >> @hidSubNav : hid/leave sub navigations boxes's from/on screen
// >> @mouseoutTime : time before hide sub navigations links
// >> @defaultDisplayedBoxId : box which is always displayed after the others collapse

SubNav = function(containerElId, triggerClsName, subnavClsName, hidSubNav, mouseoutTime, defaultDisplayedBoxId) {
	this.CONTAINER_EL = document.getElementById(containerElId);
	this.TRIGGER_CLASSNAME = triggerClsName;
	this.SUBNAV_CLASSNAME = subnavClsName;
	this.SUBNAVS = [];
	this.TRIGGERS = [];
	this.HIDSUBNAV = (hidSubNav) ? hidSubNav: false;
	this.MOUSEOUT_TIME = (mouseoutTime) ? mouseoutTime: 500;
	this.DEFAULT_BOX = (!defaultDisplayedBoxId) ? false: document.getElementById(defaultDisplayedBoxId) ? document.getElementById(defaultDisplayedBoxId): false;
	if(!this.CONTAINER_EL) { alert('Main container "' + containerElId + '" not found'); return; }
	this.loadSubNav();
}

SubNav.prototype = {
	loadSubNav: function() {
		if(this.DEFAULT_BOX) this.DEFAULT_BOX.style.display = 'block';
		var temp = this.CONTAINER_EL.getElementsByTagName('*');
		for(i in temp)
			if(temp[i].className && temp[i].className == this.SUBNAV_CLASSNAME) this.SUBNAVS.push(temp[i]);
		for(i=0; i<temp.length; i++)
			if(temp[i].className && temp[i].className == this.TRIGGER_CLASSNAME) this.TRIGGERS.push(temp[i]);
		delete(temp);
		
		if(!this.SUBNAVS.length) { alert('No element found for "' + this.SUBNAV_CLASSNAME + '" class name'); return; }
		if(!this.TRIGGERS.length) { alert('No element found for "' + this.TRIGGER_CLASSNAME + '" class name'); return; }
		if(this.SUBNAVS.length != this.TRIGGERS.length) { alert('Error while loading class names'); return; }
		
		sn = this.SUBNAVS;
		for(i=0; i<sn.length; i++) {
			sn[i].trigger = this.TRIGGERS[i];
			sn[i].trigger.refEl = sn[i];
			sn[i].refArr = sn;
			sn[i].refObj = this;
			sn[i].trigger.onmouseover = this.subnavShow;
			sn[i].trigger.onmouseout = (this.HIDSUBNAV) ? this.subnavStartTimer: null;
			sn[i].onmouseover = (this.HIDSUBNAV) ? this.subnavKillTimer: null;
			sn[i].onmouseout = (this.HIDSUBNAV) ? this.subnavRestartTimer: null;
		}
	},
	subnavShow: function() {
		var el = this.refEl;
		if(el.refObj.DEFAULT_BOX && el.refObj.DEFAULT_BOX.className != el.refObj.SUBNAV_CLASSNAME)
			el.refObj.DEFAULT_BOX.style.display = 'none';
		var arr = el.refArr;
		for(var i in arr) {
			arr[i].style.display = 'none';
			clearInterval(arr[i].trigger.timer);
		}
		el.style.display = 'block';
	},
	subnavHide: function(obj) {
		return function() {
			var el = obj.refEl;
			el.style.display = 'none';
			clearInterval(obj.timer);
			if(el.refObj.DEFAULT_BOX) el.refObj.DEFAULT_BOX.style.display = 'block';
		}
	},
	subnavStartTimer: function() {
		obj = this.refEl.refObj;
		this.timer = setInterval(obj.subnavHide(this), obj.MOUSEOUT_TIME);
	},
	subnavKillTimer: function() {
		clearInterval(this.trigger.timer);
	},
	subnavRestartTimer: function() {
		obj = this.refObj;
		this.trigger.timer = setInterval(obj.subnavHide(this.trigger), obj.MOUSEOUT_TIME);
	}
}

// <<<<< [Sub navigation object]
