﻿google.load("swfobject", "2.1");

var videoIDs = new Array("0zUdQzW2eYI", "VQC1pBQBVto", "IcKq7wRWE80", "mHusN2K9PJU");

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
    var _randomIndex = Math.floor(Math.random() * 4);

    ytplayer = document.getElementById("ytPlayer");
    ytplayer.cueVideoById(videoIDs[_randomIndex]);    
    
    ytplayer.addEventListener("onStateChange", "onVideoEnded");

    var _autoplay = $.cookie('autoplay');
    if (_autoplay == 'true' || _autoplay == '') {
        $('#video_autoplay').attr('checked', 'true');
        ytplayer.playVideo();
    }
}

function loadPlayer() {
    var params = { allowScriptAccess: "always" };
    var atts = { id: "ytPlayer" };
    swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                           "&enablejsapi=1&playerapiid=player1",
                           "videoDiv", "278", "190", "8", null, null, params, atts);

}

function onVideoEnded(videoState) {
    if (videoState == 0) {
        ytplayer = document.getElementById("ytPlayer");
        ytplayer.seekTo(0, false);
        ytplayer.pauseVideo();
    }
}

function _run() {
    loadPlayer();
}

google.setOnLoadCallback(_run);

$(function() {
    $('#video_player').resizable({
        aspectRatio: 312 / 281,
        minHeight: 281,
        minWidth: 312,
        stop: function(event, ui) {
            if (ui.size.width != 312)
                $(this).css('background-image', 'none');
            else
                $(this).css('background-image', 'url("images/video_player_bg.gif")');

            //resize the video screen
            var _resizeRatio = ui.size.height / 281;

            if (ytplayer) {
                ytplayer.height = parseInt(190 * _resizeRatio);
                ytplayer.width = parseInt(278 * _resizeRatio);
            }
        }
    });

    $('#video_player').draggable({ zIndex: 1000 });

    $('.video_control_button:eq(0)').click(function() {
        if (ytplayer) {
            ytplayer.playVideo();
        }
    });

    $('.video_control_button:eq(1)').click(function() {
        if (ytplayer) {
            ytplayer.pauseVideo();
        }
    });

    $('#video_autoplay').click(function() {
        if ($(this).attr('checked')) {
            $.cookie('autoplay', 'true');
        } else {
            $.cookie('autoplay', 'false');
        }
    });

    $('.video_state_buttons > img:first').click(MinimizeVideoPlayer);
    $('.video_state_buttons > img:last').click(function() {
        $('#video_player').fadeOut(500);
    });
});


function MinimizeVideoPlayer() {
    $('#video').hide();
    $('#video_controls').hide();
    $('#video_player').css('left', '0px').animate({ height: '15px', width: '156px' }, 250, function() {
        $('.video_state_buttons > img:first').unbind('click').click(RestoreVideoPlayer);
    });
}

function RestoreVideoPlayer() {
    $('#video_player').css('left', '-48px').animate({ height: '281px', width: '312px' }, 250, function() {
        $('#video').show();
        $('#video_controls').show();
        $('.video_state_buttons > img:first').unbind('click').click(MinimizeVideoPlayer);
    });
}
