(function (wdw, doc, $) {
    window.App = window.App || {};

    App.GeneralView = (function () {
        var header = $('.header'),
            main = $('.main_content'),
            clothes = $('.clothes'),
            footer = $('.footer'),
            content_height = 0, window_height = 0;

        function contentHeight() {
            var value = header.height() + main.height() + footer.height();
            return value;
        }

        function setClothesPosition() {
            var value = header.height() + main.height() - clothes.height();
            clothes.css('top', value);
        }

        function setFooterPosition() {
            var value = header.height() + main.height();
            footer.css('top', value);
        }

        function setNodesPosition() {
            setClothesPosition();
            setFooterPosition();
        }

        content_height = contentHeight();
        window_height = $(window).height();

        function init() {
            if (window.height > content_height)
                setNodesPosition();
        }

        return {
            'init': init
        };
    })();

    App.Menu = (function () {
        var uri = wdw.location.pathname,
            persist_submenu = false,
            body_id, el, node, submenu, fixed;

        function persistSubMenu() {
            var re = /\/na-midia|biografia|icone|exposicao|fotos|look-do-dia\//;
            return re.test(uri);
        }

        function toogleInnerMenu(options) {
            submenu[options.fn]('fast', function () {
                submenu.addClass(options.add).removeClass(options.remove);
            });
        }

        function showInnerMenu() {
            toogleInnerMenu({
                fn: 'slideDown',
                add: 'show',
                remove: 'hide'
            });
        }

        function hideInnerMenu() {
            toogleInnerMenu({
                fn: 'slideUp',
                add: 'hide',
                remove: 'show'
            });
        }

        function showSpecificMenu() {
            submenu = fixed.node.find('ul');
            showInnerMenu();
        }

        function controlInnerMenu(e) {
            e.stopPropagation();
            var fn, to_add, to_remove;
            node = $(this);
            submenu = node.find('ul');

            if (submenu.length) {
                e.preventDefault();
                if (!persist_submenu) {
                    if (submenu.hasClass('show'))
                        hideInnerMenu();
                    else
                        showInnerMenu();
                }
            }
        }

        function setEvent() {
            el.delegate('.item', 'click', controlInnerMenu);
        }

        function setUpFixedItens(el_selector, to_fix) {
            var i = 0,
                j = to_fix.length;
            for (; i < j; i += 1) {
                if (to_fix[i].re.test(uri)) {
                    to_fix[i].node = $(el_selector + ' ' + to_fix[i].selector);
                    fixed = to_fix[i];
                    persist_submenu = true;
                }
            }
        }

        function init(el_selector, to_fix) {
            el = $(el_selector);
            body_id = doc.body.id;

            if (to_fix)
                setUpFixedItens(el_selector, to_fix);

            if (persist_submenu)
                showSpecificMenu();

            setEvent();
        }

        return {
            init: init
        };
    })();

    App.Register = (function () {
        var el, target, node, form, close;

        function processResponse(data) {
            var k;

            if (data.status === 'error') {
                $('label', form).removeClass('error');
                for (k in data.message) {
                    if (data.message.hasOwnProperty(k)) {
                        $('label[for=id_' + k + ']', form)
                            .addClass('error')
                            .attr('title', data.message[k]);
                    }
                }
                return false;
            }
            hideForm();

            $.gritter.add({title: 'Sucesso', text: data.message, time: 3000});
        }

        function setHeaders(xhr, options) {
            xhr.setRequestHeader('X-CSRFToken', $('input[name=csrfmiddlewaretoken]', form).attr('value'));
        }

        function handleForm(e) {
            e.preventDefault();
            form = $('form', node);

            $.ajax({
                type: form.attr('method'),
                url: form.attr('action'),
                data: form.serialize(),
                beforeSend: setHeaders,
                success: processResponse,
                dataType: 'json'
            });
        }

        function toogleForm(options) {
            node[options.fn](function () {
                node.addClass(options.add).removeClass(options.remove);
            });
        }

        function showForm() { toogleForm({add: 'show', remove: 'hide', fn: 'fadeIn'}); }

        function hideForm() { toogleForm({add: 'hide', remove: 'show', fn: 'fadeOut'}); }

        function toogleHandler(e) {
            e.preventDefault();
            e.stopPropagation();
            var fn = node.hasClass('show') ? hideForm : showForm;
            fn();
        }

        function setEvent() {
            target.bind('click', toogleHandler);
            close.bind('click', hideForm);
        }

        function init(options) {
            el = $(options.el);
            target = $(options.target, el);
            node = $(options.node, el);
            close = $(options.close);
            setEvent();

            if (options.ajax)
                $('input[type=submit], button[type=submit]', node).bind('click', handleForm);

        }

        return {
            init: init,
            showForm: showForm
        };
    })();

    App.Footer = (function () {
        var el;

        function showExtraArea() {
            el.animate({'bottom': 0}, 175);
        }

        function hideExtraArea() {
            el.animate({'bottom': -28}, 87.5);
        }

        function controlAnimation(e) {
            var fn = e.type === 'mouseenter' || e.type === 'focusin' ? showExtraArea : hideExtraArea;
            fn();
        }

        function init(node) {
            el = $(node);
            el.bind({
                mouseenter: controlAnimation,
                mouseleave: controlAnimation,
                focusin: controlAnimation,
                focusout: controlAnimation
            });
        }

        return {
            init: init
        };
    })();

    App.Modal = (function () {
        var el;

        function init(node) {
            el = $(node);
            el.fancybox({
                'transitionIn': 'fade',
                'transitionOut': 'fade',
                'href': el.attr('href'),
                'scrolling': 'auto',
                'autoScale': false,
                'autoDimensions': false,
                'width': 480,
                'height': 560,
                'onComplete': function () {
                    $('#fancybox-wrap').css('margin', '-280px 0 0 -240px');
                }
            });
        }

        return {
            init: init
        };
    })();
})(this, document, jQuery);

(function ($) {
    $(document).ready(function () {
        window.App.GeneralView.init();
        window.App.Menu.init('.horz-list.nav', [
            {
                'selector': '> .item.first',
                're': /^\/(na-midia|biografia|icone|exposicao|fotos|look-do-dia)\//
            }
        ]);
        window.App.Register.init({
            el: '.btn-register',
            target: ' > a',
            node: '.form-register',
            close: '.close',
            ajax: true
        });
        window.App.Footer.init('.footer');
        window.App.Modal.init('.btn-privacy');

        function todabellaMotto() {
            return '<p>Ser TodaBella é ser elegante.</p><p>A palavra elegância vem do latim <em>eligere</em>, que significa escolher.</p><p>Alguém verdadeiramente elegante sabe escolher suas roupas, suas compras, suas atitudes, suas palavras, suas ideias.</p>';
        }
        function controlTipsy(e) {
            e.preventDefault();
            var el = $(this);
            el.tipsy(e.type === 'focusout' ? 'hide' : 'show');
        }
        $('.bella-call').tipsy({
            'title': todabellaMotto,
            'html': true,
            'gravity': 'n',
            'opacity': 1
        });
        $('.bella-call').bind({
            focusin: controlTipsy,
            focusout: controlTipsy
        });
    });
})(jQuery);

