﻿function PlayedGame(gameID) {
    var img = $('#PlayedGame').attr("src");
    $('#PlayedGame').attr("src", img.replace("accepted", "accepted-on"));
    $.getJSON("/Games/PlayedGame", { id: gameID }, OnWSPlayed);
}
function OnWSPlayed(results) {
    var img = $('#PlayedGame').attr("src");
    if (results.answer === true) {
        $('#PlayedGame').attr("src", img.replace("accepted", "accepted-on"));
        $('#PlayedGame').removeAttr("onclick");
        $('#PlayedGameCount').html(results.playCount);
    }
    else {
        $('#PlayedGame').attr("src", img.replace("accepted-on", "accepted"));
    }
}

function UpdateTitleViewCount(gameId) {
    $.getJSON("/Games/UpdateViewCountGame", { id: gameId });
}

function VoteUp(ctrl, voteID, IsAuth) {
    if (IsAuth === 'True') {
        if (ctrl.src.match("up-on") == null) {
            ctrl.src = ctrl.src.replace("off", "on");
            var item = document.getElementById('ibVoteDown');
            if (item.src.match("down-on") != null) {
                item.src = item.src.replace("on", "off");
                $.getJSON("/Games/VoteDownOn", { id: voteID });
                $.getJSON("/Games/VoteUp", { id: voteID }, OnVoteRequestComplete);
            }
            else { $.getJSON("/Games/VoteUp", { id: voteID }, OnVoteRequestComplete); }
        }
        else {
            ctrl.src = ctrl.src.replace("on", "off");
            $.getJSON("/Games/VoteUpOn", { id: voteID }, OnVoteRequestComplete);
        }
    }
    else
    { LogIn(); }
}

function VoteDown(ctrl, voteID, IsAuth) {
    if (IsAuth === 'True') {
        if (ctrl.src.match("down-on") == null) {
            ctrl.src = ctrl.src.replace("off", "on");
            var item = document.getElementById('ibVoteUp');
            if (item.src.match("up-on") != null) {
                item.src = item.src.replace("on", "off");
                $.getJSON("/Games/VoteUpOn", { id: voteID });
                $.getJSON("/Games/VoteDown", { id: voteID }, OnVoteRequestComplete);
            }
            else { $.getJSON("/Games/VoteDown", { id: voteID }, OnVoteRequestComplete); }
        }
        else {
            ctrl.src = ctrl.src.replace("on", "off");
            $.getJSON("/Games/VoteDownOn", { id: voteID }, OnVoteRequestComplete);
        }
    }
    else
    { LogIn(); }
}
function OnVoteRequestComplete(results) {
    if (results != '') {
        document.getElementById('lblVoteCount').innerHTML = results.voteCount;
    }
}

function LogIn() {
    alert("You must Login to Do That (No Registration Required!)");
}

function FavoriteOff(img, favoriteID) {
    img.src = img.src.replace("on", "off");
    img.setAttribute("onclick", "FavoriteOn(this,'" + favoriteID + "');");
    $.getJSON("/Games/SetFavorite", { id: favoriteID }, OnWSFavoriteReturn);
}
function FavoriteOn(img, favoriteID) {
    img.src = img.src.replace("off", "on");
    img.setAttribute("onclick", "FavoriteOff(this,'" + favoriteID + "');");
    $.getJSON("/Games/SetFavorite", { id: favoriteID }, OnWSFavoriteReturn);
}

function OnWSFavoriteReturn(results) {
    if (results.answer === true) {
        $('#FavoriteCount' + results.guid).html(results.favoriteCount);
    }
}

function PlayedTheGame(ctrl, playedID) {
    if (ctrl.src.match("vote-accepted-on") == null) {
        ctrl.src = ctrl.src.replace("accepted", "accepted-on");
        $.getJSON("/Games/SetPlayed", { id: playedID }, OnPlayedRequestComplete);
        fbs_click();
    }
}
function OnPlayedRequestComplete(results) {
    if (results.answer === true) {
        $('#PlayedGameCount').html(results.playCount);
    }
}

function fbs_click() {
    u = location.href;
    t = document.title;
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
    return false;
}

function AddSiteMapNode(url, modified) {
    $.getJSON("/Utilities/AddNodeToSiteMap", { url: url, modified: modified });
}


function SubmitComment(ID) {
    $.getJSON("/Games/AddComment", { id: ID, comment: document.getElementById('AddCommentText' + ID).value });
    $('#divAddComment' + ID).html(document.getElementById('AddCommentText' + ID).value);
}

function Report(span, ID, type) {
    span.innerHTML = "Reported";
    $.getJSON("/Games/Flag", { id: ID, type: type });
}
