﻿var currControlId;

function vote(cmsContentId, voteValue, controlId) {
    var animation = $find(myAe).get_OnClickBehavior().get_animation();
    if (!animation.get_isPlaying()) {
        currControlId = controlId;
        DisableOnClick(currControlId);
        MvcVoteHandler.Vote(cmsContentId, voteValue, SucceededCallback, FailedCallback);
    }
}

function DisableOnClick(controlId) {
    var otherControlId = controlId.toString().indexOf("_up") == -1 ? controlId.toString().replace("_down", "_up") : controlId.toString().replace("_up", "_down");
    DisableVoteControl(controlId);
    DisableVoteControl(otherControlId);
}

function DisableVoteControl(controlId) {
    $get(controlId).style.border = "solid 1px #ededed";
    $get(controlId).style.cursor = "default";
    $get(controlId).style.background = "#ededed";
    $get(controlId).onclick = null;
}

// This is the failed callback function.
function FailedCallback(error) {
    var stackTrace = error.get_stackTrace();
    var message = error.get_message();
    var statusCode = error.get_statusCode();
    var exceptionType = error.get_exceptionType();
    var timedout = error.get_timedOut();

    //                    alert(
    //                        "Stack Trace: " + stackTrace + "<br/>" +
    //                        "Service Error: " + message + "<br/>" +
    //                        "Status Code: " + statusCode + "<br/>" +
    //                        "Exception Type: " + exceptionType + "<br/>" +
    //                        "Timedout: " + timedout);
}

// This is the succeeded callback function.
function SucceededCallback(result) {
    switch (result) {
        case "0": Success(); return;
        case "1": ShowModalTxtPopup("You must login to vote.<br><br><a href=\"/user/sign-in.aspx?ReturnUrl=%2fmvc%2fgallery%2fdefault.aspx\">Click here to sign in.</a>"); return;
        case "2": ShowModalTxtPopup("You already voted on this item."); return;
    }
}

function Success() {
    var eleCount = $get(currControlId.toString() + "_count");
    eleCount.innerHTML = parseInt(eleCount.innerHTML) + 1;
    animate(currControlId);
}

function animate(ctrl) {
    $get(myTarget).value = ctrl;
    if ($find(myAe) != null) {
        var animation = $find(myAe).get_OnClickBehavior().get_animation();
        animation.play();
    }
}

function getTargetControl() {
    return document.getElementById(myTarget).value;
}

//Modal Popup
function ShowModalPopup(pic) {
    $get("modal_image").src = pic;
    $find("ModalBehaviour").show();
}
function HideModalPopup() {
    $find("ModalBehaviour").hide();
    $get("modal_image").src = "http://static.asp.net/asp.net/images/ajax-loader600x400.gif";
}

//Modal Popup
function ShowModalTxtPopup(msg) {
    $get("modal-container2-txt").innerHTML = msg;
    $find("ModalBehaviour2").show();
}
function HideModalTxtPopup() {
    $find("ModalBehaviour2").hide();
}

Sys.Application.add_load(function() {
    document.body.appendChild($get("modal-container"));
    document.body.appendChild($get("modal-container2"));
});

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();