function centra(IdImg) {
 document.getElementById(IdImg.link).style.position="absolute";
 document.getElementById(IdImg.link).style.left="50%";
 document.getElementById(IdImg.link).style.top="50%";
 document.getElementById(IdImg.link).style.marginLeft="-"+(IdImg.lwidth/2)+"px";
 document.getElementById(IdImg.link).style.marginTop="-"+(IdImg.lheight/2)+"px";
 
 document.getElementById(IdImg.link+"_shd").style.position="absolute";
 document.getElementById(IdImg.link+"_shd").style.left="50%";
 document.getElementById(IdImg.link+"_shd").style.top="50%";
 document.getElementById(IdImg.link+"_shd").style.marginLeft="-"+((IdImg.lwidth)/2 - 5)+"px";
 document.getElementById(IdImg.link+"_shd").style.marginTop="-"+((IdImg.lheight)/2 - 5)+"px";
}

function crea_pagina(category,page,max_photo) {
start=max_photo * page - max_photo;
cont=0;
cont_category=0;
photo_in_page=0;
// while (cont+start<max & photo_in_page<max_photo) {
while (cont<max & photo_in_page<max_photo) {
   if (category==img[cont].category) {
	cont_category++;
	if (cont_category>start) {
//      document.writeln('<span class="foto"><a href="unafoto.html?img='+(cont)+'"><img '+alt(img[cont].title)+' src="img/little'+img[cont].link+'.png" width='+img[cont].lwidth+' height='+img[cont].lheight+'></a></span>');
// Gestione ridimensionamento per browser mobili		
	  shd_dim = 10;
	  imgResize = 32 / 100;
      if (document.body.clientWidth != 'undefined') {
		 documentWidth = document.body.clientWidth;
      } else {
		 documentWidth = window.innerWidth;
      }
      if ((documentWidth * imgResize) < Math.max(img[cont].lwidth, img[cont].lheight)) {
         shd_dim = shd_dim * imgResize;
         if (img[cont].lheight < img[cont].lwidth) {
            img[cont].lheight = img[cont].lheight * (documentWidth * imgResize) / img[cont].lwidth;
            img[cont].lwidth = documentWidth * imgResize;
         } else {
            img[cont].lwidth = img[cont].lwidth * (documentWidth * imgResize) / img[cont].lheight;
            img[cont].lheight = documentWidth * imgResize;
         }
      }
      photo_in_page++;
      if (photo_in_page%2==0) {
         document.writeln('<div class="fotor">');
		 document.writeln('<a href="unafoto.html?img='+(cont)+'"><img id="'+img[cont].link+'_shd" '+alt(img[cont].title)+' src="img/little'+img[cont].shd+'" width='+(img[cont].lwidth+shd_dim)+' height='+(img[cont].lheight+shd_dim)+'></a>');
		 document.writeln('<a href="unafoto.html?img='+(cont)+'"><img id="'+img[cont].link+'"'+alt(img[cont].title)+' src="img/little'+img[cont].link+'.jpg" width='+img[cont].lwidth+' height='+img[cont].lheight+'></a>');
		 document.writeln('</div>');
		 centra(img[cont]);
         document.writeln('</span>');
      } else {
         document.writeln('<span">');
         document.writeln('<div class="fotol">');
		 document.writeln('<a href="unafoto.html?img='+(cont)+'"><img id="'+img[cont].link+'_shd"'+alt(img[cont].title)+' src="img/little'+img[cont].shd+'" width='+(img[cont].lwidth+shd_dim)+' height='+(img[cont].lheight+shd_dim)+'></a>');
		 document.writeln('<a href="unafoto.html?img='+(cont)+'"><img id="'+img[cont].link+'"'+alt(img[cont].title)+' src="img/little'+img[cont].link+'.jpg" width='+img[cont].lwidth+' height='+img[cont].lheight+'></a>');
		 document.writeln('</div>');
		 centra(img[cont]);
      }
    }
   }
   cont++;
}
if (photo_in_page%2!=0) {
   document.writeln('<div class="fotor"></div>');
   document.writeln('</span>');
}
//document.writeln('<table><tbody><tr>');
document.writeln('<span class="precsuc">');
if (page>1) {
//     document.writeln('<td><a href="foto.html?c='+category+'&amp;p='+(page-1)+'" style="font-size:medium"><< Precedenti</a></td>');
     document.writeln('<div class="prec"><a href="foto.html?c='+category+'&amp;p='+(page-1)+'&amp;maxPp='+max_photo+'"><img src="img/icon/back.png" '+alt("Precedenti")+' height="22px" width="22px"> Precedenti</a></div>');
}
nextPage=page;
while (cont<max)  {
   if (category==img[cont].category) {
     nextPage++;
//     document.writeln('<td><p style="text-align:right;"><a href="foto.html?c='+category+'&amp;p='+page+'" style="font-size:medium">Successive >></a></p></td>');
     document.writeln('<div class="suc"><a href="foto.html?c='+category+'&amp;p='+nextPage+'&amp;maxPp='+max_photo+'">Successive <img src="img/icon/forward.png" '+alt("Successive")+' height="22px" width="22px"></a></div>');
     break;
   }
   cont++;
}
// document.writeln('</tr></tbody></table>'); 
// document.writeln('<br><br><br>');
 document.writeln('</span>'); 

 cont=0;
 pageToGo=1;
 cont_category=0;
 while (cont<max) {
   if (category==img[cont].category) {
      cont_category++;
	  if (((cont_category-1) % max_photo) == 0) {
         pageToGo++;
      }
   }
   cont++;
 }

 pageToGo--;
 cont=0;
 if (pageToGo>1) {
    document.writeln('<div class="numPage">');
    document.writeln('<form name=frmPage>');
    document.writeln('<p>Vai alla pagina: ');
    document.writeln('<select name="p" onChange="cambiaPagina(p.options[p.selectedIndex].value,find_parameter(\'c=\',\'\'),find_parameter(\'maxPp=\',4))">');
    while (cont<pageToGo) {
       if (cont+1==page) {
          document.writeln('<option value='+(cont+1)+' selected="selected">'+(cont+1)+'</option>');
       } else {
          document.writeln('<option value='+(cont+1)+'>'+(cont+1)+'</option>');
       }
       cont++;
    }
    document.writeln('</select>');
    document.writeln('</p>');
    document.writeln('</form>');
    document.writeln('</div>');
 }
 document.writeln('<br><br><br>');
}


function cambiaPagina(pp,categoria,maxP) {
self.location.href="foto.html?c="+categoria+"&p="+pp+"&maxPp="+maxP;
//self.location.reload();
}


function crea_combo(category,page,max_photo) {
  document.writeln('<form name=frmPp>');
  document.writeln('<p>Anteprime per pagina:');
  document.writeln('<select name="Pp" onChange="cambiaPagina((Math.floor(((find_parameter(\'p=\',1)-1)*find_parameter(\'maxPp=\',4)+1)/Pp.options[Pp.selectedIndex].value)+1),find_parameter(\'c=\',\'\'),Pp.options[Pp.selectedIndex].value)">');

  cont=2;
  while ((cont<11)||(cont==20)) {
     document.writeln('<option value='+cont);
     if (max_photo==cont) {
        document.write(' selected="selected"');
     }
     document.writeln('>'+cont+'</option>');
	 cont=cont+2;
	 if (cont==12) {
	    cont=20;
     }
  }
  document.writeln('</select>');
  document.writeln('</p>');
  document.writeln('</form>');
}


