﻿var curNewBlogsPage = 1; //The current page of the new blogs section

$(document).ready(function() {
    $("#datepicker").datepicker({ maxDate: new Date, minDate: new Date(2007, 6, 12) });

    //Set the calendar date to the search date if the page is search page
    if (window.location.toString().indexOf('posts/date/') > 0) {
        dt = window.location.toString().substr(window.location.toString().lastIndexOf('/') + 1);
        $("#datepicker").datepicker('setDate', new Date(dt.substr(0, 4), dt.substr(4, 2) - 1, dt.substr(6)));
    }

    $("#btnViewDate").click(function() {
        dt = $("#datepicker").datepicker('getDate');
        mnth = (dt.getMonth() + 1).toString();
        if (mnth.length < 2) mnth = "0" + mnth;
        dy = dt.getDate().toString();
        if (dy.length < 2) dy = "0" + dy;

        window.location = "http://" + window.location.host + '/posts/date/' + dt.getFullYear().toString() + mnth + dy;
    });

    $(".opLnk").live('click', function() {
        var id = $(this).attr("id");
        id = id.substr(2);
        var pid = $(this).attr("name").substr(2);
        ShowBlogOldPosts(id, pid, 1);
        return false;
    });

    $(".cmtlink").live('click', function() {
        var id = $(this).attr("id");
        id = id.substr(2);
        $.get('/posts/postcomments/' + id + "/1", function(data) {
            $("#pst" + id + " .pInf").html(data).removeClass('hideElement');
        });
        return false;
    });

    $('.clsInf').live('click', function() {
        $(this).parents('.pInf').html("").addClass('hideElement');
    });

    $('.iUp, .iDn').live('click', function() {
        if (!signedIn) {
            $.get('/user/loginajax/', function(data) {
                $.facebox(data);
            });
        }
        else {
            var postId = $(this).parents('.post').attr('id');
            postId = postId.substr(3);

            if ($(this).hasClass('iUp')) {
                $.post('/posts/vote/' + postId + "/1", null, function(result) {
                    if (result.result == true)
                        $("#pst" + postId + " .score").html(result.voteCount);
                    else {
                        if (result.message != undefined)
                            $.facebox(result.message);
                    } 
                }, "json");
            }
            else {
                $.post('/posts/vote/' + postId + "/-1", null, function(result) {
                    if (result.result == true)
                        $("#pst" + postId + " .score").html(result.voteCount);
                    else {
                        if (result.message != undefined)
                            $.facebox(result.message);
                    } 
                }, "json");
            }
        }
    });

    $('.votebuzzup').live('click', function() {
        if (!signedIn) {
            $.get('/user/loginajax/', function(data) {
                $.facebox(data);
            });
        }
        else {
            postID = $(this).attr('id');
            postID = postID.substr(2);
            selecto = $(this);
            $.post('/posts/voteup/' + postID + '/1', null, function(result) {
                if (result.result === true)
                    selecto.html(result.voteCount);
                else {
                    if (result.message != undefined)
                        $.facebox(result.message);
                }     
            }, "json");
        }
    });

    $('.votebuzzdn').live('click', function() {
        if (!signedIn) {
            $.get('/user/loginajax/', function(data) {
                $.facebox(data);
            });
        }
        else {
            postID = $(this).attr('id');
            postID = postID.substr(2);
            selecto = $(this);
            $.post('/posts/votedn/' + postID + '/-1', null, function(result) {
                if (result.result === true)
                    selecto.html(result.voteCount);
                else {
                    if (result.message != undefined)
                        $.facebox(result.message);
                }                    
            }, "json");
        }
    });

    $('.blgtulbar > li').bind('mouseover', blgtulbar_open);
    $('.blgtulbar > li').bind('mouseout', blgtulbar_timer);
});

//Toolbar code
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

function blgtulbar_open() {
    blgtulbar_canceltimer();
    blgtulbar_close();
    ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}
function blgtulbar_close() {
    if (ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function blgtulbar_timer()
{ closetimer = window.setTimeout(blgtulbar_close, timeout); }

function blgtulbar_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

document.onclick = blgtulbar_close;

function VoteUpPost(postID) {
    if (!signedIn) {
        $.get('/user/loginajax/', function(data) {
            $.facebox(data);
        });
    }
    else {
        $.post('/posts/vote/' + postID + "/1", null, function(result) {
            if (result.result == true) {
                RefreshOldPosts(postID);
                $("#pst" + postID + " .score").html(result.voteCount);
            }
        }, "json");
    }
}

function VoteUpOldPost(blogID, postID, oldPostID, pageNum) {
    if (!signedIn) {
        $.get('/user/loginajax/', function(data) {
            $.facebox(data);
        });
    }
    else {
        $.post('/posts/vote/' + postID + "/1", null, function(result) {
            if (result.result == true) {
                RefreshOldPosts(oldPostID);
                $("#pst" + postID + " .score").html(result.voteCount);
                ShowBlogOldPosts(blogID, oldPostID, pageNum);
            }
        }, "json");
    }
}

function VoteDnPost(postID) {
    if (!signedIn) {
        $.get('/user/loginajax/', function(data) {
            $.facebox(data);
        });
    }
    else {
        $.post('/posts/vote/' + postID + "/-1", null, function(result) {
            if (result.result == true) {
                RefreshOldPosts(postID);
                $("#pst" + postID + " .score").html(result.voteCount);
            }
        }, "json");
    }
}

function VoteDnOldPost(blogID, postID, oldPostID, pageNum) {
    if (!signedIn) {
        $.get('/user/loginajax/', function(data) {
            $.facebox(data);
        });
    }
    else {
        $.post('/posts/vote/' + postID + "/-1", null, function(result) {
            if (result.result == true) {
                RefreshOldPosts(oldPostID);
                $("#pst" + postID + " .score").html(result.voteCount);
                ShowBlogOldPosts(blogID, oldPostID, pageNum);
            }
        }, "json");
    }
}

function RefreshOldPosts(postID) {
    var id = $("[name=pi" + postID + "]").attr("id");
    id = id.substr(2);
    $.get('/blogs/blogposts/' + id + "/1", function(data) {
        $("#pst" + postID + " .pInf").html(data).removeClass('hideElement');
    });
    return false;
}

//Show old blog posts
function ShowBlogOldPosts(blogID, postID, pageNum) {
    $.get('/blogs/blogposts/' + blogID + "/" + postID + "/" + pageNum, function(data) {
        $("#pst" + postID + " .pInf").html(data).removeClass('hideElement');
    });
}

//Show new blogs page
function ShowNewBlogs(pageNum) {
    $.get('/blogs/newblogswidget/' + pageNum, function(data) {
        $('#newBlogSec').html(data);
    });
}

//Show random blogs
function ShowRandomBlogs() {
    $.get("/blogs/randomblogs", function(data) {
        $('#randBlogs').html(data);
    });
}

//Refresh Comments
function RefreshComments() {
    $.get('/functions/getfreshcommentslist/', function(data) {
        $("#freshComments").html(data);
    });
}