﻿// sets size and position of the movie div
function fixWinSize(w,h)
{
    // adjust height
    h = h -10;

    $('#smmoviecontainer').css({  
        "position": "absolute",  
        "top": document.documentElement.clientHeight/2-h/2 + "px",  
        "left": document.documentElement.clientWidth/2-w/2 + "px",
        "display": "block"
    });
    
    $('#smmovieplayerwindow').css({  
        "width": w + "px",
        "height": h + "px",
        "display": "block"
    });
}

// called by movieplayer
function contentSize(winsize) {
    var w = winsize.split(":")[0];
    var h = winsize.split(":")[1];
    fixWinSize(w,h);
}

// opening movie window
function OpenWindow()
{
    $('#smmoviecontainer').fadeIn("fast");
    $('#smmovieplayerbackground').fadeIn("fast");
}

// closing movie
function CloseMovie()
{
    $('#smmoviecontainer').fadeOut("fast");
    $('#smmovieplayerwindow').text("");
    $('#smmovieplayerbackground').fadeOut("fast");
}

// adding and handling click events to flv links
$(document).ready(function() {
                            
    $('a[href*=.flv], a[href*=.f4v]').each(function(){
            $(this).click(function(){
                var movie = $(this).attr('href');
                
                // add movie background div if it does not already exist
                if($('#smmovieplayerbackground').length == 0)
                {
                    $(document.body).append("<div id='smmovieplayerbackground'>background</div>");
                    $('#smmovieplayerbackground').css({  
                        "position": "absolute",  
                        "top": "0px",  
                        "left": "0px",
                        "width": "100%",
                        "height": "100%",
                        "background-color" : "#000000",
                        "opacity" : "0.1",
                        "display" : "none"
                    }); 
                    
                    $('#smmovieplayerbackground').click(function(){
                        CloseMovie();
                    });
                }
                
                // add movie div if it does not already exist
                if($('#smmoviecontainer').length == 0)
                {
                    $(document.body).append("<div id='smmoviecontainer'><div id='smmovietoolbar'><a href='javascript:CloseMovie();'>x</a></div><div id='smmovieplayerwindow'>download flash player</div></div>");
                    
                    $('#smmovietoolbar').css({  
                        "background-color": "#000000",  
                        "color": "#eeeeee",
                        "display": "block",
                        "padding": "0 1em",
                        "text-align": "right"
                        });
                }
                
                // embed flash
                if(flashembed.isSupported([9,115]))
                {
                    $('#smmovieplayerwindow').flashembed(
                            {src:'/SMControls/Resources/Flash/VideoPlayer.swf',bgcolor:'#ffffff',quality:'high'},
                            {flvFile: movie ,maxHeight:'700'}
                            )
                }
                else
                {
                    var wi = 300;
                    var hi = 150;
                    $('#smmoviecontainer').css({  
                        "position": "absolute",
                        "background-color": "#ffffff",  
                        "top": document.documentElement.clientHeight/2-hi/2 + "px",  
                        "left": document.documentElement.clientWidth/2-wi/2 + "px",
                        "width": wi + "px",
                        "height": hi + "px"
                        });
                    $('#smmovieplayerwindow').css({  
                        "padding": "2em"
                        });
                    $('#smmovieplayerwindow').html("Your browser does not support Flash 9. Please download from <a href='http://www.adobe.com/go/getflashplayer' target='_blank'>Adobe</a>");
                }
                // show windows
                OpenWindow();
                return false;
            });
    });


});