﻿function PlayVideo(video) 
{
    if(video != null)
    {
        $f("player", 
            { 
                src: "/flash/flowplayer.commercial-3.1.5.swf",
                onFail: function() {
                    var flashMessage = '<div id="flash_message">' +
                                       '<h2>Flash is required to view this video</h2><br /><br />' +
                                       '<h3>You have no Flash plugin installed</h3><br /><br /> ' +
                                       '<p>Download latest version from <a id="adobe_link" href="javascript:warn_on_leave(\'http://get.adobe.com/flashplayer/\');">here</a></p>' +
                                       '</div>';        
                
                    document.getElementById("player").innerHTML = flashMessage;
                    
                    FixAdobeLink();
                }
            },
            {
            key: "#@5b3a29c9d09774b8bd5",
            clip: {
                    url: video,
                    provider: 'rtmp'
            },
            
            plugins: {
                      rtmp: {
                             url: '/flash/flowplayer.rtmp-3.1.3.swf',
                             netConnectionUrl: 'rtmpt://abbott.fcod.llnwd.net/a1915/o18/bfl/'
                      }
            }
        });  
    }
}

//due to a bug in Flowplayer/Firefox, see: http://flowplayer.org/forum/8/16333
function FixAdobeLink()
{
    if($.browser.mozilla)
    {
        $("a#adobe_link").click(function() { warn_on_leave('http://get.adobe.com/flashplayer/'); });    
    }    
}

function ChangeVideoPage(id) 
{
    var currentPage = parseInt(id.substring(5));
    var lastPage = $("ul#paging > li:not(\".edge_button\")").length - 1;
    
    $("div#video_container div.video_gallery_page").css("display", "none");
    
    var pageToShow = "div#video_container div#video-" + id;
    $(pageToShow).css("display", "block");

    $("ul#paging > li#page-previous").unbind();
    $("ul#paging > li#page-previous").click(function() {
        ChangeVideoPage("page-" + (currentPage - 1));
    });

    $("ul#paging > li#page-next").unbind();
    $("ul#paging > li#page-next").click(function() {
        ChangeVideoPage("page-" + (currentPage + 1));
    });
    
    ChangePage(currentPage, lastPage);  
}

function ScaleVideoThumbnail(image)
{
    // standard width
    var standardWidth = 143;

    var height = $(image).height();
    var width = $(image).width();
    var ratio;
    var calcHeight;

    if(width > standardWidth) 
    {
        ratio = height / width;
                
        calcHeight = ratio * standardWidth;        
    
        $(image).css("width", standardWidth + "px");
        $(image).css("height", calcHeight + "px");
    }
}

$(document).ready(function() {

    $("div.video_gallery_page td:nth-child(3n)").css("padding-right", "0px"); 

    $("img.video_thumbnail").each(function() {
        ScaleVideoThumbnail(this);        
    });

    AddPagingHoverEffects();    
    
    $("ul#paging > li.edge_button").click(function() {
        if(this.id == "page-first")
        {
            ChangeVideoPage("page-0");
        }
        else if(this.id == "page-last")
        {   
            var last = $("ul#paging > li:not(\".edge_button\")").length - 1;
            ChangeVideoPage("page-" + last);
        }
    });

    $("ul#paging > li:not(\".edge_button\")").click(function() {
        ChangeVideoPage(this.id);
    });
    
    if(ReadPagingUrl() > -1)
    {
        var pg = ReadPagingUrl();
        ChangeVideoPage("page-" + pg);
    }
    else
    {        
        ChangeVideoPage("page-0");
    }
});
