// prepares the regular version of the global navigation with full functionality
function gn_init() {
	var gonogo=true;
	var use_ishim=false;
	var active_ishim=null;
	var fMSIE=false;
	var fMSIE6=false;
	
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		fMSIE=true;
		try
		{
			document.execCommand("BackgroundImageCache", false, true); 
		}
		catch(err)
		{
		}
		var temp=navigator.appVersion.split("MSIE")
		var version=parseFloat(temp[1])
		if (version<=5.9) {
			gonogo=false;
		}
		if (version>=6 && version<7) {
			fMSIE6=true;
			use_ishim=true;
			$('gn').insertAdjacentHTML('beforeEnd', '<iframe src="'+gn_iframe+'" id="gn_ishim" frameborder="0" scrolling="0" style="position:absolute;top:0;left:0;width:0;height:0;display:none;z-index:498;filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0.0);"></iframe>');
			var gn = $('gn');
			var lis = gn.select('li');
			lis.each(function(li) {
				var lnk = li.firstDescendant();
				lnk.style.height='1%';
			});
		}
	}
	
	function gn_ishim(show,d_2) {
		var ishim = $('gn_ishim');
		if(show==true && use_ishim==true) {	
			ishim.style.display = 'block';
			var d_2_dim = d_2.getDimensions();
			var d_2_pos = d_2.positionedOffset();
			ishim.style.width = d_2_dim.width;
			ishim.style.height = d_2_dim.height;
			ishim.style.top = d_2_pos.top;
			ishim.style.left = d_2_pos.left;
		}
		
		if (active_ishim==d_2 && show==false && use_ishim==true)	{
			ishim.style.display = 'none';
		}
		active_ishim=d_2;
	}
	
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		gonogo=false;
	}

	if (gonogo==true)	{
		if (typeof gnCategory!='undefined')	{
			var t = 0;
			var g = gnCategory*1;
			var headers = $$('#gn h2');
			headers.each(function(header) {
				if (g==t) {
					var a =header.down('a');
					a.addClassName('gn_selected');
				}
				t++;
			});
		}
		
		// Adds Ids to each nav link based on the text content
		var anchors = $$('#gn a');
		anchors.each(function(a) {			
			var re1 = new RegExp("<(.|\n)*?>", "g");
			var re2 = new RegExp("&amp;", "g");
			var re3 = new RegExp("[^a-z,A-Z]", "g");
			a.name = "gnl_" + a.innerHTML.replace(re1,"").replace(re2,"").replace(re3,"");
		});

		var headers = $$('#gn h2');
		headers.each(function(header) {
			
			var a_1 = header.down('a');
			var d_2 = header.next();
			var u_2 = d_2.down('ul');
			var l_2s = u_2.childElements();
			var u_3s = u_2.select('ul');
			
			l_2s.each(function(l_2) {
				if (l_2.next()==null){					
					l_2.style.paddingBottom='10px';
				}
			});

			u_3s.each(function(u_3) {
				var l_3s=u_3.childElements();
				l_3s.each(function(l_3) {
					if (l_3.next()==null)
					{
						l_3.style.marginBottom='10px';
					}
				});
			});

			if (header.className=='gn_right') {
				var gn_right=true;
				u_2.setStyle({
					'float': 'right'
				});
			}
			else {
				var gn_right=false;
			}

			d_2.setStyle({
				opacity: 0.0
			});

			function d_2_show(a_1,d_2) {		
				var a_1_dim = a_1.getDimensions();		
				var a_1_pos = a_1.positionedOffset();				
				var h = a_1_dim.height;
				var w = a_1_dim.width;
				var l = a_1_pos.left;
				var t = a_1_pos.top;
				var gn = $('gn');
				var d_2_cs = gn.select('div.gn_dd');
				d_2_cs.each(function(d_2_c) {
					if (d_2_c.style.zIndex='500') {
						var h_c = d_2_c.previous();
						var a_1_c = h_c.down('a');
						d_2_hide(a_1_c,d_2_c);
					}
				});

				d_2.setStyle({
					top: (t+h) + 'px'
				});
				if (gn_right==true) {
					d_2.setStyle({
						left: l+w-220 + 'px'
					});
				}
				else {
					d_2.setStyle({
						left: l + 'px'
					});
					d_2.style.left = l; 
				}
				d_2.setStyle({
					'z-index': '500'
				});
				a_1.addClassName('gn_active');
				gn_ishim(true,d_2);
			}

			function d_2_hide(a_1,d_2) {
				d_2.setStyle({
					'marginLeft': '0px',
					width: '220px',
					height: 'auto',
					left: '-1000em',
					'z-index': '499'
				});
				a_1.removeClassName('gn_active');
				gn_ishim(false,d_2);
			}

			a_1.observe('focus', function() {
				d_2_show(a_1,d_2);
				d_2.setStyle({
					opacity: 1.0
				});
			});
				
			a_1.observe('blur', function() {
				d_2_hide(a_1,d_2);
			});

			a_1.observe('keydown', function(event) {
				event = new Event(event);
				if (event.key=='down') {
					u_2.firstDescendant().firstDescendant().focus();
				}
				else if (event.key=='left' && header.previous()) {
					header.previous().previous().down('a').focus();
				}
				else if (event.key=='right' && header.next().next()) {
					header.next().next().down('a').focus();
				}				
			});

			a_1.observe('mouseenter', function(event){
				var rt = event.element();
				while (rt && rt != d_2 && rt.nodeName != 'BODY'){
					rt = rt.parentNode;
				}
				if (rt == d_2) {
					return;
				}
				
				d_2.appear({
					duration:0.3,
					transition: Effect.Transitions.sinoidal,
					beforeStart: function() {
						d_2_show(a_1,d_2);
					}
				});
			});

			a_1.observe('mouseleave', function(event){
				var rt = event.relatedTarget;
				if (fMSIE) {
					rt = event.toElement;
				}
				
				while (rt && rt != d_2 && rt.nodeName != 'BODY') {
					rt = rt.parentNode;
				}
					
				if (rt == d_2) {
					return;
				}
				if (fMSIE6) {
					d_2_hide(a_1,d_2);
				} else {
					d_2.fade({
						duration:0.3,
						transition: Effect.Transitions.sinoidal,
						beforeStart: function() {
							d_2.setStyle({
								'z-index': '499'
							});
							a_1.removeClassName('gn_active');
							gn_fo(a_1);
						},
						afterFinish: function() {
							d_2_hide(a_1,d_2);
						}
					});
				}
			});

			d_2.observe('mouseleave', function(event){
				var rt = event.relatedTarget;
				if (fMSIE) {
					rt = event.toElement;
				}
				while (rt && rt != a_1 && rt.nodeName != 'BODY'){
					rt = rt.parentNode;
				}
				if (rt == a_1){
					return;
				}
				
				if (fMSIE6) {
					d_2_hide(a_1,d_2);
				} else {
					d_2.fade({
						duration: 0.3,
						transition: Effect.Transitions.sinoidal,
						beforeStart: function() {
							d_2.setStyle({
								'z-index': '499'
							});
							a_1.removeClassName('gn_active');
							gn_fo(a_1);
						},
						afterFinish: function() {
							d_2_hide(a_1,d_2);
						}
					});
				}
			});

			l_2s.each(function(l_2) {			
				var a_2 = l_2.firstDescendant();
				if (gn_right==true) {
					if (a_2.next()!=null) {	
						a_2.addClassName('left_arrow');
					}
				}
				else {
					if (a_2.next()!=null) {
						a_2.addClassName('right_arrow');
					}
					a_2.style.paddingLeft='10px';
				}

				var gn_foto;

				a_2.observe('mouseenter',  function() {
					//gn_foto = gn_fo.delay(.3,gn_fo,a_2);
					gn_fo(a_2);
					var bkgd_pos = 'background-position: 206px 50%';
					if(gn_right==true) {
						var bkgd_pos = 'background-position: 10px 50%';
					}
						
					new Effect.Morph(a_2, {
						style: 'background:#990000;' + bkgd_pos,
						transition: Effect.Transitions.sinoidal,
						duration: .3
					});
				});

				a_2.observe('mouseleave', function(event) {
//					$clear(gn_foto);
					//if (!(a_2.hasClassName('gn_out'))) {
//						a_2.morph('background-color:000000;');
						var bkgd_pos = 'background-position: 206px 50%';
						if(gn_right==true) {
							var bkgd_pos = 'background-position: 10px 50%';
						}
						new Effect.Morph(a_2, {
							style: 'background:#000000;'  + bkgd_pos,
							transition: Effect.Transitions.linear,
							duration: .3
						});
					//}
				});

				a_2.observe('focus', function() {
					d_2_show(a_1,d_2);
					a_2.setStyle({
						'background-color': '#990000'
					});
					d_2.setStyle({
						opacity: 1.0
					});
				});

				a_2.observe('blur', function() {
					d_2_hide(a_1,d_2);
					a_2.setStyle({
						'background-color': '#000'
					});
				});

				a_2.observe('keydown', function(event) {
					event = new Event(event);
					if (event.key=='up' && l_2.previous()) {
						l_2.previous().firstDescendant().focus();
					}
					else if (event.key=='up' && !(l_2.previous())) {
						a_1.focus();
					}
					else if (event.key=='down' && l_2.next()) {
						l_2.next().firstDescendant().focus();
					}
					else if (event.key=='left' && header.previous()) {
						header.previous().previous().down('a').focus();
					}
					else if (event.key=='right' && header.next().next()) {
						header.next().next().down('a').focus();
					}				
				});
			});

			function gn_fo(a_2) { 				
				var u_2_h = u_2.getDimensions().height;
				u_3s.each(function(u_3) {				
					if (u_3.style.left=='auto' && u_3!=a_2.next()) {
						var u_3_h = u_3.getDimensions().height;

						if (gn_right==true) {
							new Effect.Morph(u_3, {
								style: 'width:0px; height:0px; opacity:0.0; margin-left:0px;',
								transition: Effect.Transitions.linear,
								duration: .3,
								beforeStart:  function(){
									u_3.previous().removeClassName('gn_out');
								},
								afterFinish: function(){
									u_3.setStyle({
										left: '-1000em',
										height: 'auto'
									});
									if (!(a_2.next())) {
										d_2.setStyle({
											'marginLeft': '0px',
											width: '220px',
											height: 'auto'
										})
										gn_ishim(true,d_2);
									}
								}
							});
						} else	{
							new Effect.Morph(u_3, {
								style: 'width:0px; height:0px; opacity:0.0;',
								transition: Effect.Transitions.linear,
								duration: .3,
								beforeStart:  function(){
									u_3.previous().removeClassName('gn_out');
								},
								afterFinish: function(){
									u_3.setStyle({
										left: '-1000em',
										height: 'auto'
									});
									if (!(a_2.next())) {
										d_2.setStyle({
											'marginLeft': '0px',
											width: '220px',
											height: 'auto'
										})
										gn_ishim(true,d_2);
									}
								}
							});
						}
						
						new Effect.Morph(u_3.previous(), {
							style: 'backgroundColor:#000000;',
							transition: Effect.Transitions.linear,
							duration: .3
						});
					}

					if (u_3==a_2.next() && u_3.style.left!='auto') {						
						var u_3_h = u_3.getDimensions().height;
						if (gn_right==true) {
							new Effect.Morph(u_3, {
								style: 'width:220px;height:' + u_3_h + 'px;opacity:1.0;margin-left:-220px;',
								transition: Effect.Transitions.linear,
								duration: .3,
								beforeStart:  function(){
									a_2.addClassName('gn_out');
									u_3.setStyle({
										left: 'auto'
									});
									d_2.setStyle({
										width: '440px'
									});
									if (gn_right==true) {
										d_2.setStyle({
											'marginLeft': '-220px'
										});
									}
									if (u_3_h>u_2_h) {
										d_2.setStyle({
											height: u_3_h+30 + 'px'
										});
									}
									else {
										d_2.setStyle({
											height: 'auto'
										});
										var d_2_h = d_2.getDimensions().height;
										d_2.setStyle({
											height: d_2_h+30 + 'px'
										});
									}
									gn_ishim(true,d_2);
								}
							});
						}
						else {
							new Effect.Morph(u_3, {
								style: 'width:220px;height:' + u_3_h + 'px;opacity:1.0;',
								transition: Effect.Transitions.linear,
								duration: .3,
								beforeStart:  function(){
									a_2.addClassName('gn_out');
									u_3.setStyle({
										left: 'auto'
									});
									d_2.setStyle({
										width: '440px'
									});
									if (gn_right==true) {
										d_2.setStyle({
											'marginLeft': '-220px'
										});
									}
									if (u_3_h>u_2_h) {
										d_2.setStyle({
											height: u_3_h+30 + 'px'
										});
									}
									else {
										d_2.setStyle({
											height: 'auto'
										});
										var d_2_h = d_2.getDimensions().height;
										d_2.setStyle({
											height: d_2_h+30 + 'px'
										});
									}
									gn_ishim(true,d_2);
								}
							});
						}
					}
				})
			}
		});
	}
	else {
		if (typeof gnCategory!='undefined') {
			var t = 0;
			var g = gnCategory*1;
			var headers = $$('#gn h2');
			headers.each(function(header) {
				if (g==t) {
					var a = header.down('a');
					a.addClassName('gn_selected');
				}
				t++;
			});
		}

		var headers = $$('#gn h2');
		headers.each(function(header) {
			
			var a_1 = header.down('a');
			var d_2 = header.next();
			var u_2 = d_2.down('ul');
			var l_2s = u_2.childElements();
			var u_3s = u_2.select('ul');
			
			l_2s.each(function(l_2) {
				if (l_2.next()==null) {
					l_2.style.paddingBottom='10px';
				}
			});

			u_3s.each(function(u_3) {
				var l_3s=u_3.childElements();
				l_3s.each(function(l_3) {
					if (l_3.next()==null) {
						l_3.style.marginBottom='10px';
					}
				});
			});

			if (header.className=='gn_right')	{
				var gn_right=true;
				u_2.setStyle({
					'float': 'right'
				});
			}
			else	{
				var gn_right=false;
			}

			function d_2_show(a_1,d_2) {
				var a_1_dim = a_1.getDimensions();
				var a_1_pos = a_1.positionedOffset();
				var h = a_1_pos.height;
				var w = a_1_pos.width;
				var l = a_1_pos.left;
				var t = a_1_pos.top;

				var gn = $('gn');
				var d_2_cs = gn.select('div.gn_dd');
				d_2_cs.each(function(d_2_c) {
					if (d_2_c.style.zIndex='500') {
						var h_c = d_2_c.previous();
						var a_1_c = h_c.down('a');
						d_2_hide(a_1_c,d_2_c);
					}
				});

				d_2.setStyle({
					top: (t+h) + 'px'
				});
				if (gn_right==true) {
					d_2.setStyle({
						left: l+w-220 + 'px'
					});
				}
				else {
					d_2.setStyle({
						left: l + 'px',
						'z-index': '500'
					});
				}
				a_1.addClassName('gn_active');
				gn_ishim(true,d_2);
			}

			function d_2_hide(a_1,d_2) {
				d_2.setStyle({
					'marginLeft': '0px',
					width: '220px',
					height: 'auto',
					left: '-1000em',
					'z-index': '499'
				});
				a_1.removeClassName('gn_active');
				gn_ishim(false,d_2);
			}

			a_1.observe('focus', function() {
				d_2_show(a_1,d_2);
			});
				
			a_1.observe('blur', function() {
				d_2_hide(a_1,d_2);
			});

			a_1.observe('keydown', function(event) {
				event = new Event(event);
				if (event.key=='down') {
					u_2.firstDescendant().firstDescendant().focus();
				}
				else if (event.key=='left' && header.previous) {
					header.previous().previous().down('a').focus();
				}
				else if (event.key=='right' && header.next().next()) {
					header.next().next().down('a').focus();
				}
			});

			a_1.observe('mouseenter', function(event){
				var rt = event.relatedTarget;
				while (rt && rt != d_2 && rt.nodeName != 'BODY')
					rt = rt.parentNode;
				if (rt == d_2) return;
				d_2_show(a_1,d_2);
			});

			a_1.observe('mouseleave', function(event){
				var rt = event.relatedTarget;
				if (fMSIE) {
					rt = event.toElement;
				}
				while (rt && rt != d_2 && rt.nodeName != 'BODY')
					rt = rt.parentNode;
				if (rt == d_2) return;
				
				a_1.removeClassName('gn_active');
				d_2_hide(a_1,d_2);
				gn_fo(a_1);
			});

			d_2.observe('mouseleave', function(event){
				var rt = event.relatedTarget;
				while (rt && rt != a_1 && rt.nodeName != 'BODY')
				rt = rt.parentNode
				if (rt == a_1) return;
				
				a_1.removeClassName('gn_active');
				d_2_hide(a_1,d_2);
				gn_fo(a_1);
			});

			l_2s.each(function(l_2) {				
				var a_2 = l_2.firstDescendant();
				if (gn_right==true) {
					if (a_2.next()!=null) {	
						a_2.addClassName('left_arrow');
					}
				}
				else	{
					if (a_2.next()!=null) {
						a_2.addClassName('right_arrow');
					}
					a_2.style.paddingLeft='10px';
				}

				a_2.observe('mouseenter',  function() {
					gn_fo(a_2);
					a_2.setStyle({
						'background-color': '#990000'
					});
				});

				a_2.observe('mouseleave', function() {
					gn_fo(a_2);
					a_2.setStyle({
						'background-color': '#000'
					});
				});

				a_2.observe('focus', function() {
					d_2_show(a_1,d_2);
					a_2.setStyle({
						'background-color': '#990000'
					});
				});

				a_2.observe('blur', function() {
					d_2_hide(a_1,d_2);
					a_2.setStyle({
						'background-color': '#000'
					});
				});

				a_2.observe('keydown', function(event) {
					event = new Event(event);
					if (event.key=='up' && l_2.previous())
					{
						l_2.previous().firstDescendant().focus();
					}
					else if (event.key=='up' && !(l_2.previous()))
					{
						a_1.focus();
					}
					else if (event.key=='down' && l_2.next())
					{
						l_2.next().firstDescendant().focus();
					}
					else if (event.key=='left' && header.previous())
					{
						header.previous().previous().down('a').focus();
					}
					else if (event.key=='right' && header.next().next())
					{
						header.next().next().down('a').focus();
					}
				});
			});

			function gn_fo(a_2) { 
				var u_2_h = u_2.getDimensions().height;

				u_3s.each(function(u_3) {
					
					if (u_3.style.left=='auto' && u_3!=a_2.next())
					{
						var u_3_h = u_3.getDimensions().height;

						if (gn_right==true) {
							u_3.setStyle({
								'marginLeft': '-220px'
							});
						}
						
						u_3.previous().removeClassName('gn_out');
						u_3.setStyle({
							left: '-1000em'
						});
						u_3.setStyle({
							height: 'auto'
						});
						if (!(a_2.next()))
						{
							d_2.setStyle({
								'marginLeft': '0px',
								width: '220px',
								height: 'auto'
							});
							gn_ishim(true,d_2);
						}
					}

					if (u_3==a_2.next() && u_3.style.left!='auto')
					{
						var u_3_h = u_3.getDimensions().height;
						
						if (gn_right==true) {
							u_3.setStyle({
								'marginLeft': '-220px'
							});
						}

						a_2.addClassName('gn_out');
						u_3.setStyle({
							left: 'auto',
							height: 'auto',
							width: '220px'
						});
						d_2.setStyle({
							width: '440px'
						});
						if (gn_right==true)
						{
							d_2.setStyle({
								'marginLeft': '-220px'
							});
						}
						if (u_3_h>u_2_h)	{
							d_2.setStyle({
								height: u_3_h+30 + 'px'
							});
						}
						else {
							d_2.setStyle({
								height: 'auto'
							});
							var d_2_h = d_2.getDimensions().height;
							d_2.setStyle({
								height: d_2_h+30
							});
						}
						gn_ishim(true,d_2);
					}
				})
			}
		});
	}
}