
function on_fastview_init()
	{
	$("body").append( $.ajax( { type: "GET", url: "/fastview/fastview.xml", async: false } ).responseText );
	$("#fastview_window").click( on_fastview_close );
	}

function on_fastview_open( url, title )
	{
	var re_img = /\.jpg$|\.jpeg$|\.png$|\.gif$/;
	if ( url.toLowerCase().match( re_img ) )
		{
		$("#fastview_cover").show();
		
		$("#fastview_content").css( { width: 500, height: 200 } );
		$("#fastview_content").html( "<b>Загрузка изображения...</b>" );
		
		$("#fastview_window").show();
		
		i = new Image();
		i.onload = function()
			{
			i.onload = null;
			
			var p = on_fastview_pagesize();
			var pw = p[0] - 200;
			var ph = p[1] - 200;
			var iw = i.width;
			var ih = i.height;
			
			if ( iw > pw )
				{
				ih = ih * ( pw / iw ); 
				iw = pw; 
				if ( ih > ph )
					{ 
					iw = iw * ( ph / ih );
					ih = ph;
					}
				}
			else if ( ih > ph )
				{ 
				iw = iw * ( ph / ih ); 
				ih = ph; 
				if ( iw > pw )
					{ 
					ih = ih * ( pw / iw ); 
					iw = pw;
					}
				}
			
			var img_html = "<img src="+url+" width="+iw+" height="+ih+" />";
			
			if ( title && title != "" )
				{
				$("#fastview_content").css( { width: iw, height: ih } );
				$("#fastview_content").html( img_html + "<div id='fastview_caption'><a style='float:right;padding:0px 0px 4px 15px;' href='javascript:on_fastview_close();'>Закрыть</a><b>" + title + "</b>" + "<div style='clear:both' /></div>" );
				$("#fastview_content").css( { width: iw, height: ih + $("#fastview_caption").height() + 5 } );
				}
			else
				{
				$("#fastview_content").css( { width: iw, height: ih } );
				$("#fastview_content").html( img_html );
				}
			
			$("#fastview_window").show();
			}
			
		i.src = url;
		}
	}
	
function on_fastview_close()
	{
	$("#fastview_window").hide();
	$("#fastview_cover").hide();
	}
	
function on_fastview_pagesize() // from thickbox
	{
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || ( de && de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || ( de && de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [ w, h ];
	return arrayPageSize;
	}
	
$(document).ready
	(
	function()
		{
		on_fastview_init();
		$( "a.thickbox, area.thickbox, input.thickbox, a.fastview, area.fastview, input.fastview" ).click
			(
			function()
				{
				on_fastview_open( this.href, this.title );
				this.blur();
				return false;
				}
			);
		$(document).bind( 'keydown', 'esc', function( evt ) { on_fastview_close(); return false; } );
		}
	);

