$(document).ready(function(){
	
	$('.photoGallery a').click(function(){
		showPhoto(this);
		return false;
	})

	$(document).bind('keydown', 'esc', function(evt){
		popup = $('#galleryPopup');
		if ( popup.length == 1 ) {
			closePhoto(popup,blind);
			evt.stopPropagation( );  
			evt.preventDefault( );
			return false;
		}
	});
})
function showPhoto(a) {
	a = $(a);
	ps_title = a.attr('title');
	ps_set = a.attr('rel');
	ps_currentPhoto = $('.photoGallery a[rel="'+ps_set+'"]').index(a);
	ps_setPhotos = $('.photoGallery a[rel="'+ps_set+'"]').length;

	popup = $('<div class="popup galleryPopup" id="galleryPopup" style="display:block">'+
	'	<div class="popRsh">'+
	'		<div class="popContent">'+
	'			<div class="topBlock"></div>'+
	'			<div class="popTW">'+
	'				<h2>Фотоархив</h2>'+
	'				<p class="galleryPopup-photoBox">'+
	'					<img src="/img/spacer.gif" width="300" height="200" alt="" class="popup-image" />'+
	'					<span class="galleryPopup-nav galleryPopup-nav-prev" title="Предыдущая">&nbsp;</span>'+
	'					<span class="galleryPopup-nav galleryPopup-nav-next" title="Следующая">&nbsp;</span>'+
	'				</p>'+
	'				<p class="galleryPopup-info"></p>'+
	'			</div>'+
	'		</div>'+
	'	</div>'+
	'	<a href="#" class="popClose"><img src="/img/blank.gif" width="9" height="9" alt="Закрыть" /></a>'+
	'	<div class="popBottom"><div class="popBsh"></div></div>'+
	'	<span class="loader"></span>'+
	'	<span class="g_photo_n">'+(ps_currentPhoto+1)+'</span>'+
	'</div>');
	
	blind = $('<div class="blind"></div>');
	pageHeight = (document.documentElement.offsetHeight > document.body.clientHeight) ? document.documentElement.offsetHeight : document.body.clientHeight;
	blind.css({
		height:pageHeight,
		display:'block'
	}).click(function(){
		closePhoto(popup,blind);
		return false;
	}).appendTo('body');
	
	popup.appendTo('body');
	setCenter(popup);
	popup.show();
	
	loadPhoto(ps_set,ps_currentPhoto,popup);
	
	popup.find('.galleryPopup-nav-prev').click(function(event){
		event.stopPropagation();
		if (!popup.is(':animated')) {
			ps_currentPhoto = parseInt(popup.find('.g_photo_n').html())-2;
			ps_currentPhoto = checkSequence(ps_currentPhoto,ps_setPhotos);
			loadPhoto(ps_set,ps_currentPhoto,popup);
		};
		return false;
	});
	popup.find('.galleryPopup-nav-next').click(function(event){
		event.stopPropagation();
		if (!popup.is(':animated')) {
			ps_currentPhoto = parseInt(popup.find('.g_photo_n').html());
			ps_currentPhoto = checkSequence(ps_currentPhoto,ps_setPhotos);
			loadPhoto(ps_set,ps_currentPhoto,popup);
		};
		return false;
	})
	popup.find('.popClose').click(function(){
		closePhoto(popup,blind);
		return false;
	})

	return false;
}

function setCenter(item) {
	windowHeight = document.documentElement.clientHeight;
	currentOffset = document.documentElement.scrollTop || document.body.scrollTop;
	currentTop = currentOffset + parseInt((windowHeight - ($(item).height()+40)) / 2);
	pLeft = ($('#wrapper').width() - $(item).width()) / 2;
	if (currentTop < (currentOffset+20)) { 
		$(item).css({top: (currentOffset+20), left: pLeft});
	}
	else { 
		$(item).css({top: currentTop, left: pLeft}); 
	}
	return false;
}

function checkSequence(ps_currentPhoto,ps_setPhotos) {
	if (ps_currentPhoto < 0) {
		return ps_setPhotos-1;
	} else if (ps_currentPhoto >= ps_setPhotos) {
		return 0;
	} else {
		return ps_currentPhoto;
	}
}

zflag = 0;

function loadPhoto(ps_set,ps_currentPhoto,popup){
	img = popup.find('.popup-image');
	loader = popup.find('.loader');
	old_width = img.width();
	old_height = img.height();
	src = $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('href');
	ps_title = $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('title');
	popup.find('p.galleryPopup-info').html('');
	
	
	img.animate({
		opacity:0
	},200,function(){
		img.remove();
		loader.show();
		img = $(new Image());
		img.load(function(){
			popup.find('p.galleryPopup-info').html(ps_title);
			
			img.appendTo(popup.find('p.galleryPopup-photoBox'));
			img_width = img.width();
			img_height = img.height();
			
			n_width = (img_width < old_width) ? img_width-old_width : img_width-old_width;
			n_height = (img_height < old_height) ? img_height-old_height : img_height-old_height;
			n_left = parseInt(n_width/2);
			n_top = parseInt(n_height/2);
			
			
			popup.find('p.galleryPopup-photoBox').animate({
				height:'+='+n_height+'px',
				width:'+='+n_width+'px'
			},200);
			
			currentOffset = document.documentElement.scrollTop || document.body.scrollTop;
			popT = parseInt(popup.css('top'));
			zTop = popT - (currentOffset+20);
			if(n_top > zTop){
				zflag = 1;
				zdv = n_top - zTop;
				
				popup.animate({
					width:'+='+n_width+'px',
					marginLeft:'-='+n_left+'px',
					top:'-='+zTop+'px'
				},200,function(){
					loader.hide();
					popup.find('.g_photo_n').html(ps_currentPhoto+1);
					img.fadeIn(200);
				});
			}
			else if(n_top == zTop){
				popup.animate({
					width:'+='+n_width+'px',
					marginLeft:'-='+n_left+'px',
					top:'-='+zTop+'px'
				},200,function(){
					loader.hide();
					popup.find('.g_photo_n').html(ps_currentPhoto+1);
					img.fadeIn(200);
				});
			}
			else if(n_top < zTop) {
				if(zflag == 1){
					zflag = 0;
					if((n_top+zdv) <= zTop){
						n_top = n_top + zdv;
					}
					else {
						n_top = zTop
					}
				};
				popup.animate({
					width:'+='+n_width+'px',
					marginLeft:'-='+n_left+'px',
					top:'-='+n_top+'px'
				},200,function(){
					loader.hide();
					popup.find('.g_photo_n').html(ps_currentPhoto+1);
					img.fadeIn(200);
				});
			};
			
		}).addClass('popup-image').hide().attr('src',src);
			
			/*popup.find('p.galleryPopup-photoBox').animate({
				height:'+='+n_height+'px'
			},200);
			popup.animate({
				width:'+='+n_width+'px',
				marginLeft:'-='+n_left+'px',
				top:'-='+n_top+'px'
			},200,function(){
				loader.hide();
				popup.find('.g_photo_n').html(ps_currentPhoto+1);
				img.fadeIn(200);
			});
			
		}).addClass('popup-image').hide().attr('src',src)*/
		
	});
	
}
function closePhoto(popup,blind){
	popup.remove();
	blind.remove();
}
