Application.Controller.Index = (function($) {
	
	function index(){	
		
	    // Mais detalhes notícias home
	    // Páginas: Home
	    $('.home_noticias h3 a').click(function(){
	        var $li = $(this).closest('li');
	        //var $liCurrent = $('.home_noticias li.current').not($li);
	        var speed = 350;
	        
	        // Se está fechado, abre
	        if(!$li.hasClass('current')) {
	            $li.toggleClass('current');
	            $li.children('.content').slideToggle({'duration':speed, 'easing':'easeInOutCirc'});
	            setTimeout(function(){
	                $li.find('.detalhes').fadeOut(100);
	                $li.find('.minimizar').delay(150).fadeIn(100);
	            }, speed);
	        }
	        else {
	            $li.children('.content').slideToggle({'duration':speed, 'easing':'easeInOutCirc'});
	            setTimeout(function(){
	                $li.toggleClass('current');
	                $li.find('.minimizar').fadeOut(100);
	                $li.find('.detalhes').delay(150).fadeIn(100);
	            }, speed);
	        }
	    });
	    
	    // Esteira vertical
	    // Páginas: Home
	    var alturaLi = $('.esteiraVertical ul.lista > li').outerHeight();
	    $('.esteiraVertical ul.controls .previous').live('click', function(){
	        $('.esteiraVertical .crop').scrollTo({ top:'-='+alturaLi*2, left:0}, 700, {easing:'jswing'} );
	    });
	    $('.esteiraVertical ul.controls .next').live('click', function(){
	        $('.esteiraVertical .crop').scrollTo({ top:'+='+alturaLi*2, left:0}, 700, {easing:'jswing'} );
	    });
	    
	    //hover states on the static widgets
	    $('#dialog_link, ul#icons li').hover(
	        function() { $(this).addClass('ui-state-hover'); }, 
	        function() { $(this).removeClass('ui-state-hover'); }
	    );
	    
	    $('.box img.cadastro').live('click',function(){

			$("#cadastro_usuario").dialog('open');
			$('<div id="cadastro_usuario" title="Cadastre-se"></div>').appendTo($('body'));
			$('#cadastro_usuario').load(Application.build_url('cadastro/'));
			
			$("#cadastro_usuario").dialog({
				bgiframe: false,
				autoOpen: true,
				height: 290,
				width: 450,
				modal: true,
				close: function() {
					$("#cadastro_usuario").remove();
				}
			});
		
	    });
	    
        $('#estados ul li a').live('click', function() {
            
            var uf = $(this).find('span.value').html();
            var estado_select = $(this).html();
            
            if (uf == 0) {
                return;
            }
            
            $('form#criar_cadastro input[name=estado]').val(uf);
            
            $('#estados p').html('<a href="javascript:;"><span>'+estado_select+'</span></a>');
            $('#estados ul').hide();
            
        });
        
        $("form#criar_cadastro").live('submit', function() {

            $form = $(this);

            // limpa erros anteriores
            $form.find('.details').remove();
            $form.find('.message').hide();
            $form.find('.error').removeClass('error');

            $.ajax({
                type: 'post',
                url: $(this).attr('action'),
                data: $(this).serialize(),
                beforeSend: function() {
                    $form.find('p.message.loading').show();
                },
                success: function() {
                    $form.find('p.message.loading').hide();
                    $form.clearForm();
                    $form.find('p.message.success').show();
                    $('#estados p').html('<a href="javascript:;"><span>Selecione o estado</span></a>');
                    $form.find('input[type=text]:first').focus();
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $form.find('p.message.loading').hide();

                    if (XMLHttpRequest.status == 403) {
                        var jsonErrors = eval('('+XMLHttpRequest.responseText+')');
                        for (field in jsonErrors) {
                            $('[name='+field+']').addClass('error');
                            if (field == 'email') {
                                $form.find('p.message.errors').html(jsonErrors[field]);
                            }
                        }
                        $form.find('input[type=text]:first').focus();
                        $form.find('p.message.errors').show();
                    }
                    else {
                        alert('Um erro inesperado ocorreu no servidor. Seu contato não foi submetido.');
                    }
                }
            });

            return false;
        });     
        

	}
	
    return {
        'index': index
    };
})(jQuery);
