//Количество новостей на странице и количество видимых ссылок
count_photo_on_page = 20;
count_photo_ref_on_page = 10;
//Автоматически
count_photo_pages = 1;
cur_photo_page = 0;
cur_photo_block_ref = 0;

function paging_for_photo(path)
{ count_photo_pages = photos.length / count_photo_on_page;
  count_photo_pages = parseInt(count_photo_pages);
  if (photos.length % count_photo_on_page != 0) ++count_photo_pages;
  newitem="";
  if (count_photo_pages > 1)
  { newitem+='<ul>';
    if (cur_photo_block_ref>0)
    newitem+='<li class = "turn_left"><a>...</a></li>';    	
    	
	  for(j=count_photo_ref_on_page*cur_photo_block_ref;j<count_photo_pages && j<count_photo_ref_on_page*(cur_photo_block_ref+1);j++)
		{ 
		  if (j == cur_photo_page)
        	{newitem+='<li>'; 
        	  newitem+=j+1;
        	}
          else
        	{newitem+='<li class = "no_active">'; 
        	 newitem+='<a>'+(j+1)+'</a>';
        	}
          newitem+='</li>';
        }
	if (count_photo_ref_on_page*(cur_photo_block_ref+1) < count_photo_pages)
	   newitem+='<li class = "turn_right"><a>...</a></li>';  
    newitem+='</ul>'
  }
  $('.stranicy').html(newitem);
  
  newitem="";
  k = 0;
  for(i=0;i<photos.length;i++)
  {
	 last="";
	 if (i>=count_photo_on_page*cur_photo_page && i<count_photo_on_page*(cur_photo_page+1) && i<photos.length) 
	 { 
	   if (k==0) newitem +="<div class='box'>";
	   newitem += '<div class = "photo">'; 
	   newitem +=   '<table onclick = "show_big_photo('+i+')" num = "'+ i +'">'+
	                   '<tr>' +
	                     '<td class="image">' +
	                       '<img src="'+photos[i].l_photo+'" alt = "'+photos[i].alt+'" title="'+photos[i].alt+'">'+
	                     '</td>'+
	                   '</tr>'+
	                   '<tr>'+
	                     '<td class="image_alt">' +
	                       photos[i].alt+
	                     '</td>'+
	                   '</tr>'+
	                '</table>';
	   newitem += '</div>';
	   k+=1;
	   if (k==3){k=0;newitem +="</div>";}
     }
	 else
	 {
		 newitem += '<div style="display:none">'; 
		 newitem +=    	'<img src="" alt = "">';
		 newitem += '</div>';	 
	 }
  }
  if (k<3) newitem +="</div>";
  
  $('.gallery').html(newitem);
  
  $('.no_active').click(function(){
	cur_photo_page = $(this).find('a').text()-1;
	paging_for_photo();	  
  })
  $('.turn_left').click(function(){
	--cur_photo_block_ref;
	paging_for_photo();	  
  })
  $('.turn_right').click(function(){
	++cur_photo_block_ref;
	paging_for_photo();
  })
} 
 
//Pop-up

var cur = 0;

function center_bottom_photos()
{ $('.imagesList').css('width',$('.imageShadow .tcm img').width() + 58);
  w = $('.imagesList li:first').offset().top;
  count = 0;
  $('.imagesList').children().each(function(){
    if (w==$(this).offset().top)
      count +=1;
  })
  w = count*$('.imagesList li .photo').width()+(count)*6+(count+2)*4;
  $('.imagesList').css('width',w+'px');
}

function first_image()
{ $('.imageShadow .tcm img').unbind('load');
  $('.imageShadow .tcm img').attr('src', '');
  $(document).scrollLeft(0);
  $('.popupContent').css('top', $(document).scrollTop()+0);
  $('.shadow').height($(document).height());
  $('.shadow').width($(document).width());
  $('.imageShadow .tcm img').bind('load',function(){
    $('.popupContent').width($('.imageShadow .tcm img').width() + 58);
    $('.prev').css('top',parseInt($('.imageShadowWrap').height() / 2));
    $('.next').css('top',parseInt($('.imageShadowWrap').height() / 2));
    $('.imagesList li').removeAttr('class');
    $('.imagesList').children().each(function(i)
    {  if (i == cur)
             $(this).attr('class','current');
    })
    $('.shadow').height($(document).height());
    $('.shadow').width($(document).width());
    center_bottom_photos();
  })
  $('.imageShadow .tcm img').attr('src', photos[cur].b_photo);
  $('.imageShadow td.text').text(photos[cur].alt);
}


function change_image()
{ $('.imageShadow .tcm img').unbind('load');
  $('.imageShadow .tcm img').stop();
  $(document).scrollTop($('.popupContent').css('top').substr(0,$('.popupContent').css('top').length - 2)*1 - 0 );
  $(document).scrollLeft(0);
  $('.imageShadow .tcm img').animate({opacity:0},500,function(){
	$('.imageShadow .tcm img').bind('load',function(){
	      $('.popupContent').width($('.imageShadow .tcm img').width() + 58);
          $('.prev').css('top',parseInt($('.imageShadowWrap').height() / 2));
          $('.next').css('top',parseInt($('.imageShadowWrap').height() / 2));
          $('.imagesList li').removeAttr('class');
          $('.imagesList').children().each(function(i)
          {  if (i == cur)
             $(this).attr('class','current');
          })
          $('.shadow').height($(document).height());
          $('.shadow').width($(document).width());
          $('.imageShadow .tcm img').animate({opacity:1},500);
          center_bottom_photos();
       })
       $('.imageShadow .tcm img').attr('src', photos[cur].b_photo);
	   $('.imageShadow td.text').text(photos[cur].alt);
	
   })
  
}

function show_big_photo(num)
{ cur = num; 
  $(document).ready(function(){
   $('embed').css('display','none');
   if (!$('.popup').length)
	{ newitem = "";
         newitem +='<div class="shadow"></div>';
         newitem +='<div class="popup">';
         newitem +=   '<div class="popupContent">';
         newitem +=      '<div class="imageShadowWrap">';
         newitem +=			'<span class="close ie" title="Закрыть"></span>';
         newitem += 			'<div class="next">';
         newitem +=				'<span class="nextArrow ie" title="Вправо"></span>';
         //newitem +=				'Ctrl + &rarr;';
         newitem +=			'</div>';
         newitem +=			'<div class="prev">';
         newitem +=			    '<span class="prevArrow ie" title="Влево"></span>';
         //newitem +=				'&larr; + Ctrl';
         newitem +=			'</div>';
         newitem +=			'<table class="imageShadow">';
         newitem +=				'<tr>';
         newitem +=				  '<td class="tlt"><i class="ie"></i></td>';
         newitem +=   			  '<td class="tct"></td>';
         newitem +=				  '<td class="trt"><i></i></td>';
         newitem +=				'</tr>';
         newitem +=				'<tr>';
         newitem +=				  '<td class="tlm"></td>';
         newitem +=				  '<td class="tcm"><img src="'+photos[cur].b_photo+'" alt="" title="" /></td>';
         newitem +=				  '<td class="trm"></td>';
         newitem +=				'</tr>';
         newitem +=				'<tr>';
         newitem +=				  '<td class="tlm"></td>';
         newitem +=				  '<td class="text">'+photos[cur].alt+'</td>';
         newitem +=				  '<td class="trm"></td>';
         newitem +=				'</tr>';
         newitem +=				'<tr>';
         newitem +=				  '<td class="tlb"></td>';
         newitem +=				  '<td class="tcb"></td>';
         newitem +=				  '<td class="trb"></td>';
         newitem +=				'</tr>';
         newitem +=			'</table>';
         newitem +=		'</div>';
         newitem +=		'<ul class="imagesList">';
         for (i=0;i<photos.length;i++)
         {
           if (i==cur) cl = "current";
           else cl = "";
           newitem += '<li class="'+cl+'">';
             newitem += '<div class = "photo no_margin">';
	      newitem +=   '<table><tr><td class="image"><img src="'+photos[i].l_photo+'" alt = "'+photos[i].alt+'" title="'+photos[i].alt+'" num = "'+ i +'"></td></tr></table>';
	      newitem += '</div>';
	    newitem += '</li>';
          }
          newitem +=		'</ul>';
          newitem +=	'</div>';
          newitem +='</div>';	
	   $('body').append(newitem);
	   
          $('.close').click(function()
	    {   
          $('embed').css('display','inline');
       	  $('.shadow').css('display','none');
		  $('.popup').css('display','none');	  		
	    })
	   $('.prevArrow').click(function()
	    {   
		    cur*=1;
			if (cur<=0) cur = photos.length - 1;
			else cur-=1;
			change_image();
	    })
	   $('.nextArrow, .imageShadow .tcm img').click(function()
	    {   
			cur*=1;
		    if (cur>=photos.length - 1) cur = 0;
			else cur+=1;
			change_image();
	    })
	    
          $('.popupContent .imagesList li').click(function(){
            cur = $(this).find('img').attr('num'); 
    	     change_image();    	   
          })
                
	}
	else
	  { $('.shadow').css('display','block');
           $('.popup').css('display','block');
         }
       first_image();
    })
		
}

$(document).ready(function(){										
	}
);
