"use strict";

var L = function(name)
{
    return chrome.i18n.getMessage(name);
}

// Fired when the extension is first installed, when the extension is updated to a new version, and when Chrome is updated to a new version
// http://developer.chrome.com/extensions/runtime.html#event-onInstalled
chrome.runtime.onInstalled.addListener(function (details) {

    // good place to set default options
    function setDefaults(callback) {
        storage.area.get(function (stored_options) {
            var default_options = storage.default_options,
                option,
                new_options = {};
            for (option in default_options) {
                if (!stored_options.hasOwnProperty(option)) {
                    new_options[option] = default_options[option];
                }
            }
            if (Object.keys(new_options).length !== 0) {
                // save to area if new default options is appeared
                storage.area.set(new_options, function () {
                    if (typeof callback === 'function') {
                        callback();
                    }
                });
            } else {
                if (typeof callback === 'function') {
                    callback();
                }
            }
        });
    }

    switch (details.reason) {
    case 'install': // if ext is  first installed
        setDefaults(function () {
            // show options page
            chrome.tabs.create({'url': 'options.html'});
        });
        break;
    case 'update':
        setDefaults();
        break;
    default:
        break;
    }
});

chrome.runtime.onUpdateAvailable.addListener(function (details) {
    // when an update is available - reload extension
    // update will be install immediately
    chrome.runtime.reload();
});

// Fired when recently closed tabs and/or windows are changed.
// This event does not monitor synced sessions changes.
chrome.sessions.onChanged.addListener(function () {
    chrome.storage.local.set({"changed": true});
    //alert("chrome.sessions.onChanged");
    //console.log("chrome.sessions.onChanged");
}); 

function showWindow() {
    var w = 650, h = 600;
    var left = 100; // (screen.width/2)-(w/2);
    var top = 150; // (screen.height/2)-(h/2);

    chrome.windows.create({
        url: "popup.html",
        type: "popup",
        width: w,
        height: h,
        left: left,
        top: top
    });

    //chrome.tabs.create({url:"popup.html"});
    //window.open("popup.html", "extension_popup", "width=300,height=400,status=no,scrollbars=yes,resizable=no");

    // var popupWindow = window.open(
    //     "popup.html",
    //     L("extName"),
    //     "width=" + w + ", height=" + h + ", left=" + left + ", top=" + top,
    //     true
    // );    
}

function undoLastTab() {
    chrome.sessions.restore();
}

chrome.commands.onCommand.addListener(function(command) {
    // alert(command);
    
    if(command == "undoLastTab") {
        undoLastTab();
    }
    else if(command == "openWindow") {
        showWindow();
    }
});