/**
* @version 2.0.1
* @copyright Lion Productions Kft. 2009
*/
PictureZoom = {
}
/**
* Képek hozzáadása
* A képeknek egy link-ben kell lenniük, a linkek class paraméterében szerepelni kell
* a nagy kép méreteinek, ebben a formátumban: widthxheight
* A link egy div dobozban legyen
* @params obj linkWithImg A képet tartalmazó link elem
*/
PictureZoom.addImage = function(linkWithImg) {
	var a = linkWithImg, dim;
	if (a.href && a.firstChild.src) {
		if (dim = a.className.match(/(\d+)x(\d+)/)) {
			a.maxW = dim[1];
			a.maxH = dim[2];
			a.cached = false;
			addEvent(a, 'mousedown', this.zoomIn);
			addEvent(a, 'click', function(ev) {return killEvent(ev);});
		}
	}
}

PictureZoom.zoomIn = function(ev) {
	var a = getObj(ev);
	if (a.src) a = a.parentNode;
	var PZ = PictureZoom;
	if (!a.cached) {
		// Háttér
		if (!PZ.veil) {
			PZ.veil = DOMEditor.createElement('div', {id : 'PictureZoomVeil'});
			PZ.veil.style.position 	= 'absolute';
			PZ.veil.style.top 		= 0;
			PZ.veil.style.left 		= 0;
			PZ.veil.style.width 	= '100%';
			PZ.veil.style.zIndex 	= 999;
			PZ.veil.style.filter 	= 'alpha(opacity=1)';
			PZ.veil.style.opacity 	= '0.1';
			PZ.veil.opac 			= 0;
			PZ.resize();
			DOMEditor.append(document.body, PZ.veil);
			addEvent(window, 'resize', function() {PictureZoom.resize();});
			addEvent(PZ.veil, 'click', PZ.zoomOut);
		}

		var longer	= (a.maxW > a.maxH) ? 'W' : 'H';
		var dim = windowSizes();
		var offs = findPos(a);

		// Nagy kép
		var obj = {
			className : 'PZBigPic',
			src 	  : a.href
		}
		var img1 = DOMEditor.createElement('img', obj);
		img1.style.position = 'absolute';
		img1.style.zIndex = 2;
		img1.style.left = 0;
		img1.style.top = 0;
		(longer == 'W') ? img1.width = a.offsetWidth : img1.height = a.offsetHeight;
		addEvent(img1, 'load', function(ev) {
			var img = getObj(ev);
			img.parentNode.holder.style.display = 'none';}
		);

		// Kis kép helykitöltőnek, míg a nagy kép töltődik
		obj = {
			className	: 'PZHolderPic',
			src 		: a.firstChild.src
		}
		var img2 = DOMEditor.createElement('img', obj);
		img2.style.position = 'absolute';
		img2.style.zIndex = 1;
		img2.style.left = 0;
		img2.style.top = 0;

		// Konténer a képeknek
		obj = {
			className	: 'PZContainer',
			minW		: a.offsetWidth,
			minH		: a.offsetHeight,
			linkRef		: a,
			holder		: img2,
			longer		: longer,
			posY		: -(offs.top - dim.scroll.t - Math.round((dim.win.h - a.maxH) / 2)),
			posX		: -(offs.left - dim.scroll.l - Math.round((dim.win.w - a.maxW) / 2)),
			curX		: 0,
			curY		: 0
		}
		var div = DOMEditor.createElement('div', obj);
		div.style.position 	= 'absolute';
		div.style.zIndex 	= 1000;
		addEvent(div, 'click', PZ.zoomOut);


		DOMEditor.append(div, img1, img2);
		a.parentNode.insertBefore(div, a);
		a.cached = true;
		a.bigRef = div;
	} else {
		var div = a.bigRef;
	}
	PZ.veil.style.display = '';
	PZ.tidVeil = setInterval('PictureZoom.animVeil()', 2);
	PZ.tid = setInterval('PictureZoom.anim()', 2);
	PZ.active = div;
	div.style.display = '';
	a.style.visibility = 'hidden';

	eval('PZ.percent = 100 * div.min' + div.longer + ' / a.maxW');
	return killEvent(ev);
}
PictureZoom.zoomOut = function(ev) {
	var PZ = PictureZoom;
	if (PZ.tid) clearInterval(this.tid);
	if (PZ.tidVeil) clearInterval(this.tidVeil);
	PZ.tid = PZ.tidVeil = null;
	PZ.veil.style.filter 	= 'alpha(opacity=1)';
	PZ.veil.style.opacity 	= '0.1';
	PZ.veil.style.display 	= 'none';
	PZ.veil.opac 			= 0;

	if (PZ.active.longer == 'W') {
		PZ.active.firstChild.width  = PZ.active.holder.width  = PZ.active.minW;
	} else {
		PZ.active.firstChild.height  = PZ.active.holder.height  = PZ.active.minH;
	}
	PZ.active.firstChild.style.top = PZ.active.holder.style.top = 0;
	PZ.active.firstChild.style.left = PZ.active.holder.style.left = 0;
	PZ.active.style.display = 'none';
	PZ.active.curX = PZ.active.curY = 0;
	PZ.active.linkRef.style.visibility = '';
}
PictureZoom.resize = function() {
	var win = windowSizes();
	this.veil.style.height = win.html.h + "px";
	this.veil.style.width = win.html.w + "px";
}

PictureZoom.animVeil = function() {
	if (this.veil.opac >= 80) {
		clearInterval(this.tidVeil);
		this.tidVeil = null;
	} else {
		this.veil.opac += 10;
		this.veil.style.filter  = 'alpha(opacity=' + this.veil.opac + ')';
		this.veil.style.opacity = this.veil.opac / 100;
	}
}
PictureZoom.anim = function() {
	if (this.percent >= 100 && this.active.curX == this.active.posX && this.active.curY == this.active.posY) {
		clearInterval(this.tid);
		this.tid = null;
		return;
	}

	if (this.percent < 100) {

		this.percent += 15;
		if (this.percent > 100) this.percent = 100;
		if (this.active.longer == 'W') {
			this.active.firstChild.width  = this.active.holder.width  = Math.round((this.active.linkRef.maxW * this.percent) / 100);
		} else {
			this.active.firstChild.height  = this.active.holder.height  = Math.round((this.active.linkRef.maxH * this.percent) / 100);
		}


	}

	// poziccionálás képernyő közepe felé
	if (this.active.curX != this.active.posX) {
		if (this.active.curX > this.active.posX) {
			this.active.curX -= 60;
			if (this.active.curX < this.active.posX) this.active.curX = this.active.posX;
		} else {
			this.active.curX += 60;
			if (this.active.curX > this.active.posX) this.active.curX = this.active.posX;
		}
		this.active.firstChild.style.left = this.active.holder.style.left = this.active.curX + 'px';
	}
	if (this.active.curY != this.active.posY) {
		if (this.active.curY > this.active.posY) {
			this.active.curY -= 15;
			if (this.active.curY < this.active.posY) this.active.curY = this.active.posY;
		} else {
			this.active.curY += 15;
			if (this.active.curY > this.active.posY) this.active.curY = this.active.posY;
		}
		this.active.firstChild.style.top = this.active.holder.style.top = this.active.curY + 'px';
	}


	//this.active.scrollIntoView();
	this.resize();

}


