﻿function URLEncode(unencodedValue) {
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789" + 				// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()"; 				// RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    var plaintext = unencodedValue;
    var encoded = "";
    for (var i = 0; i < plaintext.length; i++) {
        var ch = plaintext.charAt(i);
        if (ch == " ") {
            encoded += "+"; 			// x-www-urlencoded, rather than %20
        } else if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                alert("Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted.");
                encoded += "+";
            } else {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
            }
        }
    } // for

    return encoded;
};


/********************  START RATINGS CONTROL    ****************************/
function InitializeRatingsControl( readOnly )
{
    var itemContainers = $(".wrapper_arrow_ratings");
    var itemContainer, containerId, activeElem, voteId, loginPosition;

    for(var icIdx = 0; icIdx < itemContainers.length; icIdx++ )
    {
        itemContainer = itemContainers[icIdx];

        if(itemContainer != null)
        {
            containerId = itemContainer.id;
            activeElem = $("#" + containerId );
            voteId = $("#" + containerId + "-itemid").val();
            
            //-- Position login control
            if(!readOnly || readOnly == null )
            {                
                loginPosition = activeElem.position();
                $(".rate_login").css({ "left": loginPosition.left + 4 + "px", "top": loginPosition.top - 2 + "px" });
                $(".rate_message").css({ "left": loginPosition.left + 4 + "px", "top": loginPosition.top - 2 + "px" });
            }

            activeElem.fadeTo(250, 0.5);
            
            // Load the values for the item
            $.ajax({
                type: "POST",
                url: "/resources/handlers/rating.ashx",
                data: "itemid=" + $("#" + containerId + "-itemid").val(),
                dataType: "json",
                success: function(votes)
                {
                    GetItemRating(votes, (readOnly != null) ? false : true);
                }
            });
        }
    }        
}

function GetItemRating(votes, bindEvents)
{
    var containerId = "rating-" + votes.ItemId;
    var voteUp = $("#" + containerId + "-voteup");
    var voteDown = $("#" + containerId + "-votedown");
    var voteUpCount = $("#" + containerId + "-voteupcount");
    var voteDownCount = $("#" + containerId + "-votedowncount");
    var voteUpImage = $("#voteimageup-" + votes.ItemId);
    var voteDownImage = $("#voteimagedown-" + votes.ItemId);

    $("#" + containerId).fadeTo(250, 1.0);
    
    if( votes != null )
    {
        if(votes.VoteSuccess)
        {
            if(votes.VoteDirection == 2)
            {
                var currcount = Number(voteUpCount.text());
                voteUpCount.text(currcount + 1);
                voteDownCount.text(votes.VoteDistribution.VotesDown);
                
                voteUp.toggleClass("rated", true);
                voteDown.toggleClass("rated", false);
                voteUpCount.fadeTo(250, 1.0);
            }
            else if(votes.VoteDirection == 1)
            {
                var currcount = Number(voteDownCount.text());
                voteUpCount.text(votes.VoteDistribution.VotesUp);
                voteDownCount.text(currcount + 1);
                
                voteUp.toggleClass("rated", false);
                voteDown.toggleClass("rated", true);
                voteDownCount.fadeTo(250, 1.0);
            }
            voteUp.unbind("click");
            voteDown.unbind("click");
        }
        else
        {
            voteUpCount.text(votes.VoteDistribution.VotesUp);
            voteDownCount.text(votes.VoteDistribution.VotesDown);        
        }
                       
        if(bindEvents)
        {
            if(!votes.HasVoted)
            {
                voteUp.click(function() { SubmitVote(containerId, 2, votes.IsAuthenticated); });
                voteDown.click(function() { SubmitVote(containerId, 1, votes.IsAuthenticated); });
                voteUpImage.attr("src", "/resources/images/ui/icons/arrow-active-nr-up.png");
                voteDownImage.attr("src", "/resources/images/ui/icons/arrow-active-nr-down.png");
            }
            else
            {
                if(votes.VoteDirection == 2)
                {
                    voteUp.addClass("rated");
                    voteUpImage.attr("src", "/resources/images/ui/icons/arrow-inactive-rated-up.png");
                }
                else if(votes.VoteDirection == 1)
                {
                    voteDown.addClass("rated");
                    voteDownImage.attr("src", "/resources/images/ui/icons/arrow-inactive-rated-down.png");
                }                
            }
        }
    }
}

function SubmitVote(containerId, vote, isAuthenticated )
{
    if(!isAuthenticated)
    {
        var loginContainer = $("#" + containerId + "-login");
        loginContainer.fadeIn(500);
        loginContainer.mouseleave(function() { $(this).fadeOut(500); });
    }
    else
    {
        voteId = $("#" + containerId + "-itemid").val();
        var voteUpImage = $("#voteimageup-" + voteId);
        var voteDownImage = $("#voteimagedown-" + voteId);
        
        var voteUpCount = $("#" + containerId + "-voteupcount");
        var voteDownCount = $("#" + containerId + "-votedowncount");

        if(vote == 2)
        {
            voteUpImage.toggleClass("voteanimated");
            voteUpImage.attr("src", "/resources/images/ui/icons/icon-rate-up-ani.gif");
            voteUpCount.fadeTo(250, 0.5);
        }
        else if(vote == 1)
        {
            voteDownImage.toggleClass("voteanimated");
            voteDownImage.attr("src", "/resources/images/ui/icons/icon-rate-down-ani.gif");
            voteDownCount.fadeTo(250, 0.5);
        }

        $.ajax({
            type: "POST",
            url: "/resources/handlers/rating.ashx",
            data: "itemid=" + $("#" + containerId + "-itemid").val() + "&rating=" + vote,
            processData: false,
            dataType: "json",
            success: function(votes)
            {
                GetItemRating(votes, false)
            }
        });
    }
}

/********************  END RATINGS CONTROL    ****************************/
//-- Mediaplayer helpers --//
function InitPage() { PageGate(); }
function SetPlayerTime(time) { var ctrl = document.getElementById("SilverlightMediaPlayer"); if (ctrl != null) { ctrl.Content.Page.SetVideoTime(time); } window.location.hash = "videoplayer"; }

/* CommunityBarMin.js */
var msc_menuDisplayTime, msc_displayTime, msc_speed, msc_wait; var msc_itemCount, msc_itemIndex, msc_links, msc_MouseIsOver, msc_isFirstTime; var msc_fadeOutElement, msc_fadeOutOpacity, msc_fadeInElement, msc_fadeInOpacity; var msc_isMouseOverLink; function msc_hideMenu() { if (!msc_MouseIsOver) { document.getElementById("cb_menu").style.display = "none" } else { setTimeout(msc_hideMenu, msc_menuDisplayTime * 500) } } function cb_showmenu() { var cbMenu; cbMenu = document.getElementById("cb_menu"); cbMenu.style.display = "block"; setTimeout(msc_hideMenu, msc_menuDisplayTime * 1000); msc_MouseIsOver = true } function msc_menuMouseOut() { msc_MouseIsOver = false; setTimeout(msc_hideMenu, msc_menuDisplayTime * 500) } function msc_menuMouseOver() { msc_MouseIsOver = true } function msc_setTitle(title) { window.title = title } function msc_mouseOverLink() { msc_isMouseOverLink = true } function msc_mouseOutLink() { msc_isMouseOverLink = false } function msc_rotateLinks() { if (!msc_isMouseOverLink) { var currentItemID = "cb_link" + msc_itemIndex; var currentItem = document.getElementById(currentItemID); if (currentItem && !msc_isFirstTime) { currentItem.style.display = "none" } msc_itemIndex++; if (msc_itemIndex > msc_itemCount) { msc_itemIndex = 1 } currentItemID = "cb_link" + msc_itemIndex; currentItem = document.getElementById(currentItemID); if (currentItem) { currentItem.style.display = msc_isFirstTime ? "inline" : "block" } } setTimeout(msc_rotateLinks, msc_displayTime * 1000) } function msc_initVar() { var e; msc_menuDisplayTime = 1; msc_displayTime = 10; msc_itemCount = 20; msc_itemIndex = Math.floor(Math.random() * msc_itemCount); msc_fadeOutElement = null; msc_fadeOutOpacity = null; msc_fadeInElement = null; msc_fadeInOpacity = null; msc_isFirstTime = true; msc_isMouseOverLink = false; msc_rotateLinks(); msc_isFirstTime = false; msc_MouseIsOver = false; for (var i = 1; i <= msc_itemCount; i++) { e = document.getElementById("cb_link" + i.toString()); if (e) { e.onmouseover = msc_mouseOverLink; e.onmouseout = msc_mouseOutLink } } } function msc_fadeOut(elementID, opacity) { if (elementID === undefined || elementID === null || opacity === undefined || opacity === null) { elementID = msc_fadeOutElement; if (msc_fadeOutOpacity === null) { opacity = 99 } else { opacity = msc_fadeOutOpacity } } else { msc_fadeOutElement = elementID } if (opacity >= 5) { msc_setOpacity(elementID, opacity); opacity -= 4; msc_fadeOutOpacity = opacity; setTimeout(msc_fadeOut, 50) } else { msc_fadeOutElement = null; msc_fadeOutOpacity = null; document.getElementById(elementID).style.display = "none" } } function msc_fadeIn(elementID, opacity) { if (elementID === undefined || elementID === null || opacity === undefined || opacity === null) { elementID = fadeInElement; if (msc_fadeInOpacity === null) { opacity = 0 } else { opacity = msc_fadeInOpacity } } else { msc_fadeInElement = elementID } if (opacity < 99) { msc_setOpacity(elementID, opacity); opacity += 4; msc_fadeInOpacity = opacity; setTimeout(msc_fadeIn, 50) } else { msc_fadeInElement = null; msc_fadeInOpacity = null; msc_setOpacity(elementID, 100) } } function msc_setOpacity(elementID, opacity) { var e = document.getElementById(elementID); if (e) { e.style.filter = "alpha(style = 0, opacity:" + opacity + ")"; e.style.KHTMLOpacity = opacity / 100; e.style.MozOpacity = opacity / 100; e.style.opacity = opacity / 100 } };