/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*global define, tau, window, history*/

/**
 * InitPage module.
 * @requires {@link core/event}
 * @requires {@link core/template}
 * @requires {@link core/application}
 * @requires {@link MiniGallery/views/galleriesPage}
 * @requires {@link MiniGallery/views/photosPage}
 * @requires {@link MiniGallery/views/photoPage}
 * @requires {@link MiniGallery/views/ui}
 * @namespace MiniGallery/views/initPage
 * @memberof MiniGallery/views
 * @author Dominik Koleda <d.koleda@samsung.com>
 * @author Kamil Stepczuk <k.stepczuk@samsung.com>
 */
define({
    name: 'views/initPage',
    requires: [
        'core/event',
        'core/template',
        'core/application',
        'views/galleriesPage',
        'views/photosPage',
        'views/photoPage',
        'views/ui'
    ],
    def: function viewsInitPage(req) {
        'use strict';

        /**
         * Application module object.
         * @type {Module}
         */
        var app = req.core.application;

        /**
         * Handles tizenhwkey event.
         * @param {Event} event
         */
        function onHardwareKeysTap(event) {
            var keyName = event.keyName,
                page = tau.activePage.id;
            if (keyName === 'back') {
                if (page === 'galleries-page' || page === 'ajax-loader') {
                    app.getCurrentApplication().exit();
                } else {
                    history.back();
                }
            }
        }

        /**
         * Registers module event listeners.
         */
        function bindEvents() {
            window.addEventListener('tizenhwkey', onHardwareKeysTap);
        }

        /**
         * Initializes module.
         * @memberof MiniGallery/views/initPage
         */
        function init() {
            bindEvents();
        }

        return {
            init: init
        };

    }
});
