/*
 *      Copyright (c) 2015 Samsung Electronics Co., Ltd
 *
 *      Licensed under the Flora License, Version 1.1 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *              http://floralicense.org/license/
 *
 *      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, document, tau*/

/**
 * Zoom view module.
 *
 * @module views/zoom
 * @requires {@link helpers/doubleClick}
 * @namespace views/zoom
 */

define({
    name: 'views/zoom',
    requires: [
        'helpers/doubleClick'
    ],
    def: function viewsZoom(doubleClick) {
        'use strict';

        /**
         * Id dictionary.
         *
         * @memberof views/zoom
         * @private
         * @const {Object}
         */
        var ID = Object.freeze({
                PAGE: 'zoom-page',
                CONTENT: 'zoom-content',
                IMAGE: 'zoom-image'
            }),

            /**
             * Scroll center value.
             *
             * @memberof views/zoom
             * @private
             * @const {number}
             */
            SCROLL_CENTER = 180,

            /**
             * Page element.
             *
             * @memberof views/zoom
             * @private
             * @type {HTMLElement}
             */
            page = null,

            /**
             * Content element.
             *
             * @memberof views/zoom
             * @private
             * @type {HTMLElement}
             */
            content = null,

            /**
             * Image element.
             *
             * @memberof views/zoom
             * @private
             * @type {HTMLElement}
             */
            image = null;

        /**
         * Moves content scroll to image center.
         *
         * @memberof views/zoom
         * @private
         */
        function centerContentScroll() {
            content.scrollTop = SCROLL_CENTER;
            content.scrollLeft = SCROLL_CENTER;
        }

        /**
         * Sets content image.
         *
         * @memberof views/zoom
         * @public
         * @param {string} url Image URL.
         */
        function setImage(url) {
            image.style.backgroundImage = 'url(' + url + ')';
        }

        /**
         * Double click event handler.
         *
         * @memberof views/zoom
         * @private
         */
        function onDoubleClick() {
            tau.back();
        }

        /**
         * Registers event handlers.
         *
         * @memberof views/zoom
         * @private
         */
        function bindEvents() {
            page.addEventListener('pagebeforeshow', centerContentScroll);
            doubleClick.addHandler(image, onDoubleClick);
        }

        /**
         * Initializes the module.
         *
         * @memberof views/zoom
         * @public
         */
        function init() {
            page = document.getElementById(ID.PAGE);
            content = document.getElementById(ID.CONTENT);
            image = document.getElementById(ID.IMAGE);

            bindEvents();
        }

        return {
            init: init,
            setImage: setImage
        };
    }
});
