/*
 * 	BeZoom 0.2 - jQuery plugin
 *	written by Benjamin Mock
 *	http://benjaminmock.de/bezoom-jquery-plugin/
 *
 *	Copyright (c) 2009 Benjamin Mock (http://benjaminmock.de)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

(function($) {
	$.fn.bezoom = function(options){
		// default settings
		var settings = {
			identifier : 'bezoom',
			height : 150,
			width : 150,
			titleSource : 'title',
			imgSource : 'href'
		};
		//extending options
		options = options || {};
	   	$.extend(settings, options);
	
		this.each(function(i){
			var title = $(this).attr(settings.titleSource);
			var imgBig = $(this).attr(settings.imgSource);
			var titleAttribute = $(this).attr("title");

			// saving jquery object of small image
			var img = $(this).find('img');

			$(this).mouseenter(function(e){
				// removing zoom container if exists to avoid duplicates
				$('#bezoom').remove();
				// removing title to prevent default tooltip
				$(this).attr("title","");
				var imgSmallHeight = $(this).find('img').height();
				var imgSmallWidth = $(this).find('img').width();

				// calculating position for zoom container

				if( $('.zoom').hasClass('tl')) zoomPosition = 'top: 0; left: 0;';
				else if( $('.zoom').hasClass('tr')) zoomPosition = 'top: 0; right: 0;';
				else if( $('.zoom').hasClass('bl')) zoomPosition = 'bottom: 0; left: 0;';
				else if( $('.zoom').hasClass('br')) zoomPosition = 'bottom: 0; right: 0;';

				$('.small_map').append('<div id="bezoom" style="position:absolute;'+zoomPosition+'width:'+settings.width+'px;"><div style="width:'+settings.width+'px;height:'+settings.height+'px;overflow:hidden;position:relative;"><img id="bezoom_img" src="'+imgBig+'" style="position:relative;"></div></div>');
			}).mouseleave(function(){
				// removing zoom container
				$('#bezoom').remove();
				// setting title back
				$(this).attr("title",titleAttribute);
			});
			$(this).mousemove(function(e){
				// catching mouse movement to position imgBig

				var imgSmallHeight = $(this).find('img').height();
				var imgSmallWidth = $(this).find('img').width();

		    	// calculating image relation
		    	var imgBigWidth = $('#bezoom_img').width();
		    	var imgBigHeight = $('#bezoom_img').height();
				var widthRel = imgSmallWidth / imgBigWidth;
				var heightRel = imgSmallHeight / imgBigHeight;

				// relative mouse position
				var offset = img.offset();
				var mouseX = e.pageX - offset.left;
				var mouseY = e.pageY - offset.top;

				// positioning image according to image relation and mouse position
				var imgBigX = Math.ceil((mouseX / widthRel)-(settings.width / 2)) * (-1);
				imgBigX = Math.max((-1*imgBigWidth)+settings.width,imgBigX);
				imgBigX = Math.min(0,imgBigX);

				var imgBigY = Math.ceil((mouseY / heightRel)-settings.height*0.5) *(-1);
				imgBigY = Math.min(0,imgBigY);
				imgBigY = Math.max((-1*imgBigHeight)+settings.height,imgBigY);

				$('#bezoom_img').css('left', imgBigX);
				$('#bezoom_img').css('top', imgBigY);
			});
			
			$(this).click(function(){return false;});
		});

		return this;
	};
})(jQuery);
