//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 = $('.art-image a[rel="'+ps_set+'"]').index(a);
	ps_setPhotos = $('.art-image a[rel="'+ps_set+'"]').length;

	popup = $('<div class="photoPopup" id="photoPopup">'+
		'<strong>Восстановление железобетонных конструкций</strong>'+
		'<span class="p-nav-close"><span>Закрыть</span></span>'+
		'<img src="images/spacer.gif" alt="" class="image" />'+
	'</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 = $('.art-image a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('href');
	ps_title = $('.photoGallery .thumbs a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('title');
	//popup.find('.info span').html(ps_title);

	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))-15;
			//loader.hide();
			popup.animate({
				width:'+='+n_width+'px',
				height:'+='+n_height+'px',
				marginLeft:-n_left,
				top:'-='+n_top+'px'
				//top:40+'px'
			},200,function(){
				//popup.find('.g_photo_n').html(ps_currentPhoto+1);
				popup.find('strong').html(ps_title);
				img.fadeIn(200);
			})

		}).addClass('image').hide().attr('src',src);
		//loader.show();
	});



};
function closePhoto(popup,blind){
	popup.remove();
	blind.remove();
}
