Viewer = {
	win : window,
	border : 7,
	frame_border : 1,
	margin : 4,
	indicator_width : 70,
	indicator_height : 10,
	min_width : 100,
	min_height : 100,
	view_delay : 10,
	view_steps : 30,
	blackbg : true,
	src : '',
	w : 0,
	h : 0,
	leiras : '',
	open : false,
	getWinWidth : function() {
		var w = this.win.innerWidth;
		if(w==undefined || w===0 || isNaN(w))
			w = this.win.document.documentElement.clientWidth;
		if(w==undefined || w===0 || isNaN(w))
			w = this.win.document.body.clientWidth;
		return w;
	},
	getWinHeight : function() {
		var h = this.win.innerHeight;
		if(h==undefined || h===0 || isNaN(h))
			h = this.win.document.documentElement.clientHeight;
		if(h==undefined || h===0 || isNaN(h))
			h = this.win.document.body.clientHeight;
		return h;
	},
	getScrollLeft : function() {
		var l = this.win.pageXOffset;
		if(l==undefined || l===0)
			l = this.win.document.documentElement.scrollLeft;
		if(l==undefined || l===0)
			l = this.win.document.body.scrollLeft;
		return l;
	},
	getScrollTop : function() {
		var t = this.win.pageYOffset;
		if(t==undefined || t==0)
			t=this.win.document.documentElement.scrollTop;
		if(t==undefined || t===0)
			t=this.win.document.body.scrollTop;
		return t;
	},
	getDocWidth : function() {
		var w=0, i;
		i = this.win.document.documentElement.offsetWidth;
		if (i>w) w = i;
		i = this.win.document.documentElement.scrollWidth;
		if (i>w) w = i;
		i = this.win.document.body.offsetWidth;
		if (i>w) w = i;
		i = this.win.document.body.scrollWidth;
		if (i>w) w = i;
		return w;
	},
	getDocHeight : function() {
		var h=0, i;
		i = this.win.document.documentElement.offsetHeight;
		if (i>h) h = i;
		i = this.win.document.documentElement.scrollHeight;
		if (i>h) h = i;
		i = this.win.document.body.offsetHeight;
		if (i>h) h = i;
		i = this.win.document.body.scrollHeight;
		if (i>h) h = i;
		return h;
	},
	viewer_poz : function() {
		if (!this.open)
			return;
		var descheight = 0;
		var width = this.getWinWidth()-this.border*2-this.frame_border*2-this.margin*2;
		var fullheight = this.getWinHeight()-this.border*2-this.frame_border*2-this.margin*2-this.border;
		var height, nwidth=this.kep_width, nheight=this.kep_height, loop;
		if (width < this.min_width)
			width = this.min_width;
		do {
			height = fullheight-descheight;
			if (height < this.min_height)
				height = this.min_height;
			if (nwidth > width) {
				nheight = nheight * width / nwidth;
				nwidth = width;
			}
			if (nheight > height) {
				nwidth = nwidth * height / nheight;
				nheight = height;
			}
			this.view.style.width = Math.round(nwidth+this.border*2)+'px';
			this.view.style.height = Math.round(nheight+this.border*2+this.leiras_y)+'px';
			if (descheight < this.desc.offsetHeight) {
				descheight+=1;
				loop = true;
			} else
				loop = false;
			if (nwidth < this.min_width || nheight < this.min_height || descheight > nheight)
				loop = false;
		} while (loop);
		if (this.iframe!=undefined) {
			this.iframe.style.width = parseInt(this.view.style.width)+this.frame_border*2+'px';
			this.iframe.style.height = parseInt(this.view.style.height)+this.frame_border*2+'px';
		}
		this.kep.width = Math.round(nwidth);
		this.kep.height = Math.round(nheight);

		this.kep.style.left = this.border+'px';
		this.kep.style.top = this.border+'px';
		if (this.blackbg) {
			this.viewbg.style.left = this.kep.style.left;
			this.viewbg.style.top = this.kep.style.top;
			this.viewbg.style.width = this.kep.width+'px';
			this.viewbg.style.height = this.kep.height+'px';
		}
		this.view.style.left = Math.round(width/2-parseInt(this.kep.width)/2)+this.margin+this.getScrollLeft()+'px';
		this.view.style.top = Math.round(height/2-parseInt(this.kep.height)/2)+this.margin+this.getScrollTop()+'px';
		if (this.iframe!=undefined) {
			this.iframe.style.left = this.view.style.left;
			this.iframe.style.top = this.view.style.top;
		}
		this.desc.style.marginTop = parseInt(this.kep.style.top)+parseInt(this.kep.height)+'px';
	},
	getEventTarget : function(event) {
		if (event==undefined)
			return undefined;
		if (event.srcElement!=undefined)
			return event.srcElement;
		else
			return event.target;
	},
	getEvent : function(event) {
		return (event!=undefined ? event : window.event);
	},
	blurElement : function(event) {
		var target = Viewer.getEventTarget(Viewer.getEvent(event));
		if (target!=undefined && target.blur)
			target.blur();
	},
	addEvent : function(obj, evType, fn) {
		if (obj.addEventListener) {
			obj.addEventListener(evType, fn, false);
			return true;
		} else if (obj.attachEvent) {
			var r = obj.attachEvent("on"+evType, fn);
			return r;
		} else {
	   		return false;
	 	}
	},
	removeEvent : function(obj, evType, fn) {
		if (obj.removeEventListener) {
			obj.removeEventListener(evType, fn, false);
			return true;
		} else if (obj.detachEvent) {
			var r = obj.detachEvent("on"+evType, fn);
			return r;
		} else {
	   		return false;
	 	}
	},
	myHandleKey : function(event) {
		if (!Viewer.open)
			return;
		event = Viewer.getEvent(event);
		if (event.ctrlKey || event.altKey)
			return true;
		var keyCode = event.keyCode;
		switch (keyCode) {
			case 27 : Viewer.close(); break;
			default : return true;
		}
		return false;
	},
	myResize : function(event) {
		if (!Viewer.open)
			return;
		event = Viewer.getEvent(event);
		Viewer.viewer_poz();
		Viewer.back.style.width = Viewer.getDocWidth()+'px';
		Viewer.back.style.height = Viewer.getDocHeight()+'px';
	},
	myScroll : function(event) {
		if (!Viewer.open)
			return;
		event = Viewer.getEvent(event);
		Viewer.viewer_poz();
	},
	mouseOffsetX : function(event) {
		if (event && event.offsetX!=undefined)
			return event.offsetX;
		else if (event && event.layerX)
			return event.layerX;
		else
			return -1;
	},
	mouseOffsetY : function(event) {
		if (event && event.offsetY && event.offsetY!=undefined)
			return event.offsetY;
		else if (event && event.layerY)
			return event.layerY;
		else
			return -1;
	},
	setOpacity : function(a, o) {
		if (a.style.opacity!=undefined) {
			a.style.opacity = o;
		} else {
			a.style.filter = 'Alpha(Opacity='+(o*100)+', Style=0)';
		}
	},
	show : function(name, w, h, title) {
		this.src = name;
		this.w = parseInt(w);
		this.h = parseInt(h);
		
		this.leiras = title!=undefined ? title : '';
		this.create();
	},
	create : function() {
		var pos=0;
		this.flash_hidden();

		this.loading = false;
		this.preloading = false;
		this.kep = undefined;
		this.leiras_y = 0;

		this.back = document.createElement('div');
		this.back.style.position = 'absolute';
		this.back.style.left = '0px';
		this.back.style.top = '0px';
		this.back.style.width = this.getDocWidth()+'px';
		this.back.style.height = this.getDocHeight()+'px';
		this.back.style.backgroundColor = '#000';
		this.back.onclick = function() { Viewer.close(); };
		this.setOpacity(this.back, 0.5);
		this.back.style.zIndex = 10000;
		document.body.appendChild(this.back);

		if (navigator.userAgent.indexOf('Opera')!=-1 && parseFloat(navigator.appVersion)<=4) {
			this.iframe = undefined;
		} else {
			this.iframe = document.createElement('iframe');
			this.iframe.style.position = 'absolute';
			this.iframe.style.background = '#fff';
			this.iframe.style.border = 'none';
			this.iframe.frameBorder = 0;
			this.iframe.style.visibility = 'hidden';
			this.iframe.style.zIndex = 10001;
			document.body.appendChild(this.iframe);
		}

		this.view = document.createElement('div');
		this.view.style.position = 'absolute';
		this.view.style.background = '#fff';
		this.view.style.textAlign = 'left';
		this.view.style.border = this.frame_border+'px solid #000';
		this.view.style.overflow = 'hidden';
		this.view.style.visibility = 'hidden';
		this.view.style.zIndex = 10002;
		document.body.appendChild(this.view);

		if (this.blackbg) {
			this.viewbg = document.createElement('div');
			this.viewbg.style.background = '#000';
			this.viewbg.style.position = 'absolute';
			this.view.appendChild(this.viewbg);
		}

		this.desc = document.createElement('div');
		this.desc.style.marginLeft = this.border+'px';
		this.desc.style.marginRight = this.border+'px';
		this.desc.style.paddingLeft = '0px';
		this.desc.style.paddingRight = '0px';
		this.desc.style.paddingTop = this.border+'px';
		this.desc.style.paddingBottom = '0px';
		this.desc.style.fontFamily = 'verdana';
		this.desc.style.fontSize = '10px';
		this.desc.style.fontWeight = 'bold';
		this.desc.style.color = '#777';
		this.desc.style.visibility = 'hidden';
		this.desc.innerHTML = '';
		this.view.appendChild(this.desc);

		this.indicator = document.createElement('img');
		this.indicator.src = 'http://clicker.indygobox.com/art/gallery/indicator.gif';
		this.indicator.style.position = 'absolute';
		this.indicator.style.visibility = 'hidden';
		this.indicator.width = this.indicator_width;
		this.indicator.height = this.indicator_height;
		this.indicator.old_scrollTop = this.getScrollTop();
		this.indicator.old_scrollLeft = this.getScrollLeft();
		this.indicator.style.left = Math.round(this.getWinWidth()/2-this.indicator.width/2)+'px';
		this.indicator.style.top = Math.round((this.getWinHeight())/2-this.indicator.height/2)+'px';
		this.indicator.style.zIndex = 10003;
		document.body.appendChild(this.indicator);

		this.open = true;



		this.preloading = true;
		this.indicator.src = this.indicator.src;
		this.indicator.style.left = parseInt(this.indicator.style.left)+this.indicator.old_scrollLeft-this.getScrollLeft()+'px';
		this.indicator.style.top = parseInt(this.indicator.style.top)+this.indicator.old_scrollTop-this.getScrollTop()+'px';
		setTimeout("if (Viewer.preloading && Viewer.indicator && Viewer.indicator.style) Viewer.indicator.style.visibility='visible';", 250);

		this.loading = true;

		this.kep = document.createElement('iframe');
		this.kep.style.visibility = 'hidden';
		this.kep.style.position = 'absolute';
		this.kep.frameBorder = 0;

		this.kep.style.border = 'none';
		//this.kep.width = parseInt(this.w);
		//this.kep.height = parseInt(this.h);
		this.kep.width = 100;
		this.kep.height = 100;
//		this.kep.main = this;
/*		if (this.kep.attachEvent) {
			this.kep.attachEvent("onload", function(){Viewer.loaded();});
		} else {
			this.kep.onload = this.loaded;
		}
		this.kep.src = this.src;*/
		this.view.appendChild(this.kep);
/*
		this.kep = document.createElement('div');
		this.kep.style.visibility = 'hidden';
		this.kep.style.position = 'absolute';
		this.kep.width = parseInt(this.w);
		this.kep.height = parseInt(this.h);
		this.kep.main = this;
		this.view.appendChild(this.kep);
		this.kep.innerHTML = '<iframe src="'+this.src+'" onload="Viewer.loaded()" width="'+this.w+'" height="'+this.h+'" />';
*/
		Viewer.loaded();


		this.addEvent(this.win, "resize", this.myResize);
		this.addEvent(this.win, "scroll", this.myScroll);
		this.addEvent(this.win.document, "keydown", this.myHandleKey);
	},
	loaded : function() {
		this.main = Viewer;
		if (!this.main.open || !this.main.desc)
			return;
		this.main.desc.innerHTML = this.main.leiras!='' ? this.main.leiras+'<br style="clear: both;" />' : '';
		//this.main.kep_width = parseInt(this.main.kep.width);
		//this.main.kep_height = parseInt(this.main.kep.height);
		this.main.kep_width = parseInt(this.main.w);
		this.main.kep_height = parseInt(this.main.h);
		this.main.viewer_poz();

		this.main.view.style.visibility = 'hidden';
		if (this.main.iframe!=undefined)
			this.main.iframe.style.visibility = 'hidden';
		this.main.desc.style.visibility = 'hidden';
		this.main.kep.style.visibility = 'hidden';

		this.main.leiras_y = 0;
//		this.main.viewer_poz();

		this.main.kep.src = this.main.src;

		this.main.setOpacity(this.main.kep, 0);

		this.main.desc.style.visibility = 'visible';
		this.main.kep.style.visibility = 'visible';
		if (this.main.iframe!=undefined)
			this.main.iframe.style.visibility = 'visible';
		this.main.view.style.visibility = 'visible';

		this.main.loading = false;
		this.main.preloading = false;

		this.main.indicator.style.visibility = 'hidden';
		this.main.indicator.style.left = parseInt(this.main.view.style.left)+Math.round(400-parseInt(this.main.indicator.width)/2)+'px';
		this.main.indicator.style.left = parseInt(this.main.view.style.left)+'px';
		this.main.indicator.style.left = parseInt(this.main.view.style.left)+Math.round(parseInt(this.main.view.style.width)/2-this.main.indicator.width/2)+'px';
		this.main.indicator.style.top = parseInt(this.main.view.style.top)+Math.round(parseInt(this.main.view.style.height)/2-this.main.indicator.height/2)+'px';
		this.main.indicator.old_scrollLeft = this.main.getScrollLeft();
		this.main.indicator.old_scrollTop = this.main.getScrollTop();

		var tim = new Date();
		this.main.poztim = tim.getTime();
		setTimeout("Viewer.leiras_poz()", this.main.view_delay);

		this.main.viewer_poz();
	},
	leiras_poz : function() {
		if (this.open && !this.loading && this.leiras_y < this.desc.offsetHeight) {
			var tim = new Date();
			var step = Math.floor((tim - this.poztim) / this.view_delay);
			if (step > this.view_steps)
				step = this.view_steps;
			if (this.desc.innerHTML != '') {
				this.leiras_y = Math.round(this.desc.offsetHeight * step / this.view_steps);
				this.view.style.height = parseInt(this.kep.height)+this.border*2+this.leiras_y+'px';
				this.desc.style.marginTop = parseInt(this.kep.style.top)+parseInt(this.kep.height)+this.desc.offsetHeight-this.leiras_y+'px';
			}
			if (step < this.view_steps) {
				this.setOpacity(this.kep, Math.round(step / this.view_steps * 100) / 100);
				setTimeout("Viewer.leiras_poz()", this.view_delay);
			} else {
				if (this.iframe!=undefined)
					this.iframe.style.height = parseInt(this.kep.height)+this.border*2+this.leiras_y+'px';
				this.setOpacity(this.kep, 0.99);
			}
		}
	},
	close : function() {
		this.open = false;

		this.removeEvent(this.win, "resize", this.myResize);
		this.removeEvent(this.win, "scroll", this.myScroll);
		this.removeEvent(this.win.document, "keydown", this.myHandleKey);

		this.kep.onload = null;

		this.view.removeChild(this.desc);
		delete(this.desc);
		if (this.blackbg) {
			this.view.removeChild(this.viewbg);
			delete(this.viewbg);
		}
		document.body.removeChild(this.indicator);
		delete(this.indicator);
		document.body.removeChild(this.view);
		delete(this.view);
		if (this.iframe!=undefined) {
			document.body.removeChild(this.iframe);
			delete(this.iframe);
		}
		this.back.onclick = null;
		document.body.removeChild(this.back);
		delete(this.back);

		this.flash_visible();
	},
	flash_hidden : function() {
		var tags=document.getElementsByTagName("div");
		for(var i=0; i<tags.length; i++) {
			if (tags[i].className=='flash' && tags[i].style.visibility!='hidden') {
				if (tags[i].orig_visibility==undefined)
					tags[i].orig_visibility=tags[i].style.visibility;
				tags[i].style.visibility='hidden';
			}
		}
	},
	flash_visible : function() {
		var tags=document.getElementsByTagName("div");
		for(var i=0; i<tags.length; i++)
			if (tags[i].orig_visibility!=undefined) {
				tags[i].style.visibility=tags[i].orig_visibility;
				tags[i].orig_visibility=undefined;
			}
	}
}