/**
 * TikiCMS
 * Copyright (C) 2009-2010, Tiki Web Inteligente Ltda.
 * @requires jQuery 1.4.2 or latter
 *
 * $Id: application_controller_home.js 36 2010-07-13 21:54:05Z caio $
 */
Application.Controller.Home = (function($) {

    function index() {

        var requestInProgress = false;

        $('form#criar_assinante').submit(function() {

            var $form = $(this);
            var $email = $form.find('input[name=email]');
            var email = $email.val().replace(/(^ *| *$)/, '');

            if (requestInProgress || !email) return false;

            $.ajax({
                type: 'post',
                url: $form.attr('action'),
                data: $form.serialize(),
                beforeSend: function() {
                    requestInProgress = true;
                },
                success: function() {
                    alert('Cadastro efetuado com sucesso.');
                    $email.val('');
                    requestInProgress = false;
                },
                error: function(XMLHttpRequest) {
                    var message = '';
                    if (XMLHttpRequest.status == '403') {
                        var errors = $.parseJSON(XMLHttpRequest.responseText);
                        message = errors.email;
                    } else {
                        message = 'Erro inesperado: você não foi cadastrado. Por favor, tente novamente mais tarde.';
                    }
                    alert(message);
                    requestInProgress = false;
                }
            });

            return false;
        });
        
        
        $('.home_banner').image_transition({
            duration: 8000,
            stageSelector: '.home_banner_stage',
            itemsSelector: '.controls li',
            itemImageSelector: '.image_url',
            itemTargetSelector: '.target_url',
            onStageUpdate: function(html) {

                var $stage = $(this);

                // stage está sendo carregado pela primeira vez
                if ($stage.html() == '') {
                    $stage.fadeOut(500, function() {
                        $stage.html(html).fadeIn(500);
                    });
                }

                else {
                    var nextImgUrl = $('<div>').append(html).find('img').attr('src');
                    $stage.css('backgroundImage', 'url('+nextImgUrl+')');

                    $stage.find('a,img').fadeOut(500, function() {
                        $stage.html(html).fadeIn(500);
                    });
                }
            }
        });
        
        $('.home_banner .num').each(function() {
            var altura = $(this).height();
            $(this).css({ marginTop: - (altura / 2) });
        });
    }

    return { 'index': index };
})(jQuery);

