$ef(document).ready(function() {
	
	// slider
	$ef('#login-signup').hide();
	$ef('#fone').hide();
	$ef('#ftwo').hide();
	$ef('#fthree').hide();
	$ef('#notice').hide();
	
	$ef('#login').toggle(function()
		{
			$ef('html, body').animate({scrollTop:0}, 'slow');
			$ef('#login-signup:hidden').slideDown();
			return false;
		},
		function()
		{
			$ef('#login-signup:visible').slideUp();
			return false;
		}
	);
	
	$ef("#close").click(function()
		{	
			$ef('#login-signup:visible').slideUp();
			return false;			 		
		}
	);
	
	$ef("#fone").fadeTo(500,1,function(){
    	$ef(this).fadeIn("slow");
	});
	
	$ef("#ftwo").fadeTo(1500,1,function(){
    	$ef(this).fadeIn("slow");
	});
	
	$ef("#fthree").fadeTo(2500,1,function(){
    	$ef(this).fadeIn("slow");
	});
	
	$ef("#notice").fadeTo(0,1,function(){
    	//$ef(this).fadeIn("slow");
        $ef(this).show();
	});
	
	$ef("#close-notice").click(function()
		{	
			$ef('#notice').hide();
			return false;			 		
		}
	);	
		
	$ef(function() { 		   
		$ef('ul > li > ul').addClass("hide");
		$ef('ul li:has(ul li)').children('a').toggle(function()
		{
			$ef('html, body').animate({scrollTop:0}, 'slow');
			$ef(this).parent().children('ul').slideDown();
			$ef(this).addClass('selected');
			serializeMenu();
			return false;
		},
		function()
		{
			$ef(this).parent().children('ul').slideUp();
			$ef(this).removeClass('selected');
			serializeMenu();
			return false;
		});
		loadMenu();
	});
	
});

function openMenu(selectedChild)
{
	if(undefined != selectedChild)
	{
		var currentChild = $ef(selectedChild);
		currentChild.parent().parent().show();
		while(undefined != currentChild && !currentChild.parent().parent().is('.nav'))
		{
			currentChild.parent().parent().parent().children('a').addClass('selected');
			currentChild = $ef(currentChild.parent().parent().parent().children('a')[0]);
			currentChild.parent().parent().show();
		}
	}
}

function serializeMenu()
{
	var navMenu = $ef('.nav > li');
	var jsonRep = new Object();
	jsonRep.rep =	"{ obj: ";
	menuTree = checkChildrenOpen(navMenu, jsonRep);
	jsonRep.rep += " }";
	createCookie("menu", jsonRep.rep);
}

function loadMenu()
{
	if(null != readCookie("menu") && "" != readCookie("menu"))
	{
		var origCookie = readCookie("menu");
		origCookie = origCookie.replace(/-/g, ",");
		var menuState = eval(origCookie);
		var navMenu = $ef('.nav > li');
		if(null != menuState)
			checkChildrenState(navMenu, menuState);
	}
}

function checkChildrenState(navUL, childMenuState)
{
	for(var i = 0; i < navUL.length; i++)
	{
		if( (typeof childMenuState[i] != 'undefined') && (childMenuState[i].isTrue) )
		{
			$ef(navUL[i]).children('a').addClass("selected");
			$ef(navUL[i]).children('ul').show();
			checkChildrenState($ef($ef(navUL[i]).children('ul').children('li')), childMenuState[i].obj);
		}
	}
}

function checkChildrenOpen(navUL, json)
{
	var menuTreeBranch = new Array(navUL.length);
	
	json.rep += "[";
	for(var i = 0; i < navUL.length; i++)
	{
		json.rep += "{";
		menuTreeBranch[i] = ($ef(navUL[i]).children('.selected').length > 0);
		json.rep += "isTrue:" + menuTreeBranch[i];
		if(menuTreeBranch[i])
		{
			json.rep += "- obj: ";
			menuTreeBranch[i].children = checkChildrenOpen($ef($ef(navUL[i]).children('ul').children('li')), json);
		}
		json.rep += "}";
		if(i < navUL.length - 1)
			json.rep += "- ";
	}
	json.rep += "]";
	return menuTreeBranch;
}

var menuTree;

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
