var jvZoom = new Class({
	options:{		
	},
	initialize:function(options){
		this.setOptions(options); 
		this.container = $$('.jv_zoom_container')[this.options.id];		
		this.zoom1 = $ES('a.jv_zoom',this.container)[0];		
		this.effectType = this.zoom1.getProperty('rel').split(":")[1];
		if(this.effectType !='default' && this.effectType != 'tint' && this.effectType !='inner') this.effectType  = 'default';
		this.sImg = $ES('img',this.zoom1)[0];			
		this.img2 = new Image();        
        this.img2.src = this.zoom1.getProperty("href");
		this.destU = 0;
		this.destV = 0;
        this.currV = 0;
        this.currU = 0;
		this.init2();		
	},
	init2:function(){	
		this.mouseTrap = new Element('div',{'styles':{
			'z-index':'999',
			'position':'absolute',
			'left': '-1px',
			'top': '-1px',
			'background-image':'url(\".\")',
			'height':this.sImg.offsetHeight,
			'width':this.sImg.offsetWidth
		},'class':'mouseTrap'});
		this.mouseTrap.injectAfter(this.zoom1);		
		this.mouseTrap.addEvent('mousemove',function(event){			
			this.mx =  event.pageX || event.clientX + document.documentElement.scrollLeft;								
			this.my = event.pageY || event.clientY + document.documentElement.scrollTop;
		}.bind(this));
		this.mouseTrap.addEvent('mouseleave',function(event){			
			$clear(this.controlTimer);				
			if(this.zoomDiv)this.zoomDiv.remove();
			if(this.lens) this.lens.remove();
			if(this.tint)this.tint.remove();
		}.bind(this));
		this.mouseTrap.addEvent('mouseenter',function(event){
			this.mx = event.client.x + document.documentElement.scrollLeft;			
           	this.my = event.client.y + document.documentElement.scrollTop;           			
			var xPos = 11;
			var yPos = -1;
			var siw = this.sImg.offsetWidth;			
            var sih = this.sImg.offsetHeight;			
			var w = siw;
			var h = sih;			
			if(this.options.zoom_width != "auto") {
				siw = this.options.zoom_width.toInt();
			}
			if(this.options.zoom_height != "auto") {
				sih = this.options.zoom_height;
			}
			xPos += siw;
			//Inside
			if(this.effectType == "inner") {
				xPos = -1;
				yPost = -1;
			}
			//End inside		
			//if(this.zoomDiv){this.zoomDiv.remove();}
			this.zoomDiv = new Element('div',{'styles':{			
			'z-index':'99',
			'position':'absolute',
			'left':xPos,
			'top':yPos,
			'background-image':'url('+this.img2.src+')',
			'height':sih,
			'width':siw
		},'class':'jv_zoom_big'});		
		if(this.options.show_title == "yes" && this.sImg.getProperty("title") !="")	{
			var contentHTML = '<div style="display:block;position:absolute;z-index:99;height:'+sih+'px;width:'+siw+'px;"><div class="jv_zoom_title">'+this.sImg.getProperty("title")+'</div></div>';
			this.zoomDiv.setHTML(contentHTML);
			$ES('.jv_zoom_title',this.zoomDiv)[0].setStyle('opacity',0.5);
		} else {	
			this.zoomDiv.setHTML('<div style="display:block;position:absolute;z-index:99;height:'+sih+'px;width:'+siw+'px;"></div>');
		}
		this.zoomDiv.injectAfter(this.mouseTrap);
		this.zoomDiv.setStyle('display','');		
		this.cw = ((this.sImg.offsetWidth / this.img2.width) * this.zoomDiv.offsetWidth).toInt();		
        this.ch = ((this.sImg.offsetHeight / this.img2.height) * this.zoomDiv.offsetHeight).toInt();		
		this.lens = new Element('div',{'styles':{
			'display':'none',
			'z-index':'98',
			'position':'absolute',	
			'cursor':'move',	
			'height':this.ch,
			'width':this.cw
		},'class':'jv_zoom_lens'});
		this.lens.setHTML('<div style="display:block;width:'+this.cw+'px;height:'+this.ch+'px;">&nbsp;</div>');
		this.lens.injectAfter(this.sImg);		
		this.lens.setStyle('display','');
		this.mouseTrap.setStyle('cursor','move');
		if(this.effectType == 'tint') {	
			this.lens.setStyle('background', 'url("' + this.sImg.getProperty('src') + '")');	
			this.tint = new Element('div',{
				'styles':{
					'display':'none',
					'position':'absolute',
					'left':'0px',
					'cursor':'move',
					'top':'0px',
					'width':this.sImg.offsetWidth,
					'height':this.sImg.offsetHeight,
					'background-color':'#ff0000'
				}
			});
			this.tint.injectAfter(this.lens);
			this.tint.setStyle('display','');
			this.tint.setStyle('opacity',0.5);
		} else {
			this.lens.setStyle('opacity',0.5);
		}	
		this.controlLoop();	
		this.controlTimer = this.controlLoop.periodical(30,this);	
		}.bind(this));		
	},
	controlLoop:function(){
		 if (this.lens) {		 					
		 	    var x = (this.mx - this.sImg.getLeft() - (this.cw * 0.5)) >> 0;
                var y = (this.my - this.sImg.getTop() - (this.ch * 0.5)) >> 0;
                if (x < 0) {
                    x = 0;
                }                           
                else if (x > (this.sImg.offsetWidth - this.cw)) {
                    x = (this.sImg.offsetWidth - this.cw);
                }               
                if (y < 0) {
                    y = 0;
                }
                else if (y > (this.sImg.offsetHeight - this.ch)) {
                   y = (this.sImg.offsetHeight - this.ch);
                }            
				this.lens.setStyles({left:x,top:y});               
				this.lens.setStyle('background-position',(-x) + 'px ' + (-y) + 'px');
                this.destU = (((x) / this.sImg.offsetWidth) * this.img2.width) >> 0;
                this.destV = (((y) / this.sImg.offsetHeight) * this.img2.height) >> 0;
                this.currU += (this.destU - this.currU) / 3;
                this.currV += (this.destV - this.currV) / 3;
				this.zoomDiv.setStyle('background-position',(-(this.currU >> 0) + 'px ') + (-(this.currV >> 0) + 'px'));                          
            }          
	}
	
});
jvZoom.implement(new Events);
jvZoom.implement(new Options);

