$(document).ready( function() {
    $('.lnk_register').click( function(e) {
        e.preventDefault();
//        $.get(DIR_STATIC_SKIN+'/modal/register.html', function(data) {
			$.modal.close();
                        $('<center><img src="'+DIR_STATIC_SKIN+'/images/ajax-loader.gif" /></center>').modal({close:false});
        $.get(DIR_WEB_ROOT+'/registration/', function(data) {
	    $.modal.close();
            $(data).modal({
                opacity: 25,
                onOpen: register.open,
                onShow: register.show
            });
        });
    });
    
    // preload images for modal window
	var img = [ 'modal-lt.png', 'modal-rt.png', 
                'modal-lb.png', 'modal-rb.png', 
                'modal-l.png',  'modal-r.png', 
                'modal-t.png',  'modal-b.png', 
                'modal-lb.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = DIR_STATIC_SKIN+'/images/modal/' + this;
	});
});

var register = {
    fields: [ {name: 'login', type: 'text', req: true},
              {name: 'mail', type: 'email', req: true},
              {name: 'password', type: 'text', req: true},
              {name: 'password_confirm', type: 'text', req: true},
              {name: 'captcha', type: 'captcha', req: true} ],
              
    messages: {'empty': 'Поле обязательно для заполнения',
        'email': 'Некорректный адрес электронной почты!',
        'captcha': 'вы робот?!'
    },
    
    open: function(dialog) { // It need to correct height and position of modal window
        dialog.overlay.show();
        dialog.container.show();
        dialog.data.show();
        register.resetHeight();
    },
    
    resetHeight: function() {
        $('.simplemodal-container').height( $('.simplemodal-data').height());
        $.modal.impl.setPosition();
    },
    
    show: function() {
        
        $('#simplemodal-container .error-ico').click(function(e) {
            e.preventDefault();
            fieldset = $(this).parent().parent();
            if ( !$('.error_msg', fieldset).html() ) {
                fieldset.append('<b class="error_msg"></b>');
                $('.error_msg', fieldset).html($(this).attr('rel'));
            } else {
                $('.error_msg', fieldset).remove();
            }
        });
        
    },
    
    validate: function() {
        b = true;
        
        $('#simplemodal-container .error_msg').remove();
        
        for ( i in register.fields) {
            f = register.fields[i];
            field = $('#simplemodal-container form input[name="'+f.name+'"]');
            
            register.clearErrors(field);
            
            if (f.req) {    // if field is reqired - check
                if ( (field.val()) && (f.type=='email') ) {     // if field not empty and have should be email - check
                    if ( !validateEmail( field.val() ) ) {      // if not check not passed put error
                        register.fieldError(field, 'email');
                        b = false;
                    }
                } else if ( !field.val() ) {        // if field required and empty put empty error
                    if ( f.type=='captcha') {
                        register.fieldError(field, 'captcha');
                    } else {
                        register.fieldError(field, 'empty');
                    }
                    b = false;
                }
            }
            else {      // if field is not required but have email type and have value - check for email type
                if ( (field.val()) && (f.type == 'email') ) {
                    if ( !validateEmail( field.val() ) ) {      // if check is failed put error
                        register.fieldError(field, 'email');
                        b = false;
                    }
                }
            }
            
        }
        return b;
    },
    
    fieldError: function(field, type) {
        field_container = field.parent().parent().parent();
        if ( !field_container.hasClass('error') ) {
            field_container.addClass('error');
            l = $('#simplemodal-container form label[for="'+ field.attr('name') +'"] .error-ico')
            l.show().attr('rel', l.attr('rel') + register.messages[type]+'<br />');
        }
    },
    
    clearErrors: function(field) {
        field.parent().parent().parent().removeClass('error');
        $('#simplemodal-container form label[for="'+ field.attr('name') +'"] .error-ico').hide().attr('rel','');
    },
    
    error: function() {
    }
}

