$(document).ready(function() {

	$("#tweets").tweet({
        username: "TraderMxNews",
        join_text: "auto",
        avatar_size: 0,
        count: 3,
        auto_join_text_default: "",
        auto_join_text_ed: "",
        auto_join_text_ing: "",
        auto_join_text_reply: "",
        auto_join_text_url: "",
        loading_text: "cargando tweets..."
    });
    
    $("ul.social li a").hover( 
    	function(){
    		$(this).stop().animate({ paddingLeft: "55px"});
    	}, 
    	function(){
    		$(this).animate({ paddingLeft: "45px"});
    	}
    );
    
    function validateEmail(elementValue){
    	/*var regEx = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/; */
    	var regEx = /^([a-zA-Z]+([\.]?[\-_0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,5})$/;
        return regEx.test(elementValue);  
    }
    
    function current_timestamp(){
    	var tiempo = new Date();
    	var dia = tiempo.getDate();
    	var mes = tiempo.getMonth() + 1;
    	var ano = tiempo.getFullYear();
    	var hora = tiempo.getHours();
    	var mins = tiempo.getMinutes();
    	var segs = tiempo.getSeconds();
    	
    	if(mins < 10){
    		mins = "0" + mins;
    	}
    	if(segs < 10){
    		segs = "0" + segs;
    	}
    	
    	return ""+ano+"-"+mes+"-"+dia+" "+hora+":"+mins+":"+segs+"";
    }
    
    $("#boletin").val("Escriba su email..");
    $("#boletin").focus(function(){
    	if($(this).val() == "Escriba su email.."){
    		$(this).val("");
    	}
    });
    
    var boletincont = 0;
    
    $("a.boletinb").click(function(){
		var email = $('#boletin').val()+"";
		var timestamp = current_timestamp();
		if(validateEmail(email) && boletincont < 5){
			$.post('http://tradermx.com/ajax/boletin',
					{emailboletin: email, tiempoboletin: timestamp},
					function(data) { // Do an AJAX call
						if(data != ''){
			         	$('#mensajeboletin').html(data); // Fill the suggestions box
			         	$('#mensajeboletin').fadeIn().delay(2000).fadeOut(2000); // Show the suggestions box
			         	boletincont++;
					}
			});
		} else if(boletincont >= 5) {
			$('#mensajeboletin').html("Demasiados intentos. Vuelva a intentar mas tarde.");
			$('#mensajeboletin').fadeIn().delay(2000).fadeOut(); // Show the suggestions box
		} else {
			boletincont++;
			$('#mensajeboletin').html("Correo no valido, vuelva a intentar");
			$('#mensajeboletin').fadeIn().delay(2000).fadeOut(); // Show the suggestions box
		}
		return false;
	});
	
});
