Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}

function addEventHandler(el,event,handler){
	if (el.addEventListener)
		el.addEventListener(event,handler,false);
	else if (el.attachEvent)
		el.attachEvent("on"+event,handler);
}

function removeEventHandler(el,event,handler){
	if (el.removeEventListener)
		el.removeEventListener(event,handler,false);
	else if (el.detachEvent)
		el.detachEvent("on"+event,handler);
}

var OBJ = {
	hide: function(el) {
		el.style.display = 'none';
	},
	show: function(el) {
		el.style.display = '';
	},
	getWidth: function(el) {
	   	return el.offsetWidth; 
	},
	setWidth: function(el,w) {
	   	el.style.width = w +"px";
	},
	getHeight: function(el) {
		return el.offsetHeight;
	},
	setHeight: function(el,h) {
   		el.style.height = h +"px";
	},
	setTop: function(el,t) {
	   	el.style.top = t +"px";
	},
	setSrc: function(el,src) {
		el.src = src; 
	},
	setInnerHTML: function(el,content) {
		el.innerHTML = content;
	}
};

var fx = new Object();
fx.Base = function(){};
fx.Base.prototype.setOptions = function(options) {
	this.options = {
		duration: options["duration"] || 500,
		onComplete: options["onComplete"] || '',
		transition: options["transition"] || fx.sinoidal
	}
};

fx.Base.prototype.step = function() {
	var self = this;
	var time  = (new Date).getTime();
	if (time >= this.options.duration+this.startTime) {
		this.now = this.to;
		clearInterval (this.timer);
		this.timer = null;
		if (this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10);
	}
	else {
		var Tpos = (time - this.startTime) / (this.options.duration);
		this.now = this.options.transition(Tpos) * (this.to-this.from) + this.from;
	}
	this.increase();
};

fx.Base.prototype.custom = function(from, to) {
	var self = this;
	if (this.timer != null) return;
	this.from = from;
	this.to = to;
	this.startTime = (new Date).getTime();
	this.timer = setInterval (this.step.bind(this), 13);
};

fx.Base.prototype.hide = function() {
	this.now = 0;
	this.increase();
};

fx.Base.prototype.clearTimer = function() {
	clearInterval(this.timer);
	this.timer = null;
};

fx.Layout = function(){  };
fx.Layout.prototype = new fx.Base();
fx.Layout.prototype.initialize = function(el, options) {
	this.el = el;
	this.el.style.overflow = "hidden";
	this.iniWidth = this.el.offsetWidth;
	this.iniHeight = this.el.offsetHeight;
	this.setOptions(options);
};

fx.Height = function(){  };
fx.Height.prototype = new fx.Layout();
fx.Height.prototype.increase = function() {
	this.el.style.height = this.now + "px";
};

fx.Height.prototype.toggle = function() {
	if (this.el.offsetHeight > 0) this.custom(this.el.offsetHeight, 0);
	else this.custom(0, this.el.scrollHeight);
};

fx.Width = function(){  };
fx.Width.prototype = new fx.Layout();
fx.Width.prototype.increase = function() {
	this.el.style.width = this.now + "px";
};

fx.Width.prototype.toggle = function(){
	if (this.el.offsetWidth > 0) this.custom(this.el.offsetWidth, 0);
	else this.custom(0, this.iniWidth);
};

fx.Opacity = function(){  };
fx.Opacity.prototype = new fx.Base();
fx.Opacity.prototype.initialize = function(el, options) {
	this.el = el;
	this.now = 1;
	this.increase();
	this.setOptions(options);
};

fx.Opacity.prototype.increase = function() {
	if (this.now == 1 && (/Firefox/.test(navigator.userAgent))) this.now = 0.9999;
	this.setOpacity(this.now);
};
	
fx.Opacity.prototype.setOpacity = function(opacity) {
	if (opacity == 0 && this.el.style.visibility != "hidden") this.el.style.visibility = "hidden";
	else if (this.el.style.visibility != "visible") this.el.style.visibility = "visible";
	if (window.ActiveXObject) this.el.style.filter = "alpha(opacity=" + opacity*100 + ")";
	this.el.style.opacity = opacity;
};

fx.Opacity.prototype.toggle = function() {
	if (this.now > 0) this.custom(1, 0);
	else this.custom(0, 1);
};

fx.sinoidal = function(pos){
	return ((-Math.cos(pos*Math.PI)/2) + 0.5);
}

function gzt_img (){
	this.fileLoadingImage = "/nm2008/i/loading.gif";		
	this.filebtm_closeCloseImage = "/nm2008/i/closelabel.gif";
	this.resizeSpeed = 6;	
	this.borderSize = 10;	
	this.resizeDuration = (11 - this.resizeSpeed) * 100;
	
	this.initialize();
}

gzt_img.prototype.initialize = function() {
	var self = this;
	// if (!document.getElementsByTagName){ return; }
	// var anchors = document.getElementsByTagName('a');
	// for (var i=0; i<anchors.length; i++){
		// var anchor = anchors[i];
		// var relAttribute = String(anchor.getAttribute('rel'));
			
		// if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
			// anchor.onclick = function () { self.start(this); return false;};
		// }
	// }

	var objBody = document.getElementsByTagName("body").item(0);
	if(!objBody) return;
	
	var objOverlay = document.createElement("div");
	objOverlay.className = 'overlay';
	objOverlay.onclick = function() { self.end(); return false; }
	objBody.appendChild(objOverlay);
	this.overlay = objOverlay;
		
	var objLightbox = document.createElement("div");
	objLightbox.className = 'lightbox';
	objLightbox.style.display = 'none';
	objLightbox.onclick = function() { self.end(); return false; }
	objBody.appendChild(objLightbox);
	this.lightbox = objLightbox;
	
	var objouter_img_depot = document.createElement("div");
	objouter_img_depot.className = 'outer_img_depot';
	objLightbox.appendChild(objouter_img_depot);
	this.outer_img_depot = objouter_img_depot;
	
	var objbtm_close = document.createElement("div");
	objbtm_close.className = 'btm_close';
	objouter_img_depot.appendChild(objbtm_close);
		
	var objbtm_closeCloseImage = document.createElement("img");
	objbtm_closeCloseImage.setAttribute('src', this.filebtm_closeCloseImage);
	objbtm_closeCloseImage.style.cursor = "pointer";
	objbtm_closeCloseImage.onclick = function(){ self.end(); }
	objbtm_close.appendChild(objbtm_closeCloseImage);
	this.btm_closeCloseImage = objbtm_closeCloseImage;

	var objimg_depot = document.createElement("div");
	objimg_depot.className = 'img_depot';
	objouter_img_depot.appendChild(objimg_depot);
	this.img_depot = objimg_depot;
	
	var objLightboxImage = document.createElement("img");
	objLightboxImage.className = 'lightboxImage';
	objimg_depot.appendChild(objLightboxImage);
	this.lightboxImage = objLightboxImage;
	
	var objLoading = document.createElement("div");
	objLoading.className = 'loading';
	objimg_depot.appendChild(objLoading);
	this.loading = objLoading;
	
	var objLoadingLink = document.createElement("a");
	objLoadingLink.className = 'loadingLink';
	objLoadingLink.setAttribute('href','#');
	objLoadingLink.onclick = function() { self.end(); return false; }
	objLoading.appendChild(objLoadingLink);
	this.loadingLink = objLoadingLink;
	
	var objLoadingImage = document.createElement("img");
	objLoadingImage.setAttribute('src', this.fileLoadingImage);
	objLoadingLink.appendChild(objLoadingImage);
	this.loadingImage = objLoadingImage;

	var objimg_data_depot = document.createElement("div");
	objimg_data_depot.className = 'clearfix img_data_depot';
	objLightbox.appendChild(objimg_data_depot);
	this.img_data_depot = objimg_data_depot;

	var objimg_data = document.createElement("div");
	objimg_data.className = 'img_data';
	objimg_data_depot.appendChild(objimg_data);
	this.img_data = objimg_data;
	
	var objimg_details = document.createElement("div");
	objimg_details.className = 'img_details';
	objimg_data.appendChild(objimg_details);
	this.img_details = objimg_details;
	
	var objCaption = document.createElement("span");
	objCaption.className = 'caption';
	objimg_details.appendChild(objCaption);
	this.caption = objCaption;
	
	var overlayEffect = new fx.Opacity();
	overlayEffect.initialize(objOverlay, { duration: 50 });	
	overlayEffect.hide();
	this.overlayEffect = overlayEffect;
	
	var imageEffect = new fx.Opacity();
	imageEffect.initialize(objLightboxImage, { duration: 50, onComplete: function() { img_detailsEffect.custom(0,1); }});
	imageEffect.hide();
	this.imageEffect = imageEffect;
		
	var img_detailsEffect = new fx.Opacity();
	img_detailsEffect.initialize(objimg_data_depot, { duration: 60, onComplete: function() { /* navEffect.custom(0,1);  */}}); 
	img_detailsEffect.hide();
	this.img_detailsEffect = img_detailsEffect;
};
	
gzt_img.prototype.start = function(imageLink) {
	var arrayPageSize = this.getPageSize();
	
	/*@cc_on  
		OBJ.setWidth(this.overlay, arrayPageSize[0]);
		
		var arrayPageScroll = this.getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);
		OBJ.setTop(this.lightbox, lightboxTop);
		
		var version = @_jscript_version; 
	   	version = version.toString().substring(2,3);
   		if(version==6 || version==7) this.isIE = true;
	@*/   
	if(!this.isIE){
		if(arrayPageSize[0]>arrayPageSize[2]+16)
			var w = arrayPageSize[0];
		else var w = arrayPageSize[0]-17;
		OBJ.setWidth(this.overlay, w);
	}

	this.prev_width = arrayPageSize[0] - 16; // -scrollwidth
	this.prev_height = arrayPageSize[1];
		
	this.hideFlashes();	
	this.hideSelectBoxes();

	OBJ.setHeight(this.overlay, arrayPageSize[1]);
	this.overlayEffect.custom(0,0.8);
		
	OBJ.show(this.lightbox);
		
	this.image = [imageLink.getAttribute("href"), imageLink.getAttribute("title")];
	this.changeImage(this.image[0]);

        addEventHandler(document.body, "keydown", this.doOnKeyDown.bind(this));

};


gzt_img.prototype.changeImage = function(imageLink) {
	var self = this;
		
	OBJ.show(this.loading);
	this.img_detailsEffect.hide();
	this.imageEffect.hide();
		
	var imgPreloader = new Image();
	imgPreloader.onload = function(){
		OBJ.setSrc(self.lightboxImage, imageLink);
		self.resizeimg_depot(imgPreloader.width, imgPreloader.height);
		
		var arrayPageSize = self.getPageSize();  
		if(arrayPageSize[2] < imgPreloader.width){
			self.overlay.style.width = imgPreloader.width + 20;
			self.lightbox.style.position = "absolute";
		}
	
		if(arrayPageSize[1] < imgPreloader.height) self.overlay.style.height = imgPreloader.height + 40; // это если картинка реально больше всего 
		if(arrayPageSize[3] < imgPreloader.height + self.lightbox.offsetTop){ // это если картинка не влезает в экран
			self.lightbox.style.position = "absolute";
			var arrayPageScroll = self.getPageScroll();
			var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);
			OBJ.setTop(self.lightbox, lightboxTop);
		}
	}
	imgPreloader.src = imageLink;
};

gzt_img.prototype.resizeimg_depot = function( imgWidth, imgHeight) {
	var self = this;
	this.wCur = OBJ.getWidth(this.outer_img_depot);
	this.hCur = OBJ.getHeight(this.outer_img_depot);

	wDiff = (this.wCur - this.borderSize * 2) - imgWidth;
	hDiff = (this.hCur - this.borderSize * 2) - imgHeight;
		
	reHeight = new fx.Height();
	reHeight.initialize(this.outer_img_depot, { duration: this.resizeDuration });
	reHeight.custom(OBJ.getHeight(this.outer_img_depot),imgHeight+(this.borderSize*2)); 
	reWidth = new fx.Width();
	reWidth.initialize(this.outer_img_depot, { duration: this.resizeDuration, onComplete: function() { self.imageEffect.custom(0,1); }});
	reWidth.custom(OBJ.getWidth(this.outer_img_depot),imgWidth+(this.borderSize*2));

	if((hDiff == 0) && (wDiff == 0)){
		if (navigator.appVersion.indexOf("MSIE")!=-1){ this.pause(250); } else { this.pause(100);} 
	}

	OBJ.setWidth( this.img_data_depot, imgWidth + (this.borderSize * 2));
	this.showImage();
};
	
gzt_img.prototype.showImage = function(){
	OBJ.hide(this.loading);
	this.updateDetails(); 
};
	
gzt_img.prototype.updateDetails = function() {
	OBJ.show(this.caption);
	OBJ.setInnerHTML(this.caption, this.image[1] || "&nbsp;");
};

gzt_img.prototype.end = function() {
	var self = this;
	OBJ.hide(this.lightbox);
	this.imageEffect.toggle();
	this.overlayEffect.custom(0.8,0);
	this.overlayEffect.options["onComplete"] = function() { if(self.prev_width) OBJ.setWidth(self.overlay, self.prev_width); this.options["onComplete"] = null;  };
	this.showSelectBoxes();
	this.showFlashes();
	
	if(!this.isIE){  this.lightbox.style.position = "fixed"; OBJ.setTop(this.lightbox, "100"); }
        removeEventHandler(document.body, "keydown", this.doOnKeyDown.bind(this));
}

gzt_img.prototype.showSelectBoxes = function(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
};

gzt_img.prototype.hideSelectBoxes = function(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
};

gzt_img.prototype.hideFlashes = function(){
	if(navigator.userAgent.indexOf('Opera')!=-1 || this.isIE){
		var arr = document.embeds;
		for(var i=0; i<arr.length; i++)
			if(arr[i].parentElement) arr[i].parentElement.style.visibility = "hidden";
	}
	else {
		for (var ems = document.embeds, i = 0, em; em = ems[i]; i++) {
		    em.setAttribute("wmode", "opaque");
		    var nx = em.nextSibling;
			var pn = em.parentNode;
	    	pn.removeChild(em);
	    	pn.insertBefore(em, nx);
		}
	}
}

gzt_img.prototype.showFlashes = function(){
	if(navigator.userAgent.indexOf('Opera')!=-1 || this.isIE){
		var arr = document.embeds;
		for(var i=0; i<arr.length; i++)
			if(arr[i].parentElement) arr[i].parentElement.style.visibility = "visible";
	}
}

gzt_img.prototype.pause = function(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
};

gzt_img.prototype.getPageScroll = function(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	var arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
 
gzt_img.prototype.getPageSize = function(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		var pageHeight = windowHeight;
	} else { 
		var pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		var pageWidth = windowWidth;
	} else {
		var pageWidth = xScroll;
	}

	var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

gzt_img.prototype.doOnKeyDown = function(ev){ ev = ev||window.event; if(ev.keyCode==27) this.end();  };

var MyGztImg = new gzt_img();