// JavaScript Document

// Behold the DED namespace!
	var DED = window.DED || {};
	DED.example = function() {
		var YUD = YAHOO.util.Dom;
		var YUE = YAHOO.util.Event;
		var YUA = YAHOO.util.Anim;
		var onMenuCollapse = new YAHOO.util.CustomEvent('menu collapse');
		var onMenuOpen = new YAHOO.util.CustomEvent('menu open');
		var last = [];
		var ua = navigator.userAgent.toLowerCase();
		var isOpera = (ua.indexOf('opera') != -1);
		var isIE = (ua.indexOf('msie') != -1 && !isOpera);
		return {
			init : function() {
				var oLi = YUD.getElementsByClassName('main','a','menucont');
				for ( var i=0, j=oLi.length; i<j; ++i ) {
					
					// Get the first sibling tag (ie the submenu container; need to get past whitespace);
					subCont = oLi[i].nextSibling;

					while (subCont.nodeType != 1) {
						subCont = subCont.nextSibling;
					}

					YUE.on(oLi[i], 'click', this.collapse, subCont.offsetHeight);
																				
				}

				// Initialises all subs with "close" in the style to hidden.  Initialise one with "open"
				// to be the last opened
				YUD.batch(oLi, function(oEl) {
						// Get the first sibling tag (ie the submenu container; need to get past whitespace);
						subCont = oEl.nextSibling;
						while (subCont.nodeType != 1) {
							subCont = subCont.nextSibling;
						}
		
						if ( YUD.hasClass(subCont, 'close') ) {
							subCont.style.height = 0;
						}
						else if ( YUD.hasClass(subCont, 'open') ) {
							last.push(subCont);
						}
					}
				);
				onMenuOpen.subscribe(this.closeLast);
				
				// hide the cover...
				var cover = YUD.get('menucover');
				var attributes = { opacity: { to: 0} };
				var anim = new YAHOO.util.Anim(cover, attributes, 0.15, YAHOO.util.Easing.easeIn);
				anim.onComplete.subscribe(function (e, ele) { YUD.get('menucover').style.display='none'});	
				anim.animate();
			},
			collapse : function(e, iH) {
				// Get the first sibling tag (ie the submenu container; need to get past whitespace);
				oEl = this.nextSibling;
				while (oEl.nodeType != 1) {
					oEl = oEl.nextSibling;
				}				
				
				if (!YUD.hasClass(oEl, 'subcont'))
					return;

				last.push(oEl);
				if ( last.length > 3 ) {
					last.shift();
				}
				if ( YUD.hasClass(oEl, 'close') ) {
					YUD.removeClass(oEl, 'close');					
					
					oEl.style.height = '0';
					var iHeight = (isIE ? parseInt(iH) : parseInt(iH));
					var attributes = {
						opacity: { from: 0, to: 1 },
						height : {
							from : 0,
							to : iHeight
						}
					};
					
					var anim = new YAHOO.util.Anim(oEl, attributes, 0.5, YAHOO.util.Easing.easeIn);
					anim.animate();
					onMenuOpen.fire();
				}
				else {
					YUD.addClass(oEl, 'close');
					
					var attributes = {
						opacity: { from: 1, to: 0 },
						height : { 
							to : 0
						}
					};
					var anim = new YAHOO.util.Anim(oEl, attributes, 0.5, YAHOO.util.Easing.easeIn);
					anim.animate();
					onMenuCollapse.fire();
				}
			},
			closeLast : function() {
				var oLast = last[(last.length)-2];
			
				if ( last.length === 1) {
					return;
				}
				else if ( oLast === last[(last.length)-1] ) {
					return;
				}
				if ( !YUD.hasClass(oLast, 'close') ) {
					YUD.addClass(oLast, 'close');
					
					var attributes = {
						opacity: { from: 1, to: 0 },
						height : {
							to : 0
						}
					};
					var anim = new YUA(oLast, attributes, 0.5, YAHOO.util.Easing.easeIn);
					anim.animate();
				}
			}
		};
	}();
	
	// We need to delay this since to ensure the menu html has fully loaded
	YAHOO.util.Event.onAvailable('lastelement',DED.example.init, DED.example, true);


