/**
 * HORIZONTAL PULLDOWN MENU
 * @author Romain Berton
 * @email berton[at]fullsix.com
 */
// ENCAPSULATION DU SCRIPT
(function() {
// FONCTION D'AJOUT DE GESTIONNAIRE D'EVENEMENT
var connect = function(oEl, sEvType, fn, bCapture) {
	return document.addEventListener ? oEl.addEventListener(sEvType, fn, bCapture || false): oEl.attachEvent ? oEl.attachEvent('on' + sEvType, fn): false;
};
// INSTANCE DE GESTION DE CLASSES CSS
var css = {
	remove: function(oEl, sClass) {
		var rep = oEl.className.match(' ' + sClass) ? ' ' + sClass : sClass;
		oEl.className = oEl.className.replace(rep, '');
	},
	add: function(oEl, sClass) {
		if(!css.has(oEl, sClass)) oEl.className += oEl.className ? ' ' + sClass : sClass;
	},
	has: function(oEl, sClass) {
		return new RegExp('\\b' + sClass + '\\b').test(oEl.className);
	}
};
// CLASSE MENU
var Menu = function() {};
// PROTOTYPE DE LA CLASSE MENU
var Mp = Menu.prototype = {
	menus: [],
	show: 'show',
	active: 'active',
	// IDENTIFIE TOUS LES MENUS AYANT LA CLASSE MP.LABEL ET LES AJOUTE AU TABLEAU MP.MENUS
	identify: function(oMenu) {
		Mp.menus.push(oMenu);
		return Mp;
	},
	// VERIFIE SI UN ELEMENT EST CONTENU DANS UN AUTRE
	contains: function(container, containee) {
		if(!container || !containee) return;
		for(var n = containee; n && n != container; n = n.parentNode);
		return n;
	},
	// INITIALISE UN MENU
	init: function(oMenu) {
		var aLis = oMenu.getElementsByTagName('li'), iLi = aLis.length;
		while(iLi-- > 0) {
			css.remove(aLis[iLi], Mp.show);
			css.remove(aLis[iLi].getElementsByTagName('a')[0], Mp.active);
		}
	},
	// AJOUTE DES GESTIONNAIRES D'EVENEMENT SUR CHAQUE MENU
	addBehaviour: function() {
		var iMenu = Mp.menus.length;
		while(iMenu-- > 0) {
			//si pas de menu, je sors -- ajout Flo
			if (!Mp.menus[iMenu]) return;
			
			
			Mp.init(Mp.menus[iMenu]);
			var aLis = Mp.menus[iMenu].getElementsByTagName('li'), iLi = aLis.length;
			while(iLi-- > 0) {
				var oLi = aLis[iLi];
				var oA = oLi.getElementsByTagName('a')[0];
				var oUl = oLi.getElementsByTagName('ul')[0];
				if(window.attachEvent && oLi.parentNode != Mp.menus[iMenu] && (oA.lastChild.nodeValue.length > 23 || oA.getElementsByTagName('br')[0]))// Fix IE (grmpf)
					oLi.getElementsByTagName('img')[0].style.height = '3.4em';
				// GESTIONNAIRE D'EVENEMENT LORSQUE LA SOURIS SURVOLE L'ELEMENT
				connect(oLi, 'mouseover', (function(oMenu, oLi, oUl) {
					return function(e) {
						// INITIALISATION DES ELEMENTS DE LISTE FRERES DE L'ELEMENT DE LISTE COURANT
						var oParent = oLi.parentNode;
						var aLis = oParent.getElementsByTagName('li'), iLi = aLis.length;
						while(iLi-- > 0)
							if(!Mp.contains(aLis[iLi],(e.target || e.srcElement)) && aLis[iLi].parentNode == oMenu && css.has(aLis[iLi], Mp.show)) {
								// SI L'ELEMENT DE LISTE COMPORTE UN SOUS-MENU, ON MASQUE LE SOUS-MENU
								if(aLis[iLi].getElementsByTagName('ul')[0]) {
									var oSsMenu = aLis[iLi].getElementsByTagName('ul')[0];
									var aParams = [{'prop':'height', 'onStart':oSsMenu.scrollHeight, 'onComplete':0, 'interval':300}];
									var iKey = aParams.length;
									while(iKey-- > 0) (new AnimThisProp()).initialize(oSsMenu, aParams[iKey], iKey);
								}
								else {
									css.remove(aLis[iLi], Mp.show);
									css.remove(aLis[iLi].getElementsByTagName('a')[0], Mp.active);
								}
							}
							else {
								// SI L'ELEMENT DE LISTE COMPORTE UN SOUS-MENU, ON ANNULE LA TEMPO ET ON AFFICHE LE SOUS-MENU
								if(oUl) {
									if(oUl.tempo) clearTimeout(oUl.tempo);
									if(!css.has(oLi, Mp.show) && !css.has(oLi.parentNode.parentNode, Mp.show)) {
										var aParams = [{'prop':'height', 'onStart':0, 'onComplete':'auto', 'interval':200}];
										var iKey = aParams.length;
										while(iKey-- > 0) (new AnimThisProp()).initialize(oUl, aParams[iKey], iKey);
									}
								}
								css.add(oLi, Mp.show);
								css.add(oLi.getElementsByTagName('a')[0], Mp.active);
							}
					};
				})(Mp.menus[iMenu], oLi, oUl));
				// GESTIONNAIRE D'EVENEMENT LORSQUE LA SOURIS QUITTE L'ELEMENT
				connect(oLi, 'mouseout', (function(oLi, oUl) {
					return function() {
						var removeClass = function() {
							// SI L'ELEMENT DE LISTE COMPORTE UN SOUS-MENU, ON MASQUE LE SOUS-MENU
							if(oUl) {
								var aParams = [{'prop':'height', 'onStart':oUl.scrollHeight, 'onComplete':0, 'interval':150}];
								var iKey = aParams.length;
								while(iKey-- > 0) (new AnimThisProp()).initialize(oUl, aParams[iKey], iKey);
							}
							else {
								css.remove(oLi, Mp.show);
								css.remove(oLi.getElementsByTagName('a')[0], Mp.active);
							}
						}
						if(oUl) {
							if(oUl.tempo) clearTimeout(oUl.tempo);
							oUl.tempo = setTimeout(removeClass, 500);
						}
						else removeClass();
					};
				})(oLi, oUl));
				// GESTIONNAIRE D'EVENEMENT AU FOCUS D'UN LIEN
				connect(oA, 'focus', (function(oMenu, oLi) {
					return function(e) {
						// CONSTITUTION D'UN TABLEAU REGROUPANT TOUS LES ELEMENTS DE LISTE PARENTS DU LIEN DE CONTROLE
						var oTempLi = oLi;
						var aLis = [oTempLi];
						while(oTempLi.parentNode != oMenu) {
							oTempLi = oTempLi.parentNode;
							if(oTempLi.nodeName.toLowerCase() == 'li') aLis.push(oTempLi);
						}
						// AFFECTATION DES STYLES SI LES LIENS DE CONTROLE OU LES SOUS-MENUS FONT PARTIS DES ELEMENTS DE LISTE DU TABLEAU
						var iLi= aLis.length;
						while(iLi-- > 0) {
							oA = aLis[iLi].getElementsByTagName('a')[0];
							if(Mp.contains(aLis[iLi], oLi)) {
								if(aLis[iLi].getElementsByTagName('ul')[0]) aLis[iLi].getElementsByTagName('ul')[0].style.height = 'auto';
								css.add(aLis[iLi], Mp.show);
								css.add(oA, Mp.active);
							}
						}
					}
				})(Mp.menus[iMenu], oLi));
				// GESTIONNAIRE D'EVENEMENT A LA PERTE DE FOCUS D'UN LIEN
				connect(oA, 'blur', (function(oMenu, oLi) {
					// INITIALISATION DU MENU
					return function() {
						return Mp.init(oMenu);
					};
				})(Mp.menus[iMenu], oLi));
			}
		}
		return Mp;
	}
};
// OBJET D'ANIMATION DE LA HAUTEUR
var AnimThisProp = function() {};
var Ap = AnimThisProp.prototype = {
	start: function(oEl, oParam, sAnim) {
		if(!oParam.start) {
			oParam.end = oEl.scrollHeight;
			oParam.start = (new Date()).getTime();
		}
		return Ap.middle(oEl, oParam, sAnim);
	},
	middle: function(oEl, oParam, sAnim) {
		if(!oParam.start) return;
		var iNow = (new Date()).getTime();
		var iDiff = oParam.start - iNow;
		if(oParam.onStart == 0 && iNow < (oParam.start + oParam.interval) && parseInt(oEl.style[oParam.prop]) < oParam.end) {
			var iValue = Math.abs(parseInt(iDiff * oParam.end / oParam.interval));
			oEl.style[oParam.prop] = iValue + 'px';
		}
		else if(oParam.onStart != 0 && iNow < (oParam.start + oParam.interval) && parseInt(oEl.style[oParam.prop]) > 0) {
			var iValue = Math.abs(parseInt(iDiff * oParam.end / oParam.interval));
			oEl.style[oParam.prop] = (parseInt(oEl.style[oParam.prop]) > iValue ? parseInt(oEl.style[oParam.prop]) - iValue : 0) + 'px';
		}
		else Ap.end(oEl, oParam, sAnim);
	},
	end: function(oEl, oParam, sAnim) {
		oEl.style[oParam.prop] = oParam.onComplete;
		if(parseInt(oEl.style[oParam.prop]) == 0) {
			css.remove(oEl.parentNode, Mp.show);
			css.remove(oEl.parentNode.getElementsByTagName('a')[0], Mp.active);
		}
		else if(parseInt(oEl.style[oParam.prop]) != 0) {
			css.add(oEl.parentNode, Mp.show);
			css.add(oEl.parentNode.getElementsByTagName('a')[0], Mp.active);
		}
		return clearInterval(oEl[sAnim]);
	},
	initialize: function(oEl, oParam, iKey) {
		var sAnim = 'anim' + iKey;
		oEl.style[oParam.prop] = oParam.onStart + 'px';
		if(oEl[sAnim]) clearInterval(oEl[sAnim]);
		return oEl[sAnim] = setInterval(function() { return new Ap.start(oEl, oParam, sAnim); }, 1);
	}
};
// CONTROLE DES PRERECQUIS ET INSTANCE DE MENU
if(document.getElementsByTagName && document.getElementById && document.createElement) {
// AJOUT DE LA CLASSE HASJS SUR L'ELEMENT HTML
	css.add(document.documentElement, 'hasJS');
	return connect(window, 'load', function() {
		popNotice();
		// VERIFICATION DE LA PRESENCE DE CSS
		var oNewDiv = document.createElement('div');
		document.body.appendChild(oNewDiv);
		oNewDiv.style.visibility = 'hidden';
		oNewDiv.style.width = '20px';
		oNewDiv.style.padding = '10px';
		if(oNewDiv.offsetWidth != 40) {
			document.body.removeChild(oNewDiv);
			return css.remove(document.documentElement, 'hasJS');
		}
		document.body.removeChild(oNewDiv);
		// CHARGEMENT DU MENU
		var oMenu = document.getElementById('menu');
		
		if (document.getElementById('facebookLink')){
			setTimeout(function(){
				window.location.href = document.getElementById('facebookLink').href;
			},3500);
		}
		
		return new Menu().identify(oMenu).addBehaviour();
	});
}

function cancelClick(e) {
	if(e && e.stopPropagation && e.preventDefault) {
		e.stopPropagation();
		e.preventDefault();
	}
	else if(e && window.event) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	return false;
}

function popNotice() {
	var aAs = document.getElementsByTagName('a'), iA = aAs.length;
	while(iA-- > 0) if(css.has(aAs[iA], 'pops')) connect(aAs[iA], 'click', (function(oA) {
		return function(e) {
			window.open(oA.href, '', 'height=400, width=400, top=100, left=100, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=no, status=no');
			return cancelClick(e);
		};
	})(aAs[iA]));
}

})();
