var imagePopup = { 
	
	_active: false,	// imagepopup is open
	
	_winCenterX: 0,
	_winCenterY: 0,
	_imgW: 0,
	_imgH: 0,
	_imgLimitW: 0,
	_imgLimitH: 0,
	_objImage: null,
	_resizeStep: 0,
	_imageFadeStep: 0,
	_winFadeStep: 100,
	_winFadeeMode: 0,
	_captionH: 0,

	
	/* icon id */
	_iconId: "",

	/* setup image */
	setup: function(obj, image, options) {
		if (jet.client == "msie6") { return; }
		obj.onmouseover = "";
		var a;
		if (a = obj.parentNode) {
			if (a.nodeName == "A") {
				var id =jet.autoId(obj);
				a.href="javascript:void(0);"
				a.target="";
				a.onclick = function() { imagePopup.show(id, image, options); };
				jet.preloadImage(image);
			}
		}
	},
	
	/* show image */
	show: function(id, image, options) {
		if (this._active) { this._deactivate(); }
		this._iconId = id;
		this._resizeStep = 0;
		this._imageFadeStep = 0;
		this._winFadeStep = 100;
		var w = 640;
		var h = 480;
		var caption = false;
		if (typeof options != 'undefined') {
			if (options.w) { w= options.w ;}
			if (options.h) { h= options.h ;}
			if (options.caption) { caption= options.caption;}
		}
		this._imgLimitW = w;
		this._imgLimitH = h;
		this._active = true;
		this._makeBox(image, 192, 192, caption);
		setTimeout("jet.addEvent(document,'onclick',imagePopup._waitClick)",10);
	},
	
	/* preload skin images */
	preloadImages: function() {
		for(var i=0,il=4; i<il; i++) {
			jet.preloadImage(this._skin.winEdgeImages[i]);
			jet.preloadImage(this._skin.winCornerImages[i]);
		}
		jet.preloadImage(this._skin.loaderImage);
	},

	_zoomImage: function() {
		if (this._active) {
			this._removeLoader();
			var image = this._objImage;
			this._resizeImage(image, this._imgLimitW, this._imgLimitH);
			var box = jet.getEl('_IMAGEPOPUP');
			if (image.width == this._imgW && image.height == this._imgH) {		
				image.style.visibility = "visible";
				box.appendChild(image);
				this._ImageFadeIn();
			} else {
				this._resizeStep ++;
				var w = 192 + Math.round(((image.width - 192) / 15) * this._resizeStep);
				var h = 192 + Math.round(((image.height - 192) / 15) * this._resizeStep);
				this._resizeWindow(w, h);
				setTimeout('imagePopup._zoomImage()',20);
			}
		}
	},
	
	_ImageFadeIn: function() {
		if (this._active) {
			var img = this._objImage;
			this._imageFadeStep += 20;
			if (jet.clientName() == "msie") { this._imageFadeStep = 100; }
			this._setOpacity(img, this._imageFadeStep);
			if (this._imageFadeStep < 100) {
				setTimeout('imagePopup._ImageFadeIn()',20);
			}
		}
	},
		
	/* ----------------------------------------------------- LAYERS */
	_makeBox: function(image, w, h, caption) {
		
		//  create and hide the box 
		var box = jet.createEl("div");
		box.id = "_IMAGEPOPUP";
		box.style.position = "absolute";
		box.className = "imagePopup";
		this._setOpacity(box, 0);
		this._winFadeStep = 0;
		document.body.appendChild(box);
		this._winCenterX = jet.getWinScroll().x + Math.round(jet.getWinSize().w / 2);
		this._winCenterY = jet.getWinScroll().y + Math.round(jet.getWinSize().h / 2);
		this._makeBoxCorner(box, 0);
		this._makeBoxCorner(box, 1);
		this._makeBoxCorner(box, 2);
		this._makeBoxCorner(box, 3);
		this._makeBoxEdge(box, 0);
		this._makeBoxEdge(box, 1);
		this._makeBoxEdge(box, 2);
		this._makeBoxEdge(box, 3);
		this._makeBoxBg(box);
		this._makeCaption(box, caption);
		this._resizeWindow(w, h);
		document.body.appendChild(box);
		
		/* create image object */
		var img = jet.createEl("img");
		img.style.position = "absolute";
		this._setOpacity(img, 0);
		img.style.top = (this._captionH + this._skin.winImageMargins[0])+"px";
		img.style.left = this._skin.winImageMargins[3]+"px";
		img.src = image;
		this._objImage = img;
		
		if (img.width && img.height) {
			this._resizeImage(img, this._imgLimitW, this._imgLimitH);
			this._resizeWindow(img.width, img.height);
			this._setOpacity(img, 1);
			box.appendChild(img);
			this._winFadeIn();
		} else {	
			this._setOpacity(box, 1);
			this._winFadeStep = 100;
			this._showLoader();
			img.onload = function(){ imagePopup._zoomImage(); };
			
		}
	},
	
	_makeBoxCorner: function(box, n) { 
		var el = jet.createEl("img");
		el.style.position = "absolute";
		el.src = this._skin.winCornerImages[n];
		eval("el.style."+this._cssPos[this._edge(n)]+" = '0'");
		eval("el.style."+this._cssPos[this._edge(n+1)]+" = '0'");
		eval("el.style.width = '"+this._skin.winBordImgSizes[this._edge(n+1)]+"px'");
		eval("el.style.height = '"+this._skin.winBordImgSizes[this._edge(n)]+"px'");
		box.appendChild(el);
	},
	
	_makeBoxEdge: function(box, n) { 
		var el = jet.createEl("img");
		el.id = "_IMAGEPOPUP_EDGE"+n;
		el.style.position = "absolute";
		el.src = this._skin.winEdgeImages[n];
		eval("el.style."+this._cssPos[n]+" = '0'");
		eval("el.style."+this._cssSize[n]+" = '"+this._skin.winBordImgSizes[n]+"px'");
		eval("el.style."+this._cssPos[this._edge(n-1)]+" = '"+this._skin.winBordImgSizes[this._edge(n-1)]+"px'");
		box.appendChild(el);
	},

	_makeBoxBg: function(box) { 
		var el = jet.createEl("div");
		el.id = "_IMAGEPOPUP_BG";
		el.style.position = "absolute";
		if (this._skin.winMiddleImage) {
			el.style.backgroundImage = "url("+this._skin.winMiddleImage+")";
		}
		if (this._skin.winMiddleColor) {
			el.style.backgroundColor = this._skin.winMiddleColor;
		}
		eval("el.style."+this._cssPos[0]+" = '"+this._skin.winBordImgSizes[0]+"px'");
		eval("el.style."+this._cssPos[3]+" = '"+this._skin.winBordImgSizes[3]+"px'");
		box.appendChild(el);
	},
	
	_makeCaption: function(box, caption) { 
		if (caption) {
			var el = jet.createEl("div");
			el.style.position = "absolute";
			el.style.height = this._skin.captionHeight+"px";
			el.style.top = this._skin.winBordImgSizes[0]+"px";
			el.style.left = this._skin.winBordImgSizes[3]+"px";
			el.style.overflow = "hidden";
			el.style.fontFamily = this._skin.captionFont;
			el.style.color = this._skin.captionColor;
			el.style.fontSize = this._skin.captionSize;
			el.appendChild(jet.createTxt(caption));
			box.appendChild(el);
			this._captionH = this._skin.captionHeight;
		} else {
			this._captionH = 0;
		}
	},

	/* ----------------------------------------------------- RESIZE WINDOW */
	_resizeWindow: function(w, h) {
		var box = jet.getEl('_IMAGEPOPUP');
		this._imgW = w;
		this._imgH = h;
		var boxW = (this._skin.winImageMargins[3] + this._skin.winImageMargins[1] + w);
		var boxH = (this._skin.winImageMargins[0] + this._skin.winImageMargins[2] + this._captionH + h);
		box.style.width =  boxW + "px";
		box.style.height = boxH + "px";
		box.style.left = (this._winCenterX - (Math.round(w / 2) + this._skin.winImageMargins[3])) + "px";
		box.style.top =  (this._winCenterY - (Math.round((h + this._captionH) / 2) + this._skin.winImageMargins[0])) + "px";
		jet.getEl("_IMAGEPOPUP_EDGE0").style.width = jet.getEl("_IMAGEPOPUP_EDGE2").style.width = (boxW - (this._skin.winBordImgSizes[3] + this._skin.winBordImgSizes[1]))+"px";
		jet.getEl("_IMAGEPOPUP_EDGE1").style.height = jet.getEl("_IMAGEPOPUP_EDGE3").style.height = (boxH - (this._skin.winBordImgSizes[2] + this._skin.winBordImgSizes[0]))+"px";
		jet.getEl("_IMAGEPOPUP_BG").style.width = (boxW - (this._skin.winBordImgSizes[3] + this._skin.winBordImgSizes[1]))+"px";
		jet.getEl("_IMAGEPOPUP_BG").style.height = (boxH - (this._skin.winBordImgSizes[2] + this._skin.winBordImgSizes[0]))+"px";
	},

	
	/* ----------------------------------------------------- RESIZE LOADED IMAGE */
	_resizeImage: function(image, wMax, hMax) {
		if (image.width <= wMax && image.height <= hMax) { return false; }
		var pcw = wMax / image.width;
		var pch = hMax / image.height;
		if (pcw < pch) {
			image.width = wMax;
			image.height = Math.floor(image.height * pcw);
		} else {
			image.width = Math.floor(image.width * pch);
			image.height = hMax;
		}	
		return true;
	},
	
	/* ----------------------------------------------------- OPACITY */
	
	_setOpacity: function(obj, opacity) {
		if (jet.clientName() != "msie") {
			obj.className = "opacity"+opacity;
		}
	},
		

	/* ----------------------------------------------------- LOADER */
	_showLoader: function() {
		var box = jet.getEl('_IMAGEPOPUP');
		var el = jet.createEl("img");
		el.style.position = "absolute";
		el.id = '_IMAGEPOPUP_LOADER';
		box.appendChild(el);
		el.src = this._skin.loaderImage;
		el.style.top = (Math.round((this._imgH -32)/ 2)+this._skin.winImageMargins[0])+"px";
		el.style.left = (Math.round((this._imgW - 32) / 2)+this._skin.winImageMargins[3])+"px";
	},
	
	_removeLoader: function() {
		var el = jet.getEl('_IMAGEPOPUP_LOADER');
		if (el) {
			var box = jet.getEl('_IMAGEPOPUP');
			box.removeChild(el);
		}
	},
	
	/* ----------------------------------------------------- UTILS */
	_edge: function(n) {
		n < 0 && (n += Math.ceil(Math.abs(n)/4)*4);
		n > 3 && (n -= Math.floor(n/4)*4);
		return n; 
	},
	_cssPos: ["top", "right", "bottom", "left"],
	_cssSize: ["height", "width", "height", "width"],
	

	/* ----------------------------------------------------- SKIN DATA */
	_skin: {
		winBordImgSizes: [23, 31, 31, 23],	// size of border images 
		winImageMargins: [23, 31, 31, 23],	// image margins form outer box
		winMiddleImage: false,
		winMiddleColor: "#F5F5F5",
		winCornerImages: ["/img/popupimage_c1.png", "/img/popupimage_c2.png", "/img/popupimage_c3.png", "/img/popupimage_c4.png"],
		winEdgeImages: ["/img/popupimage_e1.png", "/img/popupimage_e2.png", "/img/popupimage_e3.png", "/img/popupimage_e4.png"],
		loaderImage: "/img/popupImage_loader.gif",
		captionHeight: 20,
		captionColor: "#888888",
		captionSize: "1em",
		captionFont: "Times, serif"
	},
	

	/* ----------------------------------------------------- EVENTS */
	_waitClick: function(e) {
		if (imagePopup._active) { 
			imagePopup._winFadeOut();
		}
	},
	
	_clickedOutsideBox: function(e) {
		if (!e) { e = window.event; }
		var clickEl = jet.getEventTarget(e);
		var el = clickEl;
		while(el != null) { 
			if(el.id == "_IMAGEPOPUP") { return false; }
			el = el.offsetParent;
		}
		return true;
	},
	
	_winFadeIn: function() {
		this._winFadeMode = "in";
		this._winFade("in");
	},
	
	
	_winFadeOut: function() {
		this._winFadeMode = "out";
		this._winFade("out");
	},
	
	_winFade: function(dir) {
		if (this._winFadeMode == dir && this._active) {
			var box = jet.getEl('_IMAGEPOPUP');
			if (dir == "in") {
				this._winFadeStep += 20;
				if (jet.clientName() == "msie") { this._winFadeStep = 100; }
				this._setOpacity(box, this._winFadeStep);
				if (this._winFadeStep < 100) {
					setTimeout('imagePopup._winFade("in")',20);
				} else {
					this._winFadeMode = false;
				}
			} else if (dir == "out") {
				this._winFadeStep -= 20;
				if (jet.clientName() == "msie") { this._winFadeStep = 0; }
				this._setOpacity(box, this._winFadeStep);
				if (this._winFadeStep > 0) {
					setTimeout('imagePopup._winFade("out")',20);
				} else {
					this._deactivate();
				}
			}
		}
	},
	
	_deactivate: function() {
		if (this._active) {
			var box = jet.getEl('_IMAGEPOPUP');
			if (box) {
				document.body.removeChild(box);
				jet.removeEvent(document,"onclick",imagePopup._waitClick);
				this._active = false;
				this._winCenterX = 0;
				this._winCenterY = 0;
				this._imgW = 0;
				this._imgH = 0;
				this._imgLimitW = 0;
				this._imgLimitH = 0;
				this._objImage = null;
				this._resizeStep = 0;
				this._imageFadeStep = 0;
				this._winFadeStep = 100;
				this._winFadeMode = false;
				this._captionH = 0;
			}
		}
	}
	
}

imagePopup.preloadImages();