/*
* Copyright (C) 2010 Joel Sutherland
* Licenced under the MIT license
* http://www.newmediacampaigns.com/page/zoomable-jquery-image-gallery-jphotogrid
*/
(function($) {
	$.fn.jphotogrid = function(settings, callback) {
		settings = $.extend(true, {
			radius: 170,
			zoom: .5,
			baseCSS: {
				'z-index': 3,
				opacity: 1.0,
				width: 130,
				height: 130
			}
		}, settings);

		// store original size (ratio)
		function saveSizes(el){
			$("img", el).each(function(){
					$(this).data('ratio', 1);
					$(this).load(function() {
						$(this).data('ratio', this.naturalWidth / this.naturalHeight);
					});
				})
		}

		//Convert floats to absolute
		function toAbsolute(el){
			$(el)
				.css('height', $(el).height()) // preserve the right height even if all content is absolute
				.find("img").each(function(){
					var pos = $(this).position();
					$(this).data('ptop',pos.top);
					$(this).data('pleft',pos.left);
					$(this).data('ratio',pos.left);
				})
				.css('position', 'absolute');
		}
		

		
		function mapSphere(pos, offset){
			var ret = Math.sqrt(settings.radius*settings.radius - Math.pow((pos.x-offset.x), 2) - Math.pow((pos.y-offset.y), 2))/settings.radius;
			return isNaN(ret) ? 0 : ret;
		}

		var lastMouseEvent, notBefore;
		function updateMappings(e, elements){
			/*
			var d = (new Date()).getTime();
			if(notBefore>d) return;

			notBefore = d + 50; // znovu povolit az za tolik ms
			*/
			//if(Math.random() < 0.7) return;

			if (e) lastMouseEvent = e;
			else e = lastMouseEvent || {pageX:-999, pageY:-999};

				var mouse = {
					x: e.pageX,
					y: e.pageY
				};
				elements.each(function(){

					var pos = $(this).offset(); // position relative to document
					var center = {
						x: pos.left + $(this).outerWidth()/2,
						y: pos.top + $(this).outerHeight()/2
					};
					var zoom = mapSphere(center, mouse);
					var css = {
						'z-index':Math.floor(zoom*200),
						opacity: zoom + settings.baseCSS.opacity,
						width: settings.baseCSS.width * (zoom * settings.zoom +1),
						height: settings.baseCSS.height * (zoom * settings.zoom +1),
						left: $(this).data('pleft') - (zoom * settings.baseCSS.width * settings.zoom * 0.5),
						top: $(this).data('ptop') - (zoom * settings.baseCSS.height * settings.zoom * 0.5) 
					};
					//console.log(css);
					$(this).css(css);


				})

			}


		return $(this).each(function(){
			saveSizes(this);

			var container = $(this);
			container.css({position: 'relative'});
			$('img', this).css(settings.baseCSS);

			toAbsolute(this);

			/*
			container.append('<div class="infobox"></div>');
			$('.infobox', container).css({
				position:'relative',
				'z-index':1000,
				width:'40%',
				//border:'1px solid black',
				margin:'20px auto',
				'text-align':'center',
				'background-color':'white'
				}).hide();
			*/
		
			container.find("a img, img").css('cursor', 'pointer');

			var elements = $("a img, img", container);
			$(document).mousemove(function(e){updateMappings(e, elements);});
			updateMappings(null, elements);

		});
	}
})(jQuery);

/** auto **/
jQuery(document).ready(function(){jQuery('.gallery').jphotogrid();});


