function ImageManipulator() {
/////
	var ZLIMIT = 3;
	var image_id = 0;
///////
	
	var $container = $('.container');
	var $box = $('.imageBox', $container);
	var $load= $('#load', $container);
	var $img = $('img:first', $box);
	
	var limx = 0;
	var limy = 0;
	var limz = 0;
	var tmpImg = new Image();

	tmpImg.onload = function() {
		var iw = this.width;
		var ih = this.height;
		 
		$box.css({left:0,top:0, width:iw, height:ih});
		
		limx = iw - $container.width();
		limy = ih - $container.height();
		limz = 0;
	
		$box.css({left: -limx/2, top: -limy/2});

		$img.attr('src', tmpImg.src);
		$box.fadeIn(100);
		$('img:gt(0)', $box).remove();
		$load.hide();
	};
	
	function partImg(id, bw, bh, img) {
		var src = "/images/fx/part,"+Math.ceil(bw)+","+Math.ceil(bh)+","+Math.ceil(img.left)+","+Math.ceil(img.top)+","+Math.ceil(img.width)+","+Math.ceil(img.height)+"/"+image_id;
		$box.append("<img class='loading' src='"+src+"' id='"+id+"' style='margin:0px; visibility: hidden; position:absolute;' unselectable='on'>");
		$load.show();
		
		$('#'+id, $box)
			.mousedown(function(e) { try {e.preventDefault();} catch(ex){} })
			.css(img)
			.load(function() { 
				$(this).css({visibility: 'visible'}).removeClass('loading');
				if (!$('.loading', $box).size()) $load.hide();
			});
	}
	
	function insertImg(bx,by,bw,bh) {
		var cw = $container.width();
		var ch = $container.height();
		var bx = bx || $box.position().left;
		var by = by || $box.position().top;
		var bw = bw || $box.width();
		var bh = bh || $box.height();

		var tdw,tdh;
		var dw = cw;// wielkosc plackow
		var dh = ch; 
				
		for(var i = 0; i < bw; i+=dw) {
			for(var j = 0; j < bh; j+=dh) {
				if (!((i + bx > 1 || i + bx + dw > 1) && i + bx < cw && (j + by > 1 || j + by + dh > 1) && j + by < ch)) continue;
		
				var id = i+''+j+''+limz;
 
				if ($('#'+id, $box).attr('id')) continue;
				if (i + dw > bw) tdw = bw - i; else tdw = dw;
				if (j + dh > bh) tdh = bh - j; else tdh = dh;

				partImg(id, bw, bh, {left: i, top: j, width: tdw, height: tdh});
		}}
		$container.dblclick(function(e){ zoom(e,1); });
	}

	function zoom(e, d) {
		var bx,by,bw,bh,z = 1.5;
		var mx = e.pageX - $container.position().left;
		var my = e.pageY - $container.position().top;
		var cx = $container.width(), cy= $container.height();
		 
		if (d > 0 && ++limz > ZLIMIT) return (limz = ZLIMIT);
		else if (d < 0) { 
			if (limz-- <= 0) return (limz = 0);
			z = 1/z;
			mx = my = 0; 
		}
			
		bw = $box.width() * z;
		bh = $box.height()* z;

		limx = bw - cx; cx*=0.5; 
		limy = bh - cy; cy*=0.5;
		
		if (!mx && !my) { mx = cx; my = cy; }
		 
		bx = ($box.position().left - mx ) * z + cx;
		by = ($box.position().top  - my ) * z + cy;
							
		if (bx > 0) bx = 0;	else if (bx < -limx) bx = -limx; 
		if (by > 0) by = 0; else if (by < -limy) by = -limy;
				
		$('img:gt(0)', $box).remove();
		$container.unbind('dblclick');
		$box.animate({width:bw, height:bh, left:bx, top:by}, 200, function(){ insertImg(bx,by,bw,bh); });
	}

	function resetImg() {
		//tmpImg.src = '/images/fx/fit2,'+$container.width()+','+$container.height()+'/'+image_id;
		tmpImg.src = '/images/fx/fit,'+$container.width()+','+$container.height()+'.place,'+$container.width()+','+$container.height()+'.overlay,99,99/'+image_id;
		$load.show();
	}
	
	$('body').mouseup(function(e) { 
		$(this).unbind('mousemove'); 
		$(this).css({cursor: ''});
		if ($('img', $box).size() > 1) insertImg(); 
	});
	$container.mousedown(function(e){  
		$(this).css({cursor: 'move'});
		var x = e.clientX;
		var y = e.clientY;
		
		$('body').mousemove(function(e){
			var p = $box.position();
			p.left -= x - e.clientX;
			p.top  -= y - e.clientY;
			if (p.left > 0 || p.left < -limx) delete p.left;
			if (p.top  > 0 || p.top  < -limy) delete p.top;
			x = e.clientX;
			y = e.clientY;
			$box.css(p);
		});
	});

	$('#thumbnails img').click(function(){
		image_id = $(this).attr('src').split('/').pop();
		if (image_id == $img.attr('src').split('/').pop()) return resetImg();
		$('#thumbnails img').removeClass('active');
		$(this).addClass('active'); 
		$box.fadeOut(100, resetImg);
	});

	$('#control #zoomin',  $container).click(function() { 
        this.blur(); // most browsers
        this.hideFocus = true; // ie
        this.style.outline = 'none'; // mozilla
        $container.dblclick(); 
    });

	$('#control #zoomout', $container).click(function(e){ 
        this.blur(); // most browsers
        this.hideFocus = true; // ie
        this.style.outline = 'none'; // mozilla
        zoom(e, -1); 
    });
	
	$container.dblclick(function(e){ zoom(e,1); });
	$('#thumbnails img:first', $container).click();


	function tload() { 
		var src = this.src;
		$('#thumbnails img:eq('+this.id+')', $container).fadeOut(100, function() {
			$(this).attr('src', src).fadeIn(100);
		}); 
	}
	var tmp = new Array($('#thumbnails img').size());
	$('#thumbnails img', $container).each(function(i){
		if(!this.complete) {
			tmp[i] = new Image();
			tmp[i].src = $(this).attr('src');
			tmp[i].id = i;
			$(this).attr('src', $load.attr('src'));
			tmp[i].onload = tload;
		}
	});
	
}