$("document").ready( function() { 
	// tooltip do titulo noticias / home
	$("#news .title, #list_news .title, #not_loggedin .register, .right_tip").tipTip({
		defaultPosition: "right"
	});
	
	// tooltip do titulo noticias / list
	$("#list_news .title").tipTip({
		defaultPosition: "top"
	});
	
	// tooltip padrao
	$("[title]").tipTip({
		defaultPosition: "bottom"
	});
	
	/*
		Login Pop up
	*/
	    $('a.entrar').click(function() {
			// fade in
	        $('#login').fadeIn("fast");
			// make it draggable
			$("#login").draggable();
			return false;
	    });
	
		$('#login .close').click(function() {
			$("#login").fadeOut("fast");
			return false;
		});
	
	/*
		Show Cities select field when an state has been selected
	*/

	$("#state").change(function(){
		
			var state = $(this).val(); // get the selected state
	
			$.post("/registrar/cities",{
				uf : state
			}, function(data){
				$("#cities").empty().append(data);
				$.uniform.update("#cities");
			});
		
	});
	
	/*
		Show subcategories select field when a category has been selected
	*/
	
	$("#categories").change(function(){
		var category = $(this).val();

		$.post("/tutoriais/ajax_subcategories",{
			cat_id : category
		}, function(data){
			$("#subcategories").empty().append(data);
			$.uniform.update("#subcategories");
		});
	});
	
	
	
	/*	
		Clear field on focus
	*/

	$.fn.clearOnFocus = function(){

	    /* No attribution required,
	       don't use excessively */

	    return this.focus(function(){
	        var v = $(this).val();
	        $(this).val( v === this.defaultValue ? '' : v );
	    }).blur(function(){
	        var v = $(this).val();
	        $(this).val( v.match(/^\s+$|^$/) ? this.defaultValue : v );
	    });

	};
	
	$('.clear_focus').clearOnFocus();
	
	
	/*
		newsletter Send
	*/
	
	$("#subscribe").click(function(){
		
	var name = $("#name").val();	
	var surname = $("#surname").val();	
	var email = $("#email").val();
	
	if(name == "Nome" || surname == "Sobrenome" || email == "Email") {
		alert("Você precisa preencher os campos corretamente!");
		return false;
	} else if(name == "" || surname == "" || email == "") {
		alert("Você precisa preencher todos os campos!");
		return false;
	}
	
	var reg_email = /(^[a-zA-Z0-9-\._]{2,}@[a-zA-Z0-9-]{2,}(\.[a-zA-Z]{2,3}|\.[a-zA-Z]{2,3}\.[a-zA-Z]{2,3})$)/; // validate email
	
	if(!reg_email.test(email)) {
		alert("O email está incorreto, tente novamente!");
		return false;
	}
	
	$.post("/newsletter/ajaxInsert",{
	name 	: name,
	surname	: surname,
	email 	: email	
	}, function(data){
		if(data) {
			$(".newsletter1, .newsletter2").fadeOut("fast");
			$(".newsletter_sent").fadeIn("fast");
		} else {
			$(".newsletter1, .newsletter2").fadeOut("fast");
			$(".newsletter_error").fadeIn("fast");
		}
	});
	
	return false;
	
	});
	
	
});
