/*
 * Scroll to comments box
 */
$( function() {
    if(document.location.href.indexOf('comments')>0)
    {
        $.scrollTo('#yourcomments',{
            speed:500
        } );
    }
});
/**
 *
 *  Archive Links
 */
$("#archiveLinks > h6 > a").click(function(){
    var yr = $(this).attr("year"); 
    var blogid = $(this).attr("blogId");
    blogDao.getArchiveMonthsbyPastYears(blogid,yr,function(months){
        if($("#p"+yr).html()=='') {
            var s="";
            $.each(months,function(){
                s+="<li>";
                if(this.postCount > 0){
                    s+="<a href=\"../blogs/blog?Id="+this.blogId+"&month="+this.month+"&year="+yr+"&pc="+this.postCount+"\" title=\""+this.postCount+" Posts\">";
                }
                s+=this.monthName;
                
                if(this.postCount > 0) {
                    s+="</a> ";
                }
                s+=" ";

                if(this.month !== 4 && this.month !== 8 && this.month !== 12){
                    s+="<span style=\"font-size:.8em;\">|</span> </li>";
                }
                else{
                    s+=" </li>";
                }
            });
            $("#p"+yr).append(s);
        }
    });
});

/**
 * Add comment, show form on blog page
 */
$(".readmore, .readpost").click(function(){
    var pid = $(this)?$(this).attr("id").substring(2):0;
    if(pid>0){
        //$("#sum-"+pid).hide();
        $("#read-"+pid).hide();
        $("#pp-"+pid).fadeIn("normal", function(){ });
    }
});
/*
 * Show the Comment box
 */
function showComment(_postId)
{
    $('#div-'+_postId).toggle();
    $("#aPostLink-" + _postId).hide();

    //insert random question
    var ran_unrounded=Math.random()*3;
    var ran_number=Math.floor(ran_unrounded);

    $("#question-"+_postId).html(questions[ran_number].title);
    $("#answer-"+_postId).val(questions[ran_number].answer);
}
/*
 * Validate & Add Comment
 */
function addComment(_postId)
{

    var msg="";
    $("#err-"+_postId).html("").hide();

    if(msg.length==0 && $("#name-"+_postId).val().length<3)
        msg = "Please enter your name (or screen name).";
    if(msg.length==0 && $("#email-"+_postId).val().length<3 ||
        !isEmail($("#email-"+_postId).val()) )
        msg = "Please enter a valid email address.";
    if(msg.length==0 && $("#comments-"+_postId).val()<3)
        msg = "Please enter some comments about this post.";
    if($("#answer-"+_postId).val()!=$("#userAnswer-"+_postId).val())
        msg = "Please enter the correct answer to the question below.";

    if(msg.length>0)
    {
        $("#err-"+_postId).html(msg).show();
        return false;
    }
    else
    {
        var _memberId = $("#memberId").val();
        var _ip = $("#ip").val();

        var _name = $("#name-"+_postId).val();
        var _email = $("#email-"+_postId).val();
        var _url = $("#url-"+_postId).val();
        var _comments = $("#comments-"+_postId).val().replaceP().stripHTML();

        var pc = { 
            postId: _postId,
            memberId: _memberId,
            ip:_ip,
            name: _name,
            email: _email,
            url: _url ,
            copy: _comments,  
            status: 1
        };

        postCommentDao.insertPostComment(pc, function(data){
            document.location.href = "post?Id="+_postId+"&comments";
        });
        return true;
    }
}

var questions = new Array();
function buildQuestionArray()
{
    var q1 = new Object();
    q1.title = "question:* What is 2 x 2?"
    q1.answer = "4";

    var q2 = new Object();
    q2.title = "question:* What is 10 x 1 ?"
    q2.answer = "10";

    var q3 = new Object();
    q3.title = "question:* What is 3 x 2 ?"
    q3.answer = "6";

    var q4 = new Object();
    q4.title = "question:* What is 1 x 1 ?"
    q4.answer = "1";

    questions = [q1,q2,q3,q4];        
}
buildQuestionArray();
