﻿function playViral(id)
{
    window.open(id,"_new");
}

var MSNSubscribe = {
    open: false,
    minHeight: 0,
    maxHeight: 180,
    minWidth: 90,
    maxWidth: 169,
    get: function()
    {
        var rssShell = document.getElementById("rssShell");
        rssShell.onclick = function()
        {
            MSNSubscribe.click();        
        };
        rssShell.onmouseover = function()
        {
            MSNSubscribe.over({"id":"rssShell","classId":"rssShellOver"});           
        };
        rssShell.onmouseout = function()
        {
            MSNSubscribe.out({"id":"rssShell","classId":"rssShellOut"});           
        };        
    },    
    over: function(whichOne)
    {
        document.getElementById(whichOne.id).className = whichOne.classId;        
    },
    out: function(whichOne)
    {
        document.getElementById(whichOne.id).className = whichOne.classId;        
    },
    click: function(b)
    {                
        if(b != null)
        {
            this.open = b;
        }
        else
        {
            if(this.open)
            {
                this.open = false;    
            }
            else
            {
                this.open = true;
            }    
        }
        
        var rssShell = document.getElementById("rssShell");
        
        if(this.open)
        {
            rssShell.onmouseover = function()
            {
                MSNSubscribe.over({"id":"rssShell","classId":"rssShellClickOver"});           
            };
            rssShell.onmouseout = function()
            {
                MSNSubscribe.over({"id":"rssShell","classId":"rssShellClickOut"});                          
            };
            
            $("#rssShell").animate({left: new String((this.minWidth - this.maxWidth) + "px"),width: new String(this.maxWidth + "px"),height: new String(this.maxHeight + "px")},"fast","swing");
        }
        else
        {
            $("#rssShell").animate({left: "0px",width: new String(this.minWidth + "px"),height: new String(this.minHeight + "px")},"fast","swing",function()
            {                
                rssShell.onmouseover = function()
                {
                    MSNSubscribe.over({"id":"rssShell","classId":"rssShellOver"});           
                };
                rssShell.onmouseout = function()
                {
                    MSNSubscribe.over({"id":"rssShell","classId":"rssShellOut"});           
                };               
            });
        }     
    }    
};

var MSNTopBox = {
    timeout: null,
    get: function() {
        if (theFeed != null) {
            var topTV = XmlHelper.selectNodes(theFeed.documentElement, "//group[@id='mostpopulartv']/item", null);
            var topMovies = XmlHelper.selectNodes(theFeed.documentElement, "//group[@id='topboxofficemovies']/item", null);
            var topMusic = XmlHelper.selectNodes(theFeed.documentElement, "//group[@id='topmusicvideos']/item", null);

            MSNTopBox.set([topTV, topMovies, topMusic]);
        }
        else {
            setTimeout(MSNTopBox.get, 500);
        }
    },
    set: function(topVideos) {
        for (var x = 0; x < topVideos.length; x++) {
            if (topVideos[x] == null) {
                continue;
            }

            var dropDownImage = (x == 1) ? "topDropDownImageMovie" : "topDropDownImage";
            var whichBoxDomain = new Array("http://videoguide.msn.com/play/tv/","http://videoguide.msn.com/play/movies/","http://videoguide.msn.com/play/music/");
            var domain = whichBoxDomain[x];
            
            for (var y = 0; y < 12; y++) {
                var theLink = document.getElementById("top" + (x + 1) + "Link" + (y + 1));

                if (y < topVideos[x].length) {
                    var theTitle = topVideos[x][y].getElementsByTagName("title")[0].childNodes[0].nodeValue;                    
                    var theURL = new String(domain + "?ocid=vgtop&g=" + topVideos[x][y].getElementsByTagName("id")[0].getAttribute("guid"));
                    var theImage = topVideos[x][y].getElementsByTagName("thumb")[0].getAttribute("url");
                    var theDesc = topVideos[x][y].getElementsByTagName("description")[0].childNodes[0].nodeValue;

                    var dropDownID = new String("top" + (x + 1) + "DropDown" + (y + 1));
                    var dropDownShellID = new String("top" + (x + 1) + "DropDown" + (y + 1) + "Shell");
                    var dropDownHolderID = new String("top" + (x + 1) + "DropDown" + (y + 1) + "Holder");
                    var titleID = new String("top" + (x + 1) + "Title" + (y + 1));


                    theLink.innerHTML = "<div class='topLinkTitleOut' id='" + titleID + "' onmouseover='MSNTopBox.over(\"" + titleID + "\",\"" + dropDownShellID + "\",\"" + dropDownHolderID + "\")' onmouseout='MSNTopBox.out(\"" + titleID + "\",\"" + dropDownShellID + "\",\"" + dropDownHolderID + "\")' onclick='MSNTopBox.click(\"" + theURL + "\")'>" + this.trim(theTitle, 39) + "</div>" +
                                        "<div id='" + dropDownShellID + "' class='topLinkDropDownShellOut'>" +
                                        "<div id='" + dropDownHolderID + "' class='topLinkDropDownHolder'>" +
                                        "<div class='topLinkDropDown' id='" + dropDownID + "'><div class='topDropDownLeft'><img class='" + dropDownImage + "' src='" + theImage + "' /></div><div class='topDropDownRight'><div class='topTitle'>" + theTitle + "</div><div class='topDescription'>" + theDesc + "</div></div></div>" +
                                        "<div class='topBubbleArrow'></div>" +
                                        "</div></div>";
                }
                else {
                    theLink.style.display = "none";
                }
            }

            if (topVideos[x].length > 6) {
                var moreButton = document.getElementById("topMoreButton" + (x + 1));
                moreButton.style.display = "block";

                moreButton.onclick = function() {
                    MSNTopBox.more();
                };
            }
        }
    },
    moreOpen: false,
    more: function() {
        var displayMore = null;
        var moreButton = null;

        if (this.moreOpen) {
            this.moreOpen = false;
            displayMore = "none";
            moreButton = "topMoreButton";
        }
        else {
            this.moreOpen = true;
            displayMore = "block";
            moreButton = "topCloseButton";
        }

        document.getElementById("topHolderBottom1").style.display = displayMore;
        document.getElementById("topHolderBottom2").style.display = displayMore;
        document.getElementById("topHolderBottom3").style.display = displayMore;

        document.getElementById("topMoreButton1").className = moreButton;
        document.getElementById("topMoreButton2").className = moreButton;
        document.getElementById("topMoreButton3").className = moreButton;
    },
    over: function(titleID, dropShellID, dropHolderID) {
        MSNTopBox.timeout = setTimeout("MSNTopBox.overTimeout('" + titleID + "','" + dropShellID + "','" + dropHolderID + "')", 250);
    },
    overTimeout: function(titleID, dropShellID, dropHolderID) {
        document.getElementById(titleID).className = "topLinkTitleOver";
        document.getElementById(dropShellID).className = "topLinkDropDownShellOver";
        document.getElementById(dropHolderID).style.top = new String((-1 * ($("#" + dropShellID).outerHeight() + 16)) + "px");
        document.getElementById(dropHolderID).style.left = "-5px";
    },
    out: function(titleID, dropShellID, dropHolderID) {
        clearTimeout(MSNTopBox.timeout);

        document.getElementById(titleID).className = "topLinkTitleOut";
        document.getElementById(dropShellID).className = "topLinkDropDownShellOut";
        document.getElementById(dropHolderID).style.top = "0px";
        document.getElementById(dropHolderID).style.left = "0px";
    },    
    click: function(url) {
        location.href = url;
    },
    trim: function(title,length)
    {
        if(title == null || length == null)
        {
            return;
        }
        
        if(title.length > length)
        {
            var newLength = length-3;
            title = title.substr(0,newLength);
                    
            if(title.indexOf(" ") > -1)
            {
                while(title.charAt((newLength-1)) != " " && newLength > 0)
                {
                    newLength--;
                    title = title.substr(0,newLength);
                }
                
                title = title.substr(0,(title.length - 1));
            }
            
            title += "...";
        }
             
        return title;
    }
};

var MSNLatestTV = {
    timeout: null,
    get: function() {
        var url = "http://api.search.live.net/json.aspx?Sources=Video&FORM=CT&AppId=D41D8CD98F00B204E9800998ECF8427E051F7680&Market=en-us&Video.Offset=0&Video.Count=7&JsonType=callback&JsonCallback=MSNLatestTV.set";
        url += "&Query=site:msn.com";
        
        scriptAppend("BingScript",url);
    },
    set: function(data) 
    {
        if(null != data && null != data.SearchResponse && null != data.SearchResponse.Video && null != data.SearchResponse.Video.Total && null == data.SearchResponse.Errors && data.SearchResponse.Video.Total > 0 && null != data.SearchResponse.Video.Results)
        {    
            var total = data.SearchResponse.Video.Total;
            var plural = (total != 1) ? "s" : "";
            
            document.getElementById("latestBingCount").innerHTML = total + " result" + plural + " - ";
            
            var tv = data.SearchResponse.Video.Results;
                        
            for (var x = 0; x < 7; x++) 
            {
                var theLink = document.getElementById("latestTVLinkHolder" + (x + 1));

                if (tv[x] != null) {
                    var theURL;
                    var newWin = true;
                    var url = (null != tv[x].Url) ? tv[x].Url : null;
                    var clickThroughURL = (null != tv[x].ClickThroughPageUrl) ? tv[x].ClickThroughPageUrl : null;
                    
                    if(null != url)
                    {
                        newWin = false;
                        theURL = MSNLatestTV.FixURL(url);
                    }
                    else if(null != clickThroughURL)
                    {
                        theURL = clickThroughURL;
                    }
                    else
                    {
                        theURL = "http://www.bing.com/videos?FORM=CT"
                    }
                    
                    var theTitle = (null != tv[x].Title) ? tv[x].Title : "Bing Video";                    
                    var theImage = (null != tv[x].StaticThumbnail && null != tv[x].StaticThumbnail.Url) ? tv[x].StaticThumbnail.Url : "http://entimg.msn.com/i/videodestination/en-us/images/search/default_movie_160x120.jpg";
                    var theDesc = (null != tv[x].Description) ? tv[x].Description : "";

                    var dropDownID = new String("latestTVDropDown" + (x + 1));
                    var dropDownShellID = new String("latestTVShell" + (x + 1));
                    var dropDownHolderID = new String("latestTVHolder" + (x + 1));
                    var titleID = new String("latestTVTitle" + (x + 1));

                    theLink.innerHTML = "<div class='latestTVTitleOut' id='" + titleID + "' onmouseover='MSNLatestTV.over(\"" + titleID + "\",\"" + dropDownShellID + "\",\"" + dropDownHolderID + "\")' onmouseout='MSNLatestTV.out(\"" + titleID + "\",\"" + dropDownShellID + "\",\"" + dropDownHolderID + "\")' onclick='MSNLatestTV.click(\"" + theURL + "\","+newWin+")'>" + this.trim(theTitle) + "</div>" +
                                        "<div id='" + dropDownShellID + "' class='latestTVShellOut'>" +
                                        "<div id='" + dropDownHolderID + "' class='latestTVHolder'>" +
                                        "<div class='latestTVDropDown' id='" + dropDownID + "'><div class='latestTVLeft'><img class='latestTVImage' src='" + theImage + "' /></div><div class='latestTVRight'><div class='latestTVTitle'>" + theTitle + "</div><div class='latestTVDescription'>" + theDesc + "</div></div></div>" +
                                        "<div class='latestTVBubbleArrow'></div>" +
                                        "</div></div>";
                }
                else 
                {
                    theLink.style.display = "none";
                }
            }
        }
    },
    over: function(titleID, dropShellID, dropHolderID) {
        MSNLatestTV.timeout = setTimeout("MSNLatestTV.overTimeout('" + titleID + "','" + dropShellID + "','" + dropHolderID + "')", 250);
    },
    overTimeout: function(titleID, dropShellID, dropHolderID) {
        document.getElementById(titleID).className = "latestTVTitleOver";
        document.getElementById(dropShellID).className = "latestTVShellOver";
        document.getElementById(dropHolderID).style.top = new String((-1 * ($("#" + dropShellID).outerHeight() + 20)) + "px");
        document.getElementById(dropHolderID).style.left = "-5px";
    },
    out: function(titleID, dropShellID, dropHolderID) {
        clearTimeout(MSNLatestTV.timeout);
        document.getElementById(titleID).className = "latestTVTitleOut";
        document.getElementById(dropShellID).className = "latestTVShellOut";
        document.getElementById(dropHolderID).style.top = "0px";
        document.getElementById(dropHolderID).style.left = "0px";
    },
    click: function(url,newWin) {
        if(newWin)
        {
            window.open(url,"BingVideoWindow");
        }
        else
        {
            location.href = url;
        }
    },
    trim: function(title) {
        return MSNTopBox.trim(title,50);
    },
    FixURL: function(url)
    {
        if((url.indexOf("video.msn.com") > -1 || url.indexOf("soapbox.msn.com") > -1) && url.indexOf("videoguide.msn.com") == -1)
        {                            
            var guidArray1 = new Array();
            var guidArray2 = new Array();                
            
            if(url.indexOf("uuids=") > -1)
            {
                guidArray1 = url.split("uuids=");                     
            }
            else if(url.indexOf("uuid=") > -1)
            {
                guidArray1 = url.split("uuid=");                     
            }
            else if(url.indexOf("g=") > -1)
            {
                guidArray1 = url.split("g=");                     
            }
            else if(url.indexOf("guid=") > -1)
            {
                guidArray1 = url.split("guid=");                     
            }
            else if(url.indexOf("vid=") > -1)
            {               
                guidArray1 = url.split("vid=");                     
            }
            else
            {
                return url;
            }
            
            guidArray2 = [""];
            queryVar = "";
                 
            if(guidArray1.length > 1)
            {
                guidArray2 = guidArray1[1].split("&");
            
                queryVar = "&g=";
            }
                           
            return "http://videoguide.msn.com/play/?ocid=vgeasytv" + queryVar + guidArray2[0];           
        }
        else
        {
            return url;
        }
    }
};

var MSNCarousels = {
    carousel1: {
        title: "most watched today",
        groupID: "mostpopular"
    },
    carousel2: {
        title: "most commented video",
        groupID: "mostcommented"
    },
    carousel3: {
        title: "popular user video",
        groupID: "viralmix"
    }
};

var MSNRightBox = {
    timeout: null,
    box1: {
        title: "must-see viral video",
        groupID: "topviral",
        videos: null
    },
    box2: {
        title: "celebrity buzz video",
        groupID: "mostwatchedcelebrity",
        videos: null
    },
    get: function() {
        if (MSNRightBox.box1.title == null || MSNRightBox.box1.groupID == null || MSNRightBox.box2.title == null || MSNRightBox.box2.groupID == null) {
            return;
        }

        if (theFeed != null) {
            MSNRightBox.box1.videos = XmlHelper.selectNodes(theFeed.documentElement, "//group[@id='" + MSNRightBox.box1.groupID + "']/item", null);
            MSNRightBox.box2.videos = XmlHelper.selectNodes(theFeed.documentElement, "//group[@id='" + MSNRightBox.box2.groupID + "']/item", null);

            MSNRightBox.setBox1();
        }
        else {
            setTimeout(MSNRightBox.get, 250);
        }
    },
    setBox1: function() {
        if (MSNRightBox.box1.videos == null) {
            MSNRightBox.setBox2();
            return;
        }

        var videos = MSNRightBox.box1.videos;

        for (var x = 0; x < 4; x++) {
            var theLink = document.getElementById("rightBox1LinkHolder" + (x + 1));

            if (videos[x] != null) {
                var theTitle = videos[x].getElementsByTagName("title")[0].childNodes[0].nodeValue;
                var LinkGuid = videos[x].getElementsByTagName("id")[0].getAttribute("guid");
                var theURL = new String();                    
                var NewWindow = false;
                if(LinkGuid.toLowerCase().indexOf("http://") > -1)
                {
                    NewWindow = true;
                    theURL = videos[x].getElementsByTagName("link")[0].getAttribute("url");
                }
                else
                {
                    theURL = "http://videoguide.msn.com/play/?ocid=vgmust&g=" + LinkGuid;
                }
                var theDesc = videos[x].getElementsByTagName("description")[0].childNodes[0].nodeValue;
                var theImage = videos[x].getElementsByTagName("thumb")[0].getAttribute("url");

                var dropDownID = new String("rightBox1DropDown" + (x + 1));
                var dropDownShellID = new String("rightBox1Shell" + (x + 1));
                var dropDownHolderID = new String("rightBox1Holder" + (x + 1));
                var dropDownArrowClass = ((x+1)%4 == 0) ? "rightBoxBubbleArrowRight" : "rightBoxBubbleArrowLeft";
                var dropDownLeftPosition = ((x+1)%4 == 0) ? "-135px" : "-5px";
                var titleID = new String("rightBox1TitleText" + (x + 1));

                theLink.innerHTML = "<div class='rightVideoBox' onmouseover='MSNRightBox.over(\"" + titleID + "\",\"" + dropDownShellID + "\",\"" + dropDownHolderID + "\",\"" + dropDownLeftPosition + "\")' onmouseout='MSNRightBox.out(\"" + titleID + "\",\"" + dropDownShellID + "\",\"" + dropDownHolderID + "\")' onclick='MSNRightBox.click(\"" + theURL + "\"," + NewWindow + ")'>" +
                                    "<div class='rightBoxImage'><img src='" + theImage + "' /></div><div id='" + titleID + "' class='rightBoxTextOut'>" + this.trim(theTitle, 30) + "</div>" +
                                    "</div>" +
                                    "<div id='" + dropDownShellID + "' class='rightBoxShellOut'>" +
                                    "<div id='" + dropDownHolderID + "' class='rightBoxDropDownHolder'>" +
                                    "<div class='rightBoxDropDown' id='" + dropDownID + "'><div class='rightBoxDropDownTitle'>" + theTitle + "</div><div class='rightBoxDropDownText'>" + theDesc + "</div></div>" +
                                    "<div class='"+dropDownArrowClass+"'></div>" +
                                    "</div></div>";
            }
            else {
                theLink.style.display = "none";
            }
        }

        MSNRightBox.setBox2();
    },
    setBox2: function() {
        if (MSNRightBox.box2.videos == null) {
            return;
        }

        var videos = MSNRightBox.box2.videos;

        for (var x = 0; x < 8; x++) {
            var theLink = document.getElementById("rightBox2LinkHolder" + (x + 1));

            if (videos[x] != null) {
                var theTitle = videos[x].getElementsByTagName("title")[0].childNodes[0].nodeValue;
                var LinkGuid = videos[x].getElementsByTagName("id")[0].getAttribute("guid");
                var theURL = new String();                    
                var NewWindow = false;
                if(LinkGuid.toLowerCase().indexOf("http://") > -1)
                {
                    NewWindow = true;
                    theURL = videos[x].getElementsByTagName("link")[0].getAttribute("url");
                }
                else
                {
                    theURL = "http://videoguide.msn.com/play/?ocid=vgbuzz&g=" + LinkGuid;
                }                
                var theDesc = videos[x].getElementsByTagName("description")[0].childNodes[0].nodeValue;
                var theImage = videos[x].getElementsByTagName("thumb")[0].getAttribute("url");

                var dropDownID = new String("rightBox2DropDown" + (x + 1));
                var dropDownShellID = new String("rightBox2Shell" + (x + 1));
                var dropDownHolderID = new String("rightBox2Holder" + (x + 1));
                var dropDownArrowClass = ((x+1)%4 == 0) ? "rightBoxBubbleArrowRight" : "rightBoxBubbleArrowLeft";
                var dropDownLeftPosition = ((x+1)%4 == 0) ? "-135px" : "-5px";
                var titleID = new String("rightBox2TitleText" + (x + 1));

                theLink.innerHTML = "<div class='rightVideoBox' onmouseover='MSNRightBox.over(\"" + titleID + "\",\"" + dropDownShellID + "\",\"" + dropDownHolderID + "\",\"" + dropDownLeftPosition + "\")' onmouseout='MSNRightBox.out(\"" + titleID + "\",\"" + dropDownShellID + "\",\"" + dropDownHolderID + "\")' onclick='MSNRightBox.click(\"" + theURL + "\"," + NewWindow + ")'>" +
                                    "<div class='rightBoxImage'><img src='" + theImage + "' /></div><div id='" + titleID + "' class='rightBoxTextOut'>" + this.trim(theTitle, 30) + "</div>" +
                                    "</div>" +
                                    "<div id='" + dropDownShellID + "' class='rightBoxShellOut'>" +
                                    "<div id='" + dropDownHolderID + "' class='rightBoxDropDownHolder'>" +
                                    "<div class='rightBoxDropDown' id='" + dropDownID + "'><div class='rightBoxDropDownTitle'>" + theTitle + "</div><div class='rightBoxDropDownText'>" + theDesc + "</div></div>" +
                                    "<div class='"+dropDownArrowClass+"'></div>" +
                                    "</div></div>";
            }
            else {
                theLink.style.display = "none";
            }
        }
    },
    over: function(titleID, dropShellID, dropHolderID, leftPosition) {
        MSNRightBox.timeout = setTimeout("MSNRightBox.overTimeout('" + titleID + "','" + dropShellID + "','" + dropHolderID + "','" + leftPosition + "')", 250);
    },
    overTimeout: function(titleID, dropShellID, dropHolderID, leftPosition) {
        document.getElementById(titleID).className = "rightBoxTextOver";
        document.getElementById(dropShellID).className = "rightBoxShellOver";
        document.getElementById(dropHolderID).style.top = new String((-1 * ($("#" + dropShellID).outerHeight() + 106)) + "px");
        document.getElementById(dropHolderID).style.left = leftPosition;
    },
    out: function(titleID, dropShellID, dropHolderID) {
        clearTimeout(MSNRightBox.timeout);
        document.getElementById(titleID).className = "rightBoxTextOut";
        document.getElementById(dropShellID).className = "rightBoxShellOut";
        document.getElementById(dropHolderID).style.top = "0px";
        document.getElementById(dropHolderID).style.left = "0px";
    },
    click: function(url,newWindow) 
    {            
        if(newWindow == null || newWindow == false)
        {
            location.href = url;
        }
        else if(newWindow == true)
        {
            window.open(url,"_new");
        }
        else
        {
            return;
        }       
    },        
    trim: function(title,length)
    {  
        return MSNTopBox.trim(title,length);
    }
};



