﻿if(!window.Msn){window.Msn={};}
if (!Msn.Video){Msn.Video={};}

Msn.Video.Widget = new function() 
{
    this.widgets = new Array();

    this.lastOnMediaChanged;
    this.lastOnVideoLoaded;
    this.lastOnAdLoaded;
    this.lastOnMediaCompleted = false;
    this.lastOnPlaylistInitFailed = false;
    
    // player callbacks
    Msn.Video.OnPlaylistInitFailed = function()
    {
        //window.alert("player: onPlaylistInitFailed");
        Msn.Video.Widget.lastOnPlaylistInitFailed = true;
        Msn.Video.Widget.call("OnPlaylistInitFailed");
    }
    
    Msn.Video.OnVideoLoadError = function(uuid)
    {   
        //window.alert("player: onVideoLoadError");
        Msn.Video.Widget.lastOnMediaCompleted = true;
        Msn.Video.Widget.call("OnVideoLoadError", uuid);
    }
    
    Msn.Video.OnMediaChanged = function(uuid) 
    {
        //window.alert("player: onMediaChanged " + uuid);
        Msn.Video.Widget.lastOnMediaChanged = uuid;
        Msn.Video.Widget.call("OnMediaChanged", uuid);
    }
    
    Msn.Video.OnMediaCompleted = function() 
    {
        //window.alert("player: onMediaCompleted");
        Msn.Video.Widget.lastOnMediaCompleted = true;
        Msn.Video.Widget.call("OnMediaCompleted");
    }
    
    Msn.Video.OnVideoLoaded = function(uuid) 
    {
        //window.alert("player: onVideoLoaded " + uuid);
        Msn.Video.Widget.lastOnVideoLoaded = uuid;
        Msn.Video.Widget.call("OnVideoLoaded", uuid);
    }
    
    Msn.Video.OnAdLoaded = function(adData)
    {   
        //window.alert("player: onAdData " + adData.flashUrl);
        Msn.Video.Widget.lastOnAdLoaded = adData;
        Msn.Video.Widget.call("OnAdLoaded", adData);
    }

    // outgoing messages
    this.OnExternalInterfaceLoaded = function()
    {
        for (var i=0; i<this.widgets.length; i++) 
        {
            if (!this.widgets[i].initialized && this.widgets[i].OnMediaChanged != undefined) 
            {
                var w = this.widgets[i];
                w.initialized = true;

                if (this.lastOnPlaylistInitFailed)
                {
                    w.OnPlaylistInitFailed();
                }
                else
                {
                    if (this.lastOnMediaChanged)
                    {
                        w.OnMediaChanged(this.lastOnMediaChanged);
                    }
                    
                    if (this.lastOnAdLoaded)
                    {
                        w.OnAdLoaded(this.lastOnAdLoaded);
                    }
                    
                    if (this.lastOnVideoLoaded && !this.lastOnMediaCompleted)
                    {
                        w.OnVideoLoaded(this.lastOnVideoLoaded);
                    }
                }
            }
        }
    }
    
    this.OnVideoClicked = function(guid, playerId)
    {
        this.inlinePlayerPlay(playerId, guid);
    }
    
    this.OnAddToPlaylist = function(guid, playerId)
    {
        this.inlinePlayerAddToPlaylist(playerId, guid);
    }
    
    // utility functions 
    this.Build = function(div, w, h, fv, swfUrl) 
    {
        var swfLoc = swfUrl ? swfUrl : "http://images.video.msn.com/flash/galleryWidget/VideoWidget.swf";
        var fvs = "";
        for(var i in fv) { fvs += i + "=" + fv[i] + "&amp;"; }
        Msn.Video.Widget.Flash.Build(swfLoc,'8', w, h, div, {allowScriptAccess:'always', allowFullScreen:'true', wmode:'transparent', base:swfLoc, flashvars:fvs});

        var obj = document.getElementById(div+"obj");
        if (obj) this.widgets.push(obj);
        
        var names = fv["categoryNames"] ? decodeURIComponent(fv["categoryNames"]).split(";") : new Array();
        var urls = fv["categoryRequests"] ? decodeURIComponent(fv["categoryRequests"]).split(";") : new Array();
        var mk = fv["vcMarket"] ? fv["vcMarket"] : "us";
        var title = fv["containerTitle"] ? decodeURIComponent(fv["containerTitle"]) : "";
        fv["cbPrefix"] = "Msn.Video.";

        for (var i=0; i<Math.min(names.length, urls.length); i++) {
            var url = "http://catalog.video.msn.com/" + urls[i] + "&mk=" + mk + "&ff=8a&responseEncoding=rss&ps=50&ind=1";
            var name = title  && title != "" ? title + " - " + names[i] : names[i];
            this.addRssFeed(name, url);
        }
        
        window[div+"obj_DoFSCommand"] = Msn.Video.Widget.fscommand;
        if (obj.attachEvent) obj.attachEvent("fscommand", Msn.Video.Widget.fscommand);

        return obj;
    }
    
    this.call = function(method, arg)
    {
        for (var i=0; i<this.widgets.length; i++)
        {
            try
            {
                if (this.widgets[i].initialized && this.widgets[i][method])
                {
                    this.widgets[i][method](arg);
                }
            }
            catch (e) {}
        }
    }
    
    this.reset = function()
    {
        this.lastOnMediaChanged = null;
        this.lastOnVideoLoaded = null;
        this.lastOnAdLoaded = null;
        this.lastOnMediaCompleted = false;
        this.lastOnPlaylistInitFailed = false;
    }
    
    this.fscommand = function(a, b)
    {
        for (var i=0; i<Msn.Video.Widget.widgets.length; i++) 
        {
            try
            {
                if (Msn.Video.Widget.widgets[i].OnFSCommand) 
                {
                    Msn.Video.Widget.widgets[i].OnFSCommand(a, b);
                }
            }
            catch (e)
            {
            }
        }
    }
      
    this.inlinePlayerPlay = function(id, uuid) 
    {
        var player = document.getElementById(id);

        if (player && player.vidPlayId) 
        {
            player.vidPlayId(uuid);
            return true;
        }
        
        return false;
    }
        
    this.inlinePlayerPause = function() 
    {
        var player = document.getElementById(id);
        
        if (player && player.vidPause) 
        {
            player.vidPause();
        }

        return true;
    }
            
    this.inlinePlayerAddToPlaylist = function(id, uuid) 
    {
        var player = document.getElementById(id);

        if (player && player.queAddId) 
        {
            player.queAddId(uuid);
            return true;
        }
        
        return false;
    }
            
    this.inlinePlayerCheckAvailable = function(id) 
    {
        var player = document.getElementById(id);

        if (player && player.vidPlayId && player.queAddId && player.vidPause) 
        {
            return true;
        }
        
        return false;
    }   
        
    this.addRssFeed = function(name, href) 
    {
        var head = document.getElementsByTagName("head");
        if (head.length == 0) return;
        else head = head[0];
      
        var link = document.createElement("link");
        link.type = "application/rss+xml";
        link.title = name;
        link.href = href;
        link.rel = "alternate";
        
        head.appendChild(link);
    }
}

Msn.Video.Widget.Flash=new function(){var me=this;var flv=null;var d=document;var w=window;function E(id){return d.getElementById(id);}
function flVer(){if(flv===null){flv=0;var i,f=null,p=w.navigator.plugins;if(p&&p.length){f=(p["Shockwave Flash"]||p["Shockwave Flash 2.0"]);if(f&&(i=f.description)){flv=parseInt(i.substring(i.indexOf('.')-2));}}
else if(w.ActiveXObject){for(i=15;i>2&&!f;--i){eval("try{f=new ActiveXObject('ShockwaveFlash.ShockwaveFlash.'+i);}catch(e){}");if(f){flv=i;}}}}
return flv;}
this.Version=function(){return flVer();};this.Build=function(f,v,wf,hf,id,pf){var m=5;if(v){var p=v.lastIndexOf(' ');if(p>0){p=parseInt(v.substring(p));}
else{p=parseInt(v);}
m=(isNaN(p)?m:p);}
if(m<=flVer()){var defs={movie:f,quality:'high',wmode:'transparent'};
var el;
if(navigator && navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
el='<embed type="application/x-shockwave-flash" src="'+ f +'" width="'+ wf +'" height="'+ hf +'"';
el += ' id="'+ id +'obj" name="'+ id +'obj" ';
if(pf)
{for(var r in defs)
{if(!pf[r])
{pf[r]=defs[r];}}}
else
{pf=defs;}
for(var p in pf)
{if(pf[p])
{el+=p + '="' +pf[p] + '" ';}}
el+='</embed>';
}
else{
el='<object id="'+id+'obj" type="application/x-shockwave-flash" width="'+
wf+'" height="'+hf+'" data="'+f+'">';if(pf)
{for(var r in defs)
{if(!pf[r])
{pf[r]=defs[r];}}}
else
{pf=defs;}
for(var p in pf)
{if(pf[p])
{el+='<param name="'+p+'" value="'+pf[p]+'"/>';}}
el+='</object>';}var dv=(id?d.getElementById(id):null);if(dv){dv.innerHTML=el;}
else{d.write(el);}}};
return this;};



