$(function(){
	$('#close').click(function(event){
		event.preventDefault();
		hideVideo();
	});

	$(window).resize(function(){
		if ( $('#cont').offset().left > 0 ) {
			if ( $('#hider').is(':visible') ) {
				placeVideo();
			} else {
				$('#cont').css('left', $('.video_box').offset().left);
			}
		}
	
	});

});

function toggleHider(){
	$('#hider').css({
		top: 0,
		left: 0,
		height: $(document).height(),//'100%',
		width: '100%',
		opacity: 0.5
	})
	.toggle();
}

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function ytGraphicSetup() {
	$('.yt_video').slideToggle();
	$('.yt_video a').css({'background': 'white url(/i/play_off.jpg) top left no-repeat'});
	$('.yt_video a').hover(
		function(){
			$(this).css('background-image', $(this).css('background-image').replace(/off/, 'on'));
		},
		
		function(){
			$(this).css('background-image', $(this).css('background-image').replace(/on/, 'off'));
		}
	);
}

function setVideoClicks(){
	$('.yt_video a, a.yt_plain').click(function(event){
		event.preventDefault();
		var $daLink = $(this);
		var youTubeId = $daLink.attr('id');
		var videoId = 'video' + parseInt($('.yt_video a, a.yt_plain').index($daLink));
		if ( $daLink.hasClass('yt_slide') ) { //slide in video

			if ( $daLink.closest('div').next('.video_box').is(':visible') ) { //already playing
				hideVideo();
			} else { //not already playing
				stopAndHidePlayer();
				$('#hider').hide();
				$('.video_box[id!=' + videoId + ']').slideUp(function(){
					$(this).remove();	
				});

				var d = $('<div>')
				.addClass('video_box')
				.attr('id', videoId)
				.insertAfter($daLink.closest('div'))
				.slideDown(function(){
					var os = d.offset();
					$('#cont').css({
						left: os.left,
						top: os.top
					});
					$('#player').get(0).loadVideoById(youTubeId, 1, 'hd720');
				});
			} 

		} else { //dtl video

			if ($('#cont').offset().left < 0) {
				placeVideo();
				document.getElementById('player').loadVideoById(youTubeId, 1, 'hd720');
			} else {
				hideVideo();		
			}

		}
	});
}

function stopAndHidePlayer(){
	$('#player').get(0).stopVideo();
	$('#cont').css('left', -100000);
}

function hideVideo(){
	stopAndHidePlayer();
	$('.video_box').slideUp(function(){
		$('.video_box').remove();
	}); 
	$('#hider').slideUp(1);
}

function placeVideo() {
	var wc = $('#cont');
	var tPlace = $(window).scrollTop() + (($(window).height() / 2) - (wc.height()/2));

	var cw = $('#cont').width();
	var ww = $(window).width();
	var nw = (ww/2) - (cw/2);

	$('#hider').css({
		top: 0,
		left: 0,
		height: $(document).height(),//'100%',
		width: '100%',
		opacity: 0.5
	})
	.slideDown();	

	$('#cont').animate({"left": nw, "top": tPlace}, "1000");
}

