﻿var VideoControlJS = 
{
    nav6up: null,
    oldOnResize: null,
    loadedFrames: {},
    resizeFrames: {},
    content: {},
    
    loadFrame: function(id, content, lcid, build)
    {
        var agt = navigator.userAgent.toLowerCase();
        var major = parseInt(navigator.appVersion);
        var nav = ((agt.indexOf('mozilla') != -1) && ((agt.indexOf('spoofer') == -1) && (agt.indexOf('compatible') == -1)));
        
        VideoControlJS.nav6up = nav && (major >= 5);
    
    
        if (! VideoControlJS.loadedFrames[id])   // If it has already been loaded, then don't reload it.
        {
            if (content == null)
            {
                content = VideoControlJS.content[id];
            }
            else
            {
                VideoControlJS.content[id] = content;
            }

            try
            {
                var iframe = window.document.getElementById(id);
                if ((iframe.style.height == '0px') || (iframe.style.height == '') || (iframe.style.height == 'auto'))
                {
                    VideoControlJS.resizeFrames[id] = true;
                }
                
                var iframeDoc = (iframe.contentWindow || iframe.contentDocument);
                if (iframeDoc.document) iframeDoc = iframeDoc.document; // if we have a window ref get a doc ref
                    
                VideoControlJS.writeDocument(iframeDoc, content, lcid, build);
                setTimeout("VideoControlJS.resizeFrame('"+id+"');", 0);  // delay reizeFrame() call to give FireFox a chance to load the dom.
            }
            catch(LoadException)            
            {
                setTimeout("VideoControlJS.loadFrame('"+id+"', null, "+lcid+", '"+build+"');", 5);
                return;
            }
            
            VideoControlJS.loadedFrames[id] = true;
            VideoControlJS.content[id] = null;

            // Capture the window onResize event if this is the first HTML control loaded.
            //
            var size = 0;
            for(var x in VideoControlJS.loadedFrames)
            {
                size++;
            }
            if (size == 1)
            {
                VideoControlJS.oldOnResize = window.onresize;
                window.onresize = VideoControlJS.onResize;
            }
        }
    },
    
    
    onResize: function()
    {
        if (typeof(VideoControlJS.oldOnResize) == 'function')
        {
            VideoControlJS.oldOnResize();
        }
        
        for(var id in VideoControlJS.loadedFrames)
        {
            VideoControlJS.resizeFrame(id);
        }
    },
    
    resizeFrame: function(id)
    {
        if (VideoControlJS.resizeFrames[id])
        {
            var iframe = window.document.getElementById(id);
            var iframeDoc = (iframe.contentWindow || iframe.contentDocument);
            if (iframeDoc.document) iframeDoc = iframeDoc.document; // if we have a window reference get a document reference.

            if ((iframeDoc.readyState && (iframeDoc.readyState == 'complete')) || (!iframeDoc.readyState && iframeDoc.body))
            {
                // set the width before setting the height
                this.SetWidthToFitContent(iframe, iframeDoc);

                var docHeight = iframeDoc.body.scrollHeight;
                iframe.style.height = docHeight + 'px';
            }
            else
            {
                setTimeout("VideoControlJS.resizeFrame('"+id+"');", 5);    // delay reizeFrame() call to give FireFox a chance to load the dom.
                return;
            }
        }
    },

    SetWidthToFitContent: function(iframe, iframeDoc)
    {
        var contentWidth = null;        
            
        if (!VideoControlJS.nav6up)
        {
            if (iframeDoc.body.scrollWidth > 0)
            {
                iframeDoc.body.style.width = "100%";
                contentWidth = iframeDoc.body.scrollWidth + 2;
            }            
        }
        else
        {
            if (iframe.contentWindow.document.documentElement.scrollWidth > 0)
            {
               iframe.style.width = "10px";
                contentWidth = iframe.contentWindow.document.documentElement.scrollWidth;
            }
        }
        iframe.style.width = contentWidth + "px";
    },
   
    
    writeDocument: function(doc, content, lcid, build)
    {
        doc.open();
        
        doc.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
        doc.writeln('<html xmlns="http://www.w3.org/1999/xhtml">');
        doc.writeln('<head>');
        doc.writeln('<title>none</title>');
        doc.writeln('<link type="text/css" rel="stylesheet" href="/_layouts/' + lcid + '/wh/stylesV2/masterroot.css?b='+ build +'" />');
        doc.writeln('<link type="text/css" rel="stylesheet" href="theme.css" />'); // This file is not cached, so no versioning
        doc.writeln('<base target="_parent" />');
        doc.writeln('</head>');
        doc.writeln('<body onbeforeunload="window.top.VideoControlJS.flashKillCallback(this);" class="MSC_Body" style="border: 0px none black; margin: 0px; padding: 0px; background-color: transparent;">');
        
        doc.writeln(content);
        
        doc.writeln('</body>');
        doc.writeln('</html>');
        
        doc.close();
    }, 
    
    // flash video bug 113319
    flashKillCallback: function(ctrl)
    {
        if(VideoControlJS.nav6up == false)
        {
            if(ctrl.window["__flash__removeCallback"])
            {
                ctrl.window["__flash__removeCallback"] = function (instance, name) 
                {  
                    try 
                    {  
                        if (instance) 
                        {  
                            instance[name] = null;  
                        }  
                    } 
                    catch (flashEx) 
                    {  }  
                };
             }
         }
    }      
    
};
