
$(document).ready(function(){
	$('.popupfotolist a').click(function(){
		showPhoto(this);
		return false;
	})
		
	$(document).bind('keydown', 'esc', function(evt){
		popup = $('#photoPopup');
		if ( popup.length == 1 ) {
			closePhoto(popup,blind);
			evt.stopPropagation();
			evt.preventDefault();
                        bool_close_popup = true;
			return false;
		}
                return false;
	});
})
function showPhoto(a) {
	a = $(a);
	//ps_title = a.attr('title');
	ps_set = a.attr('rel');
	ps_currentPhoto = $('.popupfotolist a[rel="'+ps_set+'"]').index(a);
	ps_setPhotos = $('.popupfotolist a[rel="'+ps_set+'"]').length;
	
	
	popup = $('<div class="photoPopup" id="photoPopup"><div class="ton"></div>'+
	'	<p class="navLinks1">'+
	'		<a href="#" class="ar-left p-nav-prev"><img src="img/leftst.gif"></a>'+
	'	</p>'+
	'	<div class="cont">'+
	'	<span class="p-nav-close" title="Закрыть"></span>'+
	'	<img src="img/loader.gif" alt="" class="loader" />'+
	'	<img src="img/spacer.gif" alt="" class="image" />'+
	'	<span class="g_photo_n">'+(ps_currentPhoto+1)+'</span>'+
	'	<span class="g_qnt">'+ps_setPhotos+'</span>'+
	'	</div>'+
	'	<p class="navLinks2">'+
	'		<a href="#" class="ar-right p-nav-next"><img src="img/rightst.gif"></a>'+
	'	</p>'+
	'</div>')
		
	blind = $('<div class="blindPopup"></div>');
	pageHeight = (document.documentElement.offsetHeight > document.body.clientHeight) ? document.documentElement.offsetHeight : document.body.clientHeight;
	blind.height(pageHeight).click(function(){
		closePhoto(popup,blind);
		return false;
	}).appendTo('body');
	
	popup.appendTo('body');
	setCenter(popup);
	popup.show();
	
	loadPhoto(ps_set,ps_currentPhoto,popup);
	if (ps_setPhotos>1) {
		popup.find('.p-nav-prev').click(function(event){
			event.stopPropagation();
			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('.p-nav-next').click(function(event){
			event.stopPropagation();
			ps_currentPhoto = parseInt(popup.find('.g_photo_n').html());
			ps_currentPhoto = checkSequence(ps_currentPhoto,ps_setPhotos);
			loadPhoto(ps_set,ps_currentPhoto,popup);
			return false;
		});
	} else {
		popup.find('.p-nav-prev').hide();
		popup.find('.p-nav-next').hide();
	}
	popup.find('.p-nav-close').click(function(){
		closePhoto(popup,blind);
		return false;
	})

	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;
	}
}
function setCenter(item) {
	windowHeight = document.documentElement.clientHeight;
	currentOffset = document.documentElement.scrollTop || document.body.scrollTop;
	currentOffset = currentOffset + parseInt((windowHeight - ($(item).height()+148)) / 2);
	pLeft = $(item).width() / 2 + 40;
	$(item).css({top:currentOffset,marginLeft:-pLeft}).show();
}
function loadPhoto(ps_set,ps_currentPhoto,popup){
	img = popup.find('.image');
	nav = popup.find('.nav');
	loader = popup.find('.loader');
	old_width = popup.width();
	old_height = popup.height();
	popup.css({
		width:old_width,
		height:old_height
	});
	src = $('.popupfotolist a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('href');
	pv = $('.popupfotolist a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').hasClass('videoLink');
	//ps_title = $('.popupfotolist .thumbs a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('title');
	//popup.find('.info span').html(ps_title);
	if (!pv) {
		img.animate({
			opacity:0
		},200,function(){
			img.remove();
			img = $(new Image());
			img.load(function(){
				//img.appendTo(popup);
				img.prependTo(popup.find('.cont'));
				img_width = img.width();
				img_height = img.height();
				n_width = (img_width < 200) ? 200-old_width : (img_width)-old_width;
				n_height = (img_height < 120) ? 120-old_height : (img_height)-old_height;
				n_left = parseInt(img_width/2) + 40;
				n_top = parseInt((n_height/2));
				loader.hide();
				popup.animate({
					width:'+='+n_width+'px',
					height:'+='+n_height+'px',
					marginLeft:-n_left,
					top:'-='+n_top+'px'
				},200,function(){
					popup.find('.g_photo_n').html(ps_currentPhoto+1);
					img.fadeIn(200);
				})
				
			}).addClass('image').hide().attr('src',src);
			loader.show();
		});
	} else {
		img.remove();
		if (!popup.find('.videoContent').length) {
			videoContent = $('<div class="videoContent"></div>').css({opacity:0}).appendTo(popup);
		} else {
			
		};
		videoContent.animate({opacity:0},200,function(){
			popup.find('.title').html( $('.popupfotolist a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('title') )
			popup.find('.videoContent').empty();
			loader.show();
			
			videoContent.load(src,function(){
				loader.hide();
				vc_width = videoContent.width();
				vc_height = videoContent.height()
				n_width = (vc_width < 300) ? 300-old_width : vc_width-old_width;
				n_height = (vc_height < 120) ? 120-old_height : vc_height-old_height;
				vc_height = (vc_height < 120) ? 120 : vc_height;
				n_left = parseInt(vc_width/2);
				n_top = parseInt((n_height/2));
				
				popup.animate({
					width:'+='+n_width+'px',
					height:vc_height,
					marginLeft:-n_left,
					top:'-='+n_top+'px'
				},200,function(){
					popup.find('.g_photo_n').html(ps_currentPhoto+1);
					videoContent.animate({opacity:1},200)
				});
				
			});
		});
	};
}
function closePhoto(popup,blind){
    popup.remove();
    blind.remove();
}