var imgZoom = new Class({

	initialize: function(options){
		if(options){
			for(var o in options){
				this[o] = options[o];
			}
		}

		this.open = false;
		this.loading = true;

		this.loadImageZoom();
	},

	loadImageZoom: function(){
		this.zoom = new Asset.image(this.src,{onload:this.onLoadImageZoom.bind(this)});
	},

	calculePositions: function(){
		this._holder = this.item.getCoordinates();
		this._mask = {
			width: this.size ? this.size.width : this._holder.width,
			height: this.size ? this.size.height : this._holder.height,
			left: this._holder.left+220,
			top: this._holder.top-0
			//top: this._holder.top+360 en Explorer al parecer
		};
		this._arrow = {
			width: (this._mask.width*this._holder.width)/this.zoom.width,
			height: (this._mask.height*this._holder.height)/this.zoom.height,
			left: this._holder.left,
			top: this._holder.top
		};
		this._delta = {
			x: this._holder.width-this._arrow.width,
			y: this._holder.height-this._arrow.height
		};

		['arrow','holder','mask'].each(function(l,i){
			this[l].setStyles({
				'z-index': i+100,
				'position': 'absolute',
				'width': this['_'+l].width+'px',
				'height': this['_'+l].height+'px',
				'left': this['_'+l].left+'px',
				'top': this['_'+l].top+'px'
			});
		},this);
	},

	onLoadImageZoom: function(){
		this.holder = new Element('div').inject(document.body);
		this.arrow = new Element('div').inject(document.body);;
		this.mask = new Element('div').inject(document.body);

		this.zoom.setStyle('position','absolute').inject(this.mask);
		this.arrow.setStyle('background-color','black').setOpacity(0.5);
		this.holder.setStyle('background-image','url(transparent.gif)');
		this.mask.setStyle('overflow','hidden');

		this.hide();

		this.holder.addEvents({
			'mousemove': this.update.bind(this),
			'mouseleave': this.hide.bind(this)
		});

		this.item.addEvents({
			'mouseenter': this.show.bind(this)
		});

		this.loading = false;

		window.addEvent('resize',this.calculePositions.bind(this));
	},

	update: function(e){
		if(this.open){
			e = new Event(e);

			// arrow position
			var ax = e.page.x-this._arrow.width/2;
			var ay = e.page.y-this._arrow.width/2;

			if(window.ie6){
				ax += document.body.scrollLeft;
				ay += document.body.scrollTop;
			}

			if(ax > this._holder.left+this._delta.x){
				ax = this._holder.left+this._delta.x;
			}else if(ax < this._holder.left){
				ax = this._holder.left;
			}
			if(ay > this._holder.top+this._delta.y){
				ay = this._holder.top+this._delta.y;
			}else if(ay < this._holder.top){
				ay = this._holder.top;
			}

			this.arrow.setStyles({left:ax+'px',top:ay+'px'});

			// zoom position
			var zx = 0-(ax-this._holder.left)*this.zoom.width/this._holder.width;
			var zy = 0-(ay-this._holder.top)*this.zoom.height/this._holder.height;

			this.zoom.setStyles({left:zx+'px',top:zy+'px'});
		}
	},

	show: function(){
		if(!this.loading){
			this.open = true;
			this.calculePositions();
			$$(this.holder,this.arrow,this.mask).setStyle('display','block');
		}
	},

	hide: function(){
		this.open = false;
		$$(this.holder,this.arrow,this.mask).setStyle('display','none');
	}

});
/*Date: Tue, 07 Sep 2010 13:02:23 GMT */