//gallery script for immer-club
$(document).ready(function(){
	$('.pgWrapper 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 = $('.pgWrapper a[rel="'+ps_set+'"]').index(a);
	ps_setPhotos = $('.pgWrapper a[rel="'+ps_set+'"]').length;        

	popup = $('<div class="popupWrapper" id="galleryPopup">'+
	'	<span class="p_close"></span>'+
	'	<div class="popupTop"><div class="popupTop-l"></div><div class="popupTop-r"></div></div>'+
	'	<div class="popupContent">'+
	'	<div class="loader"></div>'+
	'		<div class="popupImage"><img src="img/spacer.gif" width="500" height="300" alt="" class="image" /></div>'+
	'		<div class="popupControls">'+
	'			<span class="popupNav p_prev">Предыдущая</span>'+
	'			<span class="popupNav p_next">Следующая</span>'+
        '			<span class="p_rate">'+
	'			</span>' +
	'			<span class="g_photo_n">'+(ps_currentPhoto+1)+'</span>'+
	'			<span class="g_qnt">'+ps_setPhotos+'</span>'+
	'		</div>'+
	'	</div>'+
	'	<div class="popupBottom"><div class="popupBottom-l"></div><div class="popupBottom-r"></div></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();
	$.post("/club/action.php",{check:rfotos[ps_currentPhoto]},function(data,textStatus){ 
		if(data=="Y") {
                $("span.p_rate").attr("style", "top: 23px;");
        $("span.p_rate").html('Вы уже голосовали!');
                }
                else if(data=="N") {
                $("span.p_rate").attr("style", "top: 11px;");
        $("span.p_rate").html('				<a href="#" onclick="rgolos(1,rfotos[ps_currentPhoto]); return false;">1</a>'+
	'				<a href="#" onclick="rgolos(2,rfotos[ps_currentPhoto]); return false;">2</a>'+
	'				<a href="#" onclick="rgolos(3,rfotos[ps_currentPhoto]); return false;">3</a>'+
	'				<a href="#" onclick="rgolos(4,rfotos[ps_currentPhoto]); return false;">4</a>'+
	'				<a href="#" onclick="rgolos(5,rfotos[ps_currentPhoto]); return false;">5</a>');
                }
  },"html");
	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);
$.post("/club/action.php",{check:rfotos[ps_currentPhoto]},function(data,textStatus){ 
		if(data=="Y") {
                $("span.p_rate").attr("style", "top: 23px;");
        $("span.p_rate").html('Вы уже голосовали!');
                }
                else if(data=="N") {
                $("span.p_rate").attr("style", "top: 11px;");
        $("span.p_rate").html('				<a href="#" onclick="rgolos(1,rfotos[ps_currentPhoto]); return false;">1</a>'+
	'				<a href="#" onclick="rgolos(2,rfotos[ps_currentPhoto]); return false;">2</a>'+
	'				<a href="#" onclick="rgolos(3,rfotos[ps_currentPhoto]); return false;">3</a>'+
	'				<a href="#" onclick="rgolos(4,rfotos[ps_currentPhoto]); return false;">4</a>'+
	'				<a href="#" onclick="rgolos(5,rfotos[ps_currentPhoto]); return false;">5</a>');
                }
  },"html");
		loadPhoto(ps_set,ps_currentPhoto,popup);
                $("span.p_rate").attr("style", "top: 11px;");
                $("span.p_rate").html(rreiting);
		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);
$.post("/club/action.php",{check:rfotos[ps_currentPhoto]},function(data,textStatus){ 
		if(data=="Y") {
                $("span.p_rate").attr("style", "top: 23px;");
        $("span.p_rate").html('Вы уже голосовали!');
                }
                else if(data=="N") {
                $("span.p_rate").attr("style", "top: 11px;");
        $("span.p_rate").html('				<a href="#" onclick="rgolos(1,rfotos[ps_currentPhoto]); return false;">1</a>'+
	'				<a href="#" onclick="rgolos(2,rfotos[ps_currentPhoto]); return false;">2</a>'+
	'				<a href="#" onclick="rgolos(3,rfotos[ps_currentPhoto]); return false;">3</a>'+
	'				<a href="#" onclick="rgolos(4,rfotos[ps_currentPhoto]); return false;">4</a>'+
	'				<a href="#" onclick="rgolos(5,rfotos[ps_currentPhoto]); return false;">5</a>');
                }
  },"html");
		loadPhoto(ps_set,ps_currentPhoto,popup);
       
                $("span.p_rate").attr("style", "top: 11px;");
                $("span.p_rate").html(rreiting);
		return false;
	})
	popup.find('.p_close').click(function(){
		closePhoto(popup,blind);
		return false;
	})

	return false;
}
function rgolos(golos,ph)
{
  $("span.p_rate a").attr("onclick","return false;");
  $.post("/club/action.php",{golosov:golos,photo:ph},function(data,textStatus){ 
		if(data=="OK") {
          $("span.p_rate").text("Ваш голос учтен!");
          $("span.p_rate").attr("style", "top: 23px;");
}
  },"html");
}
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');
	popupImage = popup.find('.popupImage');
	old_width = popup.width();
	old_height = popupImage.height();
	src = $('.pgWrapper a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('href');
	popupImage.height(old_height);
	img.animate({
		opacity:0
	},200,function(){
		img.remove();
		loader.show();
		img = $(new Image());
		img.load(function(){
			
			img.appendTo(popupImage);
			img_width = img.width();
			img_height = img.height();
			n_width = (img_width < 430) ? 460-old_width : (img_width+30)-old_width;
			n_height = (img_height < 120) ? 120-old_height : (img_height)-old_height;
			n_left = parseInt(n_width/2);
			n_top = parseInt(n_height/2);
			popupImage.animate({
				height:'+='+n_height+'px'
			},200)
			popup.animate({
				width:'+='+n_width+'px',
				left:'-='+n_left+'px',
				top:'-='+n_top+'px'
			},200,function(){
				loader.hide();
				popup.find('.g_photo_n').html(ps_currentPhoto+1);
				img.fadeIn(200);
			})
			
		}).addClass('image').hide().attr('src',src)
		
	});
	
}
function closePhoto(popup,blind){
	popup.remove();
	blind.remove();
}
