/*
 * 	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
 *
 *
 *	DATART.cz version
 */

(function($) {
	$.fn.bezoom = function(options){
		// default settings
		var settings = {
			marginLeft : 10,
			identifier : 'bezoom',
			height : 250,
			width : 250,
			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);

		var bezoomImageMove = function(e, imgBigWidth, imgBigHeight, imgSmallWidth, imgSmallHeight, offset) {
			// catching mouse movement to position imgBig

		   	// calculating image relation
		    	var imgBigWidth = imgBigWidth || $('#'+settings.identifier+'_img').width();
			var imgBigHeight = imgBigHeight || $('#'+settings.identifier+'_img').height();

			if (imgBigWidth && imgBigHeight) {
				var widthRel = imgSmallWidth / imgBigWidth;
				var heightRel = imgSmallHeight / imgBigHeight;

				// relative mouse position
				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);

				$('#'+settings.identifier+'_img').css({ 'left': imgBigX, 'top': imgBigY });
			}
		}

		this.each(function(i){
			var titleAttribute;

			$(this).mouseenter(function(e){
				titleAttribute = $(this).attr("title");

				var imgBig = $(this).attr(settings.imgSource);
				var img = $(this).find('img');

				// removing zoom container if exists to avoid duplicates
				$('#'+settings.identifier).remove();
				// removing title to prevent default tooltip
				$(this).attr("title","");

				$(this).parent().append('<div id="'+settings.identifier+'" style="background:#fff; position:absolute; top: 0;left:350px;width:'+settings.width+'px;z-index:1000;"><div style="border:1px solid #ccc; width:'+settings.width+'px;height:'+settings.height+'px;overflow:hidden;position:relative;"><span>Načítám...</span><img id="'+settings.identifier+'_img" src="" style="position:relative;display:none;"></div></div>');

				if (img.data('cache')) {
					$('#' + settings.identifier).find('span').remove();
					$('#'+settings.identifier+'_img').attr('src', img.data('cache')).show();

					bezoomImageMove(e, img.data('cacheWidth'), img.data('cacheHeight'), img.width(), img.height(), img.offset());
				}
				else {
					var i = new Image();
					i.onload = function() {
						$('#' + settings.identifier).find('span').remove();
						$('#'+settings.identifier+'_img').attr('src', this.src).show();
						img.data('cache', this.src).data('cacheWidth', this.width).data('cacheHeight', this.width);

						bezoomImageMove(e, this.width, this.width, img.width(), img.height(), img.offset());
					}
					i.src = imgBig;
				}
			})
			.mouseleave(function(){
				// removing zoom container
				$('#'+settings.identifier).remove();
				// setting title back
				$(this).attr("title",titleAttribute);
			})
			.mousemove(function(e){
				var img = $(this).find('img');
				bezoomImageMove(e, false, false, img.width(), img.height(), img.offset());
			});
		});

		return this;
	};
})(jQuery);