var engine = {
    fixes: function() {
        $("a[rel*='external']").attr('target', '_blank');
        $('a').not('[rel="internal"]').not('[rel="external"]').addClass('pushState');
        engine.vertical('#newsletter .entry');
        (new Forms()).init('#newsletter form, #contact form');
        $('form .text').defaultText();
        engine.navigation();
    },

    getViewport: function() {
        return $(window).height();
    },

    vertical: function(element) {
        $(element).each(function() {
            var $self = $(this);
            $self.css('marginTop', -($self.innerHeight() / 2));
        });
    },

    navigation: function(path) {
        var pushState = (history && history.pushState) ? true : false;
        if (!pushState) {
            var redirect = $.address.baseURL().match(/([house\.pl]+\/(pl|en|ru)(\/)?)$/) ? false : true;
            if (redirect) {
                var address = $.address.baseURL().match(/((www\.)?[house\.pl]+\/(pl|en|ru))\/+(.*)$/);
                if (address) {
                    document.location.href = 'http://{0}#/{1}'.format(address[1], address[address.length -1]);
                };
            };
        };
        var path = stipSlashes($.address.value());
        path = pushState ? path : stipSlashes('{0}/{1}'.format(lang, path));
        $('header nav a').not('[rel]').each(function() {
            var elem = $(this);
            var href = stipSlashes(elem.data('href'));
            if (href) {
                elem.toggleClass('active', href.split('/')[1] == path.split('/')[1]);
                if (href === path) {
                    elem.removeAttr('href');
                } else {
                    elem.attr('href', href);
                }
            };
        });

        $('#events .indicator').click(function() {
            if ($(this).parents('dd').hasClass('collapsed')) {
                $(this).parents('dd').animate({'height': 142}, {duration: 200, easing: 'easeOutQuint', complete: function() {
                    $(this).removeClass('collapsed');
                }});
            } else {
                $(this).parents('dd').animate({'height': 370}, {duration: 300, easing: 'easeOutQuint', complete: function() {
                    $(this).addClass('collapsed');
                }});
            }
            return false;
        }).parents('dd').filter('.collapsed').animate({'height': 370}, {duration: 300, easing: 'easeOutQuint'});
    }
};;



$(document).ready(function() {
    var ua = $.browser;
    if (ua.webkit && ua.version.slice(0,3) == "533") {
        $('body').append('<style>* {-webkit-transform: none !important;}</style>');
    }
    $('header nav a').each(function() {
        $(this).data('href', $(this).attr('href'));
    });
    loader = new Loader();
    if (typeof application === 'function') {
        (new application());
    }
});

