﻿var PUVideo = {
    QueryVars: location.search ? location.search.substr(1).split("&") : new Array(),
    VideoLoaded: false,
    Video: null,
    UUID: null
};

PUVideo.Get = {
    VideoByConfig: function()
    {
        PUVideo.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=PUVideo.Set.Video"); 
    },
    VideoByUUID: function(uuid)
    {
        PUVideo.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=PUVideo.Set.Video");                       
    },
    Player: function()
    {
        OnVideoLoaded = function(uuid)
        {                             
            if(uuid != null && uuid != PUVideo.UUID)
            {
                PUVideo.Events.OnVideoLoaded = PUVideo.Set.MetaData;
                PUVideo.Get.VideoByUUID(uuid);
            }
        }
        
        if(PUVideo.UUID != null)
        {
            Msn.Video.createWidget('puPlayer', 'CustomPlayer', 508, 454, {"player.vcq": "videoByUuids.aspx%3Fuuids=" + PUVideo.UUID, "configCsid": "MsnEntertainment", "configName": "puVidHubPlayerUUID"}, 'CustomPlayer1');
        }
        else
        {
            Msn.Video.createWidget('puPlayer', 'CustomPlayer', 508, 454, {"configCsid": "MsnEntertainment", "configName":"puVidHubPlayer"}, 'CustomPlayer1');
        }
    },
    RelatedVideo: function()
    {
        if(PUVideo.UUID != null)
        {
            Msn.Video.createWidget('puGallery', 'Gallery', 320, 392, {"gallery.vcq": "videoByUuids.aspx%3Fuuids=" + PUVideo.UUID, "configCsid": "MsnEntertainment", "configName": "puVidHubRelatedGallery", "gallery.imageHeightGrid": "102", "gallery.imageWidthGrid": "136","cssPath":"http://entimg.msn.com/i/paralleluniverse/videoHub_puGallery.css","player.ap":"true"}, 'Gallery2');   
        }
        else
        {
            Msn.Video.createWidget('puGallery', 'Gallery', 320, 392, {"configCsid": "MsnEntertainment", "configName":"puVidHubRelatedGallery", "gallery.imageHeightGrid": "102", "gallery.imageWidthGrid": "136","cssPath":"http://entimg.msn.com/i/paralleluniverse/videoHub_puGallery.css"}, 'Gallery2');
        }       
    }
};

PUVideo.Set = {
    Video: function(data)
    {
        if(data != null && data.video[0] != null)
        {
            PUVideo.Video = data.video[0];
                                  
            PUVideo.VideoLoaded = true;
            
            if(PUVideo.Events.OnVideoLoaded != null)
            {
                PUVideo.Events.OnVideoLoaded();    
            }
        }
        else
        {
            PUVideo.VideoLoaded = "<error>";
        }
    }  
};

PUVideo.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 PUVideo.QueryVars)
{    
    var aVar = PUVideo.QueryVars[x].split("=");    
    var varName = aVar[0].toLowerCase();        
    if(varName == "uuids" || varName == "uuid" || varName == "g" || varName == "guid" || varName == "vid")
    {       
        PUVideo.UUID = aVar[1];                  
    }                              
} 

PUVideo.Events.OnVideoLoaded = PUVideo.Set.MetaData;

if(PUVideo.UUID != null)
{
    PUVideo.Get.VideoByUUID(PUVideo.UUID);   
}
else
{
    PUVideo.Get.VideoByConfig();
}