(function(){
    var MENU_STATUS = "admin-menu-status";
    var MENU_ID = "menu";
    var TOGGLER_ID = "menu-toggler";
    var MENU_CONTAINER_ID = "menu-container";
    var CONTENT_ID = "contentu";

    var header = null;
    var toggler = null;
    var menuContainer = null;
    var content = null;
    var status = null;

    function setCookie(c_name, value, exdays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
        document.cookie = c_name + "=" + c_value;
    }

    function getCookie(c_name) {
        var c_value = document.cookie;
        var c_start = c_value.indexOf(" " + c_name + "=");
        if (c_start == -1) {
            c_start = c_value.indexOf(c_name + "=");
        }
        if (c_start == -1) {
            c_value = null;
        }
        else {
            c_start = c_value.indexOf("=", c_start) + 1;
            var c_end = c_value.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = c_value.length;
            }
            c_value = unescape(c_value.substring(c_start, c_end));
        }
        return c_value;
    }

    function header_toggler_click_handler() {
        if (Element.visible(header)) {
            setCookie(MENU_STATUS, status = "COLLAPSED", 7);
        } else {
            setCookie(MENU_STATUS, status = "EXPANDED", 7);
        }

        if( status == "EXPANDED"){
            Element.show(header);
            Element.removeClassName(toggler, "show-header");
            Element.addClassName(toggler, "hide-header");

            Element.removeClassName(menuContainer, "menu-collapsed");
            Element.addClassName(menuContainer, "menu-expanded");
        }else{
            Element.hide(header);
            Element.removeClassName(toggler, "hide-header");
            Element.addClassName(toggler, "show-header");

            Element.removeClassName(menuContainer, "menu-expanded");
            Element.addClassName(menuContainer, "menu-collapsed");
        }
    }

    function init() {
        header = $(MENU_ID);
        toggler = $(TOGGLER_ID);
        menuContainer = $(MENU_CONTAINER_ID);
        content = $(CONTENT_ID);

        if( !header && !toggler){
            return;
        }

        var headerStatus = getCookie(MENU_STATUS);
        if (headerStatus != null && headerStatus == "COLLAPSED") {
            Element.hide(header);
            Element.addClassName(toggler, "show-header");
            Element.addClassName(menuContainer, "menu-collapsed");
        }else{
            Element.addClassName(toggler, "hide-header");
            Element.addClassName(menuContainer, "menu-expanded");
        }

        toggler.onclick = header_toggler_click_handler;

        var hd = Element.getDimensions( $("emid-app-header"));
        var md = Element.getDimensions( menuContainer);
        var wd = getWindowDim();

        content.style.width = "" + (hd.width - md.width - 10) + "px";
        content.style.height = "" + (wd.height - hd.height - 3 - 10) + "px";
    }

    function getWindowDim(){
        var winW = 630, winH = 460;
        if (document.body && document.body.offsetWidth) {
            winW = document.body.offsetWidth;
            winH = document.body.offsetHeight;
        }
        if (document.compatMode=='CSS1Compat' &&
            document.documentElement &&
            document.documentElement.offsetWidth ) {
            winW = document.documentElement.offsetWidth;
            winH = document.documentElement.offsetHeight;
        }
        if (window.innerWidth && window.innerHeight) {
            winW = window.innerWidth;
            winH = window.innerHeight;
        }

        return {
            height: winH,
            width: winW
        }
    }

    jQuery(document).ready(function(){
        //init();
    });
})();
