$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("#close-notice").click(function() {
        $ef('#notice').hide();
        return false;
    });


    $ef('ul > li > ul').addClass("hide");
    loadMenu();
    
    
    $ef('ul li:has(ul li)').children('a').click(function() {
        menu_section = $ef(this);
        menu_section.toggleClass('selected');
        if ( menu_section.hasClass('selected') ) {
            $ef('html, body').animate({scrollTop:0}, 'slow');
            menu_section.parent().children('ul').slideDown();
        }
        else {
            menu_section.parent().children('ul').slideUp();
        }
        serializeMenu();
        return false;
        loadMenu();
    });


    $ef('.rselected').each(function() {
        var menu_section = $ef(this).parent().parent();
        $ef('html, body').animate({scrollTop:0}, 'slow');
        menu_section.slideDown();
        menu_section.parent().children('a').addClass('selected');
        serializeMenu();
        return false;
    });

});

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("mainmenu", jsonRep.rep);
}

function loadMenu() {
    if (null != readCookie("mainmenu") && "" != readCookie("mainmenu")) {
        var origCookie = readCookie("mainmenu");
        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(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);
}


