//JavaScript Document
//For dilenbest.ru
//Author: Averkov Oleg
//site: kiora.ru
//doc create 20.02.2010
//doc update 20.02.2010

//Обьявление переменных
var count_pages  = 0;
var befoPage = 3;
var afterPage = 4;
var numBlock = 0;
var count_img = 0;
var count_col = 2;
var count_row = 10;
var curr_pages = 0;
var curr_img = 0;
var count_img_in_str = 0;

//запись переменных
function WriteVal()
{
	writeCookie('count_col',count_col,100000);
	writeCookie('count_row',count_row,100000);
}


// определяем вид таблицы с картинами
function determNumPic()
{
	count_col = readCookie('count_col');
	count_row = readCookie('count_row');
	if(count_col == '')
	{
		switch (screen.width) 
		{
			case 640:
				count_col = 1; count_row = 1; break ;
		   case 800:
				count_col = 2; count_row = 1; break ;
		   case 960:
				count_col = 2; count_row = 1; break ;
		   case 1024 :
				count_col = 3; count_row = 2; break ;
			case 1280 :
				count_col = 3; count_row = 3; break ;
			case 1600 :
				count_col = 3; count_row = 3; break ;				
		   default :
			  count_col = 3; count_row = 3;
		} 
		
	}
}

function BuildPage(contentElement, numPage)
{
	//determNumPic();
	count_img_in_str = count_col * count_row;
	//Определяем количество страниц галереи
	count_img = pic_arr.length;
	if ((count_img%count_img_in_str) >0){ count_pages = (count_img-count_img%count_img_in_str)/count_img_in_str+1}else{count_pages = count_img/count_img_in_str};
	
	strGal='<table width="100%" align="center"  border="0" cellspacing="0" cellpadding="0">';
	emptyPic='<td class="cellPic">&nbsp;</td>'
	var lastPic = 0;
	//определяем первую картину
	var firstPic = count_img_in_str * (numPage-1);
	//определяем последнюю картину на странице
	if (numPage == count_pages){ lastPic = count_img}else{ lastPic = firstPic + count_img_in_str - 1;}	
	//alert('page '+numPage+': '+firstPic+' - '+lastPic);
	for(i=0;i<count_row;i++)
	{
		strGal = strGal + "<tr>";
		for(j=firstPic+i*count_col; j< firstPic+(i+1)*count_col; j++)
		{
			if(j>=count_img){strGal = strGal + emptyPic;}
			else{strGal = strGal + '<td class="cellPic" align="center">'+
									'<a href="'+pic_arr[j].pic_full_path +'"  rel="lightbox[1]" title="'+pic_arr[j].pic_name +'" >'+
									'<img src="'+pic_arr[j].pic_full_path_tamb +'" alt="'+pic_arr[j].pic_name +'" />'+
									'</a>'+
									'<div align="center" class="caption" style="margin-bottom:20px;">'+pic_arr[j].pic_name +'</div></td>';}
		}
		strGal = strGal + "</tr>";

	}
	strGal = strGal + '</table>';
	//отображаем получившуюся галерею на страницу
	set_element(contentElement,strGal);
	
	//построение строки навигации
	var strNavigation = '';
	

	if(numPage > afterPage){strNavigation ='<a href="javascript:BuildPage(1)">1</a>&nbsp;&middot;&nbsp;...' }
	var i = numPage - befoPage;
	//alert (i);	alert (count_pages); alert(numPage + stepPage);
	
	while (i < numPage + afterPage)
   {
	   if (i > count_pages){break;}
	   //alert (i);
	   if (i>0)
	   {
	   		var tempStr = '&nbsp;&middot;&nbsp;';
		   	if(i == 1){tempStr = '';}
			
		   if(i == numPage) 
		   {
		   
		   	strNavigation = strNavigation + tempStr + i;
			}
		   else{strNavigation = strNavigation + tempStr+'<a href="javascript:BuildPage(' + i + ')">' + i +'</a>';}	
	   }
		i++;
   }
	if(numPage + afterPage -1< count_pages){strNavigation = strNavigation + '&nbsp;&middot;&nbsp;...&nbsp;&middot;&nbsp;<a href="javascript:BuildPage(' + count_pages + ')">'+count_pages+'</a>' }

	set_element("menuNav",strNavigation);

	//запись номера последней страницы
	writeCookie('numPage',numPage,1);
	
}

function BuildStrNav()
{
	//'<a href="#">1</a>&nbsp;&middot;&nbsp;<a href="#">...</a><span id="menuNav">&nbsp;</span>&nbsp;&middot;&nbsp;<a href="#">...</a>&nbsp;&middot;&nbsp;<a href="#">20</a>'
	
}
