﻿// sets size and position of the movie div
function fixWinSize(w,h)
{
    // adjust height
    h = h -10;

    var top = $(document).scrollTop() + document.documentElement.clientHeight/2-h/2;

    $('#smmoviecontainer').css({  
        "position": "absolute",  
        "top": top + "px",  
        "left": document.documentElement.clientWidth/2-w/2 + "px",
        "display": "block",
        "width": w + "px",
        "height": h + "px"
    });
    
    $('#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], a[href*=.mp4],a[href*=.flv.aspx], a[href*=.f4v.aspx], a[href*=.mp4.aspx]').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": $(document).height(),
                        "background-color" : "#000000",
                        "opacity" : "0.5",
                        "z-index" : "99",
                        "display" : "none"
                    }); 
                    
                    $('#smmovieplayerbackground').click(function(){
                        CloseMovie();
                    });
                }
                
                // add movie div if it does not already exist
                if($('#smmoviecontainer').length == 0)
                {
                    var header = $(this).attr('title') != "" ? $(this).attr('title') : "Movie Player";
                    $(document.body).append("<div id='smmoviecontainer'><div id='smmovietoolbar'><a href='javascript:CloseMovie();'>close</a><span>" + header + "</span> </div><div id='smmovieplayerwindow'>Starting movie...</div></div>");
                    
                    $('#smmovietoolbar').css({  
                        "background-color": "#000000",  
                        "color": "#eeeeee",
                        "display": "block",
                        "padding": "0",
                        "z-index" : "100",
                        "text-align": "left"
                        });
                    
                    $('#smmovietoolbar a').css({  
                        "float": "right",
			"margin-right": "1em"
                        });

                    $('#smmovietoolbar span').css({  
			"margin-left": "1em"
                        });

                    var wi = 200;
                    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",
                        "z-index" : "100"
                        });

                }

                // embed flash
                if(flashembed.isSupported([9,115]))
                {
                    $('#smmovieplayerwindow').flashembed(
                            {src:'/SMControls/Resources/Flash/VideoPlayer.swf',bgcolor:'#ffffff',quality:'high'},
                            {flvFile: movie ,maxHeight:'700'}
                            )
                }
                else
                {
                    $('#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;
            });
    });


});