var mtgp2Video = {
    QueryVars: location.search ? location.search.substr(1).split("&") : new Array(),
    VideoLoaded: false,
    Video: null,
    UUID: null
};

mtgp2Video.Get = {
    VideoByConfig: function()
    {
        mtgp2Video.VideoLoaded = false;
        
        var rand = Math.ceil(Math.random() * 4);        
        scriptAppend("uuidScript","http://edge"+rand+".catalog.video.msn.com/videoByTag.aspx?tag=puVidHubRelatedGallery&ns=MsnEntertainment_Gallery&mk=us&vs=1&responseEncoding=json&callBackName=mtgp2Video.Set.Video"); 
    },
    VideoByUUID: function(uuid)
    {
        mtgp2Video.VideoLoaded = false;
        
        var rand = Math.ceil(Math.random() * 4);        
        scriptAppend("uuidScript","http://edge"+rand+".catalog.video.msn.com/videoByUuids.aspx?uuids="+uuid+"&responseEncoding=json&callBackName=mtgp2Video.Set.Video");                       
    },
    Player: function()
    {
        OnVideoLoaded = function(uuid)
        {                             
            if(uuid != null && uuid != mtgp2Video.UUID)
            {
                mtgp2Video.Events.OnVideoLoaded = mtgp2Video.Set.MetaData;
                mtgp2Video.Get.VideoByUUID(uuid);
            }
        }
        
        if(mtgp2Video.UUID != null)
        {
            Msn.Video.createWidget('mtgp2_playerContainer', 'CustomPlayer', 970, 1260, {"player.vcq": "videoByUuids.aspx%3Fuuids=" + mtgp2Video.UUID, "configCsid": "MsnEntertainment", "configName": "entertainment_video_hub","player.ap":"true"}, 'CustomPlayer1');
        }
        else
        {
            Msn.Video.createWidget('mtgp2_playerContainer', 'CustomPlayer', 970, 1260, {"configCsid": "MsnEntertainment", "configName": "entertainment_video_hub"}, 'CustomPlayer1');
        }
    }
};

mtgp2Video.Set = {
    Video: function(data)
    {
        if(data != null && data.video[0] != null)
        {
            mtgp2Video.Video = data.video[0];
                                  
            mtgp2Video.VideoLoaded = true;
            
            if(mtgp2Video.Events.OnVideoLoaded != null)
            {
                mtgp2Video.Events.OnVideoLoaded();    
            }
        }
        else
        {
            mtgp2Video.VideoLoaded = "<error>";
        }
    }  
};

mtgp2Video.Events = {
    OnVideoLoaded: null
};

function scriptAppend(id,src)
{
    if(!id || !src){return}
    if(document.getElementById(id) != null){removeFromDOM(id);}
    var s1 = document.createElement("script");
    s1.id = id;
    s1.src = src;
    s1.type = "text/javascript";   
    headAppend(s1);
}

function headAppend(o)
{
    var head = document.getElementsByTagName("head")[0];
    head.appendChild(o);
}


for(var x in mtgp2Video.QueryVars)
{    
    var aVar = mtgp2Video.QueryVars[x].split("=");    
    var varName = aVar[0].toLowerCase();        
    if(varName == "uuids" || varName == "uuid" || varName == "g" || varName == "guid" || varName == "vid")
    {       
        mtgp2Video.UUID = aVar[1];                  
    }                              
} 