var j = jQuery;
var fondPlayer1;
var fondPlayer2;
var fondPlayer3;
var fontPlayer4;
var isWin = (navigator.appVersion.indexOf("Win")!=-1);
var urlName = new Array(['fm1','beslutsangest'],
						['fm2','riskfritt'],
						['fm3','ordningreda'],
						['fm4','viktigtval']);
VideoJS.DOMReady(function(){
	fondPlayer1 = VideoJS.setup("fond-fm1", {
		playerFallbackOrder: ["flash", "html5"],
		controlsBelow: false, // Display control bar below video instead of in front of
		controlsHiding: true, // Hide controls when mouse is not over the video
		defaultVolume: 0.3, // Will be overridden by user's last volume if available
		flashVersion: 9, // Required flash version for fallback
		mute: false,
		linksHiding: true // Hide download links when video is supported
   	});
   	fondPlayer2 = VideoJS.setup("fond-fm2", {
		playerFallbackOrder: ["flash", "html5"],
		controlsBelow: false, // Display control bar below video instead of in front of
		controlsHiding: true, // Hide controls when mouse is not over the video
		defaultVolume: 0.3, // Will be overridden by user's last volume if available
		flashVersion: 9, // Required flash version for fallback
		mute: false,
		linksHiding: true // Hide download links when video is supported
   	});
   	fondPlayer3 = VideoJS.setup("fond-fm3", {
		playerFallbackOrder: ["flash", "html5"],
		controlsBelow: false, // Display control bar below video instead of in front of
		controlsHiding: true, // Hide controls when mouse is not over the video
		defaultVolume: 0.3, // Will be overridden by user's last volume if available
		flashVersion: 9, // Required flash version for fallback
		mute: false,
		linksHiding: true // Hide download links when video is supported
   	});
   	fondPlayer4 = VideoJS.setup("fond-fm4", {
		playerFallbackOrder: ["flash", "html5"],
		controlsBelow: false, // Display control bar below video instead of in front of
		controlsHiding: true, // Hide controls when mouse is not over the video
		defaultVolume: 0.3, // Will be overridden by user's last volume if available
		flashVersion: 9, // Required flash version for fallback
		mute: false,
		linksHiding: true // Hide download links when video is supported
   	});
	fondPlayer1.onEnded(temp);
	fondPlayer2.onEnded(temp);
	fondPlayer3.onEnded(temp);
	fondPlayer4.onEnded(temp);
	fondPlayer1.onPlay(videoClick);
	fondPlayer2.onPlay(videoClick);
	fondPlayer3.onPlay(videoClick);
	fondPlayer4.onPlay(videoClick);
});

var btnArr = new Array();
var currVid;
j(document).ready(function(){
	


     // Must come after the video.js library
 
    // Add VideoJS to all video tags on the page when the DOM is ready
    //VideoJS.setupAllWhenReady();
 
    /* ============= OR ============ */
 
    // Setup and store a reference to the player(s).
    // Must happen after the DOM is loaded
    // You can use any library's DOM Ready method instead of VideoJS.DOMReady
 
 	j('#buttons .btns').each(function(){
 		 j(this).hover(
 			function(){
 				j(this).animate({top:'-10px'},200);
 			},
 			function(){
 				j(this).animate({top:'0px'},200);
 			}
 		);
 		btnArr.push(j(this).attr('id'));
 		
 		j(this).mouseup(function(e){

			var id = j(this).attr('id');
			currVid = id;
			for( var f=0;f<btnArr.length;f++){
				j('#'+btnArr[f]).fadeOut();
				if(id == urlName[f][0]){
					window.location.hash = urlName[f][1];
				}
			}
			j('#film-'+id).css('height','540px');
			j('#film-'+id).css('width','960px');
			j('#fond-'+id).animate({height:'540px'});
			j('#film-'+id).find('a').show(200,function(){
				//videoClick();
				var id = j(this).attr('id');
				$f(id).play();
			});
			j('#film-'+id).fadeIn(200,function(){

					//j('#fond-'+id)[0].player.play();

				j('#closevideo').show();
			});
 		});
 		for(var o=0; o< btnArr.length;o++){
 			if(('#'+urlName[o][1])==window.location.hash){
 				j('#'+urlName[o][0]).trigger('mouseup');
 				
 			}
 		}
 	});
 	
 	j('#closevideo').mouseup(function(){
 		j('.videoplayer').each(function(){
			if(fondPlayer1){fondPlayer1.pause();}
			if(fondPlayer2){fondPlayer2.pause();}
			if(fondPlayer3){fondPlayer3.pause();}
			if(fondPlayer4){fondPlayer4.pause();}

 		});
 		temp();
 	});
	/*j('#fallimg').bind('click',videoClick);
  	j('#fallimg').bind('mouseup',videoClick);    
  	j('#video-holder').bind('click',videoClick);
  	j('#video-holder').bind('mouseup',videoClick);
  	j('#video-holder > object').bind('click',videoClick);
  	j('#video-holder > object').bind('mouseup',videoClick);
*/
	$f("flash_fallback_1", "http://fondkollen.se/jslib/flowplayer-3.2.7.swf", {
			clip: {
				autoPlay:true,
				autoBuffering:true,
				onStart: function(){
					videoClick('IE');
				},
				onFinish: function() {
					temp();
				}
			}/*,
			playlist:[{url: "http://video-js.zencoder.com/oceans-clip.mp4",autoPlay:false,autoBuffering:true}]*/
		});
	$f("flash_fallback_2", "http://fondkollen.se/jslib/flowplayer-3.2.7.swf", {
			clip: {
				autoPlay:true,
				autoBuffering:true,
				onStart: function(){
					videoClick('IE');
				},
				onFinish: function() {
					temp();
				}
			}/*,
			playlist:[{url: "http://video-js.zencoder.com/oceans-clip.mp4",autoPlay:false,autoBuffering:true}]*/
		});
	$f("flash_fallback_3", "http://fondkollen.se/jslib/flowplayer-3.2.7.swf", {
			clip: {
				autoPlay:true,
				autoBuffering:true,
				onStart: function(){
					videoClick('IE');
				},
				onFinish: function() {
					temp();
				}
			}/*,
			playlist:[{url: "http://video-js.zencoder.com/oceans-clip.mp4",autoPlay:false,autoBuffering:true}]*/
		});
	$f("flash_fallback_4", "http://fondkollen.se/jslib/flowplayer-3.2.7.swf", {
			clip: {
				autoPlay:true,
				autoBuffering:true,
				onStart: function(){
					videoClick('IE');
				},
				onFinish: function() {
					temp();
				}
			}/*,
			playlist:[{url: "http://video-js.zencoder.com/oceans-clip.mp4",autoPlay:false,autoBuffering:true}]*/
		});

});

function videoClick(){
	
	j('#video-holder').css('cursor','default');
	j('#video-holder').animate({height:'540px'},400,function(){
//		j('#wplayer_api').attr('height','540');
		//j('#flash_fallback_1').attr('height','540');
		j('#video-holder').find('object').css('height','540px');
		j('#fondkollen_video').css('height','540px');
	});
}
function temp(){
	
	j('#video-holder').animate({height:'300px'},100);
	j('#closevideo').hide();
	//j('#fondkollen_video').animate({height:'300px'},200);
	//j('#video-holder').css('height','300px');
	j('#film-'+currVid).fadeOut(200,function(){
		for( var f=0;f<btnArr.length;f++){
			j('#'+btnArr[f]).fadeIn();
		}
	});
	$f('*').each(function(){
		$f().stop();
	});
}



