﻿$(document).ready(function() {
    
    // Inject a video placeholder into the page
    if($('#flashPlayContainer').length == 0) {
        $('body').append('<div id="flashPlayContainer" class="flashPlayContainer">&nbsp;</div>');
    }
    
 
    
});

// Function to inject the call to play the video
var lastVideoPlayer;
function playVideo(videoFile, width, height, videoContainer) {

    lastVideoPlayer = videoContainer;

    // Stop the slider whilst the video is playing.
    try
    {
     pauseSlide();
    }catch(err){}

    // Generate a quick link option to this video container, call and dispose
    $("#"+videoContainer)
    .after('<a id="link_'+videoContainer+'" style="display: hidden;" href="#'+videoContainer+'">link</a>');
    
    $("#"+videoContainer)
    .html("<a href='"+videoFile+"' style='display:block;padding:5px;width:"+ width +"px;height:"+ height +"px;' id='"+ lastVideoPlayer +"flowLink'></a>");
    
    $('#link_'+videoContainer).fancybox({
        'hideOnContentClick':   false,
        'overlayOpacity'	:	0.5,
        'overlayColor'		:	'#000',
        'frameWidth'        :   width,
        'frameHeight'       :   height,
        'transitionIn'	    :   'fadeIn',
		'transitionOut'	    :   'fadeOut',
		onStart             :   function() {

            var player = flowplayer(lastVideoPlayer+"flowLink", "Flash/FlowPlayer/flowplayer-3.2.2.swf",{
                clip: {
		            autoPlay: true,
		            autoBuffer: true,
		            onMetaData: function(clip) { 
		            
		                var clipwidth = clip.metaData.width;
                        var clipheight= clip.metaData.height;
		           
                        var aWrap = jQuery(this.getParent()); 
                        aWrap.css({width: clipwidth, height: clipheight}); 
                        
                        $("#fancybox-wrap").css({width:clipwidth+10,height:clipheight+10});
                        $("#fancybox-inner").css({width:clipwidth+10,height:clipheight+10});
                        $("#fancybox-outer").css({width:clipwidth+30,height:clipheight+30});
                        
                        var xScroll, yScroll;
                        if (self.pageYOffset) {
                            yScroll = self.pageYOffset;
                            xScroll = self.pageXOffset;
                        } else if (document.documentElement && document.documentElement.scrollTop) {
                            yScroll = document.documentElement.scrollTop;
                            xScroll = document.documentElement.scrollLeft;
                        } else if (document.body) {
                            yScroll = document.body.scrollTop;
                            xScroll = document.body.scrollLeft; 
                        }

                        var scroll = [xScroll, yScroll]; 
                        var pos = [$(window).width(), $(window).height(), scroll[0], scroll[1]];
                        
                        $("#fancybox-wrap").css('left',((clipwidth + 36)> pos[0]?pos[2]:pos[2]+Math.round((pos[0]-clipwidth-36)/2)));
                        $("#fancybox-wrap").css('top',((clipheight + 50)> pos[1]?pos[3]:pos[3]+Math.round((pos[1]-clipheight-50)/2)));
                    }  
	            }
            });
            
            player.onLoad(function() {
                // try registering a google event on playing
                try
                {
                 pageTracker2._trackEvent("Video","VideoPlay",videoFile);
                }catch(err){}
                
            });

            
            
            
        },
        onCleanup	        :	function() {
        
            if($f().isPlaying()) {
                try
                {
                 pageTracker2._trackEvent("Video","VideoCloseWhilePlaying",videoFile);
                }catch(err){}
            }
            
            if($f().getState() == -1) {
              
                try
                {
                 pageTracker2._trackEvent("Video","VideoCloseAtEnd",videoFile);
                }catch(err){}
            }

            // Destroy currently playing video
            $('#'+lastVideoPlayer).html('');
            
            // Continue playing the slideshow
            try
            {
                playSlide();
                intTimerId = setTimeout("nextSlide();", intSlideTime); 
            }catch(err){}
        }
    }); 
	
	// Automatically click the fake link to show the fancy player.
    $('#link_'+videoContainer).click();
    $('#link_'+videoContainer).remove();
}
