
$(document).ready(function(){
	$('.fotoCont a').click(function(){
		//$('#notepadLoader').show();
		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 sssh() {
	//$('#notepadLoader').show(); 
	fistImg = $('div.pg-thumbs a').eq(0);
	fff = 1;
	showPhoto(fistImg); 
	$('#photoPopup a.zssh').addClass('sShow').html('<span>Остановить слайд-шоу</span>');
	//('.zssh').trigger('click');

	//slideShow();
}

function showPhoto(a) {
	a = $(a);
	//ps_title = a.attr('title');
	ps_set = a.attr('rel');
	ps_currentPhoto = $('.fotoCont a[rel="'+ps_set+'"]').index(a);
	ps_setPhotos = $('.fotoCont a[rel="'+ps_set+'"]').length;
	
	popup = $('<div class="photoPopup" id="photoPopup">'+
			'<div class="popupHead"><a href="#" class="zssh"><span>Запустить слайд-шоу</span></a> <a href="#" class="popClose p-nav-close"><span>Закрыть</span></a></div>'+
			'<img src="/img/blank.gif" alt="" class="image" />'+
			'<a href="#" class="p-nav-prev" title="Предыдущая"><img src="/img/blank.gif" width="20" height="22" alt="Предыдущая" /></a>'+
			'<a href="#" class="p-nav-next" title="Следующая"><img src="/img/blank.gif" width="20" height="22" alt="Следующая" /></a>'+
			'</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');
	setCenterPH(popup);
	blind.show();
	$('select').css({visibility: 'hidden'});
	popup.show();

	zs = popup.find('.zssh');
	zs.click(function(){							   
		if(zs.hasClass('sShow')) {
			slideShowClose();
		}
		else {
			slideShow();
		}
		return false;
	});
	
	loadPhoto(ps_set,ps_currentPhoto,popup);
	if (ps_setPhotos>1) {
		popup.find('.p-nav-prev').click(function(event){
		event.stopPropagation();
		slidePrev();
		slideShowClose(zs);
		return false;
		});
		popup.find('.p-nav-next').click(function(event){
		event.stopPropagation();
		slideNext();
		slideShowClose(zs);
		return false;
		});
	} else {
		popup.find('.p-nav-prev').hide();
		popup.find('.p-nav-next').hide();
	};

	
	popup.find('.p-nav-close').click(function(){
slideShowClose();
		closePhoto(popup,blind);
		return false;
	});

	return false;
}


function slidePrev() {
		ps_currentPhoto --;
		//ps_currentPhoto = parseInt(popup.find('.g_photo_n').html())-2;
		ps_currentPhoto = checkSequence(ps_currentPhoto,ps_setPhotos);
		loadPhoto(ps_set,ps_currentPhoto,popup);
}
function slideNext() {
		ps_currentPhoto ++;
		//ps_currentPhoto = parseInt(popup.find('.g_photo_n').html());
		ps_currentPhoto = checkSequence(ps_currentPhoto,ps_setPhotos);
		loadPhoto(ps_set,ps_currentPhoto,popup);
};
var zzz;
var fff = 0;

function slideShow() {
	$('#photoPopup a.zssh').addClass('sShow').html('<span>Остановить слайд-шоу</span>');
	zzz = setTimeout( function() { slideNext(); }, 2000);
	fff = 1;
	return false;
};
function slideShowClose() {
	$('#photoPopup a.zssh').removeClass('sShow').html('<span>Запустить слайд-шоу</span>');
	clearTimeout(zzz);
	fff = 0;
	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 setCenterPH(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;
	pLeft = ($(item).width()) / 2;

	if (currentOffset < 20) { $(item).css({top: 20, marginLeft: -pLeft}).fadeIn(); }
	else { $(item).css({top: currentOffset, marginLeft: -pLeft}).fadeIn(); }
	
	return false;
}

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 = $('.fotoCont a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('href');

	//ps_title = $('.fotoCont .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()+28);
			
			n_width = (img_width < old_width) ? img_width-old_width : (img_width)-old_width;
			n_height = (img_height < old_height) ? img_height-old_height : (img_height)-old_height;
			n_left = parseInt(img_width/2);
			n_top = parseInt((n_height/2));
			
			scrollHeight = document.documentElement.scrollTop || document.body.scrollTop;
			windowHeight = document.documentElement.clientHeight;
			
			topPop = (parseInt(popup.css('top')));
			if ( (n_top + scrollHeight) >= topPop ) {
				n_top = ((topPop - scrollHeight)-10);
			};
			//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);
				//popup.find('strong').html(ps_title);
				img.fadeIn(200);
				if (fff == 1) {
					zzz = setTimeout( function() { slideNext(); }, 2000);
				}
			});
			
			
			
		}).addClass('image').hide().attr('src',src);
		//loader.show();
	});

		
	
};
function closePhoto(popup,blind){
	slideShowClose();
	popup.remove();
	blind.remove();
	$('select').css({visibility: 'visible'});
}
