
/* Store ALL newly opened windows here! 
 * To close them on logout. */
var eco_opened_windows = Array();

/* Include all js for specified module -- includes in order in which it is
 * specified in the configuration array */
var get_scripts_in_order = function (module_id, i) {
    if (i < modules[module_id].js.length) {
        $.getScript(modules[module_id].js[i], function () {
            get_scripts_in_order(module_id, ++i);
        });
    } else {
        $.unblockUI();
    }
}

var set_dimensions = function () {
    act_height = $(window).height() - $('#header').height() - height_magic;
    if ( act_height < min_height ) { act_height = min_height; }
    $('#content').css('height', act_height + 'px');
    act_width = $(window).width() - width_magic;
    if ( act_width < min_width ) { act_width = min_width; }
    $('#content').css('width', act_width + 'px');
    $('#content').css('margin-left', '-' + (act_width - min_width)/2 + 'px');
}

var menu_callback = function () {
        module_id = $(this).attr('id');
        // At first hide everything
        $(".contents").hide();
        // Deselect all menu items
        $(".kicker").removeClass("selected");
        // Make current menu item selected
        $(this).addClass('selected');
        if ($("#contents_" + module_id).length > 0) {
            // Module already loaded - just make it visible
            $("#contents_" + module_id).show();
        } else {
            // Load the module
            $.ajax({
                url: modules[module_id].url,
                data: {'data':form},
                cache : false,
                type : form_method,
                beforeSend: function(){
                    $.blockUI({
                        message: "<img src=\""+web_root+"design/images/ajax-loader.gif\" alt=\"loading...\">",
                        css: { backgroundColor: 'transparent', color: '#fff', border: '0px'}
                    })
                },
                success: function(html){
                    var ndiv = document.createElement('div');
                    // Set ID of created content DIV
                    $(ndiv).attr('id', "contents_" + module_id);
                    // Add class
                    $(ndiv).addClass('contents');
                    // Make it visible
                    //$("#{/literal}{$module_item.id}{literal}").show();
                    // Append AJAX contents
                    $(ndiv).html(html);
                    // Append to root DIV
                    $("#content").append(ndiv);
                    set_dimensions();
                    // $('#content').css('height', $(window).height() - $('#header').height() - height_magic);
                    // Include all js scripts -- also unlocks UI
                    get_scripts_in_order(module_id, 0);
                }
            });
        }
}

/* XXX 
 * Settings variables -- here for now. Later maybe in some config.
 * XXX */
var session_life = 1800000; // session life 30 minutes
var session_check_interval = 60000; // check session every 60 seconds
var session_refresh_interval = 600000; // refresh session after 10 minutes.

/* Set last activity to now() */
var start_d = new Date();
var last_act = start_d.getTime();

/* Handle session timout */
var session_timeout_handler = function () {
    var last_session_refresh;
    d = new Date();
    if ((d.getTime() - last_act) > session_life) {
        /* Session timed out -- logout */
//        console.log("Session timed out at %s", d.toLocaleString());
        if ($.cookie('eco_user_logged') === 'true') {
            /* User is logged in -- so logout. */
//            console.log('user logged in -- logout ...');
            if (typeof(isTmpWindowOpen) != "undefined") if (isTmpWindowOpen()) {
                window['tmpW'].close();
            }
            top.location = web_root + 'include/logout.php';
        } else {
            /* User not logged -- restart session (to keep session alive as
             * long as browser window is opened). */
            last_act = d.getTime();
        }
    } else {
        /* Still active session -- check if the PHP session do not need to
         * refresh. Standart lifetime of PHP session 1440s. */
//        console.log($.cookie('eco_session_last_refresh'));
        last_session_refresh = $.cookie('eco_session_last_refresh') * 1000;
        if (d.getTime() - last_session_refresh > session_refresh_interval) {
            /* Session refresh needed -- otherwise server side session
             * would possibly timeout. */
            $.ajax({
                url : web_root + 'include/_defines.php',
                data : '',
                cache : false
            });
        }
    }
    setTimeout("session_timeout_handler()", session_check_interval);
}

/* "Ping" session -- to tell that session is alive. */
var ping_session = function () {
    var act_d = new Date();
    last_act = act_d.getTime();
//    console.log("Session trigered at %s", act_d.toLocaleString());
}

/* Trigger session activity on click && keyup */
$('*').click( function () { ping_session(); });
$('*').keyup( function () { ping_session(); });

/* Start session checking timer. */
setTimeout("session_timeout_handler()", session_check_interval);

