dojo.addOnLoad(function() {
    dojo.connect(window, 'keydown', this, function (event) {
        // attach profiler on F11
        if (event.keyCode == dojo.keys.F9) {
            if (dojo.byId('profilerTable')) {
                Profiler.show();
                if (dojo.byId('profilerTable') && dojo.byId('profilerTable').style.display != 'none') {
                    document.location = '#profiler';
                }
            }
        }
    });

    FlashMessenger.hideNotices();
});

var Profiler = {

    firstCellHighlight: '#FFAC7F',
    cellHighlight:      '#e5e5e5',

    show: function () {
        var profilerTable = dojo.byId('profilerTable');
        profilerTable.style.display = (profilerTable.style.display == '' ? 'none' : '');
    },

    bookmark: function (profilerRow) {

        for (i=0; i < profilerRow.cells.length; i++) {
            cell = profilerRow.cells[i];
            if (cell.style.backgroundColor == '') {
                if (i == 0) {
                    cell.style.backgroundColor = this.firstCellHighlight;
                } else {
                    cell.style.backgroundColor = this.cellHighlight;
                }
            } else {
                cell.style.backgroundColor='';
            }
        }
    }
}

var FlashMessenger = {

    error : function (message) {
        dojo.byId('fmErrorsAlerts').innerHTML  = '<li>' + message +     '</li>';
        dojo.byId('fmErrors').style.display = '';
        this.hideNotices();
    },

    notice : function (message) {
        dojo.byId('fmNoticesAlerts').innerHTML = '<li>' + message +     '</li>';
        dojo.byId('fmNotices').style.display = '';
        this.hideNotices();
    },

    hideNotices : function() {

        if (dojo.byId('fmNotices') && dojo.byId('fmNotices').style.display != 'none') {
            dojo.fadeOut({

                node:     "fmNotices",
                duration: 500,
                delay:    3000,

                onEnd: function() {
                    dojo.byId('fmNoticesAlerts').innerHTML = '';
                    dojo.byId('fmNotices').style.display = 'none';
                    FlashMessenger.onHide();
                }
            }).play();
        }
    },

    onHide: function() {}
}

var Popup = {

    start : function (content,title) {
        var p = dijit.byId('fmPopup');
        p.set('content',content);
        p.set('title',title);
        p.show();
    },

    end: function() {
        var p = dijit.byId('fmPopup');
        p.set('content','');
        p.set('title','');
        p.hide();
    }
}
