// FancyPlayer.js - A spicy mix of FancyBox and Flowplayer

jQuery(document).ready(function() {

	var videoclip;
	var player;
	var vidid;
	var captions;
	var capShow;
	var ccbutShow;
	
	jQuery(".video_link").hover(function(){
		vidid = jQuery(".video_link").index(this);
		videoclip = jQuery(".video_link").eq(vidid).attr("name");
		if (jQuery(".video_link").eq(vidid).hasClass('captions')) {
		captions = jQuery(".video_link").eq(vidid).attr("name")+'.srt';
		capShow = true;
		ccbutShow = { width: 20, height: 15, right: 5, bottom: 32, label: 'SUBS' };
		} else { 
		captions = 'videos/empty.srt';
		capShow = false;
		ccbutShow = null;
		}
	});
	
	jQuery(".video_link").fancybox({
		'hideOnContentClick':false,
		'overlayOpacity' :.6,
		'zoomSpeedIn'    :400,
		'zoomSpeedOut'   :400,
		'easingIn'		 : 'easeOutBack',
		'easingOut'		 : 'easeInBack',
		
		'onComplete' :function(){

			if (videoclip == 'image') {
				
				jQuery("#fancybox-right, #fancybox-left").css({height:jQuery("#fancybox-div").height(), bottom: '0'});
				
            } else {
				player = flowplayer("fancybox-content",{src: swfplayer, wmode: 'opaque'},{
							
				play:{opacity:0},
			  //key: '#$flowplayerkeycode',

				plugins: {
					
				/*
				captions: { 
					url: swfcaptions, 					 
					// pointer to a content plugin (see below) 
					captionTarget: 'content',
					showCaptions: capShow,
					button: ccbutShow
				},
				 */
				/* 
					configure a content plugin so that it  
					looks good for showing subtitles 
				*/
				content: {
					url:swfcontent,
					bottom: 25, 
					height:40, 
					backgroundColor: 'transparent', 
					backgroundGradient: 'none', 
					border: 0, 
					textDecoration: 'outline', 
					style: {  
						body: {  
							fontSize: 16,  
							fontFamily: 'Arial', 
							textAlign: 'center', 
							color: '#ffffff' 
						}  
					}  
				},
				
				controls:  {
				backgroundColor: 'transparent',
				progressColor: 'transparent',
				bufferColor: 'transparent',
				all:false,
                                //fullscreen:true,
				scrubber:true,
				volume:true,
				mute:true,
				play:true,
				height:30,
				autoHide: 'always'		

				}

				},
				clip:{
					autoPlay:true,
					autoBuffering:true,
					url:videopath+videoclip+'',
					captionUrl: videopath+captions+'', 
					onStart:function(clip){
						var wrap=jQuery(this.getParent());
						var clipwidth = clip.metaData.width;
						var clipheight= clip.metaData.height;
						var pos = jQuery.fn.fancybox.getViewport();
						jQuery("#fancybox-outer").css({width:clipwidth+20,height:clipheight+20});
						jQuery("#fancybox-outer").css('left', ((clipwidth + 36) > pos[0] ? pos[2] : pos[2] + Math.round((pos[0] - clipwidth	- 36)	/ 2)));
						jQuery("#fancybox-outer").css('top',  ((clipheight + 50) > pos[1] ? pos[3] : pos[3] + Math.round((pos[1] - clipheight - 50)	/ 2)));
						jQuery("#fancybox-right, #fancybox-left").css({height:clipheight-60, bottom: '70px'});
						
					},
					onFinish:function(){
						jQuery('#fancybox-close').trigger('click');
					}
				}
			});
			
			player.load();
			
		}
			
			jQuery('#fancybox-right, #fancybox-right_ico').click(function(){
				vidid++;
				videoclip = jQuery(".video_link").eq(vidid).attr("name");
				if (jQuery(".video_link").eq(vidid).hasClass('captions')) {
				captions = jQuery(".video_link").eq(vidid).attr("name")+'.srt';
				capShow = true;
				ccbutShow = { width: 20, height: 15, right: 5, bottom: 32, label: 'CC' };
				} else {
				captions = 'videos/empty.srt';
				capShow = false;
				ccbutShow = null;
				}
			});
			
			jQuery('#fancybox-left, #fancybox-left_ico').click(function(){
				vidid--;
				videoclip = jQuery(".video_link").eq(vidid).attr("name");
				if (jQuery(".video_link").eq(vidid).hasClass('captions')) {
				captions = jQuery(".video_link").eq(vidid).attr("name")+'.srt';
				capShow = true;
				ccbutShow = { width: 20, height: 15, right: 5, bottom: 32, label: 'SUBS' };
				} else {
				captions = 'videos/empty.srt';
				capShow = false;
				ccbutShow = null;
				}
			});
		},
		'callbackOnClose':function(){
			jQuery("#fancybox-content_api").remove();
		}
	});
	
}); 
