$(document).ready(function(){
	/*
     * Visual
     */
	$('#email_address').focus();
	
    $.include([paths.js + 'jquery.tooltip.min.js', paths.css + 'jquery.tooltip.css',paths.js + 'jquery.validate.min.js'], function(){
        $('span.clue a').tooltip({
            track: true,
            showURL: false,
            bodyHandler: function(){
                return 'This is the ID of your association';
            }
        });
		
		$('span.clue a').bind('click',function(){
			return false;
		});
		
		/*
		* Chamada do plugin validate assim como suas opcoes e regras.
		*/
        var contactValidate = $("#form-login").validate({
            focusCleanup: false,
			
			invalidHandler: function(form){
				scrollTo('body,html');
				$('.list').empty();
			},
			
			 highlight: function(element, errorClass) {
 				$(element).not('select').addClass(errorClass);
 				$(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
			 },
			 
			unhighlight: function(element, errorClass) {
 				$(element).removeClass(errorClass);
 				$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
			},
			
            
	        rules: {
	            'email_address': {
	                required: true,
	                email: true,
	                maxlength: 255
	            },
	            'association_password': {
	                required: true,
	                maxlength: 60
	            }
	        },
            messages: {
	        	email_address: {
                    required: '<strong>Email</strong>',
                    email: '<strong>Email</strong>',
                    maxlength: '<strong>Email</strong> must be under 255 characters'                    	
                },
                association_password: {
                    required: '<strong>Password</strong>',
                    email: '<strong>Password</strong>',
                    maxlength: '<strong>Password</strong> must be under 60 characters'
                }
            },
            
            onkeyup: false,
            onfocusout: false,
            focusInvalid: true,
            onsubmit: true,
			errorElement: 'li',
			
            errorPlacement: function(error, element){
                $(element).blur();
                var id = element.attr('id');
                var name = element.attr('name');
                var errorli = error.appendTo('.list');
                
                if (!$('#generated-' + id).length) {
                    errorli.append(' | <a href="#' + id + '" id="generated-' + id + '">Show me</a>');
                }
                else {
                    errorli.remove();
                }
                $('#generated-' + id).bind('click', function(event){
                    event.stopPropagation();
                    scrollTo('#' + id, function(element){
                        element.focus();
                    });
                    return false;
                });
                $('#error').show();
            }
        
        
        });
		
    });
    
    
    $('#login-button','#form-login').bind('click', function(){
		$('#login-error').addClass("hidden");
        $('.left').find('label').removeClass('error');
    });
    
    /*
     * Clique no forgot
     */
	$('#link_forgot').bind('click', function(e){
		if ( $('#email_address').val() == '' ) 
		{
			window.location = '/account/forgot/';
		}
		else 
		{
			window.location = '/account/forgot/email_address/' + $('#email_address').val();
		}
	}); 



});

