/*
 * 	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 : 0,
			identifier : 'bezoom',
			height : 382,
			width : 506,
			titleSource : 'title',
			imgSource : 'href',
			bgColor : '#f5f4ef',     // background color for title
			color : '#a29062',       // font color for title
			size : '1.2em',           // font size for title
			fontFamily: 'Verdana, Arial, Helvetica, sans-serif'
		};
		//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 imgSmallHeight = $(this).find('img').height();
				var imgSmallWidth = $(this).find('img').width();

				// calculating position for zoom container
				var pos = $(this).position();
				var y = Math.ceil(pos.top-0)-imgSmallHeight;
				var x = Math.ceil(pos.left-0)+imgSmallWidth+settings.marginLeft;

				var bid = '<div id="'+settings.identifier+'" style="border-left:1px solid #CCCCCC;position:absolute;top:0px;left:'+x+'px;width:'+settings.width+'px;overflow:hidden;">';
				bid += '<div style="position:absolute;top:0px;left:0px;width:496px;background-color:'+settings.bgColor+';color:'+settings.color+';font-size:'+settings.size+';font-family:'+settings.fontFamily+';padding:6px;font-weight:bold;-moz-opacity:0.8;filter:alpha(opacity=80);opacity:0.8;z-index:2;overflow:hidden;text-align:right;">'+title+'</div>';
				bid += '<div style="position:relative;width:'+settings.width+'px;height:'+settings.height+'px;overflow:hidden;z-index:1;">';
				bid += '<img id="'+settings.identifier+'_img" src="'+imgBig+'" style="position:relative;top:0px;left:0px;"></div></div>';

				$('#zoomInfo').append(bid);
			}).mouseleave(function(){
				// removing zoom container
				//$('#'+settings.identifier).remove();
				// setting title back
				$(this).attr("title",titleAttribute);
			});
			$(this).mousemove(function(e){
				// relative mouse position
				var offset = img.position();
				var contLeftOffset = $("#wrapper").offset().left;
				var contTopOffset = $("#zoomInfo").offset().top;
				var mouseX = (e.pageX - contLeftOffset) - offset.left;
				var mouseY = (e.pageY - contTopOffset) - offset.top;

				// catching mouse movement to position imgBig
				var imgSmallHeight = $(this).find('img').height();
				var imgSmallWidth = $(this).find('img').width();

				// calculating image relation
				var imgBigWidth = $('#'+settings.identifier+'_img').width();
				var imgBigHeight = $('#'+settings.identifier+'_img').height();
				var widthRel = imgSmallWidth / imgBigWidth;
				var heightRel = imgSmallHeight / imgBigHeight;

				// 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);

				$('#'+settings.identifier+'_img').css('left', imgBigX);
				$('#'+settings.identifier+'_img').css('top', imgBigY);
			});
		});

		return this;
	};
})(jQuery);