/*
##########################################
INIT functions for core javascript library
##########################################

Must be called after all core js requirement in HTML/XHTML
*/

//// init controllers
if (!window.page) {
    window.codex = new CodexController();
    window.post = new PostController();
    window.auth = new AuthController();
    window.page = new PageController();
    window.newsletter = new NewsletterController();
    window.search = new SearchController();
};

//// init history manager
if (!window.historyManager) {
    window.historyManager = new HistoryManager();
    window.historyChange = function(hash) {
        console.log('historyChange('+hash+')');
        window.historyManager.addState(hash);
        page.load(hash,'content');
    }
    window.historyManager.addEvent('onHistoryChange',historyChange); // get default page controller for history change
};

// console compatibility
if (!window.console) {
    window.console = new ConsoleController();
};

function init() {
    //// normalize URL
    console.log('init::normalizeUrl('+location.href+')');
    // check url and go to the right place
    if (location.href.split(location.host)[1] != "/" && location.href.split(location.host)[1].split('/')[1] != '#') {
        if (historyManager.getCurrentLocation() != "") {
            console.log('HREF (HistoryManager) : '+historyManager.getCurrentLocation());
            location.href = "/#" + historyManager.getCurrentLocation();
        }
        else{
            console.log('HREF (URI) : '+location.href.split(location.host)[1]);
            location.href = "/#" + location.href.split(location.host)[1];
        }
    }
    else if (location.href.split(location.host)[1] != "/")  {
        console.log('HREF (HistoryManager) : '+historyManager.getCurrentLocation());
        page.load(historyManager.getCurrentLocation());
    }
    initUI();
}

function initUI() {
    checkBrowser('/stylesheets/style');
    if(session.adminMode == true) {
        if (!window.editPanel) {
            window.editPanel = new EditPanel();
        };
        if (window.editPanel) {
            editPanel.updateEditMode(historyManager.getCurrentLocation());
            window.editPanel.currentElement = null;
        };
    };
}