/**
 * @author Thiago Miranda
*/
function refreshCaptcha(){
	$.ajax({
			type: "GET",
			url: baseUrl+'index/generatecaptcha',
			dataType: 'json',
			complete: function(XMLHttpRequest){
				var imgCaptcha = XMLHttpRequest.responseText;
				$('#captcha-container img').fadeOut().remove();
				$('#captcha-container').fadeIn().html(imgCaptcha);
			}
		});
}

//Function for close the modal on the Esc key press 
function escClose(){
	$(document).bind('keyup',function(e){
		if(e.which == 27){
			$('#modal').jqmHide();
		}
	}); 
}

/*
 * Function to create the Modal Div
 */
function createDiv(){
    var modalDiv = helpers.div('', {
        id: 'modal',
        'class': 'jqmWindow medium'
    });
    $('body').append(modalDiv);
}

/*
 * Function called on the modal loading
 */
function loadModal(hash){
    escClose();
    hash.w.show(1, function(){
        var form = $('#frmRegister');
        validateModal(form);
        $('.jqmClose').unbind().bind('click', function(){
            $('#modal').jqmHide();
        });
        $('#refresh').bind('click', function(){
            refreshCaptcha();
            return false;
        });
    });
}


/*
 * Validates the Modal when the user submits
 */
function validateModal(form){
            $(form).validate({
				errorElement: 'span',
                onfocusout: false,
                onkeyup: false,
                onclick: false,
                rules: {
                    name: {
                        required: true,
						rangelength: [3, 100]
                    },
                    email: {
                        required: true,
                        email: true,
						remote: baseUrl + 'index/check.email'
                    },
                    usertype: {
                        required: true
                    },
                    captcha: {
                        required: true,
						remote: baseUrl+'index/iscaptcha'
                    }
                },
				messages: {
					name: {
						required: 'Name is required',
						rangelength: 'Name must have 3 to 100 characters'
					},
					email: {
						required: 'Email is required',
						email: 'Please type a valid email format',
						remote: jQuery.format('The email <i>{0}</i> already exists, please choose another')
					},
					usertype: {
						required: 'Please select a type of user'
					},
					captcha: {
						required: 'Please type the verification code',
						remote: 'Incorrect verification code'
					}
				},
				submitHandler: function(form){
					$(form).ajaxSubmit({
						dataType: 'json',
						success: function(msg){
							$('.emailDuplicate').remove();
							if(msg.status){
                                $('#subscribe-form form').slideUp('normal', function(){
                                    $('#subscribe-success').fadeIn('normal').removeClass('hidden');
                                }).parent().fadeOut('normal');
                                $('.jqmWindow').animate({
                                    top: '30%'
                                });
							}else{
								refreshCaptcha();
							}
						}
					});
				},
				invalidHandler: function(){
					$('.emailDuplicate').remove();
					refreshCaptcha();
				}
    });
}

function centerPersonas() {
	$('div.personas').css({
		left:'56%',
		margin:'-'+($('div.personas').height() / 2)+'px 0 0 -'+($('div.personas').width() / 2)+'px'
	});
	
	if ($(window).width() < 1260) {
		$('div.personas').hide();
		$('div.copyright').css({
			position:'relative'
		});
	}
	else {
		$('div.personas').show();
		$('div.copyright').css({
			position:'absolute'
		});
	}
	
	if ($(window).height() < 750) {
		$('div.copyright').css({
			position:'relative'
		});
	} else {
		$('div.copyright').css({
			position:'absolute'
		});
	}
}

$(document).ready(function(){
	
	$(window).resize(function() {
		centerPersonas();
	});

	centerPersonas();
	
	$(document).bind('keyup', function(e){
		return !(e.keyCode == 39);
	});
	
	createDiv();
	/*
	 * Bind to open the Modal
	 */
    $('#get-started-now').bind('click', function(){
        $.include(['' + paths.js + 'jquery.jqModal.js','' + paths.js + 'jquery.validate.min.js','' + paths.js + 'jquery.form.js'], function(){
            $('#modal').jqm({
                ajax: baseUrl+'index/subscribe',
                overlay: 40,
                modal: true,
                onHide: function(hash){
                    $('#modal').remove();
                    createDiv();
                    hash.w.fadeOut(1, function(){
                        hash.o.remove();
                    });
                    $(document).unbind('keyup');
                },
                onLoad: loadModal,
                onShow: function(hash){
					if (/WebKit/i.test(navigator.userAgent)) {
						hash.w.show();
					}
                }
            });
			$('#modal').jqmShow();
        });
    });
	
});