﻿/// <reference path="../Edentity.Global.js" />
/// <reference path="../Cosmo.WebServices.js" />
/// <reference path="../External/jquery-1.3.2-vsdoc2.js" />
/// <reference path="../CookieContext.js" />

Edentity.RegisterNamespace("Cosmo.Controls.BSReport");

(function(BSR, $) {

    var CookieName = "Cosmo.BSReportVotes",
        WebServiceURL = Edentity.ResolveUrl("~/WebServices/AjaxServices.asmx"),
        GraphWidth = 168,
        DefaultBorderWidth = 1;

    BSR.OnInit = function($container, confessionId) {
        if (userHasVoted(confessionId)) {
            BSR.ShowResults($container, confessionId);
        } else {
            bindVoteButtons($container, confessionId);
            $(".VotingView", $container).show();
        }
    }
    
    BSR.ShowResults = function($container, confessionId) {
        getResults(confessionId, function(result) {
            showResults(result, $container);
        });
    };
    
    function bindVoteButtons($container, confessionId) {
        $(".TrueButton", $container).click(function() { submitVote($container, confessionId, true); });
        $(".BSButton", $container).click(function() { submitVote($container, confessionId, false); });
    }
    
    function submitVote($container, confessionId, isTrue) {
        Cosmo.WebServices.CallAsmx(WebServiceURL + "/SubmitBSReportVote", "{ConfessionId:" + confessionId + ",IsTrue:" + isTrue + "}", function(result) {
            showResults(result, $container);
            rememberVote(confessionId);
        });
    }
    
    function getResults(confessionId, fnCallback) {
        Cosmo.WebServices.CallAsmx(WebServiceURL + "/BSReportResults", "{ConfessionId:" + confessionId + "}", fnCallback);
    }
    
    function showResults(resultObj, $container) {
        if (!resultObj || !resultObj.d) return;
        
        var percentTrue = getPercentTrue(resultObj.d);
        drawResultGraph(percentTrue, $(".TrueResultRow", $container));
        drawResultGraph(100 - percentTrue, $(".BSResultRow", $container));
        $(".VotingView", $container).hide();
        $(".ResultsView", $container).show();
    }
    
    function getPercentTrue(voteCounts) {
        var numTrue = voteCounts.TrueVotes,
            numBS = voteCounts.BSVotes,
            total = numTrue + numBS || 1; //prevents div. by 0 errors
        return Math.round(numTrue / total * 100);
    }
    
    function drawResultGraph(percentage, $resultRow) {
        var pixelsFilled = (percentage / 100) * GraphWidth;
        $(".Graph", $resultRow).css({
            "border-right-width": GraphWidth - pixelsFilled + DefaultBorderWidth,
            width: pixelsFilled
        });
        $(".Percentage", $resultRow).text(percentage + "%");
    }
    
    function voteCookie() {
        return window.getCookie(CookieName);
    }
    
    function userHasVoted(confessionId) {
        if (!voteCookie()) return false;
        return voteCookie().indexOf("," + confessionId + ",") >= 0;
    }
    
    function rememberVote(confessionId) {
        var cookie = voteCookie();
        if (cookie == "") {
            cookie = ",";
        }
        cookie += confessionId + ",";
        window.setCookie(CookieName, cookie, -1);
    }

})(Cosmo.Controls.BSReport, jQuery);
