	
	function image_box_node(parent, href, key) {
		this.parent = parent;
		this.href = href;
		this.key = key;
		this.src = null;
	}
	
	image_box_node.prototype.construct = function() {
		var self = this;
		this.src = this.href.href;
		
		this.href.href = "javascript:void(0)";
		this.href.onclick = function(trgEvent) {
			cancelEvent(trgEvent == null ? window.event : trgEvent);
			self.parent.open_gallery(self.key, self);
			return false;
		}
	}
	
	function image_box_collector() {
		var self = this;
		
		this.galleries = new Array();
		this.overlay = null;

		this.loader = null;
		this.is_loaded = false;

		this.panel = null;
		this.close_box = null;
		this.arrow_left = null;
		this.arrow_right = null;
		this.title = null;
		this.info = null;
		this.img = null;
		
		this.width = null;
		this.height = null;
		
		this.open_key = null;
		this.open_item = null;
		
		this._onresize = function() { self.position(); }
		this._onscroll = function() { self.position(); }
	}
	
	image_box_collector.prototype.construct = function() {
		var self = this;
		
		this.loader = document.createElement("IMG");
		this.loader.onload = function() { if (self.o_ds == null) self.evt_loader(); }
	
		var f, c, elms = document.getElementsByTagName("A");
		
		for (f = 0; f < elms.length; f++) {
			c = elms[f];
			
			if (c.rel != null && c.rel.length > 8 && c.rel.substr(0, 8) == "lightbox") {
				var start = c.rel.indexOf("[");
				var end = c.rel.indexOf("]");
				
				if (start > 0 && end > 0) {
					var key = c.rel.substr(start + 1, end - start - 1);
					
					if (this.galleries[key] == null) this.galleries[key] = new Array();
					this.galleries[key][this.galleries[key].length] = new image_box_node(this, c, key);
				}
			}
		}
		
		var key;
		for (key in this.galleries) {
			this.complete_gallery(this.galleries[key]);
		}
	}
	
	image_box_collector.prototype.complete_gallery = function(gallery) {
		var f;
		for (f = 0; f < gallery.length; f++) {
			gallery[f].construct();
		}
	}
	
	image_box_collector.prototype.get_item_stat = function(key, item) {
		var gallery = this.galleries[key];
		
		if (gallery != null) {
			var founded = false;
			var pos = 0;
			
			while (!founded && pos < gallery.length)
				if (gallery[pos] === item) founded = true;
				else pos++;
				
			if (founded) {
				var ret = new Array();
				ret["key"] = key;
				ret["item"] = item;
				ret["gallery"] = gallery;
				ret["index"] = pos;
				ret["count"] = gallery.length;
				ret["prev"] = pos == 0 ? null : gallery[pos - 1];
				ret["next"] = pos == gallery.length - 1 ? null : gallery[pos + 1];
				
				return ret;
			}
		}
		
		return null;
	}
	
	image_box_collector.prototype.open_gallery = function(key, item) {
		if (this.open_key == null) {
			var self = this;
			var stat = this.get_item_stat(key, item);
			
			if (stat != null) {
				this.open_key = stat["key"];
				this.open_item = stat["item"];
				
				this.width = null;
				this.height = null;
				
				addEvent(window, "resize", this._onresize);
				addEvent(window, "scroll", this._onscroll);
			
				this.overlay = document.createElement("DIV");
				this.overlay.className = "overlay";
				
				this.panel = document.createElement("DIV");
				this.panel.className = "image_box";
				
				this.close_box = document.createElement("A");
				this.close_box.href = "javascript:void(0)";
				this.close_box.innerHTML = "<span>&nbsp;</span>";
				this.close_box.className = "image_box_close";
				this.close_box.onclick = function() {
					self.close_gallery();
					return false;
				}
				
				this.arrow_left = document.createElement("A");
				this.arrow_left.href = "javascript:void(0)";
				this.arrow_left.innerHTML = "<span>&nbsp;</span>";
				this.arrow_left.className = "image_box_left";
				this.arrow_left.onclick = function() {
					self.go_prev();
					return false;
				}
				this.arrow_left.style.display = "none";
				
				this.arrow_right = document.createElement("A");
				this.arrow_right.href = "javascript:void(0)";
				this.arrow_right.innerHTML = "<span>&nbsp;</span>";
				this.arrow_right.className = "image_box_right";
				this.arrow_right.onclick = function() {
					self.go_next();
					return false;
				}
				this.arrow_right.style.display = "none";
				
				this.img = document.createElement("IMG");
				this.img.style.display = "none";
				this.img.className = "image_box_img";
				this.img.onclick = function() {
					self.close_gallery();
					return false;
				}
				
				this.title = document.createElement("P");
				this.title.innerHTML = "";
				this.title.className = "image_box_title";
				this.title.style.display = "none";
				
				this.info = document.createElement("P");
				this.info.innerHTML = "loading ... ";
				this.info.className = "image_box_info";
				
				this.panel.appendChild(this.close_box);
				this.panel.appendChild(this.arrow_left);
				this.panel.appendChild(this.arrow_right);
				this.panel.appendChild(this.img);
				this.panel.appendChild(this.title);
				this.panel.appendChild(this.info);
				
				document.body.appendChild(this.overlay);
				document.body.appendChild(this.panel);
				
				this.position();
				this.gallery_load(stat["item"]);
			}
		}
	}
	
	image_box_collector.prototype.evt_loader = function() {
		if (this.open_key != null) {
			var stat = this.get_item_stat(this.open_key, this.open_item);
			
			if (stat != null) {
				this.arrow_left.style.display = stat["prev"] == null ? "none" : "block";
				this.arrow_right.style.display = stat["next"] == null ? "none" : "block";
				this.title.style.display = "block";
				this.title.innerHTML = "<strong>" + stat["item"].href.title + "</strong>";
				this.info.style.display = "none";
				this.is_loaded = true;
				
				this.img.style.width = "auto";
				this.img.style.height = "auto";
				this.img.style.display = "block";
				this.img.alt = stat["item"].href.title;
				this.img.src = this.loader.src;
				
				this.position();
			}
		}
	}
	
	image_box_collector.prototype.gallery_load = function(item) {
		if (this.open_key != null) {
			var stat = this.get_item_stat(this.open_key, item);
			
			if (stat != null) {
				this.open_item = stat["item"];
				
				this.arrow_left.style.display = "none";
				this.arrow_right.style.display = "none";
				this.title.style.display = "none";
				this.info.style.display = "block";
				this.img.style.display = "none";
				this.is_loaded = false;
				
				this.loader.src = stat["item"].src;
				
				this.position();
			}
		}
	}
	
	image_box_collector.prototype.go_prev = function() {
		if (this.open_key != null) {
			var stat = this.get_item_stat(this.open_key, this.open_item);
			
			if (stat != null && stat["prev"] != null) {
				this.gallery_load(stat["prev"]);
			}
		}
	}
	
	image_box_collector.prototype.go_next = function() {
		if (this.open_key != null) {
			var stat = this.get_item_stat(this.open_key, this.open_item);
			
			if (stat != null && stat["next"] != null) {
				this.gallery_load(stat["next"]);
			}
		}
	}
	
	image_box_collector.prototype.close_gallery = function() {
		if (this.open_key != null) {
			this.open_key = null;
			this.open_item = null;
		
			removeEvent(window, "resize", this._onresize);
			removeEvent(window, "scroll", this._onscroll);
			
			context.structure_destroy(this.panel);
			context.structure_destroy(this.overlay);
		}
	}
	
	image_box_collector.prototype.position = function() {
		var off_width = document.body.offsetWidth;
		var off_height = document.body.offsetHeight;
		
		var im_width = context.get_elementWidth(this.img);
		var im_height = context.get_elementHeight(this.img);
		
		if (this.width == null || this.height == null) {
			this.width = 200;
			this.height = 200;
			
			this.panel.style.width = this.width + "px";
			this.panel.style.height = this.height + "px";
			
		} else if (im_width != 0 && im_height != 0 && this.is_loaded) {
			this.panel.style.width = "auto";
			this.panel.style.height = "auto";
			
			this.width = context.get_elementWidth(this.panel);
			this.height = context.get_elementHeight(this.panel);
			
			var wx = context.wnd_x();
			var wy = context.wnd_y();
			
			if (wx < this.width || wy < this.height) {
				var koef_w = wx / wy;
				var koef_p = this.width / this.height;
				var koef_im = im_width / im_height;
				
				var resWidth = true;
				
				if (koef_w >= 1) {
					if (koef_p > koef_w) resWidth = true; // menime sirku
					else resWidth = false; // menime vysku
					
				} else {
					if (koef_p < koef_w) resWidth = false; // menime vysku
					else resWidth = true; // menime sirku
				}
				
				if (resWidth) {
					var rest = (this.width - im_width) + 40;
					if (rest < 0) rest = 0;
					
					var tm_x = Math.round(wx - rest);
					var tm_y = Math.round(tm_x / koef_im);
					
					if (tm_x < im_width && tm_y < im_height) {
						this.img.style.width = tm_x + "px";
						this.img.style.height = tm_y + "px";
						
						this.width = context.get_elementWidth(this.panel);
						this.height = context.get_elementHeight(this.panel);
					}
					
				} else {
				
					var rest = (this.height - im_height) + 40;
					if (rest < 0) rest = 0;
					
					var tm_y = Math.round(wy - rest);
					var tm_x = Math.round(tm_y * koef_im);
					
					if (tm_x < im_width && tm_y < im_height) {
						this.img.style.width = tm_x + "px";
						this.img.style.height = tm_y + "px";
						
						this.width = context.get_elementWidth(this.panel);
						this.height = context.get_elementHeight(this.panel);
					}
				}
			}
			
		} else if (!this.is_loaded) {
			this.panel.style.width = (this.width - 90) + "px";
			this.panel.style.height = (this.height - 90) + "px";
		}
		
		var left = Math.round((off_width - this.width) / 2);
		var top = Math.round((context.wnd_y() - this.height) / 2) + context.get_scY();
		
		this.overlay.style.width = off_width + "px";
		this.overlay.style.height = off_height + "px";
		
		this.panel.style.left = left + "px";
		this.panel.style.top = top + "px";
		
		this.info.style.top = Math.round(this.height / 2 - context.get_elementHeight(this.info) / 2) + "px";
		
		this.arrow_left.style.top = Math.round(this.height / 2 - context.get_elementHeight(this.arrow_left) / 2) + "px";
		this.arrow_right.style.top = Math.round(this.height / 2 - context.get_elementHeight(this.arrow_right) / 2) + "px";
	}
	
