
$(document).scroll(function() {
	updateViewer();
});




// automatic resizing image viewer
$(window).resize(function() {
	
	
	updateViewer();
 
	
});


function updateViewer()
{
	
		// update viewer size backpanel
	$('#viewer').width($(document).width());
	$('#viewer').height($(document).height());
	$('#viewer').css({ "top": 0 } );
	
	
	// scale swf to viewport
	//var scaleX = $(window).width() / w;
	//var scaleY = $(window).height() / h;
	
	
	var scale = 1;
	//scale=Math.min(scaleX,scaleY)-.1; // .1 keeps close button in screen
	//if(scale>1){scale=1;}
	
	
	// if iframe has contend embedded in html frame set the width and height from te embed
	// workaround for .dcr director files
	if($('iframe').contents().find("EMBED").width())
	{
		w=$('iframe').contents().find("EMBED").width();
		h=$('iframe').contents().find("EMBED").height();
	}
	//if($('iframe').contents().find("EMBED"))
	//{
	//	alert ("embedded content found");
	//	w=$('iframe').contents().find("EMBED").width();
	//	h=$('iframe').contents().find("EMBED").height();
	//}
	
	
	
	
	// set width and height of iframe content area
	$('iframe').width(parseInt(w));
	$('iframe').height(parseInt(h));
		
	// set border size
	$('#iframeborder').width(parseInt(w));
	$('#iframeborder').height(parseInt(h));
	
	
	var mwidth=$('#iframeborder').width();
	var mheight=$('#iframeborder').height();
	
	$('#loaderImg').css({ "top": parseInt(mheight/2) } );
	$('#loaderImg').css({ "left": parseInt(mwidth/2) } );
	
	
	// set viewer to current position y
	//var topOffset=$(this).scrollTop();
	//$('#viewer').css({ "top": topOffset } );
	
	// center iframeborder
	var xp=($(window).width()-parseInt(mwidth))*.5;
	var yp=(($(window).height()-parseInt(mheight))*.5)+topOffset;
	
	// closebutton must be visible always
	if (yp<50)
	{
		yp=50;
	}
	
	$('#iframeborder').css({ "top": yp } );
	$('#iframeborder').css({ "left": xp } );
	
	// close button
	$('#close').css({"top": (yp-parseInt($('#close').height())), "left" : (xp+parseInt(mwidth)-parseInt($('#close').width()))});
}

	/// called from caroussel
	var w=640;
	var h=480;
	
	function openViewer(link,mwidth,mheight)
	{
		
		if(mwidth!="" && mheight!="")
		{
			w=mwidth;
			h=mheight;
		}

		$('#loaderImg').css({ "top": parseInt(h/2) } );
		$('#loaderImg').css({ "left": parseInt(w/2) } );
		
		
		// hide viewer
		$('#viewer').fadeOut(0);
		
		// get viewer overlay div and set size
		$('#viewer').width($(window).width());
		$('#viewer').height($(window).height());
		
		// set viewer to current position y
		//var topOffset=$(this).scrollTop();
		//$('#viewer').css({ "top": topOffset } );
		
		
		// set width and height of iframe overlay to max browser size
		$('iframe').width(parseInt(mwidth));
		$('iframe').height(parseInt(mheight));
		
		// set border size
		$('#iframeborder').width(parseInt(mwidth));
		$('#iframeborder').height(parseInt(mheight));
		
		
		// center iframeborder
		var xp=($(window).width()-parseInt(mwidth))*.5;
		var yp=($(window).height()-parseInt(mheight))*.5;
		
		$('#iframeborder').css({ "top": yp } );
		$('#iframeborder').css({ "left": xp } );
		
		// fading in the viewer
		$('#viewer').fadeIn(250,function() {
			// content in iframe
			showContent(link);
			//loadContent(link)
		});
		
		// position close buttom
		
		$('#close').css({"top": (yp-parseInt($('#close').height())), "left" : (xp+parseInt(mwidth)-parseInt($('#close').width()))});
		
		$('#close').bind('click',function (){
			closeViewer();
		});
		
	
		updateViewer();
		
	}
	
	
	
	

	var topOffset;
	function showContent(link)
	{
		// get iframe element
		$('iframe').attr('src', link);
		$('iframe').show();
		
		topOffset=$(this).scrollTop();

		updateViewer();
	}
	

	function closeViewer(event)
	{
	
		$('iframe').hide();
		$('iframe').attr('src', "about:blank");
		
		$('#viewer').fadeOut(250);
		
			
	}
