//gallery script for immer-club
$(document).ready(function(){
	$('.photoGallery .thumbs 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 = $('#galleryPopup');
			if ( popup.length == 1 ) {
				popup.find('.p_prev').triggerHandler('click');
				evt.stopPropagation();  
				evt.preventDefault();
				return false;
			}
		});
		
		$(document).bind('keydown', 'Ctrl+right', function(evt){
			popup = $('#galleryPopup');
			if ( popup.length == 1 ) {
				popup.find('.p_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 = $('#galleryPopup');
			if ( popup.length == 1 ) {
				popup.find('.p_prev').triggerHandler('click');
				evt.stopPropagation();  
				evt.preventDefault();
				return false;
			}
		});
		
		$(document).bind('keydown', 'Ctrl+shift+right', function(evt){
			popup = $('#galleryPopup');
			if ( popup.length == 1 ) {
				popup.find('.p_next').triggerHandler('click');
				evt.stopPropagation( );  
				evt.preventDefault( );
				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') != '' ) ? a.attr('title')+' &mdash; ' : '';
	ps_set = a.attr('rel');
	ps_currentPhoto = $('.photoGallery .thumbs a[rel="'+ps_set+'"]').index(a);
	ps_setPhotos = $('.photoGallery .thumbs a[rel="'+ps_set+'"]').length;
	
	popup = $('<div class="galleryPopup" id="galleryPopup">'+
	'	<img src="/img/pb.png" alt="" class="pb" />'+
	'	<img src="/img/loader.gif" alt="" class="loader" />'+
	'	<span class="p_close">Закрыть</span>'+
	'	<img src="img/spacer.gif" width="576" height="376" alt="" class="image" />'+
	'	<div class="nav">'+
	'		<b></b>'+
	'		<a href="#" class="p_prev"><span>Предыдущая</span></a>'+
	'		<strong><span class="g_title">'+ps_title+'</span>фотография <span class="g_photo_n">'+(ps_currentPhoto+1)+'</span> из <span class="g_qnt">'+ps_setPhotos+'</span></strong>'+
	'		<a href="#" class="p_next"><span>Следующая</span></a>'+
	'	</div>'+
	'</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);
	
	popup.find('.p_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_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;
	})
	popup.find('.p_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 = (document.body.clientWidth - $(item).width()) / 2;
	$(item).css({top:currentOffset,left: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();
	src = $('.photoGallery .thumbs a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('href');
	
	img.animate({
		opacity:0
	},200,function(){
		img.remove();
		loader.show();
		nav.css({
			marginTop:old_height-12
		})
		img = $(new Image());
		img.load(function(){
			
			img.prependTo(popup);
			img_width = img.width();
			img_height = img.height();
//			n_width = (img_width+24)-old_width;
//			n_height = (img_height+24)-old_height;
			n_width = (img_width < 400) ? 424-old_width : (img_width+24)-old_width;
			n_height = (img_height < 120) ? 144-old_height : (img_height+24)-old_height;
			n_left = parseInt(n_width/2);
			n_top = parseInt(n_height/2);
			popup.animate({
				width:'+='+n_width+'px',
				height:'+='+n_height+'px',
				left:'-='+n_left+'px',
				top:'-='+n_top+'px'
			},200);
			nav.animate({
				marginTop:img_height+12
			},200,function(){
				
				loader.hide();
				popup.find('.g_photo_n').html(ps_currentPhoto+1);
				img.fadeIn(200);
				nav.css({marginTop:0});
			})
			
		}).addClass('image').hide().attr('src',src)
		
	});
	
}
function closePhoto(popup,blind){
	popup.remove();
	blind.remove();
}