(function(){
    var EMID_HEADER_STATUS = "header-status";
    var HEADER_ID = "top-header";
    var TOGGLER_ID = "header-toggler";

    var INITIAL_STATUS = "HIDDEN";

    var header = null;
    var toggler = null;



    function header_toggler_click_handler() {
        if (Element.visible(header)) {
            Element.hide(header);
            Element.removeClassName(toggler,"hide-header");
            Element.addClassName(toggler,"show-header");
            COOKIE.setCookie(EMID_HEADER_STATUS, "HIDDEN");
        } else {
            Element.show(header);
            //jQuery(header).fadeIn("slow");
            Element.removeClassName(toggler, "show-header");
            Element.addClassName(toggler,"hide-header");
            COOKIE.setCookie(EMID_HEADER_STATUS, "SHOWN");
        }
        resizeContent();
    }

    function init() {
        header = $(HEADER_ID);
        toggler = $(TOGGLER_ID);

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

        var headerStatus = COOKIE.getCookie(EMID_HEADER_STATUS);
        if(!headerStatus || (headerStatus != "HIDDEN" && headerStatus != "SHOWN")){
            headerStatus = INITIAL_STATUS;
        }

        if (headerStatus == "HIDDEN") {
            Element.hide(header);
            Element.addClassName(toggler, "show-header");
        }else{
            Element.show(header);
            Element.addClassName(toggler, "hide-header");
        }

        COOKIE.setCookie(EMID_HEADER_STATUS, headerStatus);

        toggler.onclick = header_toggler_click_handler;
    }

    function resizeContent(){
        //definit in includes-emid.jspf
        if(!layoutOffset){
            layoutOffset = 10;
        }
        jQuery('.content').height(jQuery(window).height() - jQuery('.header').outerHeight() - layoutOffset);
    }

    function setScreenSizeCookie(){
        try{
            COOKIE.setCookie("user-sw", window.screen.width);
            COOKIE.setCookie("user-sh", window.screen.height);
        }catch(e){
            alert("Eroare setare screen size in cookies:" + e.message);
        }
    }

    jQuery(document).ready(function(){
        try {
            init();

            //on load
            setScreenSizeCookie();

            jQuery("*[data-href]").click(function(){
                var href = jQuery(this).data("href");
                var confirmMessage = jQuery(this).data("confirm");
                if(confirmMessage){
                    if(confirm(confirmMessage)){
                        window.location = href;
                    }
                    return;
                }

                window.location = href;
            });

            resizeContent();

            jQuery(".logout").click(function(){
                jQuery.ajax( _gAppName + "?action=logout", {
                    complete: function () {
                        window.location = _gAppName;
                    }
                });
            });

            jQuery(window).resize(function () {
                resizeContent()
            });
        }catch(e){
            if(console) console.error(e);
            alert("layout.js: Eroare document ready" + e.message);
        }
    });
})();
