//gallery script for immer-club
$(document).ready(function(){
	$('.photoGallery a').click(function(){
		showPhoto(this);
		return false;
	})

	// key bindings here
	if ( !$.browser.opera ) {
		// for all browsers excepts opera bind on ctrl+
		$(document).bind('keydown', 'Ctrl+left', function(evt){
			popup = $('#photoPopup');
			if ( popup.length == 1 ) {
				popup.find('.p-nav-prev').triggerHandler('click');
				evt.stopPropagation();
				evt.preventDefault();
				return false;
			}
		});

		$(document).bind('keydown', 'Ctrl+right', function(evt){
			popup = $('#photoPopup');
			if ( popup.length == 1 ) {
				popup.find('.p-nav-next').triggerHandler('click');
				evt.stopPropagation();
				evt.preventDefault();
				return false;
			}
		});
	} else {
		// for opera browser bind on ctrl+shift+
		// hey norwegian fellows!
		$(document).bind('keydown', 'Ctrl+shift+left', function(evt){
			popup = $('#photoPopup');
			if ( popup.length == 1 ) {
				popup.find('.p-nav-prev').triggerHandler('click');
				evt.stopPropagation();
				evt.preventDefault();
				return false;
			}
		});

		$(document).bind('keydown', 'Ctrl+shift+right', function(evt){
			popup = $('#photoPopup');
			if ( popup.length == 1 ) {
				popup.find('.p-nav-next').triggerHandler('click');
				evt.stopPropagation( );
				evt.preventDefault( );
				return false;
			}
		});
	}

	$(document).bind('keydown', 'esc', function(evt){
		popup = $('#photoPopup');
		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="photoPopup" id="photoPopup">'+
	'	<span class="p-nav p-nav-close" title="Закрыть"><span></span></span>'+
	'	<img src="/img/des/spacer.gif" alt="" class="image" />'+
	'	<img src="/img/des/loader.gif" alt="" class="loader" />'+
	'	<span class="p-nav p-nav-prev" title="Предыдущая"><span></span></span>'+
	'	<span class="p-nav p-nav-next" title="Следующая"><span></span></span>'+
	'	<span class="g_photo_n">'+(ps_currentPhoto+1)+'</span>'+
	'	<span class="g_qnt">'+ps_setPhotos+'</span>'+
	'</div>');

	blind = $('<div class="blind"></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()) / 2);
	pLeft = $(item).width() / 2;
	$(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 = $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('href');
	pv = $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').hasClass('videoLink');
	//ps_title = $('.photoGallery .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_width = img.width();
				img_height = img.height();
				n_width = (img_width < 300) ? 300-old_width : (img_width)-old_width;
				n_height = (img_height < 120) ? 120-old_height : (img_height)-old_height;
				n_left = parseInt(img_width/2);
				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('.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();
}
