$(document).ready(function() {

	function formatTitle(title, currentArray, currentIndex, currentOpts) {
    	return '<div class="gallery_title">' + (title && title.length ? '<span class="title_text">'+title+'</span' : '' ) + '<span class="image_count">Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</span><div class="clear_float"></div></div>';
	}

	function fancyboxInitialize(w, h) {
		
		var params = {
			'hideOnContentClick': true,
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'autoDimensions': false,
			'type': 'iframe',
			'width': w,
			'height': h,
			'padding': 10,
			'margin': 30,
			'titleShow': false,
			'overlayShow': true,
			'overlayOpacity': .85,
			'overlayColor': '#000',
			'showNavArrows': false,
			'cyclic': true,
			'centerOnScroll': true
		}
	
		// for photolab images…
		// for daily diary pdfs…
		// always use as much screen as possible
		$('a.photolab, a.dailydiary').fancybox(params);
		
		// for external links…
		var tW = (w<600) ? w : 600;
		var tH = (h<300) ? h : 300;
		$.extend(params, {'width':tW, 'height':tH, 'type':''});
		$('a.popup.external').fancybox(params);
		
		// for popups…
		// for media…
		$('a.popup:not(.external), a.swf_video').each(function(){
			// read the popup dimensions from the link 
			tW = $(this).attr('targetwidth');
			tH = $(this).attr('targetheight');
			// default to 600x300 if not set
			tW = (tW == undefined) ? 600 : parseInt(tW);
			tH = (tH == undefined) ? 300 : parseInt(tH);
			// limit the pop-up size to the window size
			tW = (tW>w || tW==0) ? w : tW;
			tH = (tH>h || tH==0) ? h : tH;
			$.extend(params, {'width':tW, 'height':tH});
			if ($(this).hasClass('swf_video')) {
				var flashvars = {'flashvars': $(this).attr('flashvars')}; 
				$.extend(params, {
					'swf': flashvars,
					'hideOnContentClick': false,
					'type':'swf'
				});
			};
			$(this).fancybox(params);
		});
			
	};
	
	var w = $(window).width();
	var h = $(window).height();
	fancyboxInitialize(w,h);
	
	var timer = '';
	
	$(window).resize( function(){
		clearInterval(timer);
		timer = setInterval(function() {
			var w = $(window).width();
			var h = $(window).height();
			fancyboxInitialize(w,h);
			clearInterval(timer);
		}, 500);
	});
		
		
});