var fancyCenterHandler = false;
var isiPad = navigator.userAgent.match(/iPad/i) != null;

function fancyCenter( )
{
	//$.fancybox.center();
	
	setTimeout( 'fancyCenter()', 500 );
}

$(document).ready(function () {
	
	if( $(window).width() < 610)
	{
		var fb_width = '100%';
	} else {
		var fb_width = '640';
	}
	
	$('a.fancybox, div#bookshelf-content ul li a').fancybox({
		'titleShow'     : false,
		'padding'		: '0',
		'width'			: fb_width,
		'height'		: 'auto',
		'autoScale'     : true,
		'type'			: 'ajax',
		'centerOnScroll': 'true',
		ajax : {
			'typ'		: 'GET',
			'cache'		: false
		},
		onComplete		: function() {
			if ($.browser.msie) {
				//$("div.fb-title, div.fb-content").textShadow();
			}
			if( $(this).has('div.fb-youtube').length )
			{
				// Calculate ratio of youtube video with the size of the window (560 × 349)
				var videoWidth = ( $("div.fb-content").width() - 80 );
				var videoRatio = videoWidth / 560;
				var videoHeight = videoRatio * 349;
			
				$("div.fb-youtube embed, div.fb-youtube object").width( videoWidth ).height( videoHeight );
			} else {
				if( $('div#fancybox-wrap').has('embed, object').length ) {
					// Calculate ratio of youtube video with the size of the window (560 × 349)
					var videoWidth = $("div#fancybox-wrap embed, div#fancybox-wrap object").width();
					var videoRatio = videoWidth / 560;
					var videoHeight = videoRatio * 349;
			
					$('div#fancybox-wrap embed, div#fancybox-wrap object').width( videoWidth ).height( videoHeight );
				}
			}
			
			if( $('ul#fb-slider').width() )
			{
				// Calculate ratio of youtube video with the size of the window (560 × 349)
				var viewportWidth = ( $('div.fb-preview').width() );
				var viewportRatio = viewportWidth / 560;
				var viewportHeight = viewportRatio * 330;
			
				//$('div.fb-preview').width( viewportWidth ).height( viewportHeight );
				
				//alert(viewportWidth + ' -- ' + viewportHeight);
			}
			
			var imgHeight = $('div.fb-preview').height();
			var imgWidth = $('div.fb-preview').height();
			
			//Fullscale images in the slider
			var imgNewHeight = $('#fb-slider img').height();
			var newHeight = ( imgHeight - imgNewHeight );
			
			//$('#fb-slider img').css('margin-top', newHeight / 2 ).css('display', 'none').css('visibility', 'visible').fadeIn('fast');
			
			fancyCenterHandler = setTimeout( 'fancyCenter()', 500 );

			$('#fb-slider').fbSlider( { width: viewportWidth });
			$('#fancybox-content').css('visibility', 'visible');
			$.fancybox.resize();			
			
		},
		onCleanup		: function() {
			clearTimeout( fancyCenterHandler );
			$('div.fb-youtube embed, div.fb-youtube object').css( 'visibility', 'hidden' );
			$('#fancybox-content').css('visibility', 'hidden');
		}
	});
});
