/**
* галерея
**/
(function($) {

	var currentOffset = 0;	//текущее положение
	
	$(window).resize(function() {
				var arrPageSizes = ___getPageSize();
				$('#jquery-overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				
			});
			
  	$.galleryshow = function(id, gal_id){
  	       var count = $('div#div_'+gal_id+' #photo_items li').length; // количество фото
  		     $(".items").css("left","-"+(833*id)+"px"); 
  		     currentOffset=id+1;
  		     // устанавливаем гор. клавиши
  		     set_hotkey();
  		                          
  		     $('#img_'+gal_id+'_'+currentOffset).show();	
  		     
			 // на картинки класса close_gallery ставим закрытие
  		     $(".close_gallery").click(function(){
  		     	// закрытие галереи
  		             var gal_id=$(this).attr('name');
  		             var addr=$(this).attr('addr');
  		             //alert(addr);
  		     	       $("#gallbox_"+gal_id).hide();
  		             $('#jquery-overlay').hide();
  		             window.location.href = addr+'#gallery_'+gal_id;
  		      	//$.galleryclose();
  		       });
             
  		     // размер страницы, для черной хрени        
  		     var arrPageSizes = ___getPageSize();       
  		       $('#jquery-overlay').css({
				backgroundColor:	"#000000",
				opacity:			0.9,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).show();
			    
			 // показываем галерею
  		     $("#gallbox_"+gal_id).show();
  		     // менем размер под тукущую фотографию
  		     $(".scrollable").animate({ "height": $('#img_'+currentOffset).height()+'px'}, 1000 );
  		    //  $(".scrollable").show();
  		     // проверяем какие кнопки показывать  
  		     $.check_buttons(gal_id);
  		     	     
  		      // скролим до картинок
  		    //  $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 200}, 500 );
  		     $("html:not(:animated),body:not(:animated)").scrollTop(200);
  		      
  		      // показываем картинки справа и  слева
  		      // пока пользователь смотрит текущую картинку, они подружаются и он незамечает подгрузки
  		      $.prepare_photo(currentOffset, gal_id);
  		      

  	};

  	// показываем картинки справа и  слева
  	// пока пользователь смотрит текущую картинку, они подружаются и он незамечает подгрузки
  	$.prepare_photo = function(id, sec_id) {
       $('#img_'+sec_id+'_'+(id+1)).show();	
  		 $('#img_'+sec_id+'_'+(id-1)).show();	  
  		 	
  	};	
  	
  	// предидущее фото
  	$.prevphoto = function(cc){
  	      // var cc = $(this).attr('name');
  		     var count = $('div_'+cc+' #photo_items li').length; // количество фото
  		     if (currentOffset<=1) return false;
  		     $(".items").animate({ "left": "+=833px"}, 1000 );	
  		     $('#img_'+cc+'_'+currentOffset).fadeOut(500);	
  		     currentOffset--;
  		     
  		     $('#img_'+cc+'_'+currentOffset).hide(); // прячем фотку, для плавного появления
  		     $.prepare_photo(currentOffset); // подгружаем соседние
  		     
  		     $(".scrollable").animate({ "height": $('#img_'+cc+'_'+currentOffset).height()+'px'}, 1000 );
  		     $('#img_'+cc+'_'+currentOffset).fadeIn(1100);    
  		     $.check_buttons(cc);
  		     
  		      var arrPageSizes = ___getPageSize();     
  		   
  		       $('#jquery-overlay').css({
				backgroundColor:	"#000000",
				opacity:			0.9,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).show();
  		
  	};
  	

  	// следущее фото
  	$.nextphoto = function(tt){
  		    //var count = $('#photo_items li').length; // количество фото
  		      var count = $('div#div_'+tt+' #photo_items li').length; // количество фото
  		   //   alert (count);
  		    if (currentOffset>=count) return false;
  		    $(".items").animate({ "left": "-=833px"}, 1000 );
  		    $('#img_'+tt+'_'+currentOffset).fadeOut(500);	
  		    currentOffset++;
  		    
  		    $('#img_'+tt+'_'+currentOffset).hide(); // прячем фотку, для плавного появления
  		    $.prepare_photo(currentOffset); // подгружаем соседние
  		    
  		    $(".scrollable").animate({ "height": $('#img_'+tt+'_'+currentOffset).height()+'px'}, 1000 );
  	
  		    $('#img_'+tt+'_'+currentOffset).fadeIn(1100);
  		    $.check_buttons(tt);
  		    
  		     var arrPageSizes = ___getPageSize();     
  		   
  		       $('#jquery-overlay').css({
				backgroundColor:	"#000000",
				opacity:			0.9,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).show();
  	};
  	
  	
  	////////////////////////////////////////////////////////////////////////////////////////////////////
  	/////////////////// попросить антона посмотреть,,,,,,,,,,,,.........
  	// скрыть или показать стрелки вправо/лево 
  	$.check_buttons = function(rr){      
  		   if (rr) 
           {   
               var count = $('div#div_'+rr+' #photo_items li').length;
               //alert('тотал: '+count+' current: '+currentOffset);
               if (currentOffset<=1) {$('#'+rr+'_prev_photo_link').css("visibility","hidden"); }//alert("prev_photo");} 
  		   			 else $('#'+rr+'_prev_photo_link').css("visibility","visible");
  		         if (currentOffset>=count) $('#'+rr+'_next_photo_link').css("visibility","hidden");
  		         if (currentOffset<count) $('#'+rr+'_next_photo_link').css("visibility","visible");
            }               
  	     };
  	
  	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);


