fp.player = {
	
	element: null,
	blurred: false,
	playing: false,
	
	play: function(stickyInfo) {
		fp.player.playing = true;
		if(!stickyInfo) fp.hideFooter() && fp.hideHeader();
		
		$('#status-pause').animate({ opacity: 0 }).hide();
		fp.player.element.playVideo();
	},
	
	pause: function() {
		fp.player.playing = false;
		fp.showHeader() && fp.showFooter();
		
		$('#status-pause').show().animate({ opacity: 0.5 });
		fp.player.element.pauseVideo();
	},
	
	toggle: function() {
		fp.player[fp.player.playing ? 'pause' : 'play']();
	},
	
	load: function(id) {
		window.location.hash = '#'+id + '|' + fp.currentSearch + '|' + fp.index;
		fp.player.current = id;
		fp.player.element.loadVideoById(id+'&hd=1');
		fp.player.play(1);
	},
	
	prev: function() {
		var prev = $('#'+fp.player.current).prev();
		if(prev.length) {
			var meta = prev.data('meta');
			$('#header p.title').html(meta.title);
			$('#header p.author').html('by '+meta.author);
			$('#coverflow').coverflow('select', prev[0]);
			fp.player.load(prev[0].id);
		}
	},
	next: function() {
		var next = $('#'+fp.player.current).next();
		if(next.length) {
			var meta = next.data('meta');
			$('#header p.title').html(meta.title);
			$('#header p.author').html('by '+meta.author);
			$('#coverflow').coverflow('select', next[0]);
			fp.player.load(next[0].id);
		}
	},
	
	resize: function(width, height) {
		fp.player.element.setSize(width, height);
	},
	
	blur: function() {
		if(!fp.player.blurred) $('#movie-blur').animate({ opacity: 0.5 });
		fp.player.blurred = true;
	},
	
	focus: function() {
		if(fp.player.blurred) $('#movie-blur').animate({ opacity: 0 });
		fp.player.blurred = false;
	}
	
};

$(document).ready(function() {
	
	if(window.location.hash) {
		$('#footer').css('bottom', '-200px');
		var data = decodeURI(window.location.hash).substr(1).split('|');
		fp.index = parseInt(data[2]);
		$('#header input')[0].value = data[1];
		fp.retrieve(data[1], function() {

			$('#coverflow').coverflow('select', $('#'+data[0])[0]);
			var meta = $('#'+data[0]).data('meta');
			
			$('#header p.title').html(meta.title);
			$('#header p.author').html('by '+meta.author);
			$('#header div.cover').replaceWith($('#'+data[0]).clone().attr('id', '').find('div').remove().end().css({ position: 'static', height: 48, width: 72, webkitTransform: '', mozTransform: '' }));
			
		});
	}
	
});

function onYouTubePlayerReady(playerId) {

	fp.player.element = $("#movie")[0];
	fp.player.element.addEventListener("onStateChange", "onYouTubePlayerStateChange");
	
	if(window.location.hash) {
		var data = window.location.hash.substr(1).split('|');
		fp.player.load(decodeURI(data[0]));
	}
	
	var slider = $('#header div.progress'), toTime = function(t) {
		t = parseInt(t);
		var sec = t % 60, min = ((t - sec)/60);
		sec = sec.toString().length > 1 ? sec : '0'+sec;
		min = min.toString().length > 1 ? min : '0'+min;
		return min + ':' + sec;
	};
	window.setInterval(function() {
		var current = fp.player.element.getCurrentTime();
		var duration = fp.player.element.getDuration();
		document.getElementById('duration').innerHTML = toTime(current) + ' / ' + toTime(duration);
		
		if(!fp.player.draggingProgress && fp.player.playing) slider.slider('value', (current / duration) * 100, true);
		
	}, 1000);
	
};

function onYouTubePlayerStateChange(newState) {
		fp.player.playing = newState == 1 ? true : false;
		if(fp.player.playing) { fp.headerIsSticky = 0; fp.hideHeader(); fp.hideFooter(); }
};