﻿
if( typeof(_videoControlFunctions)=="undefined" )
{
    _videoControlFunctions = true;
    _videoControls = Array();
    
    function GetEmbedElement(videoId)
    {
        var videoPanel = document.getElementById(_videoControls[videoId]);
        return videoPanel.getElementsByTagName("embed")[0];
    }
    
    // Show the video pop-up
    function ShowVideoPopup(videoId, videoUrl, popupTitle)
    {
    
        var videoPanel = document.getElementById(_videoControls[videoId]);
        var embedElement = GetEmbedElement(videoId);
    
        // If the Url is specified, re-render the video panel
        if(videoUrl)
        {
            // If this param has been passed, reassign the Url of the video to be displayed
            var flashvars = embedElement.attributes["flashvars"].value.split('&');
            for(key in flashvars)
            {
                if( flashvars[key].substring(0,2) == "f=" )
                {             
                    if( flashvars[key] != "f=" + videoUrl )
                    {
                        flashvars[key] = "f=" + videoUrl;
                        urlHasChanged = true;
                    }
                    
                }
            }
            
            embedElement.attributes["flashvars"].value = flashvars.join('&');
        }
        
        // Re-render the embed element -> gets around the issue where the control has to be "activated" in IE
        //& the fact that IE ignores the flashvar changes
        embedElement.parentNode.innerHTML = embedElement.parentNode.innerHTML;

        
    
        // If this is passed the change the title too
        if( popupTitle )
        {
            videoPanel.getElementsByTagName("span")[0].innerHTML = popupTitle;
        }
    
        // Position in centre of screen
        var embedElement = GetEmbedElement(videoId);
        showdeadcenterdiv(embedElement.width,embedElement.height,_videoControls[videoId]);
    
        // display
        videoPanel.attributes["class"].value = videoPanel.attributes["class"].value + " popup_visible";                

    }
    
    // close the video pop-up
    function CloseVideoPopup(videoId)
    {
        document.getElementById(_videoControls[videoId]).attributes["class"].value = 
            document.getElementById(_videoControls[videoId]).attributes["class"].value.replace(/popup_visible/g,'');

    }

    // Initialise the control
    function AddVideoPopupControl(videoId, clientSideControlId)
    {
        _videoControls[videoId] = clientSideControlId;
    }

    // Assign handlers to window events for resize & scroll    
    window.onresize = function()
    {
        for(val in _videoControls)
        {
            var embedElement = GetEmbedElement(val);
            showdeadcenterdiv(embedElement.width,embedElement.height,_videoControls[val]);
       }
    }
     
    window.onscroll = function()
    {
        for(val in _videoControls)
        {        
            var embedElement = GetEmbedElement(val);
            showdeadcenterdiv(embedElement.width,embedElement.height,_videoControls[val]);

        }
    }
     
    
    
    // Utility to move a DIV to the centre of the screen
    function showdeadcenterdiv(Xwidth,Yheight,divid)
    { 
        // First, determine how much the visitor has scrolled 

        var scrolledX, scrolledY; 
        if( self.pageYoffset ) { 
        scrolledX = self.pageXoffset; 
        scrolledY = self.pageYoffset; 
        } else if( document.documentElement && document.documentElement.scrollTop ) { 
        scrolledX = document.documentElement.scrollLeft; 
        scrolledY = document.documentElement.scrollTop; 
        } else if( document.body ) { 
        scrolledX = document.body.scrollLeft; 
        scrolledY = document.body.scrollTop; 
        } 

        // Next, determine the coordinates of the center of browser's window 

        var centerX, centerY; 
        if( self.innerHeight ) { 
        centerX = self.innerWidth; 
        centerY = self.innerHeight; 
        } else if( document.documentElement && document.documentElement.clientHeight ) { 
        centerX = document.documentElement.clientWidth; 
        centerY = document.documentElement.clientHeight; 
        } else if( document.body ) { 
        centerX = document.body.clientWidth; 
        centerY = document.body.clientHeight; 
        } 

        // Xwidth is the width of the div, Yheight is the height of the 
        // div passed as arguments to the function: 
        var leftoffset = scrolledX + (centerX - Xwidth) / 2; 
        var topoffset = scrolledY + (centerY - Yheight) / 2; 
        // The initial width and height of the div can be set in the 
        // style sheet with display:none; divid is passed as an argument to // the function 
        var o=document.getElementById(divid); 
        var r=o.style; 
        //r.position='absolute'; 
        r.top = (topoffset -100) + 'px'; 
        r.left = (leftoffset - 30) + 'px'; 

    } 


}
