if (!window.Msn){window.Msn={};}
if(!window.Msn.Video)
{
    window.Msn.Video= new function() 
    {
        var me = this;
        this.OnVideoTitle=function(text,id)
        {var id=id+"_t";if(checkString(text)&&checkString(id))
        {var el=document.getElementById(id);if(el)
        {el.innerHTML=text;}}}
        this.OnMediaCompleted=function(id)
        {var id=id+"_t";var el=document.getElementById(id);if(el){el.innerHTML="";}}
        this.OnLinkback=function(text,url,id)
        {var id=id+"_m";var c;var el=document.getElementById(id);var css;if(el)
        {el.innerHTML="";for(c=0;c<=text.length-1;c++)
        {if(checkString(text[c])&&checkString(url[c]))
        {css=(c==text.length-1)?"linkback last":"linkback";el.innerHTML=el.innerHTML+'<a class="'+css+'" href="'+url[c]+'" target="_blank">'+text[c]+'</a>';}}}}
        this.OnAdLoaded=function(adData,id)
        {var id=id+"_a";if(checkObject(adData)&&adData.imageUrl&&checkArray(adData.clickUrls,2)&&checkString(id))
        {var el=document.getElementById(id);if(el)
        {el.innerHTML='<a class="ad" href="'+adData.clickUrls[1]+'" target="_blank"><img src="'+adData.imageUrl+'" width="300" height="60"></a>';}}}
        function checkString(s)
        {return((typeof(s)=="string")&&(s.length>0));}
        function checkObject(o)
        {return(typeof(o)=="object"&&(null!=o));}
        function checkArray(array,minLength)
        {var arrayCheck=(array instanceof Array);if(minLength!='')
        {arrayCheck=arrayCheck&&(array.length>=minLength);}
        return arrayCheck;}
    }
}

Msn.Video.EVENT_FLASH_DETECT = "flash_detect";

// util functions

function isIE()
{
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf("msie") != -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function checkDefined(o)
{
    return ((typeof(o) != "undefined") && (null != o));
}

function checkString(s)
{
    return ((typeof(s) == "string") && (s.length > 0));
}

function checkObject(o)
{
    return (typeof(o) == "object" && (null != o));
}

function checkFunction(o)
{
    return (typeof(o) == "function");
}

function checkInt(f, min, max)
{
      if((null != f) && ((typeof(f) != "string") || ("" != f)) && !isNaN(f))
      {
        if(checkDefined(min))
        {
            if(f < min)
            {
                return false;
            }
        }
        if(checkDefined(max))
        {
            if(f > max)
            {
                return false;
            }
        }
        return true;
    }
    return false;            
}

function openStandardWindow(url, target)
{
    return openWindow(url, target, 1024 , 768, "resizable=yes,toolbar=yes,menubar=yes,scrollbars=yes,status=yes,location=yes");
}

// FlashObject

Msn.Video.FlashObject = function(id, s, w, h, params)
{
    var that = this;
    var attrKeys = new Array();
    var attrValues = new Array();
    
    var _objectNode = null;
    var _installCtrl = null;
    var _minVersionFailed = false;
    var _id = id;
    var _src = s;
    var _params = params;
    var _versionDetectionFailed = false;
    var _majorVersion=0;
    var _minorVersion=0;
    var _revisionVersion=0;
    var _parent;
    
    _params = _params || {};
    
    if (_params["wmode"] == undefined) _params["wmode"] = "transparent";
    if (_params["bgColor"] == undefined) _params["bgColor"] = "#000000";
    if (_params["base"] == undefined) _params["base"] = ".";
    if (_params["menu"] == undefined) _params["menu"] = "false";

    Msn.Video.FlashVersionDetectInstance.addListener(this);
    
    this.onEvent = function(source, type, param, param2)
    {
        switch(type)
        {
            case Msn.Video.EVENT_FLASH_DETECT:
                if (!Msn.Video.FlashVersionDetectInstance.checkVersion(_majorVersion, _minorVersion, _revisionVersion))
                {
                    this.onVersionDetectionFailure();
                }
            default:
                break;
        }        
    }
   
    this.getObjectNode = function()
    {
        return _objectNode;
    }    

    this.setInstallCtrl = function(ctrl, major, minor, revision)
    {
        if (ctrl != undefined)
        {
            _installCtrl = ctrl;
        }
        
        _majorVersion = major != undefined ? major : 0;
        _minorVersion = minor != undefined ? minor : 0;
        _revisionVersion = revision != undefined ? revision : 0;
    }

    this.onDispose = function()
    {        
        if (isIE())
        {
            // Clearing up Flash External interface to avoid leaks
            // Note : this is not required for FF, and actually would cause subsequent flash object not to render..
            try 
            {
                for(var f in _objectNode)
                {
                    if(checkFunction(_objectNode[f]))
                    {    
                        _objectNode[f] = empty;
                    }
                }
            }               
            catch (e) {}
        }
        
        // Clean up the flash object to avoid memory leaks
        _objectNode = null;                
    }
    
    this.onVersionDetectionFailure = function()
    {
        _versionDetectionFailed = true;
        this.render(_parent);                          
    }
    
    this.verifyIEInstall = function()
    {
        var n = document.getElementById(_id + "Install");
        
        if (n)
        {
            // Flash installation failed..
            that.onVersionDetectionFailure();
        }        
    }
    
    this.render = function(p)
    {
        _parent = p;
        _parent.innerHTML = "";
        
        if ((null != p) && checkString(_src))
        {      
            // need to check for a version failure here, since the event may have already been sent before this
            // object was created
            if (Msn.Video.FlashVersionDetectInstance && !Msn.Video.FlashVersionDetectInstance.checkVersion(_majorVersion, _minorVersion, _revisionVersion))
            {
                _versionDetectionFailed = true;
            }

            if (_versionDetectionFailed && _installCtrl) 
            {
                _installCtrl.render(p);
            }
            else
            {
                if (isIE())
                { 
                    // set object tag for IE through innerHTML               
                    p.innerHTML = "<object id=\"" + _id + "\" " +
                                    "name=\"" + _id + "\" " +
                                    "classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" " +
                                    "height=\"" + h + "\" " +
                                    "width=\"" + w + "\"> " +
                                    "<param name=\"movie\" value=\"" + _src + "\">" +
                                    "<param name=\"flashvars\" value=\"" + _params["flashvars"] + "\"/> " +
                                    "<param name=\"menu\" value=\"" + _params["menu"] + "\">" +
                                    "<param name=\"wmode\" value=\"" + _params["wmode"] + "\">" +
                                    "<param name=\"play\" value=\"0\">" +
                                    "<param name=\"quality\" value=\"High\">" +
                                    "<param name=\"allowScriptAccess\" value=\"always\">" +
                                    "<param name=\"allowFullScreen\" value=\"true\">" +
                                    "<param name=\"bgcolor\" value=\"" + _params["bgcolor"] + "\">" +
                                    "<param name=\"base\" value=\"" + _params["base"] + "\">" +
                                    "<div id=\"" + _id + "Install\"></div>" +
                                    "</object>";    

                     _objectNode = p.childNodes[0]; 
            
                     if (_installCtrl)
                     {
                        setTimeout(this.verifyIEInstall, 1000);
                     }                                         
                }
                else
                {                
                   
	                _objectNode = document.createElement("EMBED");
                    _objectNode.id = _id;
                    _objectNode.setAttribute("name", _id);
                    _objectNode.setAttribute("bgcolor", _params["bgcolor"]);
                    _objectNode.setAttribute("allowScriptAccess", "always");
                    _objectNode.setAttribute("menu", _params["menu"]);
                    _objectNode.setAttribute("quality", "high");
                    _objectNode.setAttribute("type", "application/x-shockwave-flash");
                    _objectNode.setAttribute("src", _src);                 
                    _objectNode.setAttribute("width", w);
                    _objectNode.setAttribute("height", h);
                    _objectNode.setAttribute("flashVars", _params["flashvars"]);
                    _objectNode.setAttribute("wmode", _params["wmode"]);
                    _objectNode.setAttribute("base", _params["base"]);
                    _objectNode.setAttribute("allowFullScreen", "true");
                    p.appendChild(_objectNode);  
                      
                    if (_installCtrl && !(navigator.plugins != null && navigator.plugins.length > 0 && (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]))) 
                    {
                        this.onVersionDetectionFailure();
                    }
                }
            } 
        }
        else
        {
            // Cleanup reference to flash object node
            _objectNode = null;
        }
    } 
}

// VersionDetect

Msn.Video.FlashVersionDetect = function()
{
    var that = this;
    var _major;
    var _minor;
    var _revision;
    var _listeners = new Array();
    
     // register FlashVersionDetect singleton    
    if (!checkDefined(Msn.Video.FlashVersionDetectInstance))
    {                
        Msn.Video.FlashVersionDetectInstance = this; 
    } 
    
    this.addListener = function(listener)
    {
        _listeners.push(listener);
    }
    
    this.fireEvent = function(event, param1, param2)
    {
        for (var i=0; i<_listeners.length; i++)
        {
            if (checkDefined(_listeners[i]) && checkFunction(_listeners[i].onEvent))
            {
                _listeners[i].onEvent(this, event, param1, param2);
            }
        }
    }
    
    this.render = function(p)
    {
        if (null != p)
        {
            p.style.position = "absolute";
    
            var id = "versiondetect";
            var url = "http://images.video.msn.com/flash/versionDetect.swf";
            var obj = new Msn.Video.FlashObject(id, url, 1, 1);
            obj.render(p); 
            
            var node = obj.getObjectNode();
            
            if (node)
            {
                if (node.attachEvent) node.attachEvent("fscommand", that.onVersion);
                window[id+"_DoFSCommand"] = that.onVersion; 
            }              
            else
            {
                _major = _minor = _revision = 0;
            }   
        }
    } 
    
    this.checkVersion = function(major, minor, revision)
    {
        if (_major > major || _major == undefined)
        {
            return true;
        }
        else if (_major == major)
        {
            if (_minor > minor || _minor == undefined)
            {
                return true;
            }
            else if (_minor == minor && (_revision >= revision || _revision == undefined))
            {
                return true;
            }
        }

        return false;
    }
    
    this.onVersion = function(v)
    {
        try
        {
            var temp = v.split(" ")[1]; // WIN 9,0,0,0 -> 9,0,0,0
            temp = temp.split(","); // -> [9,0,0,0]
            _major = checkInt(temp[0]) ? temp[0] : 0;
            _minor = checkInt(temp[1]) ? temp[1] : 0;
            _revision = checkInt(temp[2]) ? temp[2] : 0;

            that.fireEvent(Msn.Video.EVENT_FLASH_DETECT, v);
        }
        catch (e) {}
    }
}

// FlashInstall

Msn.Video.FlashInstall = function(content)
{
    var that = this;
    var _content = content;
    
    this.cfg = {
        REQUIRE_FLASH : "Please install the latest version of the free Adobe Flash Player. ",
        REQUIRE_FLASH_LINK: "Download now.",
        HELP_FLASH : "For help with Flash, see Adobe's ",
        HELP_FLASH_LINK: "Flash Player Support page.",
        FLASH_INSTALL_URL : "http://g.msn.com/0VD0/34/67?CM=not_auto_flash",
        FLASH_HELP_URL : "http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_15511"
    }       
   
    this.onFlashInstall = function()
    {
        setTimeout(that.onFlashInstallComplete, 1000);        
    }
    
    this.onFlashInstallComplete = function()
    {
        openStandardWindow(that.cfg.FLASH_INSTALL_URL, "_blank");
    }
    
    this.render = function(p)
    {
        if (null != p)
        {   
            if (checkDefined(_content))
            {
                p.appendChild(_content);
            }
            else
            {         
                var d = document.createElement("DIV");
                d.className = "flashInstallParent";
                p.appendChild(d);

                var n = document.createElement("DIV");
                n.className = "flashInstall";
                n.style.backgroundColor = "#e0e0e0";
                n.style.color = "#666";
                n.style.fontFamily = "Tahoma";
                n.style.fontSize = "8pt";
                n.style.fontWeight = "bold";
                n.style.padding = "30px";
                d.appendChild(n);
                
                var span = document.createElement("span");
                span.innerHTML = that.cfg.REQUIRE_FLASH;
                n.appendChild(span);
                            
                var link = document.createElement("a");
                link.setAttribute("href", that.cfg.FLASH_INSTALL_URL);
                link.setAttribute("target", "_blank");
                link.innerHTML = that.cfg.REQUIRE_FLASH_LINK;
                link.style.color = "#07519a";
                n.appendChild(link);
                
                n.appendChild(document.createElement("BR"));
                n.appendChild(document.createElement("BR"));
                
                var span = document.createElement("span");
                span.innerHTML = that.cfg.HELP_FLASH;
                n.appendChild(span);
                
                var link = document.createElement("a");
                link.setAttribute("href", that.cfg.FLASH_HELP_URL);
                link.setAttribute("target", "_blank");
                link.innerHTML = that.cfg.HELP_FLASH_LINK;
                link.style.color = "#07519a";
                n.appendChild(link);
            }
        }
    } 
}

// script interface

Msn.Video.Widgets = 
{
    player : {current: "flash/soapbox1_1.swf"},
    playerRTL : {current: "flash/soapbox1_1RTL.swf"},
    playerad : {current: "flash/soapbox1_1.swf"},
    gallery : {current: "flash/gallerywidget/1_0/gallerywidget.swf"},
    galleryRTL : {current: "flash/gallerywidget/1_0/gallerywidgetRTL.swf"},
    customplayer : {current: "flash/customplayer/1_0/customplayer.swf"},
    uploader: {
        current: "flash/uploaderwidget/1_1/uploaderwidget.swf",
        "2"  : "flash/uploaderwidget/2_0/uploaderwidget.swf",
        "2.0": "flash/uploaderwidget/2_0/uploaderwidget.swf",
        "2_0": "flash/uploaderwidget/2_0/uploaderwidget.swf",
        "1.1": "flash/uploaderwidget/1_1/uploaderwidget.swf",
        "1_1": "flash/uploaderwidget/1_1/uploaderwidget.swf",
        "1"  : "flash/uploaderwidget/uploaderwidget.swf",
        "1.0": "flash/uploaderwidget/uploaderwidget.swf",
        "1_0": "flash/uploaderwidget/uploaderwidget.swf"
    }
};

Msn.Video.createWidget = function(divId, src, w, h, flashvars, widgetId, params, version, root, downlevel)
{
    var container = document.getElementById(divId);
    
    if (checkDefined(container))
    {
        src = src.toLowerCase();
        var origSrc = src;
        
        if (Msn.Video.Widgets[src])
        {
            if (root)
            {
                if (root.indexOf("http://") != 0)
                {
                    root = "http://" + root;
                }
                
                if (root.lastIndexOf("/") != root.length-1)
                {
                    root += "/";
                }
            }
        
            version = version || "current";
            src = (root || "http://images.video.msn.com/") + (Msn.Video.Widgets[src][version] ? Msn.Video.Widgets[src][version] : Msn.Video.Widgets[src]["current"]);
        }
      
        var detectDiv = document.createElement("div");
        detectDiv.setAttribute("id", divId + "_detect");
        container.appendChild(detectDiv);
        
        var detectObj = new Msn.Video.FlashVersionDetect();  
        
        var contentDiv = document.createElement("div");
        contentDiv.setAttribute("id", divId + "_content");
        container.appendChild(contentDiv);
        
        var fvv = {};
        
        for(var i in flashvars)
        {
            fvv[i] = flashvars[i].replace(/\&/g,"%26");
        }
        
        if (origSrc == "playerad")
        {
            contentDiv = Msn.Video.buildPlayerAdElements(contentDiv, divId);
            fvv["cbprefix"] = "Msn.Video.";
            fvv["cbdata"] = divId;
        }
        
        var fvs = "";
        
        for(var i in fvv)
        {
            fvs += i + "=" + fvv[i] + "&";
        }
        
        params = params || new Object();
        params["flashvars"] = fvs;

        var flash = new Msn.Video.FlashObject(widgetId || divId + "_flash", src, w, h, params); 
        flash.setInstallCtrl(new Msn.Video.FlashInstall(downlevel), 9);
        
        detectObj.render(detectDiv);
        flash.render(contentDiv);
        
        return flash.getObjectNode();
    }
}

Msn.Video.buildPlayerAdElements = function(div, id)
{
     var v = '<div class="video1"><h3 id="' + id + '_t"></h3><div id="' + id + '_p"></div><div id="' + id + '_m"></div><div id="' + id + '_a"></div></div>';
     div.innerHTML = v;
     
     var playerNode = document.getElementById(id + '_p');
     
     return playerNode;
}