﻿
//// Now Playing title and description, expando ////
var metaDesc = document.getElementById("metaDescHolder");
var origTitleText = new String();
var origDescText = new String();
var dashSep = "&nbsp;&mdash;&nbsp;";
var cutCount = "";

// Get clip info
OnMediaChanged = function(uuid) {
    if (null != uuid) {
        //This is the event that fires when the script gets the video object from the catalogue
        MSNVidCat.VideoByUUID.OnData = function(vidObj) {
            origTitleText = vidObj.title.$;
            origDescText = vidObj.description.$;

            //if we have a description then prepend spacer
            if (null != origDescText && origDescText != "") {
                origDescText = dashSep + origDescText;
            }

            //we always show full title, so vary description length based on title length
            if (null != origTitleText && origTitleText != "") {
                document.getElementById("metaTitleHolder").innerHTML = origTitleText;
                if (origTitleText.length < 30) {
                    cutCount = 210;
                }
                else {
                    cutCount = Math.round(215 - (origTitleText.length - dashSep.length) * 1.6);
                }
                if (cutCount < origDescText.length) { //clip it
                    checkMetaLength();
                }
                else { //don't need to clip it; display
                    document.getElementById("puGalleryInfobox").className = ""; 
                    metaDesc.innerHTML = origDescText;
                }
            }
        }
        //This is the method that goes and gets the video object from the catalogue
        MSNVidCat.VideoByUUID.Get(uuid);
    }
}
//(adapted for this page from Expando v2.0)
var cutMetaText = new String();

function checkMetaLength() {
    cutMetaText = CutStringLength(origDescText, cutCount, '...');
    metaTextLess();
}

function metaTextLess() {
    metaDesc.innerHTML = cutMetaText + " <a onclick='metaTextMore()' style='cursor:pointer;'>[More]</a>";
    document.getElementById("puGalleryInfobox").className = "";
}

function metaTextMore() {
    metaDesc.className = "expanded";
    metaDesc.innerHTML = origDescText + " <p class='expclose'><a onclick='metaTextLess()' style='cursor:pointer;'>Close [x]</span></p>";
    document.getElementById("puGalleryInfobox").className = "expanded";
}
//(unchanged below this point)
function CutStringLength(theString, length, appendage) {
    var cutString = "";

    if (arguments.length >= 2 && typeof theString == "string" && typeof length == "number" && null != theString && null != length && theString.length > length) {
        cutString = theString.match(/./gi).join("").substring(0, length);

        if (cutString.indexOf(" ") > -1) {
            var loopLength = length;

            while (cutString.substring(loopLength - 1) != " " && loopLength > 0) {
                loopLength--;

                cutString = cutString.substring(0, loopLength);
            }
        }

        if (typeof appendage != "undefined" && null != appendage) {
            cutString += appendage;
        }

        if (cutString.search(/(<.*?>)+.*?/gi) > -1) {
            var startTags = cutString.replace(/(<\/.*?>)/gi, "").match(/(<.*?>)/gi);
            var endTags = cutString.match(/(<\/.*?>)/gi);

            var addTags = new Array();

            for (var x = 0; x < startTags.length; x++) {
                var checkTag = startTags[x].replace(/</gi, "").replace(/>/gi, "");
                var checkTagArray = checkTag.split(" ");
                var startTag = checkTagArray[0];

                for (var y = 0; y < endTags.length; y++) {
                    var endTag = (endTags[y] != "$") ? endTags[y].replace(/<\//gi, "").replace(/>/gi, "") : "$";

                    if (startTag.toLowerCase() == endTag.toLowerCase()) {
                        startTags[x] = "@";
                        endTags[y] = "$";
                    }
                }

                if (startTags[x] != "@") {
                    startTags[x] = "</" + startTag + ">";
                }
            }

            var addTags = startTags.reverse().join("").replace(/@/gi, "");
            cutString += addTags;
        }
    }

    return cutString;
}
