

/* Sacado de http://lineadecodigo.com/2009/01/09/validar-el-email-con-javascript/ */
function validarEmail(valor) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3,4})+$/.test(valor)){
        return false;
    } else {
        return true;
    }
}

function Cargo(div){
    $("#sidebar a[rel]").click(function(){
        UpdateDiv(this.rel, "#content");
        return false;
    });

    $("#content .post .title a").livequery('click', function(){
        UpdateDiv(this.rel, "#content");
        return false;
    });

    $("#content .post .full a").livequery('click', function(){
        UpdateDiv(this.rel, "#content");
        return false;
    });

    $("#prev a").livequery('click', function(){
        $("#calendario .sidebarcontent").load(this.rel);
        return false;
    });

    $("#next a").livequery('click', function(){
        $("#calendario .sidebarcontent").load(this.rel);
        return false;
    });

    $("#comentario_button").livequery('click', function() {
        var noticia = $("#noticia").attr("value");
        var autor = $("#autor").attr("value");
        var correo = $("#correo").attr("value");
        var url = $("#url").attr("value");
        var comentario = $("#comentario").attr("value");

        if (autor == "") {
            $("#autor_error").show();
            $("#autor").focus();
            return false;
        }

        if (correo == "") {
            $("#correo_error").show();
            $("#correo").focus();
            return false;
        }

        if (comentario == "") {
            $("#comentario_error").show();
            $("#comentario").focus();
            return false;
        }

        var dataString = 'autor='+ autor + '&correo=' + correo + '&url=' + url + '&comentario=' + comentario + '&noticia=' + noticia;

        $('#comentarios').empty();
        $('#comentarios').hide();
        $('#formulario').empty();
        $('#formulario').hide();

        $.ajax({
            type: "POST",
            url: "/ajax.php?var=includes/content/formulario.php&noticia=" + noticia,
            data: dataString,
            dataType: "html",
            success: function(respuesta) {
                $('#formulario').html(respuesta);
                $('#comentarios').load("/ajax.php?var=includes/content/comentarios.php&noticia=" + noticia, null, null);
                $('#formulario').fadeIn("1500", null);
                $('#comentarios').fadeIn("1500", null);
            }
        });

        return false;
    });

}


function UpdateDiv(Url, div){
    $.getJSON('/' + Url, function(data) {
        $(div).empty();
        $.each(data,function(entryIndex, entry) {
            var html = '<div class="post">';

            html += '<p class="meta">' + entry['meta'] + '</p>';
            html += '<h2 class="title">' + entry['title'] + '</h2>';
            html += '<div class="entry">' + entry['entry'] + '</div>';
            if(entry['full']) { 
                html += '<div class="links">' + entry['full'] + '</div>';
            }

	    if(entry['tweet_me']) {
		html += entry['tweet_me'];
	    }


            if(entry['formulario']) {
                html += '<div id="formulario">' + entry['formulario'] + '</div>';
            }

            if(entry['comments'] && entry['comments'].length > 0) {
                html += '<div id="comentarios"><h2>Comentarios</h2>'
                $.each(entry['comments'],function(i, comentario) {
                    html += '<div class="comentario">';
                    html += '<div class="meta-comentario">Escrito por <a href="' + comentario['url'] + '" rel="nofollow">' + comentario['autor'] + '</a> el <span class="meta-fecha">' + comentario['fecha'] + '</span> a las <span class="meta-hora">' + comentario['hora'] + "</span></div>";
                    html += '<div class="texto">' + comentario['comentario'] + '</div>';
                    html += '</div>';
                });
                html += '</div>';
            }

            html += '</div>';
            $(div).append($(html.replace(/ç/g, "'")));
        });
        if(readCookie('style') == "dolares") {
            $('canvas').hide();
            $('.ie').hide();
            $('#content .post').liquidCanvas("[shadow{color:#2e2e2e} border{color:#ff7200} fill{color:#f2ffec}] => roundedRect{radius:30}");
        } else {
            $('canvas').hide();
            $('.ie').hide();
            $('#content .post').liquidCanvas("[shadow{color:#2e2e2e} border{color:#889bd9} fill{color:#fff8fa}] => roundedRect{radius:30}");
        }
    });
}

