$(function() {
    $(".floating-banner .banner img").ready(function() {
        Banner.show();

        $(this).find('.fechar').click(function() {
            Banner.close();
        });
    });

    $('#image-rotator').imageRotator();

    $('#menu-secundario a').click(function(e) {
        var $this = $(this);
        var $conteudo = $('#conteudo-secundario');
        var action = $this.attr('rel');
        var $target = $conteudo.find('.' + action);
        var $ativo = $conteudo.find('div:visible');

        if (!$ativo.hasClass(action)) {
            $ativo.slideUp();

            if ($target.length) {
                $target.slideDown();
                $('html,body').animate({
                    scrollTop: $('#menu-secundario').offset().top
                    }, 1000);
            } else {
                $target = $('<div class="' + action + '"></div>').css('display', 'none').appendTo($conteudo);

                $target.load(URL + "&a=" + action, null, function() {
                    $(this).slideDown();
                    $('html,body').animate({
                        scrollTop: $('#menu-secundario').offset().top
                        }, 1000);
                });
            }
        }
    });

    var hash = document.location.hash.replace('#', '');

    if (hash) {
        $('a[rel="' + hash + '"]').click();
    }
});

var Banner = {
    timer: null,

    show: function() {
        $(".floating-banner").fadeIn('normal', function() {
            Banner.timer = setTimeout(function() {
                Banner.close();
            }, 18000)
        });
    },

    close: function() {
        clearTimeout(this.timer);
        $(".floating-banner").fadeOut();
    }
}

var Form = {
    submit: function(form) {
        $form = $(form);
		
        $form.find('input, textarea').attr('readonly', 'readonly');
        $form.find('button:submit, input:submit').text('Aguarde...').attr('disabled', 'disabled');
		
        $.ajax({
            url: $form.attr('action'),
            data: $form.serialize(),
            type: 'post',
            success: function(response) {
                if (response.indexOf('Desculpe, mas seu contato') < 0) {
                    $(':input:visible', $form).val('');
                }
                
                response += '<p style="text-align: center;"><button class="simplemodal-close">Ok</button></p>';
                
                $.modal(response, {
                    opacity: 85,
                    overlayClose: true
                });
                
                $form.find('input, textarea').removeAttr('readonly');
                $form.find('button:submit, input:submit').text('Enviar').removeAttr('disabled');
            }
        });
    }
}

var ModalZoom = function(obj) {
    var $this = $(obj);
    var $window = $(window);
    var height = $window.height() - Math.round(($window.height() * 0.2));

    TINY.box.show({
        image: $this.attr('href'),
        height: height,
        openjs: function() {
            var $tbox = $('.tbox');
            var $img = $tbox.find('img');

            if ($img.height() > height) {
                $img.parent().height(height);
                $img.magnify();
            } else {
                $img.parents('.tinner, .tcontent').height($img.height());
            }
        }
    });
}

function equalHeight($group) {
    var tallest = 0;

    $group.each(function() {
        var height = $(this).height();

        if (height > tallest) {
            tallest = height;
        }
    });

    $group.height(tallest);
}
