is_authenticated = false;



function redirect_to_login()
{
    window.location = "/accounts/login?next=" + escape(window.location.pathname);
}



function track_visit()
{
    var link_id = new RegExp(".*?(\\d+)").exec(this.id)[1];//id="tracked-link-45"
    url = "/links/" + link_id + "/track_visit/";
    $.post(url);
    if(is_authenticated)
    {
        $("a#unhide-link-" + link_id).hide().remove();
        var hide_link = $("a#hide-link-" + link_id);
        hide_link.hide();
        //$("<span>hidden</span>").insertAfter(hide_link);
        $("<a href=\"#\" id=\"unhide-link-" + link_id + "\">unhide</a>").mousedown(function()
        {
            return unhide_link.apply(this);
        }).insertAfter(hide_link);
    }
    return true;//continue on to link dest.
}

function save_link()
{
    if(is_authenticated)
    {
        var link_id = new RegExp(".*?(\\d+)").exec(this.id)[1];//id="delete-save-45"
        url = "/links/" + link_id + "/save/";
        $.post(url);
        $(this).hide();
        //$("<span>saved</span>").insertAfter($(this));
        $("<a href=\"#\" id=\"unsave-link-" + link_id + "\">unsave</a>").mousedown(function()
        {
            return unsave_link.apply(this);
        }).insertAfter($(this));
        $(this).remove();
        return false;//do not continue on to link dest.
    }
    else
    {
        redirect_to_login();
    }
}


function unsave_link()
{
    if(is_authenticated)
    {
        var link_id = new RegExp(".*?(\\d+)").exec(this.id)[1];//id="delete-save-45"
        url = "/links/" + link_id + "/unsave/";
        $.post(url);
        $(this).hide();
        //$("<span>unsaved</span>").insertAfter($(this));
        $("<a href=\"#\" id=\"save-link-" + link_id + "\">save</a>").mousedown(function()
        {
            return save_link.apply(this);
        }).insertAfter($(this));
        $(this).remove();
        return false;//do not continue on to link dest.
    }
    else
    {
        redirect_to_login();
    }
}


function hide_link()
{
    if(is_authenticated)
    {
        var link_id = new RegExp(".*?(\\d+)").exec(this.id)[1];//id="delete-save-45"
        url = "/links/" + link_id + "/hide/";
        $.post(url);
        $(this).hide();
        //$("<span>hidden</span>").insertAfter($(this));
        $("<a href=\"#\" id=\"unhide-link-" + link_id + "\">unhide</a>").mousedown(function()
        {
            return unhide_link.apply(this);
        }).insertAfter($(this));
        $(this).remove();
        return false;//do not continue on to link dest.
    }
    else
    {
        redirect_to_login();
    }
}

function unhide_link()
{
    if(is_authenticated)
    {
        var link_id = new RegExp(".*?(\\d+)").exec(this.id)[1];//id="delete-hide-45"
        url = "/links/" + link_id + "/unhide/";
        $.post(url);
        $(this).hide();
        //$("<span>unhidden</span>").insertAfter($(this));
        $("<a href=\"#\" id=\"hide-link-" + link_id + "\">hide</a>").mousedown(function()
        {
            return hide_link.apply(this);
        }).insertAfter($(this));
        $(this).remove();
        return false;//do not continue on to link dest.
    }
    else
    {
        redirect_to_login();
    }
}

function report_link()
{
    if(is_authenticated)
    {
        var link_id = new RegExp(".*?(\\d+)").exec(this.id)[1];//id="delete-report-45"
        url = "/links/" + link_id + "/report/";
        $.post(url);
        $(this).hide();
        //$("<span>reported</span>").insertAfter($(this));
        $("<a href=\"#\" id=\"unreport-link-" + link_id + "\">unreport</a>").mousedown(function()
        {
            return unreport_link.apply(this);
        }).insertAfter($(this));
        $(this).remove();
        return false;//do not continue on to link dest.
    }
    else
    {
        redirect_to_login();
    }
}

function unreport_link()
{
    if(is_authenticated)
    {
        var link_id = new RegExp(".*?(\\d+)").exec(this.id)[1];//id="delete-report-45"
        url = "/links/" + link_id + "/unreport/";
        $.post(url);
        $(this).hide();
        //$("<span>unreported</span>").insertAfter($(this));
        $("<a href=\"#\" id=\"report-link-" + link_id + "\">report</a>").mousedown(function()
        {
            return report_link.apply(this);
        }).insertAfter($(this));
        $(this).remove();
        return false;//do not continue on to link dest.
    }
    else
    {
        redirect_to_login();
    }
}

$(document).ready(function()
{
    $("a.track-visit").mousedown(function()
    {
        return track_visit.apply(this);
    });

    
    $("a.save-link").mousedown(function()
    {
        return save_link.apply(this);
    });
    
    
    $("a.unsave-link").mousedown(function()
    {
        return unsave_link.apply(this);
    });

    $("a.hide-link").mousedown(function()
    {
        return hide_link.apply(this);
    });
    
    
    $("a.unhide-link").mousedown(function()
    {
        return unhide_link.apply(this);
    });

    $("a.report-link").mousedown(function()
    {
        return report_link.apply(this);
    });
    
    
    $("a.unreport-link").mousedown(function()
    {
        return unreport_link.apply(this);
    });
});

link_rating_of = {} //will be inited elsewhere
comment_rating_of = {} //will be inited elsewhere

$(document).ready(function()
{
    $(".link .rating").each(function()
    {
        var link_id = new RegExp(".*?(\\d+)").exec(this.id)[1];//id="rate-link-45"
        var curvalue = 0;
        if(link_id in link_rating_of)
        {
            curvalue = link_rating_of[link_id];
        }
        $(this).rating("/links/" + link_id + "/rate/", {increment: 0.5, maxvalue: 5, curvalue: curvalue});
    });
    $(".comment .rating").each(function()
    {
        var comment_id = new RegExp(".*?(\\d+)").exec(this.id)[1];//id="rate-comment-45"
        var curvalue = 0;
        if(comment_id in comment_rating_of)
        {
            curvalue = comment_rating_of[comment_id];
        }
        $(this).rating("/links/0/comments/" + comment_id + "/rate/", {increment: 0.5, maxvalue: 5, curvalue: curvalue});
    });
    if(!is_authenticated)
    {
        $(".star").click(redirect_to_login);
    }
});


$(document).ready(function()
{
    $(".reply-box").hide();
    if(is_authenticated)
    {
        $(".reply-a").click(function()
        {
            var comment_id = new RegExp(".*?(\\d+)").exec(this.id)[1];
            $("#reply-box-" + comment_id).toggle("slow");
            return false;//do not continue on to link dest.
        });
    }
    else
    {
        $(".reply-a").click(redirect_to_login);
    }
});