jQuery.noConflict();
var toggle_jq$ = jQuery;

var TOGGLE_MAX_HEIGHT = (toggle_jq$.browser.msie)?720:700;

toggle_jq$(document).ready(function() {

	var date = new Date();
	date.setTime(date.getTime() + (24 * 60 * 60 * 1000));

	var anc = toggle_jq$.url.attr("anchor");
	anc=(anc)?anc:'';
	var params = anc.split('/');
	params.shift();
	var active = new Array();
	var inactive = new Array();
	if(1<params.length){
		for (var p in params){
			if(!(p%2)){
				var t = toggle_jq$('A.toggle[rel='+params[p]+']').parent();
				var ti = toggle_jq$("li").index(t);
				if(-1<ti)
					active.push(ti);
				var t2 = toggle_jq$(t).parent().parent();
				var ti2 = toggle_jq$("li").index(t2);
				if(-1<ti2)
					active.unshift(ti2);
			}
		}
	}else{
		var t = toggle_jq$('li.open > A.toggle').parent();
		var ti = toggle_jq$("li").index(t);
		toggle_jq$('li.direct > A.toggle[rel=toggle_group]').each(function(){
			var ci = toggle_jq$("li").index(toggle_jq$(this).parent());
			if(ti==ci)
				active.push(ci);
			else
				inactive.push(ci);
		});
	}
	

	var pos = toggle_jq$('ul.slave').position();
	var slave_top = pos.top;

	var path = toggle_jq$("body").attr('class');
	var global_path = path.split(' ');
	path=(global_path.length)?global_path[0]:path;
	var menu_cookie = toggle_jq$.cookie(path+"Menu");
	if(menu_cookie){
		for (var a in active)
			menu_cookie = menu_cookie.replace(active[a]+':0',active[a]+':1');
		for (var a in inactive)
			menu_cookie = menu_cookie.replace(inactive[a]+':1',inactive[a]+':0');
		var cookie = menu_cookie.split(',');
		var obj = new Object();
		for (var j=0; j<cookie.length;j++){
			var temp = cookie[j].split(':');
			obj[temp[0]]=temp[1];
		}
		var offset = 0;
		toggle_jq$("A.toggle").parent().each( function(){
			var i = toggle_jq$("li").index(this);
			if(obj[i]==0){
				toggle_jq$(this).find("A.toggle").next().hide();
				toggle_jq$(this).find("A.toggle").removeClass('minus');
				toggle_jq$(this).find("A.toggle").addClass('plus');
			}else{
				toggle_jq$(this).find("A.toggle").next().show();
				toggle_jq$(this).find("A.toggle").removeClass('plus');
				toggle_jq$(this).find("A.toggle").addClass('minus');
			}
		});
	}else{
		cookie = new Array();
		menu_cookie="";
		var j=0;
		var active_s = active.join(':');
		toggle_jq$("A.toggle").parent().each( function(){
			var i = toggle_jq$("li").index(this);
			if(active_s.match(i)){
				toggle_jq$(this).find("A.toggle").next().show();
				toggle_jq$(this).find("A.toggle").addClass('minus');
				cookie[j]=i+":1";
			}else{
				toggle_jq$(this).find("A.toggle").next().hide();
				toggle_jq$(this).find("A.toggle").addClass('plus');
				cookie[j]=i+":0";
			}
			j++;
		});
		menu_cookie=cookie.join(',');
	}

	var master_height = toggle_jq$('ul.master').height();
	var slave_height = toggle_jq$('ul.slave').height()+offset;
	var navigation_height=(master_height<slave_height)?slave_height:master_height;
	navigation_height+=slave_top;
	toggle_jq$('#navigation').height(navigation_height);

	var current_height = toggle_jq$('ul.slave').height()+pos.top;
	if(TOGGLE_MAX_HEIGHT<current_height){
		toggle_jq$('ul.slave').css({top:'175px'});
	}
	toggle_jq$.cookie(path+"Menu",cookie,{path:"/",expires:"-1"});

	toggle_jq$('A.no_toggle').click(function(){
		var loc=this.href;
		var i = toggle_jq$("li").index(toggle_jq$(this).parent());
		var group_name = this.rel;
		var offset=0;
		var done=false;
		if(group_name){
			toggle_jq$('A.toggle[rel=toggle_group]').each(function(){
				var i2 = toggle_jq$("li").index(toggle_jq$(this).parent());
				var re2 = String(i2+':1');
				if(i!=i2){
					if(!toggle_jq$(this).next().is(':hidden')){
						offset += toggle_jq$(this).next().height();
						menu_cookie=menu_cookie.replace(re2,i2+":0");
						toggle_jq$(this).removeClass('plus');
						toggle_jq$(this).addClass('minus');
						toggle_jq$(this).next().slideToggle("slow",function(){location.href=loc;});
						done=true;
					}
				}
			});
		}
		var slave_height = toggle_jq$('ul.slave').height()-offset;
		if(TOGGLE_MAX_HEIGHT<slave_height+pos.top){
			toggle_jq$('ul.slave').animate({top:'175px'},'slow');
		}else{
			toggle_jq$('ul.slave').animate({top:pos.top+'px'},'slow');
		}

		if(!done) location.href=loc;
		toggle_jq$.cookie(path+"Menu",menu_cookie,{path:"/",expires:"-1"});
		return false;
	});

	toggle_jq$('A.toggle').click(function(){
		var i = toggle_jq$("li").index(toggle_jq$(this).parent());
		var group_name = this.rel;
		var offset2=0;
		if(group_name == 'toggle_group'){
			toggle_jq$('A.toggle[rel=toggle_group]').each(function(){
				var i2 = toggle_jq$("li").index(toggle_jq$(this).parent());
				var re2 = String(i2+':1');
				if(i!=i2){
					if(!toggle_jq$(this).next().is(':hidden')){
						offset2 += toggle_jq$(this).next().height();
						menu_cookie=menu_cookie.replace(re2,i2+":0");
						toggle_jq$(this).removeClass('plus');
						toggle_jq$(this).addClass('minus');
						toggle_jq$(this).next().slideToggle("slow");
					}
				}
			});
		}
		var offset = toggle_jq$(this).next().height();
		var re = String(i+':1');
		var loc='';

		if(!toggle_jq$(this).next().is(':hidden')){
			menu_cookie=menu_cookie.replace(re,i+":0");
			offset *= -1;
			toggle_jq$(this).removeClass('minus');
			toggle_jq$(this).addClass('plus');
		}else{
			re = String(i+':0');
			menu_cookie=menu_cookie.replace(re,i+':1');
			toggle_jq$(this).removeClass('plus');
			toggle_jq$(this).addClass('minus');
		}
		var link_path=(jQuery.browser.msie)?'/'+this.pathname:this.pathname;
		loc=(link_path != window.location.pathname)?link_path:'';
		var pattern = '#/'
		if(!loc) loc=(this.href.match(pattern))?this.href:'';
		var master_height = toggle_jq$('ul.master').height();
		var slave_height = toggle_jq$('ul.slave').height()+offset-offset2;
		var navigation_height=(master_height<slave_height)?slave_height:master_height;
		navigation_height+=slave_top;
		toggle_jq$('#navigation').animate({'height':navigation_height},"slow",function(){
			if(loc) location.href = loc;
		});
		toggle_jq$(this).next().slideToggle("slow");

		if(TOGGLE_MAX_HEIGHT<slave_height+pos.top){
			toggle_jq$('ul.slave').animate({top:'175px'},'slow');
		}else{
			toggle_jq$('ul.slave').animate({top:pos.top+'px'},'slow');
		}

		toggle_jq$.cookie(path+"Menu",menu_cookie,{path:"/",expires:"-1"});
		return false;
	});
});

