/*
 * 	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 = {
			marginLeft : 10,
			identifier : 'bezoom',
			height : 200,
			width : 200,
			titleSource : 'title',
			imgSource : 'href',
			bgColor : '#5398EE',     // background color for title
			color : '#ffffff',       // font color for title
			size : '0.8em'           // font size for title
		};
		//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
				$('#'+settings.identifier).remove();
				// removing title to prevent default tooltip
				$(this).attr("title","");
				var imgSmallWidth = $(this).find('img').width();
				var imgSmallHeight = $(this).find('img').height();

				// calculating position for zoom container
				var y = img.offset().top;
				var x = img.offset().left+imgSmallWidth+settings.marginLeft;

				$('body').append('<div id="'+settings.identifier+'" style="border:1px solid #333; z-index:1000; position:absolute; top:'+y+'px;left:'+x+'px;width:'+settings.width+'px;"><div style="background-color:'+settings.bgColor+';color:'+settings.color+';font-size:'+settings.size+';">'+title+'</div><div style="width:'+settings.width+'px;height:'+settings.height+'px;overflow:hidden;position:relative;"><img id="'+settings.identifier+'_img" src="'+imgBig+'" style="position:relative;top:0;left:0"></div></div>');
				
			}).mouseleave(function(){
				// removing zoom container
				$('#'+settings.identifier).remove();
				// setting title back
				$(this).attr("title",titleAttribute);
				// Remove 'lens' div
				$('#zoomWindow').remove();
			});
			$(this).mousemove(function(e){
			
			    var imgBig = $('#'+settings.identifier+'_img');
			
			    if (imgBig == null || imgBig.length == 0)
			        return;
			
				// catching mouse movement to position imgBig

				var imgSmallHeight = img.height();
				var imgSmallWidth = img.width();

				var zoomLens = $('#zoomWindow');
			   
			    // if it is not present, add the lens window
			    if (zoomLens.length == 0 && imgBig.height() > settings.height && imgBig.width() > settings.width)
			    {
			        if (!($.browser.msie))
			        {
				        $(this).append('<div id="zoomWindow" onClick="return false;" style="background-color:white; cursor:default; opacity: 0.5; filter:alpha(opacity=50); z-index:1000; position:absolute; top:'+img.offset().top+'px;left:'+img.offset().left+'px;width:'+((settings.width/imgBig.width())*imgSmallWidth)+'px;height:'+((settings.height/imgBig.height())*imgSmallHeight)+'px;"></div>');
                        zoomLens = $('#zoomWindow');
                    }
                }

		    	// calculating image relation
		    	var imgBigWidth = imgBig.width();
		    	var imgBigHeight = imgBig.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;
				
				var lensBoxX = mouseX - (zoomLens.width()/2);
				var lensBoxY = mouseY - (zoomLens.height()/2);
				
				if (lensBoxX < 0)
				    lensBoxX = 0;

				if (lensBoxY < 0)
				    lensBoxY = 0;
				    
				if ((lensBoxX + zoomLens.width()) > imgSmallWidth)
				    lensBoxX = imgSmallWidth - zoomLens.width();

				if ((lensBoxY + zoomLens.height()) > imgSmallHeight)
				    lensBoxY = imgSmallHeight - zoomLens.height();

				zoomLens.css({top: lensBoxY+'px',left: lensBoxX+'px'});

				// 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);

				imgBig.css({top: imgBigY+'px',left: imgBigX+'px'});
			});
		});

		return this;
	};
})(jQuery);
