/**
 * @class ep.sslSwitch
 *
 */

/*
 * @copyright		© Copyright 2006-2010, epages GmbH, All Rights Reserved.
 *
 * @module			ep.sslSwitch
 *
 * @revision		$Revision: 1.9 $
 */

define("ep/sslswitch", [
    "jquery",
    "ep",

    'jquery/cookie'
], function ($, ep) {

    ep.sslSwitch = {
        handles: [],

        controller: function (event) {
            var elem = $(this),
                form = elem.filter('form'),
                tokenName = 'SecToken',
                cookies;

            if (!form.length) {
                form = $('<form method="post" action="' + elem.attr('href') + '"/>');
                cookies = $.cookie();

                if (cookies[tokenName] && form.find("input[name='" + tokenName + "']").length === 0) {
                    form.append('<input type="hidden" name="' + tokenName + '" value="' + cookies[tokenName] + '" />');
                }
                form.appendTo('body');
            }

            $.each(ep.sslSwitch.handles, function (i, handle) {
                handle(event, form);
            });

            if (!event.isDefaultPrevented()) {
                event.preventDefault();
                ep.sslSwitch['submit'](form, true);
            }
        },

        /**
         * Add an handle to the SSL switch controller.
         *
         * The `ep.sslSwitch.addHandle()` method adds handles to SSL switch controller, the controller
         * execute all handles before the location switch to SSL mode on submit the form.
         *
         * The `ep.sslSwitch.addHandle()` method handles all `form` and `a` elements which are marked with the `SSLSwitch` class.
         *
         * ### Examples
         * Send an additional parameter on switch to SSL mode.
         *
         * JavaScript:
         *
         *     ep.sslSwitch.addHandle(function( event, form ){
         *         $('<input type="hidden">')
         *             .attr({
         *                 'name':     'example',
         *                 'value':    'test'
         *             })
         *             .appendTo(form);
         *     });
         *
         *
         * ### Dependencies
         *
         *  + `ep`
         *
         * @param {Function} "function(event, form)" Add an handle to the SSL switch controller. Receives the triggered event and the form element(jQuery object) as arguments
         *
         * @method addHandle
         * @static
         * @member ep.sslSwitch
         *
         * @since 6.11.2
         */
        addHandle: function (fn) {
            ep.sslSwitch.handles.push(fn);
        },

        //#JSCOVERAGE_IF false
        /**
         * Submit the givn SSL switch form.
         *
         * The `ep.sslSwitch.submit()` method submits the givn form and switch location to SSL mode.
         *
         * You can overwrite this method to perform an way to submit the form, note that a form can be sent only once.
         * If you only want to modify the form or perform a method before the location switch to SSL mode, use the
         * `ep.sslSwitch.addHandle()` method.
         *
         * ### Examples
         * Overwrite the SSL switch submit method.
         *
         * JavaScript:
         *
         *     ep.sslSwitch.submit = function( form ){
         *         form[0].submit();
         *     };
         *
         *
         * ### Dependencies
         *
         *  + `ep`
         *
         * @param {Object} form A form element(jQuery object).
         *
         * @method submit
         * @static
         * @member ep.sslSwitch
         *
         * @since 6.11.2
         */
        submit: function (form) {
            if (arguments[1]) {
                form[0].submit();
            } else {
                form.trigger('submit');
            }
        }
        //#JSCOVERAGE_ENDIF
    };

    $(document)
        .on('click', 'a.SSLSwitch', ep.sslSwitch.controller)
        .on('submit', 'form.SSLSwitch', ep.sslSwitch.controller);

    return ep;

});