var windowWidth;
var windowHeight;

var minWidth        = 666;
var minHeight       = 500;
var maxWidth        = 1333;
var maxHeight       = 1000;

var initialWidth    = 1000;
var initialHeight   = 750;
var initialFontSize = 0.8;
var initialViewport = 0.99;
var currentFactor   = 1;
var minFactor       = 0.675;
var maxFactor       = 1.375;
var playerWidth     = 800;
var playerHeight    = 448;

function getWindowSize( replacePlayer )
    {
    if (document.body.offsetWidth)  windowWidth  = document.body.offsetWidth;
    if (window.innerWidth)          windowWidth  = window.innerWidth;
    if (document.body.offsetHeight) windowHeight = document.body.offsetHeight;
    if (window.innerHeight)         windowHeight = window.innerHeight; 

    setViewport( replacePlayer );
    }

function setViewport( replacePlayer )
    {

    if( navigator.userAgent.indexOf( "Firefox/3" ) != -1 )
    {
        return;
    }

    var dblFactorWidth  = ( windowWidth /  initialWidth  ) * initialViewport;
    var dblFactorHeight = ( windowHeight / initialHeight ) * initialViewport;
    var dblFactor       = currentFactor;
    var dblFontFactor   = currentFactor;
    var videoWidth      = 0;
    var videoHeight     = 0;

    if( dblFactorHeight >= dblFactorWidth  )
        {
        dblFactor = dblFactorWidth;
        }
    else
        {
        dblFactor = dblFactorHeight;
        }

    var newWindowWidth  = initialWidth  * dblFactor * initialViewport;
    var newWindowHeight = initialHeight * dblFactor * initialViewport;

    if( newWindowWidth > ( windowWidth * initialViewport ) )
        {
        dblFactor = dblFactorHeight;
        }

    if( newWindowHeight > ( windowHeight * initialViewport ) )
        {
        dblFactor = dblFactorWidth;
        }

    if( currentFactor != dblFactor )
        {
        if( dblFactor > maxFactor )
            {
            dblFactor = maxFactor;
            }
        if( dblFactor < minFactor )
            {
            dblFactor = minFactor;
            }

        if( dblFactor > 1 )
            {
            //dblFontFactor = 1 + ( ( dblFactor - 1 ) * 0.8 );
            dblFontFactor = dblFactor;
            }
        else
            {
            dblFontFactor = 1 - ( ( 1 - dblFactor ) * 0.9 );
            }

        switch( ( parseInt( dblFactor * 10 ) / 10 ) )
            {
            case 1.3: { dblFontFactor = 124.01; break; }
            case 1.2: { dblFontFactor = 122.01; break; }
            case 1.1: { dblFontFactor = 114.01; break; }
            case 1.0: { dblFontFactor = 106.01; break; }
            case 0.9: { dblFontFactor = 98.01; break; }
            case 0.8: { dblFontFactor = 90.01; break; }
            case 0.7: { dblFontFactor = 78.01; break; }
            case 0.6: { dblFontFactor = 70.01; break; }
            default : { dblFontFactor = 95.01; break; }
            }

        document.body.style.fontSize = dblFontFactor + "%";

        videoWidth  = parseInt( playerWidth  * dblFactor );
        videoHeight = parseInt( playerHeight * dblFactor );

        //videoHeight += "65";

        document.getElementById( "Container" ).style.width    = ( parseInt( initialWidth  * dblFactor ) ) + 'px';
        document.getElementById( "Container" ).style.height   = ( parseInt( initialHeight * dblFactor ) ) + 'px';
        document.getElementById( "Content" ).style.width      = ( parseInt( initialWidth  * dblFactor ) ) + 'px';
        document.getElementById( "Content" ).style.height     = ( parseInt( initialHeight * dblFactor ) ) + 'px';

        //alert( 'windowWidth: ' + windowWidth + '\nwindowHeight: ' + windowHeight + '\nwindowHeight: ' + parseInt( initialWidth  * dblFactor ) + '\nwindowHeight: ' +  parseInt( initialHeight * dblFactor ) );

        // enforce container refresh in firefox
        if( ( replacePlayer && navigator.userAgent.indexOf( "Firefox/1" ) != -1 ) || ( replacePlayer && navigator.userAgent.indexOf( "Firefox/2" ) != -1 ) || ( replacePlayer && navigator.userAgent.indexOf( "Safari" ) != -1 ) )
            {
            document.getElementById( "Video" ).innerHTML = document.getElementById( "Video" ).innerHTML;
            var doSomething = document.getElementById( "Player" ).innerHTML;
            tagInternalLinks( videoWidth, videoHeight );
            }
        }
    }

function tagInternalLinks( Width, Height )
    {
    var ownUrl = self.location.hostname;
    var links  = document.links;

    for( i=0; i < links.length; i++ )
        {
        if( links[ i ].href.indexOf( ownUrl ) > 0 )
            {
            if( links[ i ].search != "" )
                {
                links[ i ].href += '&VCW=' + Width + '&VCH=' + Height;
                }
            else
                {
                links[ i ].href += '?VCW=' + Width + '&VCH=' + Height;
                }
            }
        }
    }
