﻿function vote(cid, direction, voteFieldId, lnkUpId, lnkDownId) {
    //modify our clientid's to be jquery friendly
    voteFieldId = "#" + voteFieldId;
    lnkUpId = "#" + lnkUpId;
    lnkDownId = "#" + lnkDownId;

    //don't allow them to click either vote again
    $(lnkUpId).removeAttr("href").removeAttr("onclick");
    $(lnkDownId).removeAttr("href").removeAttr("onclick");
   
    //change the vote button to an ajax loader gif
    $((direction == "U") ? lnkUpId : lnkDownId).html("<img src='images/ajax-loader" + ((direction=="U") ? "_up" : "_down") + ".gif' width='32' height='32' />")
    
    //ajax
    var postData = "champid=" + cid + "&dir=" + direction;
    $.ajax({
        type: "POST",
        url: "utils/AjaxVote.aspx",
        data: postData,
        success: function(newVoteCount) {
            if (newVoteCount == -1) {
                //todo - make sure this is the best browser-neutral way to change pages
                window.location.href = "SignIn.aspx";
            }
            else {
                if(voteFieldId == "#") {
                    window.location.href = "Champion.aspx?cid="+cid
                }
                else {
                    $(voteFieldId).hide().removeClass("votes").addClass((direction == "U") ? "votedUp" : "votedDown").html(newVoteCount).fadeIn("fast")
                    $(lnkUpId).html("<img src='images/thumbs_up_gray3.png' height='32' width='32' />");
                    $(lnkDownId).html("<img src='images/thumbs_down_gray3.png' height='32' width='32' />");    
                }
                
            }
            //$ajax again and send emails
        }
    });
}

function optout() {
    var postData = "";
    $.ajax({
        type: "POST",
        url: "utils/FacebookOptOut.aspx",
        data: postData,
        success: function(returnVal) {
            $.modal.close();
            location.href = 'Default.aspx';
        }
    });
}

function submitComment(btn, commentField, cid) {
    if (commentField.value.replace(/^ +| +$/, "").length > 0) {
        btn.disabled = true;
        btn.value = "Posting...";
        document.forms[0].hidSubmitComment.value = "Y";
        window.setTimeout(new Function(postComment(commentField, cid)), 50);
    }
}

function postComment(commentField, cid) {
    var postData = "champid=" + cid + "&action=N&type=CC";
    $.ajax({
        type: "POST",
        url: "utils/AjaxVote.aspx",
        data: postData
    });
    window.setTimeout("document.forms[0].submit()", 250);
}

function showVoteUp(msg) {
    $.cursorMessage('Click to <span class="votedUp" style="font-weight: bold;">champ</span> ' + msg + '!', { offsetX: 10, offsetY: -40, hideTimeout: 1250 });
}
function showVoteDown(msg) {
    $.cursorMessage('Click to <span class="votedDown" style="font-weight: bold;">chunk</span> ' + msg + '!', { offsetX: 10, offsetY: -40, hideTimeout: 1250 });
}