/**
 *
 *
 * Created by: Becklyn GmbH, Jannik Zschiesche <jz@becklyn.com>
 * Date: 19.10.2010
 * Time: 17:34:25
 */

/**
 * The global language selector
 * @class LanguageSelect
 */
var LanguageSelect = {

    /**
     * The id of the wrapper
     * @type {String}
     */
    ID: 'languages',


    /**
     * Handler of the wrapper
     *
     * @type jQuery
     */
    $wrap: null,


    /**
     * Handler of the popup opener
     *
     * @type jQuery
     */
    $opener: null,


    /**
     * Handler of the popup
     */
    $popup: null,


    /**
     * The timeout handler for the closing of the popup
     *
     * @type Number
     */
    timeoutHandler: null,


    /**
     * The duration of the timeout, before the popup is closed
     *
     * @type int
     */
    TIMEOUT_DURATION: 500,


    /**
     * Initializes the object
     */
    init: function () {
        this.$wrap = $("#box-language-currency");
        this.$opener = this.$wrap.find('.currentValues');
        this.$popup = this.$wrap.find('.selectPopup');

        this.registerEventListener();
    },


    /**
     * Registers the event listeners (click & change)
     */
    registerEventListener: function () {

        var self = this;

        this.$wrap.on(
            "mouseenter",
            function ()
            {
                self._onMouseEnter();
            }
        ).on(
            "mouseleave",
            function ()
            {
                self._onMouseLeave();
            }
        );

        this.$popup.find('.currencyChooser .currency').on(
            "click",
            function ()
            {
                var $link = $(this);
                var $form = $link.closest('form');

                $form.find('input[name="newCurrency"]').val( $link.attr("data-currency") );
                $form.submit();
            }
        );
    },

    /**
     * Handles the event, when the mouse enters the area of the popup
     */
    _onMouseEnter: function () {
        if (this.timeoutHandler != null)
        {
            window.clearTimeout(this.timeoutHandler);
            this.timeoutHandler = null;
        }

        this.$popup.fadeIn("fast");
    },



    /**
     * Handles the event, when the mouse leaves the popup
     */
    _onMouseLeave: function ()
    {
        var self = this;

        this.timeoutHandler = window.setTimeout(
            function ()
            {
                self._closePopup();
            },
            this.TIMEOUT_DURATION
        );
    },



    /**
     * Handles the closing of the popup
     */
    _closePopup: function ()
    {
        this.$popup.fadeOut("normal");
    }


};
