var slideup = true;
var topul;
var accsliding = false;
var browserie6 = false;
var browsersafari = false;
var zcounter = 1000;
var ulchild;
var configTop = {    
	sensitivity: 5,
	interval: 100,   
	over: overTop,
	timeout: 250,
	out: outTop
};
var configSec = {    
     sensitivity: 10,
     interval: 50, 
     over: overSec, 
     timeout: 100, 
     out: outSec 
};

$(document).ready(function()
{
	if (jQuery.browser.msie){
		if (jQuery.browser.version <= 6.0) {browserie6 = true;};
	};

	if (jQuery.browser.safari){browsersafari = true;};

	if (jQuery.browser.opera) {
		window.onunload = setpos;
		pos1 = 99;
		pos2 = 99;
		arrcookies = document.cookie.split(';');
		for (i=0;i<arrcookies.length;i++){
			if (arrcookies[i].indexOf('menupos')!=-1){
				arrposcookie = arrcookies[i].split('=');
				arrpos = arrposcookie[1].split('-');
				pos1 = arrpos[0];
				pos2 = arrpos[1];
			};
		};
	};
	
	topul = document.getElementById('nav').firstChild;
	topul.className='acc0';
	var tpch = topul.childNodes;
	for (i=0; i<tpch.length; i++){
		var lftd = tpch[i].firstChild.firstChild.firstChild.firstChild;
		lftd.className = 'left';
		$(lftd).hover(overTop,outTop);
		var rttd = lftd.nextSibling;
		rttd.className = 'right';
		var ulchild = tpch[i].firstChild.nextSibling;
		if (ulchild==null){
			tpch[i].className = 'nochildren';
		} else {
			$(rttd).hover(overTop,outTop);
			$(rttd.firstChild).click(clickacc1);
			ulchild.style.display='none';
		};
	};
	if (pos1 != 99) {
		clickacc1();
		if (pos2 != 99) {
			clickacc2();
		};
	};
});

function overTop(){
	if ($(this).hasClass('left')){$(this).addClass('hoverLeft');}; 
	if ($(this).hasClass('right')){$(this).addClass('hoverRight');};
}
	
function outTop(){
	$(this).removeClass('hoverTop');
	$(this).removeClass('hoverLeft');
	$(this).removeClass('hoverRight');
}

function clickacc1() {
	if (this.tagName){
		accsliding = true;
		var clickedul = this.parentNode.parentNode.parentNode.parentNode.nextSibling;
		var clickedtr = this.parentNode.parentNode;
		
	} else {
		var clickedul = topul.childNodes[pos1].firstChild.nextSibling;
		var clickedtr = topul.childNodes[pos1].firstChild.firstChild.firstChild;
	};
	if (clickedul.className != 'acc1') {
		clickedul.className = 'acc1';
		var lich = clickedul.childNodes;
		for (var i=0; i<lich.length; i++){
			var lftd = lich[i].firstChild.firstChild.firstChild.firstChild;
			lftd.className = 'left';
			$(lftd).hover(overTop,outTop);
			var rttd = lftd.nextSibling;
			rttd.className = 'right';
			ulchild = lich[i].firstChild.nextSibling;
			if (ulchild==null){
				lich[i].className = 'nochildren';
			} else {
				$(rttd).hover(overTop,outTop);
				$(rttd.firstChild).click(clickacc2);
				ulchild.style.display='none';
			};
		};
	};
	var clickedlis = clickedul.getElementsByTagName('LI');
	for (i=0;i<clickedlis.length;i++) {
		clickedlis[i].style.position = 'static';
	};
	//$(".acc1 li").css("position","static");
	if (clickedul.style.display=='block'){
		clickedtr.className = '';
		pos1 = 99;
	}
	else {
		clickedtr.className = 'tractive';
		var j = 0;
		var tmpnode = clickedul.parentNode;
		while (tmpnode != clickedul.parentNode.parentNode.firstChild) {
			tmpnode = tmpnode.previousSibling;
			j++
		}
		pos1 = j;
	};
	
	if (this.tagName){
		$(clickedul).slideToggle(300,function(){
			accsliding = false; //for FF to stop hover during slide
			for (i=0;i<clickedlis.length;i++) {
				clickedlis[i].style.position = 'relative';
			};
			//$(".acc1 li").css("position","relative");
		});
		if (slideup){
			closeup(clickedul.parentNode);
			pos2 = 99;
		};
	} else {
		clickedul.style.display = 'block';
		for (i=0;i<clickedlis.length;i++) {
			clickedlis[i].style.position = 'relative';
		};
		//$(".acc1 li").css("position","relative");
	};
}
	
function clickacc2() {
	if (this.tagName){
		accsliding = true;
		var clickedul = this.parentNode.parentNode.parentNode.parentNode.nextSibling;
		var clickedtr = this.parentNode.parentNode;
		
	} else {
		var clickedul = topul.childNodes[pos1].firstChild.nextSibling.childNodes[pos2].firstChild.nextSibling;
		var clickedtr = topul.childNodes[pos1].firstChild.nextSibling.childNodes[pos2].firstChild.firstChild.firstChild;
	};
	if (clickedul.className!='acc2') {
		clickedul.className = 'acc2';
		clickedul.firstChild.firstChild.className = 'topa';
		
		var lich = clickedul.childNodes;
		for (i=0; i<lich.length; i++){
			$(lich[i]).hoverIntent(configSec);	
			ulchild = lich[i].firstChild.nextSibling;
			if (ulchild!=null){
				ulchild.previousSibling.className += ' flypoint';
				ulchild.className = 'fly';
				ulchild.style.display='none';
			};
		};
	};
	
	var clickedlis = clickedul.getElementsByTagName('LI');
	for (i=0;i<clickedlis.length;i++) {
		clickedlis[i].style.position = 'static';
	};
	//$(".acc2 li").css("position","static");
	if (clickedul.style.display=='block'){
		clickedtr.className = '';
		pos2 = 99;
	}
	else {
		clickedtr.className = 'tractive';
		var j = 0;
		var tmpnode = clickedul.parentNode;
		while (tmpnode != clickedul.parentNode.parentNode.firstChild) {
			tmpnode = tmpnode.previousSibling;
			j++
		}
		pos2 = j;
	};
	
	if (this.tagName){
		$(clickedul).slideToggle(300,function(){
			accsliding = false; //for FF to stop hover during slide
			for (i=0;i<clickedlis.length;i++) {
				clickedlis[i].style.position = 'relative';
			};
			//$(".acc2 li").css("position","relative");
		});
		if (slideup){
			closeup(clickedul.parentNode);
		};
	} else {
		clickedul.style.display = 'block';
		for (i=0;i<clickedlis.length;i++) {
			clickedlis[i].style.position = 'relative';
		};
		//$(".acc2 li").css("position","relative");
	};
}

function closeup(liactive){
	var lich = liactive.parentNode.childNodes;
	for (var i=0; i<lich.length; i++){
		var closeupli = lich[i];
		if (closeupli!=liactive){
			if (closeupli.firstChild.firstChild.firstChild.className=='tractive') {
				if (closeupli.lastChild.tagName=='UL'){
					var closelis = closeupli.lastChild.getElementsByTagName('LI');
					for (j=0;j<closelis.length;j++) {
						closelis[j].style.position = 'static';
					};
					$(closeupli.lastChild).slideUp(300,function(){
						for (j=0;j<closelis.length;j++) {
							closelis[j].style.position = 'relative';
						};
					});
					var tractive = closeupli.firstChild.firstChild.firstChild;
					tractive.className = '';
					tractive.lastChild.firstChild.className = '';			
				};
			};
		};
	};
}

function overSec(){
	if (!accsliding) {
		if (browsersafari){this.firstChild.title = '';};
		thisul = this.firstChild.nextSibling;
		if (thisul!=null){
			thisul.firstChild.firstChild.className = 'topa';
			for (i=0; i<thisul.childNodes.length; i++){
				$(thisul.childNodes[i]).hoverIntent(configSec);	
				ulchild = thisul.childNodes[i].firstChild.nextSibling;
				if (ulchild!=null){
					ulchild.previousSibling.className += ' flypoint';
					ulchild.className = 'fly';
					ulchild.style.display='none';
				};
			};
		};
		
		var myelement = this;
		var cumtop = 0;
		while (!($(myelement).is("body,html"))){
			cumtop = cumtop + $(myelement).position().top;
			myelement = $(myelement).offsetParent();
		};
		if(cumtop - $(window).scrollTop() + $(this).children("ul").outerHeight() > $(window).height()){
			$(this).children("ul").css("top",$(this).outerHeight()-$(this).children("ul").outerHeight());
		}
		else {
			$(this).children("ul").css("top",0);
		};
		$(this).children("ul").show(-1,function(){
			if (!browserie6){
				$(this).parent().append("<div></div>")
				var tmpht = $(this).outerHeight();
				var tmpwd = $(this).outerWidth();
				var tmpleft = $(this).position().left;
				var tmptop = $(this).position().top;
				$(this).parent().children("div").addClass('shadow');
				$(this).parent().children("div").css("height",tmpht);
				$(this).parent().children("div").css("width",tmpwd);
				$(this).parent().children("div").css("top",tmptop);
				$(this).parent().children("div").css("left",tmpleft);
				$(this).parent().children("div").css("z-index",-100);
			};
		});
		$(this).css("z-index",zcounter);
		$(this).siblings("li").css("z-index",50);
		zcounter= zcounter + 100;
		$(this).children("a").addClass('hover');
		$(this).children(".flypoint").addClass('flypointhover');
		$(this).children(".flypoint").removeClass('flypoint');
	};
}
	
function outSec(){
	if($(this).children("a").hasClass('hover')) {zcounter= zcounter - 100;};
	$(this).children("a").removeClass('hover');
	$(this).children("ul").hide();
	if ($(this).is(":has(ul)") && !browserie6)  {$(this).children("div").remove()};
	$(this).children(".flypointhover").addClass('flypoint');
	$(this).children(".flypointhover").removeClass('flypointhover');
}

function setpos(){
	document.cookie = 'menupos=' + pos1 + '-' + pos2;
}
