var imageGallery = {

	oGallery : null,
	sCount : 0,
	oCanvas : null,
	fIndex : 0,
	fSlideshow : 0,
	imClasses : {
		cDefault : null, // стиль обычной картинки
		cActive: null  // стиль выбранной картинки
	},
	fInterval : 5000,
	fTransition : null,

	create : function (oDiv,oCanvas) {

		this.oGallery = new Array();
		this.oCanvas = oCanvas;
		var im = oDiv.getElementsByTagName('div');
		for (var i = 0; i < im.length; i++) {
			this.oGallery.push(this.createSlide(im[i]));
			this.sCount++;
		} if (this.sCount) {			
			this.fIndex = 0;
			if (this.imClasses.cActive != null) {
				this.oGallery[0].elem.className = this.imClasses.cActive;
			} 
//			this.play();
		}

	},

	select : function (oId) {

		this.stop();
		if (oId != this.fIndex) this.show(oId,this.fIndex);
		return false;

	},

	show : function (oId,oCurrent) {

		this.fTransition = {total: 0,current: 0,fps : 30};
		if (this.imClasses.cDefault != null) {
			this.oGallery[oCurrent].elem.className = this.imClasses.cDefault;
		} this.oGallery[oId].src.style.zIndex = 2;
		this.oGallery[oId].src.style.visibility = 'visible';
		this.fTransition.src = this.oGallery[oCurrent].src;
		this.fTransition.dst = this.oGallery[oId].src;
		setTimeout("imageGallery.transition()",this.fTransition.fps);
		this.fIndex = oId;

	},

	transition : function () {

		if (this.fTransition.current < this.fTransition.total) {
			setOpacity(100 - Math.round(this.fTransition.current * 100/this.fTransition.total),this.fTransition.src);		
			this.fTransition.current++;
			setTimeout("imageGallery.transition()",this.fTransition.fps);
		} else {
			this.fTransition.dst.style.zIndex = 3;
			this.fTransition.src.style.visibility = 'hidden';
			this.fTransition.src.style.zIndex = 1;
			setOpacity(100,this.fTransition.src);
			if (this.imClasses.cActive != null) {
				this.oGallery[this.fIndex].elem.className = this.imClasses.cActive;
			}

		}

	},

	createSlide : function (oDiv) {		
		
		var oRef = this;
		var oIndex = this.sCount;
		var slide = {elem : oDiv};
		slide.src = document.createElement('div');
		slide.src.style.position = 'absolute';
		slide.src.style.top = 0;
		slide.src.style.left = 0;
		slide.src.style.zIndex = this.sCount ? 1 : 3;
		slide.src.style.visibility = this.sCount ? 'hidden' : 'visible';
		var im = slide.src;
		if (slide.elem.childNodes[0].getAttribute("rel")) {
			im = im.appendChild(document.createElement("a"));
			var a = slide.elem.childNodes[0].getAttribute("rel").split('#');
			if (a.length == 2) {
				var s = a[1].split('-');
				im.href = a[0];
				im.onclick = function (e) {window.open(a[0], "", 'menubar=no,directories=no,location=no,resizable=no, target=myWindow scrollbars=no,width='+s[0]+',height='+s[1]); return false;}
			} else {
				im.href = slide.elem.childNodes[0].getAttribute("rel");
				im.target = "_blank";
			}
		} im = im.appendChild(document.createElement('img'));
		im.src = slide.elem.childNodes[0].getAttribute("href");
		slide.elem.onclick = function (e) {return oRef.select(oIndex);}
		this.oCanvas.appendChild(slide.src);
		return slide;

	},

	play : function () {
		
		if (this.sCount > 1) {
			if (!this.fSlideshow) { 
				this.fSlideshow = setInterval("imageGallery.play()",this.fInterval);
			} else {
				var cur = this.fIndex;
				this.fIndex++;
				if (this.fIndex == this.sCount) this.fIndex = 0;
				this.show(this.fIndex,cur);
			}			
		}

	},

	stop : function () {

		if (this.fSlideshow) {
			clearInterval(this.fSlideshow);			
			this.fSlideshow = 0;
		}

	}

}

function setOpacity(opacity,oElem) {

	if (navigator.userAgent.indexOf("Firefox") != -1) {
		if (opacity == 100) opacity = 99.9999;
	} oElem.style.filter = "alpha(opacity=" + opacity + ")";
	oElem.style.opacity = (opacity / 100);

}

function catalogInitScripts () {

	if ((o = document.getElementById("gallerySelector")) && (c = document.getElementById("galleryCanvas"))) imageGallery.create(o,c);

}

if (window.addActiveHook) {
	addActiveHook(catalogInitScripts);
} else {
	window.onload = catalogInitScripts;
}