//gallery script for immer-club
$(document).ready(function(){
	$('.photoGalleryPic 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 = $('#photoPopupIs');
			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 = $('#photoPopupIs');
			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 = $('#photoPopupIs');
			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 = $('#photoPopupIs');
			if ( popup.length == 1 ) {
				popup.find('.p-nav-next').triggerHandler('click');
				evt.stopPropagation( );  
				evt.preventDefault( );
				return false;
			}
		});
	}
	
	$(document).bind('keydown', 'esc', function(evt){
		popup = $('#photoPopupIs');
		if ( popup.length == 1 ) {
			closePhoto(popup,blind);
			evt.stopPropagation( );  
			evt.preventDefault( );
			return false;
		}
	});
})
function showPhoto(a) {
	a = $(a);
	ps_title = a.attr('title');
	ps_name = a.attr('name');
	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="photoPopupIs" id="photoPopupIs">'+
	'	<div class="ptop"><div class="l"></div><div class="r"></div></div>'+
	'   <div class="title">'+ps_title+'</div>'+
	'	<span class="name">'+ps_name+'</span>'+
	'	<div class="content">'+
	'	<span class="p-nav-close" title="Закрыть"></span>'+
	'	<img src="img/loader.gif" alt="" class="loader" />'+
	'	<img src="img/spacer.gif" alt="" class="image" />'+
	'	<p class="navLinks">'+
	'		<a href="#" class="ar-right p-nav-next">Следующая фотография</a>'+
	'		<a href="#" class="ar-left p-nav-prev" title="Предыдущая фотография">Предыдущая фотография</a>'+
	'	</p>'+
	'	<span class="g_photo_n">'+(ps_currentPhoto+1)+'</span>'+
	'	<span class="g_qnt">'+ps_setPhotos+'</span>'+
	'	</div>'+
	'	<div class="pbottom"><div class="l"></div><div class="r"></div></div>'+	
	'</div>')
*/		


	popup = $('<div class="popup photoPopupIs" id="photoPopupIs">'+
	'	<div class="ptop"><div class="l"></div><div class="r"></div></div>'+
	'	<span class="p-nav-close" title="Закрыть"></span>'+
	'   <div class="title">'+ps_title+'</div>'+
	'	<div class="content">'+
	'	<a href="#" class="ar-left p-nav-prev"></a>'+
	'	<img src="/img/des/loader.gif" alt="" class="loader" />'+
	'	<img src="/img/des/spacer.gif" alt="" class="image" />'+
	'	<span class="g_photo_n">'+(ps_currentPhoto+1)+'</span>'+
	'	<span class="g_qnt">'+ps_setPhotos+'</span>'+
	'	<a href="#" class="ar-right p-nav-next"></a>'+
	'	</div>'+
	'	<div class="pbottom"><div class="l"></div><div class="r"></div></div>'+	
	'</div>')
	

		
/*		
<div class="popup photoPopup" id="photoPopup" style="display:none">
	<div class="ptop"><div class="l"></div><div class="r"></div></div>
	<div class="content">
		<a href="javascript:;" class="close" onClick="closePopup('popup')"></a>
		<img src="/img/des/kitchen_big.jpg" width="520" height="390" alt="">
	</div>
	<div class="pbottom"><div class="l"></div><div class="r"></div></div>
</div>
	*/
	
	if (a.hasClass('videoLink')) {
		popup.find('.p-nav-next').html('Следующее видео');
		popup.find('.p-nav-prev').attr('href','Предыдущее видео');
		//ps_title = a.attr('title');
		//$('<div class="title">'+ps_title+'</div>').prependTo(popup);
	};
	blind = $('<div class="blind"></div>');
	pageHeight = '219%';
	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);
	$(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(){
			popup.find('.title').html( $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('title') );
			popup.find('.name').html( $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('name') );
			
			var title_height = popup.find('.name').height();
			
			img.remove();
			img = $(new Image());
			img.load(function(){
				img.prependTo(popup.find('.content'));
				//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 = title_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: 470,
					height: 380
				},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( $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('title') );
			popup.find('.name').html( $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('name') );
			var title_height = popup.find('.name').height();
			
			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 = title_height + ((vc_height < 120) ? 120-old_height : vc_height-old_height);
				vc_height = title_height + ((vc_height < 120) ? 120 : vc_height);
				n_left = parseInt(vc_width/2);
				n_top = parseInt((n_height/2));
				
				popup.animate({
					width: 900,
					height: 500,
					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();
}