//*** RollOver object */
var Navigation = new Class({
	initialize: function(){
		this.activestate = "on";
		this.activeborder = "activeborder";
		this.menuflag = "parentnav";
		this.menuoffcolor = '#FFFFFF';
		this.menuoncolor = '#f8560d';
		this.menu2oncolor = '#0b549a';
		this.zero = "0.000001";
		this.one = "0.999999";
		this.tipmoving = false;
		this.ajaxdelay = 100;
		/*positions for the rollover image and tooltip offset for the main header bottom nav*/
		this.bottomNavInfo = [ {"left":-2, "top":-2, "leftoffset": 35, "topoffset": -45},
		{"left":102, "top":-28, "leftoffset": 20, "topoffset": -45},
		{"left":222, "top":-54, "leftoffset": 10, "topoffset": -45},
		{"left":320, "top":-80, "leftoffset": 20, "topoffset": -45},
		{"left":445, "top":-106, "leftoffset": 25, "topoffset": -45} ];

		var folders = document.URL.split(site_path);
		this.myfolder = "/";
		this.currentpage ="";
		this.currentsection = "";

		if(folders[1]){
			folders = folders[1];
			folders=folders.split('/');
			this.currentsection = folders[0];
			//trace("folder "+folders[0]);
			if(folders[(folders.length - 1)]){ // has trailing slash
				this.myfolder = folders[(folders.length - 2)]; // the current folder
				this.currentpage = folders[(folders.length - 1)]; // the current page
			}else{ // has no trailing slash
				this.myfolder = folders[(folders.length - 3)];
				this.currentpage = folders[(folders.length - 2)];
			}
		}else{
			this.myfolder = "/";
			this.currentpage ="";
		}

	},
	getpagename : function(_url){
	
		var folders = _url.split('/');
		var page ="";
		if(folders[1] || folders[2]){
			if(folders[(folders.length - 1)]){ // has trailing slash
				var page = folders[(folders.length - 1)]; // the current page
//				trace(":"+ page);
			}else{ // has no trailing slash
				var page = folders[(folders.length - 2)]; // the current page
			}
		}

		return page;

	},
	getnavitems : function(_ul, e){
		var _root = this;

		// _id will come in the format 'navlink-95', so need to get the '95' out
		var _id = e.id.split('-');
		_id = _id[1];

		var url = site_root+"/mitre10/loadMenu/element_id/"+_id;
		var myAjax = new Ajax(url, {
			method: 'get',
			onComplete: function(responseText, responseXML){
				var wait = setTimeout(function(){
					var jsondata = eval(responseText);

					// This checks to see if the element has the 'overview' class, which means we need a duplicate child element to
					// get to the page's contents
					var classes = e.className.split(" ");
					
					if( !fudgeNav && classes[1] && classes[1] == "overview" )
					{
						var _li = new Element('li').injectInside(_ul);
						var properties = {href: e.href};
						var _a = new Element('a').setProperties(properties).injectInside(_li);
						_a.setHTML('Overview');
					}

					// Create each child's <li> item
					for(i in jsondata){

						var title = jsondata[i].navigation_title;
						if( title )
						title = title.replace(/ /g, "-" );

						var _li = new Element('li').injectInside(_ul);
						if(jsondata[i].children > 0 )
						var properties = {href: e.href+title+'/', 'onclick' : 'javascript:void(0)', 'class' : 'parentnav', 'id' : 'navlink-'+jsondata[i].navigation_id};
						else
						{
							if( jsondata[i].param )
							var properties = {href: e.href+jsondata[i].param+'/'+title+'/'};
							else if( jsondata[i].controller )
							var properties = {href: site_root+jsondata[i].controller+'/'+title+'/'};
							else
							var properties = {href: e.href+title+'/'};
						}

						var _a = new Element('a').setProperties(properties).injectInside(_li);
						_a.setHTML(jsondata[i].navigation_title);

					}
					var slideEffect = new Fx.Morph(_ul, {
						duration: 500,
						transition: Fx.Transitions.quadOut,
						onStart: function(){
							_ul.setStyle("background-image", "none");
							_ul.setOpacity(_root.zero);
							_ul.addClass("on");
							_ul.getParent().getFirst().addClass("on")
						},
						onComplete: function(){
							_root.attachrollovers();
							_ul.setStyle("height", "auto");
							//_ul.toggleClass("on");
						}
					});
					slideEffect.start({
					'height': [_ul.offsetHeight, _ul.scrollHeight],
					'opacity' : [_root.zero, _root.one]
					});
				}, _root.ajaxdelay); // slow down the loading, just for fun
			}
		}).request();
	},
//	attachtooltips : function(){
//		_root = this;
//
//		if(! $("bottomnav-bubble-tip"))
//		_root._tooltip = new Element('div').setProperties({id:'bottomnav-bubble-tip'}).injectInside(document.body);
//		else
//		_root._tooltip = $("bottomnav-bubble-tip");
//		_root._tooltip.setStyles({left:($('bottomnav').getLeft() + _root.bottomNavInfo[0].leftoffset), top: ($('bottomnav').getTop() + _root.bottomNavInfo[0].topoffset)});
//		_root._tipEffect = new Fx.Morph(_root._tooltip, {
//			duration: 400,
//			onStart: function(){_root.tipmoving = true;},
//			onComplete: function(){_root.tipmoving = false;},
//			transition: Fx.Transitions.quadOut
//		});
//
//		var myTips = new Tips($$('.showfootertooltip'), {
//			maxTitleChars: 55,
//			fixed: true,
//			className: 'footertool',
//			onShow: function(tip){
//				tip.effects({duration: 300, transition: Fx.Transitions.quadIn}).custom({'opacity': [0, 1]});
//			},
//			onHide: function(tip){
//				tip.effects({duration: 200, transition: Fx.Transitions.quadOut}).custom({'opacity': [1, 0]});
//			},
//			offsets: {x :5, y:5}
//		});
//
//	},
	/* set the acive link and attach mouseover, mouseout and click actions */
	attachrollovers: function(){
		var _root = this; // reference to "this" wont work in the a tags loop


		$$('#mainnav li a').each(function(e){



			var _li = e.getParent();
			var _nextli = e.getParent().getNext();

			var div = new Element('div').setProperties({}).injectInside(document.body);
			div.addClass('mainnavrollover');
			div.setStyles({
				height:0,
				"background-color": e.getStyle("border-bottom-color"),
				//opacity:_root.zero,
				left: _li.getLeft() + 2,
				width: _li.getStyle('width')
			});
			/* activate the on state */
			if(e.href.indexOf(_root.currentsection) > 0){
				e.addClass(_root.activestate);
			}
			if(e.hasClass("on")){
				div.setStyle("height", "6px");
				div.setStyle('display', 'block')
			}
			else{ // attach rollovers
				//div.setStyle('background-color', e.getStyle("border-color"))
				var _divEffect = new Fx.Morph(div, {
					duration: 400
					//,transition: Fx.Transitions.Sine.easeOut
				});
				e.addEvent('mouseenter', function(){	//on mousenter stops event bubbling
					_li.toggleClass("on")
					if(_nextli)
					_nextli.toggleClass("border")
					div.setStyle("height", "0px");
					div.setStyle('display', 'block')
					_divEffect.stop();
					_divEffect.start({
					'height': [0, 6]
					//,'opacity' : [_root.zero, _root.one]
					});

				});
				e.addEvent('mouseleave', function(){
					div.setStyle("display", "none");
					_li.toggleClass("on")
					if(_nextli)
					_nextli.toggleClass("border")
				});
			}
		});

//                $$("#bottomnav ul li a").each(function(e, key){
//
//			if(e.href.indexOf(_root.currentsection) > 0){
//				e.addClass(_root.activestate);
//			}
//
//			var _li = e.getParent();
//			var _ul = e.getParent().getParent();
//			var _img = e.getFirst();
//
//			if(! $("bottomnav-on-container"))
//			var _container = new Element('div').setProperties({id:'bottomnav-on-container'}).injectInside(_ul.getParent());
//			else
//			var _container = $("bottomnav-on-container");
//
//			var _div = new Element('div').setProperties({ 'class':'bottomnav-on'}).injectInside(_container);
//			_div.setStyle("background-image", "url("+_img.src.replace(".png", "-on.png")+")");
//			_div.setStyle("visibility", "hidden");
//
//			/* activate the on state */
//			if(e.href.indexOf(_root.currentsection) > 0){
//				e.addClass(_root.activestate);
//			}
//			if(e.hasClass("on"))
//			_div.setStyle("visibility", "visible");
//
//			var _divEffect = new Fx.Morph(_div, {
//				duration: 400
//				//,transition: Fx.Transitions.Sine.easeOut
//			});
//			_div.setStyles({top:_root.bottomNavInfo[key].top, left: _root.bottomNavInfo[key].left});
//			//_div.innerHTML = "yo dude";
//
//
//			/* tracking mousemove works a bit shit
//			e.addEvent('mousemove', function(evt){
//			if(! _root.tipmoving){
//			var event = new Event(evt);
//			_x = (event.client.x - 25);
//			_y = (event.client.y - 55);
//			_root._tooltip.innerHTML = (e.title);
//			_root._tooltip.setStyles({top: _y, left: _x});
//			}
//
//			});*/
//			if(! e.hasClass("on")){
//				e.addEvent('mouseenter', function(evt){
//					_div.setOpacity(_root.zero);
//					//_div.setStyle("display", "block");
//					_divEffect.stop();
//					_divEffect.start({
//					'opacity': [_root.zero, _root.one]
//					});
//					e.getFirst().setStyle("visibility", "hidden")
//
//
//					_root._tooltip.setStyle("display", "block");
//					//_root._tooltip.setOpacity(_root.zero);
//					_root._tooltip.innerHTML = (e.title);
//					_root._tipEffect.stop();
//
//					_root._tipEffect.start({
//						//'opacity': [_root.zero, _root.one], ie cant do opacity transitions for 24 png
//						'left': [_root._tooltip.getLeft(), (e.getLeft() + _root.bottomNavInfo[key].leftoffset)],
//						'top': [_root._tooltip.getTop(), (e.getTop() + _root.bottomNavInfo[key].topoffset)]
//					});
//				});
//				e.addEvent('mouseleave', function(evt){
//					_divEffect.stop();
//					_div.setOpacity(_root.zero);
//					_div.setStyle("visibility", "hidden");
//					_root._tipEffect.stop();
//					_root._tooltip.setStyle("display", "none");
//					e.getFirst().setStyle("visibility", "visible")
//					//_root._tooltip.setOpacity(_root.zero);
//				});
//			}
//		});
		$$("#leftnav a").each(function(e){
			//Only use this fancy js, if the link doesn't have the class: "exclude-from-navigationjs"
			if (!e.hasClass('exclude-from-navigationjs')) { 
				/* check if the link is the current page */
				//			if(_root.getpagename(e.href).indexOf(_root.currentpage) != -1){			
				if (_root.getpagename(e.href) == (_root.currentpage)) {
					//			alert(_root.getpagename(e.href)+':'+(_root.currentpage)+':'+_root.getpagename(e.href).indexOf(_root.currentpage));
					e.addClass(_root.activestate);

					// Dean's hack, to get a Parent nav item showing its own landing page
					// So - if the current parent has kids, force them to be shown here...
					if( fudgeNav && ( e.getParent().getFirst("ul")) && e.getParent().getFirst("ul").getTag() == "ul" )
					{
						_ul = e.getParent().getFirst("ul");

						var _menuexists = true;
						
						var slideEffect = new Fx.Morph(_ul, {
							duration: 500,
							transition: Fx.Transitions.quadOut,
							onStart: function(){
								e.toggleClass("on");
								_ul.toggleClass("on");
							},
							onComplete: function(){
								if (_ul.offsetHeight > 10)
									_ul.setStyle("height", "auto");

							}
						});

						if (_menuexists) {

							if (_ul.offsetHeight < 10) { // slide open
								slideEffect.start({
									'height': [0, _ul.scrollHeight],
									'opacity': [_root.zero, _root.one]
								});

							}
							else { // slide closed
								slideEffect.start({
									'height': [_ul.scrollHeight, 0],
									'opacity': [_root.one, _root.zero]
								});
							}
						}
						e.getParent().getFirst("ul").addClass(_root.activestate);
						e.addClass(_root.activestate);
					}

					try {
						if (e.getParent().getParent().getTag() == "ul") {
							var _parentul = e.getParent().getParent();
							_parentul.addClass(_root.activestate);
							_parentul.setStyle("height", "auto");
							_parentul.getPrevious().addClass(_root.activestate);
							if (_parentul.getParent().getParent().getTag() == "ul") {
								var _parentul = _parentul.getParent().getParent();
								_parentul.addClass(_root.activestate);
								_parentul.setStyle("height", "auto");
								_parentul.getPrevious().addClass(_root.activestate);
							}
						}
					} 
					catch (e) {
					
					}
					
				}
				
				e.onclick = function(){
					if (e.hasClass(_root.menuflag) && !fudgeNav) {
						//e.addClass("on");
						if (e.getNext()) {
							if (e.getNext().getTag() == "ul") {
								var _menuexists = true;
								var _ul = e.getNext();
							}
							else {
								var _menuexists = false;
							}
						}
						else {
							var _ul = new Element('ul').injectAfter(e);
							var _menuexists = false;
						}
						
						var slideEffect = new Fx.Morph(_ul, {
							duration: 500,
							transition: Fx.Transitions.quadOut,
							onStart: function(){
								e.toggleClass("on");
								_ul.toggleClass("on");
							},
							onComplete: function(){
								if (_ul.offsetHeight > 10) 
									_ul.setStyle("height", "auto");
								
							}
						});
						
						if (_menuexists) {
						
							if (_ul.offsetHeight < 10) { // slide open
								slideEffect.start({
									'height': [0, _ul.scrollHeight],
									'opacity': [_root.zero, _root.one]
								});
								
							}
							else { // slide closed
								slideEffect.start({
									'height': [_ul.scrollHeight, 0],
									'opacity': [_root.one, _root.zero]
								});
							}
						}
						else { // open a bit and then call the ajax function to fill with links
							var slideOpen = new Fx.Morph(_ul, {
								duration: 100,
								onComplete: function(){
									_root.getnavitems(_ul, e);
								}
							});
							slideOpen.start({
								'height': [0, 60]
							});
						}
						return false; // cancel the href
					}
					else {
						window.location = this.href;
					}
					
				}
				e.ondblclick = function(){ // forget about opening the menu and jump straight to the href
					window.location = this.href;
				}
			}
		});
	}
});