$(document).ready(function()
{
	$('body').browserDetection();

    initVideoListItems();
    initVideoStories();
});

function initVideoListItems()
{
    var $videos = $('#videos-wrapper');

    if ($videos.length > 0)
    {
        $videos.find('div.video').hover(
            function()
            {
                $(this).addClass('video-hover');
            },
            function()
            {
                $(this).removeClass('video-hover');
            }
        )
        .click(function()
        {
            var link = $(this).find('a').attr('href');
            window.location.replace(link);

        });
    }
}

function initVideoStories()
{
    var $stories = $('#video-stories');
    if ($stories.length > 0)
    {
        var flashReadyChecker = null;

        $stories.find('div.video').hover(
            function()
            {
                $(this).find('.arrow').css('background-image', 'url(image/video/play.button.hover.png)');
            },
            function()
            {
                $(this).find('.arrow').css('background-image', 'url(image/video/play.button.png)');
            }
        )
        .click(function()
        {
            popup.open('popup-videoplayer');


            var source = $(this).find('div.metadata .source').text();
            var flashObject = $('#popup-videoplayer object').get(0);

            flashReadyChecker = setInterval(function()
            {
                if (typeof flashObject.setSource != 'undefined')
                {
                    $('#popup-videoplayer .close').unbind("click", stopFlash);
                    $('#popup-videoplayer .close').bind("click", stopFlash);
                    flashObject.setSource(source);
                    clearInterval(flashReadyChecker);
                }
            }, 1000);

        });
    }

    var $viewMore = $stories.children('.view-more');
        $viewMore.click(function()
        {
            $(this).hide();
            $stories.find('div.video').removeClass('hidden');
            $stories.find('.videos-with-hidden').removeClass('videos-with-hidden');

        });

}

function stopFlash()
{
    var flashObject = $('#popup-videoplayer object').get(0);
    if (typeof flashObject.stopVideo != 'undefined')
    {
        flashObject.stopVideo();
    }
}