// FancyPlayer.js - A spicy mix of FancyBox and Flowplayer

$(document).ready(function() {
	if($('#player').text()){
		function preloadImages()
		{
		  for(var i = 0; i<arguments.length; i++)
		  {
			$("<img>").attr("src", arguments[i]);
		  }
		}
		preloadImages("images/video_bg.png");
		
		var videoclip=$('#player').attr('href');
		var player='';
		$(".video_link").hover(function(){
			$(this).attr({"href":"#video_box"});
		},
		
		function(){
			$(this).attr({"href":""+videoclip+""});
		});
		
		
		var filename  = videoclip.substr(videoclip.lastIndexOf('/')+1, videoclip.length);
		var extension = videoclip.substr(videoclip.lastIndexOf('.')+1, videoclip.length);
		var match = /(avi)|(swf)|(flv)|(mp4)|(mpeg)/.test(extension.toLowerCase());
			if(match) {
				if(extension=='mpeg' || extension=='avi') {
					movie =
						'<CENTER>'
						+ '<OBJECT ID="Video" width="305" height="250" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"'
							+ 'type="application/x-oleobject" VIEWASTEXT>'
							+ '<PARAM NAME="FileName" VALUE="'+ videoclip +'">'
							+ '<PARAM NAME="ShowControls" VALUE="True">'
							+ '<PARAM NAME="ShowStatusBar" VALUE="True">'
							+ '<PARAM NAME="TransparentAtStart" Value="True">'
							+ '<PARAM NAME="AutoStart" Value="true">'
							+ '<PARAM NAME="AnimationatStart" Value="True">'
							+ '<PARAM NAME="autoSize" Value="false">'
							+ '<PARAM NAME="displaySize" Value="False">'
							+ '<PARAM NAME="enableContextMenu" Value="False">'
							+ '<PARAM NAME="uiMode" Value="invisible">'
							+ '<EMBED type="application/x-mplayer2"'
								+ 'pluginspage = "http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/"'
								+ 'SRC="'+ videoclip +'" Name="MediaPlayer1" Width="305" Height="250"'
								+ 'autoStart=1 transparentAtStart=1 animationAtStart=1 autoSize=0 ShowStatusBar=1 ShowControls=1'
								+ 'displaySize=0 enableContextMenu=0 uiMode="invisible" align="center">'
							+ '</EMBED>'
						+ '</OBJECT>'
						+'</CENTER>';
					$(".player").fancybox({
						'autoRun':playMovie,
						'hideOnContentClick':false,
						'overlayOpacity' :.6,
						'zoomSpeedIn'    :400,
						'zoomSpeedOut'   :400,
						'easingIn'		 : 'easeOutBack',
						'easingOut'		 : 'easeInBack',
						'callbackOnShow' :function(){
							$('#fancy_div').html(movie);
							$('#fancy_close').click(function(){
								$("#fancy_div_api").remove();
							});
						},
						'callbackOnClose':function(){
							$("#fancy_div_api").remove();
						}
					});
					$('#player').click(function(){
						$(this).fancybox({
							'autoRun':playMovie,
							'hideOnContentClick':false,
							'overlayOpacity' :.6,
							'zoomSpeedIn'    :400,
							'zoomSpeedOut'   :400,
							'easingIn'		 : 'easeOutBack',
							'easingOut'		 : 'easeInBack',
							'callbackOnShow' :function(){
								$('#fancy_div').html(movie);
								$('#fancy_close').click(function(){
									$("#fancy_div_api").remove();
								});
							},
							'callbackOnClose':function(){
								$("#fancy_div_api").remove();
							}
						});
					});
				}
				else if(extension=='swf') {
					movie = 
						'<center>'
						+ '<object width="425" height="344">'
						+ '<param name="movie" value="'+ videoclip +'"></param>'
						+ '<param name="allowFullScreen" value="true"></param>'
						+ '<param name="allowscriptaccess" value="always"></param>'
						+ '<embed src="'+ videoclip +'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>'
						+ '</object>'
						+'</center>';
					$(".player").fancybox({
						'autoRun':playMovie,
						'hideOnContentClick':false,
						'overlayOpacity' :.6,
						'zoomSpeedIn'    :400,
						'zoomSpeedOut'   :400,
						'easingIn'		 : 'easeOutBack',
						'easingOut'		 : 'easeInBack',
						'callbackOnShow' :function(){
							$('#fancy_div').html(movie);
							$('#fancy_close').click(function(){
								$("#fancy_div_api").remove();
							});
						},
						'callbackOnClose':function(){
							$("#fancy_div_api").remove();
						}
					});
					$('#player').click(function(){
						$(this).fancybox({
							'autoRun':playMovie,
							'hideOnContentClick':false,
							'overlayOpacity' :.6,
							'zoomSpeedIn'    :400,
							'zoomSpeedOut'   :400,
							'easingIn'		 : 'easeOutBack',
							'easingOut'		 : 'easeInBack',
							'callbackOnShow' :function(){
								$('#fancy_div').html(movie);
								$('#fancy_close').click(function(){
									$("#fancy_div_api").remove();
								});
							},
							'callbackOnClose':function(){
								$("#fancy_div_api").remove();
							}
						});
					});
				}
				else if(extension=='flv'||extension=='mp4') {
					$(".video_link").fancybox({
						'autoRun':playMovie,
						'hideOnContentClick':false,
						'overlayOpacity' :.6,
						'zoomSpeedIn'    :400,
						'zoomSpeedOut'   :400,
						'easingIn'		 : 'easeOutBack',
						'easingOut'		 : 'easeInBack',
						'callbackOnShow' :function(){
								player = $f("fancy_div",swfplayer,{
								play:{opacity:0},
								clip:{
									autoPlay:true,
									autoBuffering:true,
									url:videoclip,
									onStart:function(clip){
										var wrap=jQuery(this.getParent());
										var clipwidth = clip.metaData.width;
										var clipheight= clip.metaData.height;
										var pos = $.fn.fancybox.getViewport();
										$("#fancy_outer").css({width:clipwidth,height:clipheight});
										$("#fancy_outer").css('left', ((clipwidth + 36) > pos[0] ? pos[2] : pos[2] + Math.round((pos[0] - clipwidth	- 36)	/ 2)));
										$("#fancy_outer").css('top',  ((clipheight + 50) > pos[1] ? pos[3] : pos[3] + Math.round((pos[1] - clipheight - 50)	/ 2)));
									},
									onFinish:function(){
										$('#fancy_close').trigger('click');
									}
								}
							});
							player.load();
							$('#fancy_close').click(function(){
								$("#fancy_div_api").remove();
							});
						},
						'callbackOnClose':function(){
							$("#fancy_div_api").remove();
						}
					});
				}
			}
		}
	
}); 
