/* Zoomer class. Copyright by ADenis (adeniss@ukr.net) */

function Zoomer(){
	var zoomer = null;
	var prototype = null;
	var clone = null;
	//var ANIMATOR = new ANIMATOR();
	
	this.ratio = 1;
	this.eventIn = null;
	this.eventOut = null;
	this.handlerIn = null;
	this.handlerOut = null;
	
	this.zoomIn = function(object, ratio, eventIn, disableAnimation, evt){
		evt = (evt)?evt:(window.event)?window.event:"";

		this.zoomOut();
		
		//alert('1');
		
		prototype = object;		
		
		this.handlerIn = prototype.getAttribute(eventIn);
		this.handlerOut = prototype['onmouseout'];		
		this.eventIn = eventIn;
		
		prototype.style.visibility = "hidden";
		
		clone = prototype.cloneNode(true);
		
		//alert('2');
		clone.style.visibility = "visible";
		
		prototype.setAttribute(this.eventIn, null);				
		clone.setAttribute(this.eventIn, null);
		
		//alert('3');
		
		var parent = prototype.parentNode;		
		parent.replaceChild(clone, prototype);
		
		//alert('4');
		
		//displayArray(evt);
		
		var _width = clone.clientWidth;
		var _height = clone.clientHeight;
		var width = _width*ratio;
		var height = _height*ratio;
		//var _x = getX(clone);
		//var _y = getY(clone);
		
		//alert(-evt.offsetX+clone.offsetTop);
		
		var offsetX = (evt.layerX)?evt.layerX:(evt.offsetX + clone.offsetLeft);
		var offsetY = (evt.layerY)?evt.layerY:(evt.offsetY + clone.offsetTop);	
		
		var _x = evt.clientX - offsetX + clone.offsetLeft;
		var _y = evt.clientY - offsetY + clone.offsetTop;

		var x = _x - (width - _width)/2;
		var y = _y - (height - _height)/2;				
		
		//alert(-evt.layerY + clone.offsetTop);
		
		/*
		prototype.style.left = x + "px";
		prototype.style.top = y + "px";
		prototype.style.width = width + "px";
		prototype.style.height = height + "px";
		*/
		//document.body.insertBefore(prototype, document.body.firstChild);
		
		
		//alert('5');
		
		prototype.style.position = "absolute";		
		prototype.style.left = _x + "px";
		prototype.style.top = _y + "px";
		prototype.style.width = _width + "px";
		prototype.style.height = _height + "px";
		////alert(prototype.style.border);
		prototype.style.border = "5px solid white";
		prototype.style.zIndex = 200;
		
		document.body.appendChild(prototype);
		
		prototype.style.visibility = "visible";
		
		//alert('6');
		
		if(disableAnimation){
				prototype.style.left = x+"px";
				prototype.style.top = y+"px";
				prototype.style.width = width+"px";
				prototype.style.height = height+"px";
		}else{
			getAnimator().animateObj(prototype.style, 'left', prototype.style.left, x+"px", true, 0.25, null, null, 'parabola');
			getAnimator().animateObj(prototype.style, 'top', prototype.style.top, y+"px", true, 0.25, null, null, 'parabola');
			getAnimator().animateObj(prototype.style, 'width', prototype.style.width, width+"px", true, 0.25, null, null, 'parabola');
			getAnimator().animateObj(prototype.style, 'height', prototype.style.height, height+"px", true, 0.25, null, null, 'parabola');
		}
		
		//alert('7');

		addListener(prototype, 'onmouseout', this, this.zoomOut, true, false, true);
	}
	
	this.zoomOut = function(){
		if(prototype && clone){			
			getAnimator().removeFromAnimation(prototype.style);
			
			var parent = clone.parentNode;
			
			if(parent){
				prototype.style.visibility = "hidden";
				
				prototype.style.position = clone.style.position;
				
				prototype.style.left = clone.style.left;
				prototype.style.top = clone.style.top;
				prototype.style.width = clone.style.width;
				prototype.style.height = clone.style.height;
				prototype.style.border = clone.style.border;
			
				parent.replaceChild(prototype, clone);
				prototype.setAttribute(this.eventIn, this.handlerIn);
				prototype['onmouseout'] = this.handlerOut;
				
				prototype.style.visibility = "visible";
				
			}else if(prototype.parentNode){				
				prototype.parentNode.removeChild(prototype);
				delete prototype;
			}						
		}
		
		removeListener(prototype, 'onmouseout', this, this.zoomOut);
		prototype = null;
		clone = null;
	}
}

var ZOOMER = null;

function getZoomer() {
	if (!ZOOMER) {
		ZOOMER = new Zoomer();
	}
	
	return ZOOMER;
}