function prepareZoom() {	


	imgWindow = new Object;
	imgWindow.tmpImage = new Image();
	//imgWindow.preloader = 'img/design/pl-big.gif';
	imgWindow.currentImg = $('#img-window img#current-img');
	imgWindow.imgStartWidth = 0;
	imgWindow.imgStartHeight = 0;
	
	imgWindow.zooming = true;
	
	imgWindow.imgLoaded = false;
	imgWindow.startAnimationFinished = false;
	
	
	imgWindow.fixErrors = function() {
		//$('#img-window').setAttribute('unselectable', 'on');
		document.getElementById('img-window').ondragstart = function() { return false };
		$('#img-window').bind('selectstart', function() {return false;});
		
		if ($.browser.mozilla) {
			$('#img-window').css({'MozUserSelect' : '-moz-none'});
			
		}
		else if ($.browser.msie) {
			$('#img-window').bind('selectstart', function() {return false;});
		}
		
		else return false;
	}
	
	imgWindow.fixErrors();
	
	//onselectstart = function() {return false}
	
	imgWindow.switchToMode = function (arg_mode) {
		if(arg_mode == 'zoom'){
			imgWindow.mode = 'zoom';
			$('#img-window .zoom-tool').css('visibility','visible');
			$('div#img-div').css('right','90px').css('cursor','move');//cursor:move;
			$('#img-window .main-frame').animate({
				left: '5%',
				right: '5%',
				top: '5%',
				bottom: '5%'
			},500,'swing',function(){
				if(imgWindow.tmpImage.width<$('div#img-div').width() || imgWindow.tmpImage.height<$('div#img-div').height()){
					if(imgWindow.tmpImage.width<$('div#img-div').width())
						imgWindow.currentImg.css('left',($('div#img-div').width() - imgWindow.tmpImage.width)/2 +'px');
					else imgWindow.currentImg.css('left','0px');
					
					if(imgWindow.tmpImage.height<$('div#img-div').height())
						imgWindow.currentImg.css('top',($('div#img-div').height() - imgWindow.tmpImage.height)/2 +'px');
					else imgWindow.currentImg.css('top','0px');
				}
				else	imgWindow.currentImg.css('top','0px').css('left','0px');
				imgWindow.currentImg.attr('src',imgWindow.tmpImage.src);
				imgWindow.currentImg.css('visibility','visible');
			});
			
			
		}
		else {
			imgWindow.mode = 'view';
			var mfSide = ($('#img-window').width() - (imgWindow.tmpImage.width + 2 + 20 + 59))/2;
			var mfTop = ($('#img-window').height() - (imgWindow.tmpImage.height + 2 + 40))/2;
			//
			if(mfSide<10 || mfTop<10){
				imgWindow.switchToMode('zoom');
				return false;
			}
			
			$('div#img-div').css('cursor','pointer');
			$('#img-window .main-frame').animate({
				left: mfSide + 'px',
				right: mfSide + 'px',
				top: mfTop + 'px',
				bottom: mfTop + 'px'
			}, 500, 'swing', function(){
				imgWindow.currentImg.css('left', '0px').css('top', '0px');
				imgWindow.currentImg.attr('src',imgWindow.tmpImage.src);
				imgWindow.currentImg.css('visibility','visible');
			});
		}
	}
	
	
	imgWindow.scale = 1;
	
	//temp:
	
	
	imgWindow.zoom = function (arg_changeScale){
		//if( imgWindow.currentImg.attr('src') != imgWindow.preloader){
			if(imgWindow.imgStartWidth <= 0 || imgWindow.imgStartHeight <= 0){
				imgWindow.imgStartWidth = imgWindow.currentImg.width();
				imgWindow.imgStartHeight = imgWindow.currentImg.height();	
			}
			imgWindow.scale = imgWindow.scale * arg_changeScale;
			$('#img-window div#scale').text( Math.round(imgWindow.scale * 100) + '%');
			imgWindow.currentImg.css('width',imgWindow.imgStartWidth * imgWindow.scale + 'px');
			imgWindow.currentImg.css('height',imgWindow.imgStartHeight * imgWindow.scale + 'px');
			
			if(imgWindow.currentImg.width() < $('div#img-div').width() ){
				imgWindow.currentImg.css( 'left', ($('div#img-div').width() - imgWindow.currentImg.width())/2 + 'px' );
			}
			else{
				if(imgWindow.currentImg.position().left > 0){ //Если рисунок не меньше, но есть просвет слева
					imgWindow.currentImg.css( 'left', '0px' );
				}
				else if(imgWindow.currentImg.position().left < $('div#img-div').width() - imgWindow.currentImg.width() ){ //Если рисунок не меньше, но есть просвет справа
					imgWindow.currentImg.css( 'left',  $('div#img-div').width() - imgWindow.currentImg.width() + 'px' );
				}
				
				/*
				else { //Нет просвета. Просто выравнивание центра увеличения
					imgWindow.currentImg.css( 'left', imgWindow.currentImg.position().left * 1);
				}
				*/
				
				
			}
			
			
			if(imgWindow.currentImg.height() < $('div#img-div').height() ){
				imgWindow.currentImg.css( 'top', ($('div#img-div').height() - imgWindow.currentImg.height())/2 + 'px' );
			}
			else{
				if(imgWindow.currentImg.position().top > 0){ //Если рисунок не меньше, но есть просвет слева
					imgWindow.currentImg.css( 'top', '0px' );
				}
				else if(imgWindow.currentImg.position().top <  $('div#img-div').height() -imgWindow.currentImg.height() ){ //Если рисунок не меньше, но есть просвет справа
					imgWindow.currentImg.css( 'top',  $('div#img-div').height() - imgWindow.currentImg.height() + 'px' );
				}
				/*
				else { //Нет просвета. Просто выравнивание центра увеличения	
				}*/
			}
		//}
	}

	imgWindow.close = function () {
		
		$('#img-window div.main-frame').hide(500, function() {
			$('#img-window').fadeOut(300, function() {
				//imgWindow.currentImg.attr('src',imgWindow.preloader);
				imgWindow.imgStartWidth = 0;
				imgWindow.imgStartHeight = 0;
				imgWindow.scale = 1;
				imgWindow.startAnimationFinished = false;
				imgWindow.imgLoaded = false;
				$('#img-window div#scale').text( Math.round(imgWindow.scale * 100) + '%');
				imgWindow.currentImg.css('width','auto');
				imgWindow.currentImg.css('height','auto');
				imgWindow.currentImg.attr('src','');
			});
		});
		
	}
	
	
	
	$('#img-window div#zoom-in').click(function () {
		imgWindow.zoom(1.25);
	});
	
	
	
	$('#img-window div#zoom-out').click(function () {
		imgWindow.zoom(0.8);
	});
	
	
	
	$('#img-window .close-button').click(function() {
		imgWindow.close();
	});
	

}





function showImg(arg_src, arg_zoom){
	imgWindow.imgLoaded = false;
	imgWindow.zooming = arg_zoom;
	imgWindow.tmpImage.src = arg_src;
	imgWindow.scale = 1.00;
	imgWindow.mode = 'none';
	//imgWindow.currentImg.attr('src',imgWindow.preloader);
	imgWindow.currentImg.css('visibility','hidden');
	//imgWindow.currentImg.css('top','47%').css('left','47%');
	$('#img-window .zoom-tool').css('visibility','hidden');
	$('div#img-div').css('right','59px').css('cursor','default');
	$('#img-window .main-frame').css('top', '20%').css('bottom','20%').css('left', '20%').css('right', '20%');
	
			
	$(imgWindow.tmpImage).load( function () {
		imgWindow.imgStartWidth = imgWindow.tmpImage.width;
		imgWindow.imgStartHeight = imgWindow.tmpImage.height;
		imgWindow.imgLoaded = true;
		
		if (imgWindow.zooming){
			imgWindow.switchToMode('zoom');
			
		}
		
		
		else if(imgWindow.startAnimationFinished){
			imgWindow.switchToMode('view');
		}
	});
	
	$('#img-window div#img-div').mousedown(function(e){
		if(imgWindow.mode == 'zoom'){
			imgWindow.startX = e.clientX;
			imgWindow.startY = e.clientY;
			var imgPos = imgWindow.currentImg.position();
			imgWindow.imgStartClientLeft = imgPos.left;
			imgWindow.imgStartClientTop = imgPos.top;
			imgWindow.drag = true;
		}
		else if(imgWindow.mode == 'view'){
			imgWindow.drag = false;
			imgWindow.close();
		}
		else imgWindow.drag = false;
	});
	
	$('#img-window div#img-div').mousemove(function(e){
		if(imgWindow.drag){
			var moveImgX = e.clientX - imgWindow.startX;
			var moveImgY = e.clientY - imgWindow.startY;
			var imgOffset = imgWindow.currentImg.offset();
			if(imgWindow.currentImg.width() < $('div#img-div').width() ){
				imgWindow.currentImg.css( 'left', ($('div#img-div').width() - imgWindow.currentImg.width())/2 + 'px' );
			}
			else {
				if(imgWindow.imgStartClientLeft + moveImgX>0)
					imgWindow.currentImg.css('left','0px');
				else if(imgWindow.imgStartClientLeft + moveImgX + imgWindow.currentImg.width() <$('div#img-div').width())
					imgWindow.currentImg.css( 'left',  $('div#img-div').width() - imgWindow.currentImg.width() + 'px' );
				else 
					imgWindow.currentImg.css( 'left', imgWindow.imgStartClientLeft + moveImgX + 'px' );
			}
			
			if(imgWindow.currentImg.height() < $('div#img-div').height() ){
				imgWindow.currentImg.css( 'top', ($('div#img-div').height() - imgWindow.currentImg.height())/2 + 'px' );
			}
			else {
				if(imgWindow.imgStartClientTop + moveImgY>0)
					imgWindow.currentImg.css( 'top','0px');
				else if(imgWindow.imgStartClientTop + moveImgY +imgWindow.currentImg.height() < $('div#img-div').height())
					imgWindow.currentImg.css( 'top',  $('div#img-div').height() - imgWindow.currentImg.height() + 'px' );
				else 
					imgWindow.currentImg.css( 'top', imgWindow.imgStartClientTop + moveImgY + 'px');
			}
		}
	});
		
	$('div#img-window,div.main-frame,div#img-div').mouseup(function(e){
		imgWindow.drag = false;
	});
	
	$('div#img-window,div.main-frame,div#img-div').mouseout(function(e){
		imgWindow.drag = false;
	});

	
	$('#img-window').fadeIn(300,function () {
		$('#img-window div.main-frame').show(500, function(){
			imgWindow.startAnimationFinished = true;
			if(imgWindow.zooming && (imgWindow.imgLoaded || imgWindow.tmpImage.complete)){
				imgWindow.switchToMode('zoom');
			}
			
			else if(imgWindow.imgLoaded /*|| imgWindow.tmpImage.complete /* || jQuery.browser.msie*/ ){
				imgWindow.switchToMode('view');
			}
			
		});
	});
	
	
}


function applyImgWindowToElements(){
	$.each($('img.zoomable'),function (){
		$(this).css('cursor','pointer').click(function (){
			var path = new String( $(this).attr('src'));
			if(path.indexOf("/megalith/")>=0){
				path = path.replace("/megalith/","/megalith-full/");
				showImg(path, true);
			}
			else $(this).css('cursor','default');
		});
		$(this).after('<div class="hd">Zoom</div>');
		
		
		$(this).load(function(){
			if($(this).height()<($(this).width()/4))
				$(this).next().css('font-size',$(this).height()+'px');
			else $(this).next().css('font-size',$(this).width()/4+'px');
		
			$(this).next().css('left',$(this).position().left+'px').css('top',$(this).position().top+($(this).height()-$(this).next().height())/2+'px').css('width',$(this).width()+'px');
		});
		$(this).load();
		
		$(this).next().click(function(){$(this).prev().click();});
		$(this).next().css('opacity','0.3');
		$(this).hover(function(){$(this).next().css('opacity','1');},function(){$(this).next().css('opacity','0.3');});
		$(this).next().hover(function(){$(this).css('opacity','1');},function(){$(this).css('opacity','0.3');});
	});
	
// 	$.each($('img.zoomable+div.hd'),function(){
// 		
// 	});
	
	
	$('img.view').css('cursor','pointer').click(function (){
		showImg($(this).attr('src'), false);
	});
	
	$.each($('a[href$=.jpg],a[href$=.png],a[href$=.gif],a[href$=.bmp]'),function (){
		$(this).data('link',$(this).attr('href')).removeAttr('href').css('cursor','pointer').click(function (){
			showImg($(this).data('link'), false);
		});
	});

}