var SimpleViewer = 
{
	images : [],
	is_closed : true,
	images_url : [],
	current : 0,
	num_check : 0,
	is_loaded : false,
	init : function()
	{
		//Image.prototype.is_loaded = false;
		$("body").append('<div id="sw_wrapper"><table width="100%" id="sw_table"><tr><td width="200px" align="center" valign="center"><a href="javascript:void(0)" class="sw_arrow" id="sw_prev"><div></div></a></td><td align="center" id="sw_center" valign="center"><div id="sw_cont"><a href="javascript:void(0)" id="sw_close"></a><div id="sw_topleft" align="right"><div id="sw_topright"></div></div><div id="sw_centerleft"><div id="sw_centerright"><div id="sw_content" align="left"></div></div></div><div id="sw_bottomleft" align="right"><div id="sw_bottomright"></div></div></div><div id="sw_tophint"><div id="sw_tophintin"><span id="sw_current"></span>/<span id="sw_length"></span></div></div><div id="sw_bottomhint"><div id="sw_bottomhintin"></div></div></td><td width="200px" align="center" valign="center"><a href="javascript:void(0)" class="sw_arrow" id="sw_next"><div></div></a></td></tr></table></div>')
		this.render();
		document.getElementById("sw_close").onclick = function(){SimpleViewer.close()};
		document.getElementById("sw_next").onclick = function(){SimpleViewer.next()};
		document.getElementById("sw_prev").onclick = function(){SimpleViewer.prev()};
	},
	show : function(name)
	{
		this.is_closed = false;
		this.render();
		$("#sw_length").text(this.images_url.length);
		$("#sw_bottomhintin").text(name);
		$("#sw_wrapper").css("display", "block");
		this.change(this.current);
	},

	render : function()
	{
		$("#sw_table").height(this.getClientHeight());
		if ("\v"=="v")
		{
			h1 = this.getClientHeight();
			h2 = $("#wrapper").height();
			if (h1>h2) $("#sw_wrapper").height(h1);
			else $("#sw_wrapper").height(h2);
		        $("#sw_table").css("top" ,this.getBodyScrollTop());

		}
		else $("#sw_wrapper").height(this.getClientHeight());
		$("#sw_tophint").width($("#sw_center").width());
		$("#sw_bottomhint").width($("#sw_center").width());
	},

	close : function()
	{
		this.is_closed = true;
		$("#sw_wrapper").css("display", "none");
		$("#sw_tophint").css("display","none");
		$("#sw_bottomhint").css("display","none");
	},

	next : function()
	{
		if (this.is_loaded)
		{
			this.current++;
			this.current = this.current % this.images_url.length;
			this.change();
		}
	},

	prev : function()
	{
		if (this.is_loaded)
		{
			this.current--;
			if (this.current<0) {this.current = this.images_url.length-1;}
			this.change();
		}
	},

	change : function()
	{
		$("#sw_current").text(this.current+1);
		if (this.images[this.current]===undefined)
		{	
			this.is_loaded = false;
			$("#sw_content").html('');
			$("#sw_content").height(100);
			$("#sw_cont").width(100);
			this.images[this.current] = new Image();
			this.images[this.current].onload = function(){this.is_loaded = true;};
			this.images[this.current].src = this.images_url[this.current];
		}
		this.check_load();
	},

	clear : function() 
	{
		this.is_loaded = false;
		this.current = this.num_check = 0;
		this.images = new Array();
		this.images_url = new Array();
		$("#sw_content").html('');
		$("#sw_content").height(100);
		$("#sw_cont").width(100);
	},

	check_load : function()
	{
	    if (!this.is_closed) {	
		if (this.images[this.current].is_loaded) {
			this.is_loaded = true;
			this.num_check = 0;
			this.render();
			var w = this.images[this.current].width;
			var h = this.images[this.current].height;
			ww = parseInt(this.getClientWidth());
			wh = parseInt(this.getClientHeight());
			r = w/h;
			if (ww<w+200) { w = ww-200; h = w/r; }
			if (wh<h+180) { h = wh-180; w = h*r; }	
			$("#sw_content").height(h+40);
			$("#sw_cont").width(w+100);
			if ("\v"=="v") 
			{
				$("#sw_cont").css("top",($("#sw_table").height()-$("#sw_cont").height())/2);
				
			}
			var img = document.createElement("img");
			img.width = w;
			img.height = h;
			if ("\v"=="v") {img.style.width=w;img.style.height = h;}
			img.src = this.images_url[this.current];
			img.style.position ='relative';
			img.style.top = '20px';
			img.style.left = '40px';

			$("#sw_content").html('').append(img);
			$("#sw_tophint").css("display","block");
			$("#sw_bottomhint").css("display","block");
			return;	
		 }
		else if (this.num_check<10) { this.num_check++; setTimeout(function(){SimpleViewer.check_load();}, 1000); return;}
		alert("Изображение не может быть загружено.Превышен лимит ожидания.");
	   }
	},	

	add : function(url)
	{
		//this.images_url.push('http://xandr99.tmweb.ru/'+url);
		this.images_url.push(''+url);
	}, 

	getClientWidth : function()
	{
  		return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
	},

 	getClientHeight : function()
	{
	 	 return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	},

        getBodyScrollTop : function()
	{
  		return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
	}

}

