$(document).ready(function(){
	
	//player
	var _played = 0,
		_duration = 0;
	
	jwplayer("vid").setup({
		//debug: 'console',
		flashplayer: '/wp-content/themes/amberes/js/player/player.swf',
		file: 'http://www.youtube.com/watch?v=EqYOIw7nwrU',
		image: '/wp-content/themes/amberes/css/img/home/video_placeholder.jpg',
		"youtube.quality": "highres",
		height: 551,
		width: 980,
		stretching: 'fill',
		dock: true,
		autostart: false,
		controlbar: true,
		icons: false,
//		mute: true,
		plugins: {
			'hd-2': {
				'fullscreen': 'true'
			}
		},
		provider: 'youtube',
		events: {
			onTime: function(obj) {
				//console.log('fire')
				//console.log(obj.position)
				//console.log(obj);
				var pl = obj.position;
				if(pl > _played){
					$('#play').addClass('pause');
				}

				var percentage = (100/obj.duration)*obj.position;

				$('#played').css('width', percentage+'%');
				_played = pl;
				_duration = obj.duration;
			},
			onBufferChange: function(obj) {
				//console.log(obj);
				$('#loaded').css('width', obj.bufferPercent+'%');
			},
			onComplete: function(obj) {
				if($('#play').hasClass('pause')){
						$('#played').css('width', '100%');
						$('#play').removeClass('pause');
				}
			}
		}
	});
	
	$('#play').click(function(){
		if($(this).hasClass('pause')){
			jwplayer().play();
			$(this).removeClass('pause')
		} else {
			jwplayer().pause();
			$(this).addClass('pause');
		}
	});
	
	$('#fullscreen').click(function(){
		jwplayer().setFullscreen();
	})
	
	$('#duration div').click(function(e){
		var offset = $(this).offset();
		var x = e.pageX - offset.left;
		//var width = $(this).width();
		var width = 902;
		var c_perc = (100/width)*x;
		var go_to = _duration*(c_perc/100);
		console.log(go_to);
		jwplayer().seek(go_to);
	});
	
	//$('#played').click(function(e){
	//	var offset = $(this).offset();
	//	var x = e.pageX - offset.left;
	//	//var width = $(this).width();
	//	var width = 902;
	//	var c_perc = (100/width)*x;
	//	var go_to = _duration*(c_perc/100);
	//	$('#vid').jwPlayer('seek', go_to);
	//})
	
	$('#vid').click(function(){
		if($('#play').hasClass('pause')){
			$('#play').removeClass('pause')
		} else {
			$('#play').addClass('pause');
		}
	})
	
	// $('#vid').jwPlayer({
	// 		debug: 'console',
	// 		swf: '/js/player/player.swf',
	// 		file: 'http://www.youtube.com/watch?v=jvDZLE4BIVg',
	// 		image: 'http://i.ytimg.com/vi/jvDZLE4BIVg/0.jpg',
	// 		height: 550,
	// 		width: 980
	// 	})

})
