$j(function() {
	$j('#cookie-dialog .close-dialog').click(function() {
		$j('#cookie-dialog').dialogClose();
		
		return false;
	});
	
	$j('#cookie-form').submit(function() {
	  var form = $j('#cookie-dialog form');
	  form.removeClass('captcha-enabled');
	  form.find('#email, #password, #captcha').val('');
	  form.find('.observations').show();
	  
	  $j('#cookie-dialog .content .feedback').empty();
	  $j('#cookie-dialog').center().dialog();
		
		return false;
	});
	
	$j('a.remove-cookie').click(function() {
		this.blur();
		$j(this).html('').after('<span>Apagando...</span>')
		
		$j.post('/user/remove_cookie', {}, function(){
			window.location = window.location;
		});
		
		return false;
	});
  
	$j('#orkut-form').submit(function() {
		form = this;
		
		postData = encrypt($j(form).serialize());
		
		$j(form).removeClass('captcha-enabled');
		
		$j(form).find('input').attr('disabled', 'disabled');
		$j(form).find('#captcha').val('');
		$j(form).find('span.button').hide();
		$j(form).find('.observations').hide();
		$j(form).find('img.loading').show();
		$j('#cookie-dialog .content .feedback').empty();
		
		$j.ajax({
			type: form.method,
			url: form.action,
			data: "encrypted=" + encodeURIComponent(postData),
			success: function(data) {
				$j('#cookie-dialog .content').hide();
				$j('#cookie-dialog .success').show();
				$j('#cookie-dialog .close-dialog').unbind();
			},
			error: function(request) {
				$j(form).find('input').removeAttr('disabled');
				$j(form).find('span.button').show();
				$j(form).find('img.loading').hide();
				
				var feedback = $j('#cookie-dialog .content .feedback')
				  .empty()
				  .html(request.responseText);
				
				if (feedback.html().match(/CAPTCHA/)) {
				  var captchaUrl = feedback.find('#metadata').metadata().captchaUrl;
				  
          $j(form).find('.captcha-image img').attr('src', "/images/blank.gif");				  
				  $j(form).find('.captcha-image img').attr('src', captchaUrl);
				  
				  $j(form).addClass('captcha-enabled');
				}
			}
		});
		
		return false;
	})
  
	$j('#edit li.checkbox a').click(function() {
		$j(this).blur().parent().children()[0].click();
		
		return false;
	})
	$j('h3 a.toggle').click(function() {
		$j(this).toggleClass('opened')
		
		form = $j(this).parent('h3').next('form') 
		
		if ($j(this).hasClass('opened')) {
			form.show()
		} else {
			form.hide()
		}
		
		return false;
	})

	if (document.location.hash == '#cookie') {
		$j('a.toggle').not(':first').click();
	}
	
})

function setClientSettings(form) {
    form.client_width.value = document.body.clientWidth;
    form.browser.value = navigator.userAgent;
}

function checkEmail(input) {
    email = input.value;
    
    if (email.match(/hotmail.com/)) {
        message = 'Alguns emails Hotmail funcionam apenas para o MSN e n&atilde;o possuem caixa postal. Se esse for o seu caso, use outro email.'
    } else if (email.match(/uol.com.br|bol.com.br/)) {
        message = 'Alguns emails UOL ou BOL possuem filtro anti-spam. Adicione o email do Curioso (contato@ocurioso.com) na lista de emails conhecidos antes de se registrar.'
    } else {
        new Effect.Fade("email_instant_feedback");
        return;
    }
    
    $('email_instant_feedback').update(message);
    new Effect.Appear("email_instant_feedback");
}

function encrypt(data) {
    rsa = new RSAKey();
    rsa.setPublic("F03A0894AABF9CEB8B53C4D1C837498853028F8DA6ED3FE03D1C8588E8E25F311977993586CA2710268843E34CAF187B65F253077F1254C627520E74DDD457C88F6883113202CBB5804EC3F777D606E14BE81C2B220DEFBDC1DE2CA197AEDB07F7641BFD78749E9600C1BCE7865A5A044EA9886563EF53856DCAF0D7048E231B", "10001");
    encrypted = rsa.encrypt(data);
    
    return hex2b64(encrypted);
}
