//registers the player is closer for loading
var playerUnfolded = false;

$(document).ready(function () {
    $(".videoThumbTitle").fadeTo(1, 0.01);
    $(".videoLink").fadeTo(1, 0.5);
    vimeo_player_loaded();
    var vimeo_video_title;
    //$('#videoPlayerViewPort').fadeTo(2000,1);



    /* !Replace current player with autoplay element */

    $('#videoMainPlay').click(function () {
        vimeo_video_title = $('#currentPlaylist td:first a').attr('title');
        var videoId = $(this).attr('rel');
        $('#videoPlayerViewPort').fadeTo(500, 0, function () {


            $('#videoPlayerSwf').html('<iframe src="http://player.vimeo.com/video/' + videoId + '?&amp;server=vimeo.com&amp;show_title=0&amp;autoplay=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1&amp;loop=0&amp;api=1&amp;player_id=player1" width="976" height="549" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>');



            $('#videoMainPlayHolder').stop().fadeOut(500, 0, function () {

                $('#videoDetailsWrapper').stop().fadeTo(500, 0, function () {

                    $('#videoPlayerSwf').stop().animate({
                        top: '0px'
                    });

                    $('#videoPlayerViewPort').animate({
                        height: '549px'
                    }, 1000);

                    $('#videoDetailsWrapper').animate({
                        top: '-526px'
                    }, 10, function () {
                        playerUnfolded = true;
                    });
                });

            });
            vimeo_player_loaded()

        });
    })

    /* !Playlist dropdown function */
    $('#playListSelection').change(function () {

        $('#currentPlaylist').fadeTo(600, 0, function () {
            //alert($('#playListSelection').val())
            var fileToLoad = 'faux_ajax_playlists/' + $('#playListSelection').val();

            $('#currentPlaylist tr').load(fileToLoad, function () {
                $('#scrollbar1').tinyscrollbar({ axis: 'x', size: 976, scroll: false });
                $('#currentPlaylist').fadeTo(600, 1)

            });
        });


    });
    /* !Show details */

    $('#videoPlayerSwf').mouseover(function () {
        if (playerUnfolded == true) {

            $('#videoDetailsWrapper').fadeTo(500, 1);
        }
    }).mouseout(function () {
        if (playerUnfolded == true) {
            $('#videoDetailsWrapper').fadeTo(500, 0);
        }
    })


    /* !Playlist thumbnail link */


    $(".videoLink").live("mouseover mouseout", function (event) {
        if (event.type == "mouseover") {

            $(this).css({ opacity: 1 });


            //var hover_video_title = $(this).attr('title');

            $(this).children(".videoThumbTitle").css({ opacity: 1 });

        } else {
            $(this).css({ opacity: 0.5 });

            // $(this).stop().fadeTo(200, 0.5);
            $(this).children(".videoThumbTitle").css({ opacity: 0 });

        }
    });

    $(".videoThumbTitle").live("mouseover mouseout", function (event) {
        if (event.type == "mouseover") {
            //$(this).children(".videoLink").stop().fadeTo(1, 1);


            //var hover_video_title = $(this).attr('title');

            // $(this).stop().fadeTo(1, 1);


        } else {
            // $(this).stop().fadeTo(2000, 1);
            //  $(this).stop().children(".videoLink").fadeTo(2000, 1);
            //$(this).css({ opacity: 0.5 });

        }
    });






    $('.videoLink').live('click', function (event) {

        event.preventDefault();

        vimeo_video_code = $(this).attr('href');
        vimeo_video_title = $(this).attr('title');
        vimeo_video_image = $(this).attr('rel');

        $('#videoMainPlayHolder').css('background-image', 'url("' + vimeo_video_image + '")');

        $('#videoDetailsWrapper').text(vimeo_video_title);

        $('#videoMainPlay').attr('rel', vimeo_video_code);

        /*
        var val = $('embed').attr('src').replace(/clip_id=(?=\d).\d+/g, 'clip_id=' + vimeo_video_code);


        $('embed').attr('src', val);
        //alert($('embed').attr('src'));


        $('#videoPlayerSwf').html($('#videoPlayerSwf').html())
        */

        if (playerUnfolded == false) {
            $('#videoPlayerSwf').html('<iframe src="http://player.vimeo.com/video/' + vimeo_video_code + '?&amp;server=vimeo.com&amp;show_title=0&amp;autoplay=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1&amp;loop=0&amp;api=1&amp;player_id=player1" width="976" height="549" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>');

        } else {
            $('#videoPlayerSwf').html('<iframe src="http://player.vimeo.com/video/' + vimeo_video_code + '?&amp;server=vimeo.com&amp;show_title=0&amp;autoplay=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1&amp;loop=0&amp;api=1&amp;player_id=player1" width="976" height="549" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>');
        }





    });

    $('#scrollbar1').tinyscrollbar({ axis: 'x', size: 976, scroll: false });


});
        
    

/* Vimeo event for when the video player is ready to be used */
function vimeo_player_loaded() {

	if(playerUnfolded == false){

		$('#videoPlayerViewPort').fadeTo(2000,1);
		$('#videoDetailsWrapper').fadeTo(2000,1);
	
		$('#videoControls').fadeTo(3000,1);
		
	}else{
	
			$('#videoPlayerViewPort').fadeTo(2000,1);

	}

}   
