/**
* галерея
**/
(function($) {
	
	var currentOffset = 0;	//текущее положение
	
	$(window).resize(function() {
				var arrPageSizes = ___getPageSize();
				$('#jquery-overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				
			});
			
  	$.galleryshow = function(id) {
  		     var count = $('#photo_items li').length; // количество фото
  		     
  		     $(".items").css("left","-"+(833*id)+"px"); 
  		     currentOffset=id+1;
  		     
  		     // устанавливаем гор. клавиши
  		     set_hotkey();
  		         
  		     $('#img_'+(currentOffset)).show();	
  		     
			 // на картинки класса close_gallery ставим закрытие
  		     $(".close_gallery").click(function () {
  		      	$.galleryclose();
  		       });
             
  		     // размер страницы, для черной хрени        
  		     var arrPageSizes = ___getPageSize();       
  		       $('#jquery-overlay').css({
				backgroundColor:	"#000000",
				opacity:			0.9,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).show();
			    
			 // показываем галерею
  		     $("#gallbox").show();
  		     // менем размер под тукущую фотографию
  		     $(".scrollable").animate({ "height": $('#img_'+currentOffset).height()+'px'}, 1000 );
  		     // проверяем какие кнопки показывать
  		     $.check_buttons();
  		     	     
  		      // скролим до картинок
  		    //  $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 200}, 500 );
  		     $("html:not(:animated),body:not(:animated)").scrollTop(200);
  		      
  		      // показываем картинки справа и  слева
  		      // пока пользователь смотрит текущую картинку, они подружаются и он незамечает подгрузки
  		      $.prepare_photo(currentOffset);
  		      
  		     /*
  		     IE непонимает эту красоту, поэтому биндим кнопки в другом месте :) 
  		     $(window).keydown(function(event){
    			alert(event.keyCode);
    			switch (event.keyCode ? event.keyCode : event.which ? event.which : null)
					{
						case 0x25:$.prevphoto();break;
						case 0x27:$.nextphoto();break;	
				        case 0x1B:$.galleryclose();break;
					}	
				});
             */
	 		//$(".items").animate({ "left": "-="+(842*id)+"px"}, 1000  ); 
  	};
  	
  	// закрытие галереи
  	$.galleryclose = function(id) {
  		 
  		$("#gallbox").hide();
  		$('#jquery-overlay').hide();
 

  	};	
  	
  	// показываем картинки справа и  слева
  	// пока пользователь смотрит текущую картинку, они подружаются и он незамечает подгрузки
  	$.prepare_photo = function(id) {
         $('#img_'+(id+1)).show();	
  		 $('#img_'+(id-1)).show();	  
  		 	
  	};	
  	
  	// предидущее фото
  	$.prevphoto = function() {
  		     var count = $('#photo_items li').length; // количество фото
  		     
  		     if (currentOffset<=1) return false;
  		     $(".items").animate({ "left": "+=833px"}, 1000 );	
  		     $('#img_'+currentOffset).fadeOut(500);	
  		     currentOffset--;
  		     
  		     $('#img_'+currentOffset).hide(); // прячем фотку, для плавного появления
  		     $.prepare_photo(currentOffset); // подгружаем соседние
  		     
  		     $(".scrollable").animate({ "height": $('#img_'+currentOffset).height()+'px'}, 1000 );
  		     $('#img_'+currentOffset).fadeIn(1100);
  		     $.check_buttons();
  		     
  		      var arrPageSizes = ___getPageSize();     
  		   
  		       $('#jquery-overlay').css({
				backgroundColor:	"#000000",
				opacity:			0.9,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).show();
  		
  	};
  	

  	// следущее фото
  	$.nextphoto = function() {
  		    var count = $('#photo_items li').length; // количество фото
  		 
  		    if (currentOffset>=count) return false;
  		    $(".items").animate({ "left": "-=833px"}, 1000 );
  		    $('#img_'+currentOffset).fadeOut(500);	
  		    currentOffset++;
  		    
  		    $('#img_'+currentOffset).hide(); // прячем фотку, для плавного появления
  		    $.prepare_photo(currentOffset); // подгружаем соседние
  		    
  		    $(".scrollable").animate({ "height": $('#img_'+currentOffset).height()+'px'}, 1000 );
  	
  		    $('#img_'+currentOffset).fadeIn(1100);
  		    $.check_buttons();
  		    
  		     var arrPageSizes = ___getPageSize();     
  		   
  		       $('#jquery-overlay').css({
				backgroundColor:	"#000000",
				opacity:			0.9,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).show();
  	};
  	
  	// скрыть или показать стрелки вправо/лево 
  	$.check_buttons = function() {
  		   var count = $('#photo_items li').length; // количество фото
  		   if (currentOffset<=1) $('#prev_photo_link').css("visibility","hidden"); 
  		   				else $('#prev_photo_link').css("visibility","visible");
  		   if (currentOffset>=count) $('#next_photo_link').css("visibility","hidden"); 
  		                else $('#next_photo_link').css("visibility","visible");			
  		   //if (currentOffset<=1) $('#prev_photo_link').hide(); else $('#prev_photo_link').show();
  	       //if (currentOffset>=count) $('#next_photo_link').hide(); else $('#next_photo_link').show();
  	};
  	
  	function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
})(jQuery);
