/**
	zoomy v 2.0.1 27.01.2009
	Copyright (c) 2008 Filippo Buratti; info [at] cssrevolt.com [dot] com; http://www.filippoburatti.net/

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

var Zoomy = Class.create({

	initialize: function(element, options) {
	  	
		this.options = {
			appearDuration	: 0.5,
			closeButton		: true,
			draggable		: false,
			hideCaption		: false
		}
		Object.extend(this.options, options || {});
		
		this.element  = $(element);
		this.addEvent = this.prepareZoom.bindAsEventListener(this);
		this.element.observe("click", this.addEvent);
	},
	  
  prepareZoom: function(event) {
		
		Event.stop(event);	
		
		new Effect.Opacity(this.element, { from: 1.0, to: 0.3, duration: 0.5 });
		
		var zClose		= (this.options.draggable || this.options.closeButton) ? "<div id='close' title='sluiten'></div>" : "";
			
		var zSrc 		= this.element.readAttribute('href');
			
		var zTitle		= this.element.readAttribute('title');
			
		var zCaption 	= (!this.options.hideCaption) ? "<p>"+ zTitle + "</p>" : "";
			
		if ($('zoomy')) { 
			$("zoomy").remove();
		}
			
		$(document.body).insert("<div id='zoomy'>"+ zCaption + zClose +"<img src='"+ zSrc +"' alt='"+ zTitle +"' /></div>");
		$('zoomy').hide();
			
		var mX = Event.pointerX(event);
		var mY = Event.pointerY(event);;
			
		var imgPreloader = new Image();			
		imgPreloader.onload = (function(){			
			this.showZoom(mX,mY);	        
		}).bindAsEventListener(this);
		imgPreloader.src =  zSrc;
			
	},
	
	showZoom: function(mX,mY) {
		
		var zoomDim 	= $('zoomy').getDimensions();
		var zWidth  	= zoomDim.width;
		var zHeight 	= zoomDim.height;
	
		var globalDim	= document.viewport.getDimensions();
		var wWidth		= globalDim.width;
		var wHeight		= globalDim.height;
		var zoom_x, zoom_y;
			
		var Yscroll = document.viewport.getScrollOffsets().top;
		var mouse_x =  mX;
		var mouse_y =  mY - Yscroll;
			
		if ( (mouse_x - (zWidth/2) >= 0 ) &&  (mouse_x + (zWidth/2) <= wWidth)){ 
			zoom_x = mouse_x - (zWidth/2);
		} else if ( mouse_x + (zWidth/2) > wWidth ) {
			zoom_x = wWidth - zWidth-2;
		} else {
			zoom_x =0;
		}
			
		if ( mouse_y - (zHeight/2) >= 0 ){ 
			if ( mouse_y + (zHeight/2) > wHeight ) {
				zoom_y = wHeight - zHeight + Yscroll-2;
			}
			else {
				zoom_y = mouse_y - (zHeight/2)+ Yscroll-2;
			}
		}
		else {
			zoom_y = 0 + Yscroll;
		}

		$('zoomy').setStyle({top: zoom_y + 'px', left: zoom_x + 'px'});
		new Effect.Appear('zoomy', { duration: this.options.appearDuration} );
		this.setZoom();
		 
		if (this.options.draggable){
			new Draggable('zoomy');
		}	
		
		this.element.setStyle({opacity: 1.0});
	},
	
	hideZoom: function() {	
	
		if($("zoomy")) {
			$("zoomy").remove();
		}
		
		new Effect.Opacity(this.element, { from: 0, to: 1, duration: 0.5 });
	},
	
	setZoom: function() {
		
		$('zoomy').observe('click',function(event) {
			//if(event.element() ==  $('close')) {
				this.hideZoom();
			//}
			//if ((!this.options.closeButton) && (!this.options.draggable) ) {
				this.hideZoom();
		//	}
		}.bindAsEventListener(this));
			
	}
});
