/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4 */
/*global define: true, graphite: true, window: true*/

define([
    'jquery',
    'underscore',
    'backbone',
    'models/ConfigModel',
    'routes/appRouter',
    'controllers/AuthController',
    'ccweb.files.extract/models/MetricsProxyModel',
    'controllers/UploadController',
    'serverAPI',
    'DWPSDExtractManager',
    'ccweb.files.extract/Constants',
    'plugin-dependencies',
    'dreamweaver/controllers/DwCacheController'
], function ($, _, Backbone, ConfigModel, AppRouter, AuthController, MetricsProxyModel, UploadController, serverAPI, DWPSDExtractManager, Constants, deps, cacheController) {
    "use strict";

    window.dwPSDExtractManager = DWPSDExtractManager;
    window.graphite = window.graphite || {};
    window.graphite.routers = window.graphite.routers || {};

    // Create a global event dispatcher
    window.graphite.events = _.extend({}, Backbone.Events);

    //isFeatureEnabled
    window.graphite.isFeatureEnabled = function (aFeatureName) {
        // We are not maintaning any feature flags for dreamweaver. We will keep this 
        // method for graphite-api code to function. 
        return false;
    };
    
    window.onbeforeunload = function () {
        if (UploadController.hasActiveUploads()) {
            return deps.translate("PAGE_LEAVE_WARNING");
        }
    };
    
    // singleton server API class
    window.graphite.serverAPI = serverAPI;
    window.graphite.cacheController = cacheController;
    window.graphite.cacheController.initializeCache();
    
    
    DWPSDExtractManager.extractReadyForInit();

    $(function () {
        
        window.graphite.metricsProxyModel = new MetricsProxyModel();
        window.graphite.routers.main = new AppRouter();
        window.graphite.configModel = new ConfigModel();

        if (window.graphite.metricsProxyModel) {
            window.graphite.metricsProxyModel.setAuthModel(window.graphite.routers.main.authModel);
            window.graphite.metricsProxyModel.setConfigModel(window.graphite.configModel);
            window.graphite.metricsProxyModel.setWelcomeExperienceEvent();
        }

        graphite.events.trigger('register-defaults');
        Backbone.history.start();
    });
        
    $(document).on('keydown', function (e) {
        //Capture Backspace and prevent Browser navigation, in case target element is not editable.
        var $target = $(e.target || e.srcElement);
        if (e.keyCode === Constants.Shortcut.BACKSPACE && !$target.is('input,[contenteditable="true"],textarea')) {
            e.preventDefault();
        }
    });
    
    var argsObj = {};
    argsObj.callback = function (opened) {
       showDisableOverlay(!opened);
    };

    window.dwPSDExtractManager.isAnyDocumentOpened(argsObj);
});
