/**
* @version 2.6.0
* @copyright Lion Productions Kft. 2009-2011
*/
PictureZoom = {
	SPF : 1,
	MAX_FRAME : 5,
	index : 1000,
	lastAdded : false,
	nextAnim : false
}
/**
* 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, ha táblában van, nem lehet static, hanem absolute vagy relative!
* Ha képeket hozzárendelik a "lejátszó listához", akkor köztük lépkedni lehet az előző/következő linkekkel
* @params obj linkWithImg A képet tartalmazó link elem
* @params bool addToPlaylist Kép hozzáadása a lejátszó listához
*/
PictureZoom.addImage = function(linkWithImg, addToPlaylist) {
	var a = linkWithImg, dim;
	try {
		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.zoomInEvent);
				addEvent(a, 'click', function(ev) {return killEvent(ev);});
				a.style.cursor = this.addCursor('zoomin.cur');
				a.firstChild.title = a.firstChild.alt;
				if (this.lastAdded && addToPlaylist) {
					this.lastAdded.nextLink = a;
				}
				a.prevLink = (addToPlaylist) ? this.lastAdded : false;
				a.nextLink = false;
				this.lastAdded = a;
			}
		}
	} catch(err) {}
}

PictureZoom.zoomInEvent = function(ev) {
	var a = getObj(ev);
	while (a.tagName.toLowerCase() != 'a') {a = a.parentNode};
	PictureZoom.zoomIn(a);
	return killEvent(ev);
}
PictureZoom.zoomIn = function(a) {
	if (!a.cached) {
		// Háttér
		if (!this.veil) {
			this.veil = DOMEditor.createElement('div', {id : 'PictureZoomVeil'});
			this.veil.style.position 	= 'absolute';
			this.veil.style.top 		= 0;
			this.veil.style.left 		= 0;
			this.veil.style.width 	= '100%';
			this.veil.style.zIndex 	= 999;
			this.veil.style.filter 	= 'alpha(opacity=1)';
			this.veil.style.opacity 	= '0.1';
			this.veil.opac 			= 0;

			DOMEditor.append(document.body, this.veil);
			addEvent(window, 'resize', function() {PictureZoom.resize();});
			//addEvent(this.veil, 'click', this.zoomOut);

			// progressbar
			this.pbar = DOMEditor.createElement('img', {alt:'Töltés...', title:'Töltés...', src:'/skin/img/miniLoaderBBG.gif'});
			this.pbar.style.position 	= 'absolute';
			DOMEditor.append(document.body, this.pbar);
		}
		this.resize();
		this.pbar.style.display 	= '';

		// Nagy kép
		var obj = {
			className	: 'PZBigPic',
			alt			: a.firstChild.alt,
			title		: a.firstChild.alt
		}
		var img1 = DOMEditor.createElement('img', obj);
		img1.style.position = 'absolute';
		img1.style.zIndex 	= 2;
		img1.style.left 	= 0;
		img1.style.top 		= 0;
		img1.style.width 	= '100%';
		img1.style.height	= '100%';
		img1.style.cursor	= this.addCursor('zoomout.cur');
		addEvent(img1, 'load', function(ev) {
			var img = getObj(ev);
			if (!img) {
				// kép cache-ben van, ie
				PictureZoom.pbar.style.display 	= 'none';
				return;
			}
			img.parentNode.holder.style.display = 'none';
			PictureZoom.pbar.style.display 	= 'none';
		});
		img1.src = a.href;

		// Kis kép helykitöltőnek, míg a nagy kép töltődik
		obj = {
			className	: 'PZHolderPic',
			src 		: a.firstChild.src,
			alt			: 'Töltés...',
			title		: 'Töltés...'
		}
		var img2 = DOMEditor.createElement('img', obj);
		img2.style.position = 'absolute';
		img2.style.zIndex 	= 1;
		img2.style.left 	= 0;
		img2.style.top 		= 0;
		img2.style.width 	= '100%';
		img2.style.height 	= '100%';
		img2.style.cursor	= this.addCursor('zoomout.cur');

		// Konténer a képeknek
		obj = {
			className	: 'PZContainer',
			linkRef		: a,
			holder		: img2
		}
		var $ = DOMEditor.createElement('div', obj);
		$.style.position = 'absolute';
		addEvent($, 'click', this.zoomOutEvent);

		DOMEditor.append($, img1, img2);

		a.parentNode.insertBefore($, a);
		a.cached = true;
		a.bigRef = $;
	} else {
		var $ = a.bigRef;
	}

	this.animIn = true;
	$.style.zIndex 	= this.index++;

	if (!this.tidVeil && !this.nextAnim) {
		this.veil.style.display = '';
		this.veil.prop = {
			duration	: this.MAX_FRAME,
			time		: 0,
			alpha		: {
				start	: 0,
				change	: 70
			}
		}
		this.tidVeil = setInterval('PictureZoom.animVeil()', this.SPF);
	}
	if (!this.tid) {
		this.active = $;
		$.style.display = '';
		var dim = $windowSizes();
		var offs = $findPos(a);
		$.prop = {
			duration	: this.MAX_FRAME,
			time		: 0,
			width		: {
				start	: a.offsetWidth,
				change	: a.maxW - a.offsetWidth
			},
			height		: {
				start	: a.offsetHeight,
				change	: a.maxH - a.offsetHeight
			},
			left		: {
				start	: a.offsetLeft,
				change	: Math.round((dim.win.w - a.maxW) / 2) - offs.left + dim.scroll.l
			},
			top		: {
				start	: a.offsetTop,
				change	: Math.round((dim.win.h - a.maxH) / 2) - offs.top + dim.scroll.t
			}
		}

		this.tid = setInterval('PictureZoom.anim()', this.SPF);
	}

	if (this.pbar.style.display == '') {
		this.pbar.style.visibility= 'hidden';
		this.pbar.style.zIndex	= this.index;
		this.pbar.style.left		= $.prop.left.start + $.prop.left.change + Math.round((a.maxW - this.pbar.offsetWidth) / 2) + 'px',
		this.pbar.style.top		= $.prop.top.start + $.prop.top.change + Math.round((a.maxH - this.pbar.offsetHeight) / 2) + 'px'
	}
}
PictureZoom.zoomOutEvent = function(ev) {
	PictureZoom.nextAnim = false;
	PictureZoom.zoomOut();
}
PictureZoom.zoomOut = function() {
	this.animIn = false;
	if (!this.tidVeil && !this.nextAnim) {
		this.tidVeil = setInterval('PictureZoom.animVeil()', this.SPF);
	}
	if (!this.tid) {
		this.tid = setInterval('PictureZoom.anim()', this.SPF);
	}
	if (this.pbar.style.display == '') {
		this.pbar.style.visibility = 'hidden';
	}
	if (this.active.parentNode.toolbox) {
		this.active.parentNode.toolbox.style.display = 'none';
	}
}
PictureZoom.resize = function() {
	var win = $windowSizes();
	this.veil.style.height = win.html.h + "px";
}

PictureZoom.animVeil = function() {
	(this.animIn) ? this.veil.prop.time++ : this.veil.prop.time--;
	var _ = this.easingV(this.veil.prop.time, this.veil.prop.alpha.start, this.veil.prop.alpha.change, this.veil.prop.duration);
	this.veil.opac = _;
	this.veil.style.filter  = 'alpha(opacity=' + _ + ')';
	this.veil.style.opacity = _ / 100;

	if (this.veil.prop.time >= this.veil.prop.duration) {
		clearInterval(this.tidVeil);
		this.tidVeil = null;
	} else if (this.veil.prop.time < 1) {
		clearInterval(this.tidVeil);
		this.tidVeil = null;
		this.veil.style.display 	= 'none';
	}
}
PictureZoom.anim = function() {
	var $ = this.active, _;
	(this.animIn) ? $.prop.time++ : $.prop.time--;
	if ($.prop.time == 1) {
		$.linkRef.style.visibility = 'hidden';
	}
	for(var i in $.prop) {
		if (typeof $.prop[i] == 'object') {
			_ = this.easing($.prop.time, $.prop[i].start, $.prop[i].change, $.prop.duration);
			$.style[i] = _ + 'px';
		}
	}
	if ($.prop.time >= $.prop.duration) {
		// zoom in vége
		clearInterval(this.tid);
		this.tid = null;
		if (this.pbar.style.display == '') {
			this.pbar.style.visibility = '';
		}
		this.tooltip();
	} else if ($.prop.time < 1) {
		// zoom out vége
		clearInterval(this.tid);
		this.tid = null;
		$.linkRef.style.visibility = '';
		$.style.display = 'none';
		if (this.nextAnim) {
			this.zoomIn(this.nextAnim);
		}
	}
}
PictureZoom.easingV = function(t, b, c, d) {
	return c*t/d + b;
}
PictureZoom.easing = function(t, b, c, d) {
	return c*(t/=d)*t + b;
}
PictureZoom.addCursor = function(cur) {
	return 'url(/skin/img/' + cur + '), pointer';
}

PictureZoom.tooltip = function() {
	var $ = this.active.parentNode.toolbox;
	if (!$) {
		$ = this.active.parentNode.toolbox =  DOMEditor.createElement('div');
		$.className = 'PZTooltip';
		$.style.position 	= 'absolute';
		$.style.zIndex 		= this.index + 1;
		$.style.textAlign	= 'center';
		$.style.padding		= '3px';

		if (!this.active.firstChild.alt.$isEmpty()) {
			$.innerHTML = this.active.firstChild.alt;
		}
		if (this.active.linkRef.nextLink || this.active.linkRef.prevLink) {
			if ($.innerHTML != '') {
				$.innerHTML += '<br/><br/>';
			}
			var _, tpl = [];
			if (this.active.linkRef.prevLink) {
				tpl.push({
					n : 'a',
					a : {href : '#'},
					t : '&laquo; Előző',
					e : this.imgPrev
				});
			} else {
				$.innerHTML += 'Első kép';
			}
			tpl.push({
				n : 'span',
				a : false,
				t : ' | ',
				e : false
			});
			if (this.active.linkRef.nextLink) {
				tpl.push({
					n : 'a',
					a : {href : '#'},
					t : 'Következő &raquo;',
					e : this.imgNext
				});
			} else {
				tpl.push({
					n : 'span',
					a : false,
					t : 'Utolsó kép',
					e : false
				});
			}
			for(var i=0; i<tpl.length; ++i) {
				_ = DOMEditor.createElement(tpl[i].n, tpl[i].a);
				_.innerHTML = tpl[i].t;
				DOMEditor.append($, _);
				if (tpl[i].e) {
					addEvent(_, 'click', tpl[i].e);
				}
			}
		}

		DOMEditor.append(this.active.parentNode, $);
	}
	if ($.innerHTML == '') {
		$.style.display = 'none';
		return;
	}

	$.style.display		= 'block';
	$.style.width 		= parseInt(this.active.style.width) - 6 + 'px';
	$.style.left 		= this.active.style.left;
	$.style.top			= parseInt(this.active.style.top) + parseInt(this.active.style.height) + 'px';
}
PictureZoom.imgPrev = function(ev) {
	var obj = getObj(ev);
	PictureZoom.nextAnim = PictureZoom.active.linkRef.prevLink;
	PictureZoom.zoomOut();
	return killEvent(ev);
}
PictureZoom.imgNext = function(ev) {
	var obj = getObj(ev);
	PictureZoom.nextAnim = PictureZoom.active.linkRef.nextLink;
	PictureZoom.zoomOut();
	return killEvent(ev);
}

