/*

	mintAjax ImageBox Extension v1.2
	
*/

mint.ext.imagebox = {};

mint.ext.imagebox.Create = function() {
	var w = {
		classNames : [],
		groups : [],
		box : null,
		zoom : null,
		container : null,
		fader : null,
		loader : null,
		navPrev : null,
		navNext : null,
		navTitle : null,
		navClose : null,
		fadeSteps : 4,
		fadeDuration : 100,
		fadeOpacity : 80,
		sizeSteps : 15,
		sizeDuration : 450,
		showSteps : 10,
		showDuration : 250,
		startWidth : 200,
		startHeight : 200,
		boxOffset : 90,
		zIndex : 100,
		imageBorder : 10,
		lightboxMode : true,
		lang : "pl",
		langData : {en: {prev: "« prev", next: "next »"}, pl: {prev: "« poprzednie", next: "następne »"}, de: {prev: "« vorherig", next: "folgend »"}},
		loaderReady : false,
		nextImage : "next.gif",
		prevImage : "prev.gif",
		loadingImage : "loading.gif",
		closeImage : "close.gif",
		openScroll : null,
		
		AddClass : function(className) {
			this.classNames.push(className);
		},
		
		AddImage : function(a) {
			var a = $(a), that = this;
			
			if(a.getTagName() == "a") {
				AddEvent(a, "click", function() {that.LoadImage(this); return false;});
			}
		},
		
		LoadImage : function(a) {
			var a = $(a), that = this;
			
			with(this) {
				if(zoom && zoom.parentNode == box) box.removeChild(zoom);
				if(GetStyle(nav, "visibility") == "visible") HideNav();
				zoom = $C("img");
				
				if(openScroll !== null) window.scrollTo(window.scrollX, openScroll);
				
				fader.style.display = "block";
				mint.fx.Fade(fader, fadeOpacity, fadeSteps, fadeDuration);
				
				SetPos(container, GetScrollX(), GetScrollY()+boxOffset);
				
				container.style.visibility = "hidden";
				container.style.display = "block";
				
				if(loaderReady) {
					loader.style.marginTop = (GetHeight(box)/2-GetHeight(loader)/2)+"px";
					box.appendChild(loader);
				}
				
				container.style.visibility = "visible";
				
				AddEvent(zoom, "load", function() {that.ResizeBoxHeight(that)});
				SetOpacity(zoom, 0);
		
				with(zoom.style) {
					visibility = "hidden";
					margin = "auto";
					position = "relative";
					top = this.imageBorder+"px";
				}
				
				zoom.alt = a.title || $(a).down("img").alt || "";
				zoom.imageBox = this;
				zoom.group = a.group;
				zoom.groupIndex = a.groupIndex;
				zoom.src = a.href;
			}
		},
		
		ResizeBoxHeight : function(that) {
			if(this.loader && this.loader.parentNode == this.box)
				this.box.removeChild(this.loader);
				
			mint.fx.Style(this.box, "height", this.lightboxMode ? GetHeight(this.box) : this.startWidth, this.zoom.height+this.imageBorder*2, this.sizeSteps, this.sizeDuration, null, function() {that.ResizeBoxWidth(that)});
		},
		
		ResizeBoxWidth : function(that) {
			mint.fx.Style(this.box, "width", this.lightboxMode ? GetWidth(this.box) : this.startHeight, this.zoom.width+this.imageBorder*2, this.sizeSteps, this.sizeDuration, null, function() {that.ShowImage(that)});
		},
		
		ShowImage : function(that) {
			with(this) {
				box.appendChild(zoom);
				mint.fx.Fade(zoom, 100, showSteps, showDuration);
				
				zoom.style.visibility = "visible";
				
				navTitle.innerHTML = zoom.alt;
				
				if(zoom.group) {
					navPrev.style.visibility = zoom.groupIndex == 0 ? "hidden" : "visible";
					navNext.style.visibility = zoom.groupIndex == zoom.group.length-1 ? "hidden" : "visible";
				}
				
				SetY(nav, nav.clientHeight);
				SetWidth(nav, GetWidth(box));
				
				SetY(navClose, -navClose.clientHeight);
				SetWidth(navClose, GetWidth(box));
				
				var margin = Math.floor((GetWidth(navInner)-navTitle.offsetWidth-(navPrev.width+navNext.width))/2-imageBorder/2);
				
				navPrev.style.marginRight = margin+"px";
				navNext.style.marginLeft = margin+"px";
				
				nav.style.visibility = "visible";
				navClose.style.visibility = "visible";
				
				mint.fx.Style(nav, "top", null, 0, 20, 500);
				mint.fx.Style(navClose, "top", null, 0, 20, 500);
				
				openScroll = GetScrollY();
			}
		},
		
		GetScrollX : function() {
			return window.scrollX || document.scrollLeft || document.documentElement.scrollLeft;
		},
		
		GetScrollY : function() {
			return window.scrollY || document.scrollTop || document.documentElement.scrollTop;
		},
		
		HideNav : function() {
			with(this) {
				navClose.style.visibility = "hidden";
				navPrev.style.visibility = "hidden";
				navNext.style.visibility = "hidden";
				nav.style.visibility = "hidden";
			}
		},
		
		Close : function() {
			with(this) {
				container.style.display = "none";
				
				mint.fx.Fade(fader, 0, fadeSteps, fadeDuration, null, function(obj) {obj.style.display = "none";});
				
				mint.fx.Stop(box, "width");
				mint.fx.Stop(box, "height");
				
				if(loader && loader.parentNode == box) box.removeChild(loader);
				
				if(!lightboxMode) SetSize(box, startWidth, startHeight);
				
				HideNav();
				navTitle.innerHTML = "";
				
				if(zoom && zoom.parentNode == box) {
					box.removeChild(zoom);
					zoom = null;
				}
				
				openScroll = null;
			}
		},
		
		NextImage : function(e) {
			e = e || window.event;
			
			e.cancelBubble = true;
			if(e.stopPropagation) e.stopPropagation();
			
			with(this) {
				if(zoom.groupIndex+1 < zoom.group.length)
					LoadImage(zoom.group[zoom.groupIndex+1]);
			}
		},
		
		PrevImage : function(e) {
			e = e || window.event;
			
			e.cancelBubble = true;
			if(e.stopPropagation) e.stopPropagation();
			
			with(this) {
				if(zoom.groupIndex > 0)
					LoadImage(zoom.group[zoom.groupIndex-1]);
			}
		},
		
		AddToGroup : function(group, img) {
			if(!this.groups[group]) this.groups[group] = [];
			
			this.groups[group].push(img);
			
			img.group = this.groups[group];
			img.groupIndex = this.groups[group].length-1;
		},
		
		Init : function() {
			var imgs = [], that = this, doc = document.documentElement;
			
			for(var i in this.classNames) {
				imgs = imgs.concat($(document.body).getElementsByClass(this.classNames[i], "a"));
			}
			
			for(var i in imgs) {
				if(imgs[i].getAttribute("group")) {
					this.AddToGroup(imgs[i].getAttribute("group"), imgs[i]);
				}
			}
			
			if(this.lightboxMode) {
				var imgsT = document.body.getElementsByTagName("a");
				
				for(var i = 0 ; i < imgsT.length; ++i) {
					if(imgsT[i].getAttribute("rel") && /lightbox/i.test(imgsT[i].getAttribute("rel"))) {
						if(!InArray(imgs, imgsT[i])) {
							imgs.push(imgsT[i]);
							if(/^lightbox\[(.*)\]$/.test(imgsT[i].getAttribute("rel")) && RegExp.$1) {
								this.AddToGroup(RegExp.$1, imgsT[i]);
							}
						}
					}
				}
			}
			
			with(this) {
				for(var i in imgs) AddImage(imgs[i]);
				
				fader = $C("div", null, "mintImageBox-fader", {zIndex: zIndex});
				
				SetSize(fader, doc.scrollWidth < doc.clientWidth ? doc.clientWidth : doc.scrollWidth, doc.scrollHeight < doc.clientHeight ? doc.clientHeight : doc.scrollHeight);
				SetOpacity(fader, 0);
				
				document.body.appendChild(fader);
				AddEvent(fader, "click", function() {that.Close()});
				
				container = $C("div", null, "mintImageBox-container", {position: "absolute", left: "0px", top: (GetScrollY()+boxOffset)+"px", zIndex: zIndex+1}, {onclick: function(e) {that.Close()}});
				
				loader = $C("img", null, "mintImageBox-loader", {margin: "auto"}, {onload: function() {that.loaderReady = true}, src: that.loadingImage});

				nav = $C("div", null, "mintImageBox-nav", {width: startWidth+"px", zIndex: zIndex+2});
				
				AddEvent(nav, "click", function(e) {
					if (!e) var e = window.event;
					e.cancelBubble = true;
					if (e.stopPropagation) e.stopPropagation();
					return false;
				});
				
				navInner = $C("div", null, "mintImageBox-navInner");
				
				navPrev = $C("img", null, "mintImageBox-navPrev", null, {onclick: function(e) {that.PrevImage(e)}, src: prevImage}, navInner);
				navTitle = $C("span", null, "mintImageBox-navTitle").appendTo(navInner);
				navNext = $C("img", null, "mintImageBox-navNext", null, {onclick: function(e) {that.NextImage(e)}, src: nextImage}, navInner);
				
				nav.appendChild(navInner);
				container.appendChild(nav);
				
				box = $C("div", null, "mintImageBox-box", {width: startWidth+"px", height: startHeight+"px", zIndex: zIndex+3});
				container.appendChild(box);
				
				AddEvent(box, "click", function(e) {
					if (!e) var e = window.event;
					e.cancelBubble = true;
					if (e.stopPropagation) e.stopPropagation();
					return false;
				});
				
				navClose = $C("div", null, "mintImageBox-navClose", {width: startWidth+"px", zIndex: zIndex+2});
				container.appendChild(navClose);
				
				with(navClose.appendChild($C("img"))) {
					src = closeImage;
					style.marginBottom = imageBorder+"px";
				};
				
				document.body.appendChild(container);
				
				AddEvent(document.body, "keydown", function(e) {
					if(e.keyCode == 27) that.Close();
				});
			}
		}
	}
	
	if(arguments.length > 0) {
		for(var i = 0; i < arguments.length; ++i) {
			w.AddClass(arguments[i]);
		}
	}
	
	return w;
}