//------------------------------------------------------------------//
// libErgie 1.0                                                     //
// Javascript Photo Gallery                                         //
// http://sourceforge.net/projects/libergie                         //
// Main functions                                                   //
//------------------------------------------------------------------//

var A;
var CurrPage = 0;
var Current = 0;

var PSize = NCols*NRows;
var NPages = Math.floor(NImages/PSize)-((NImages%PSize) ? 0:1);

document.write("<script language=javascript src='./lang/libergie.lang."+Lang+".js' type='text/javascript'></script>");
document.write("<link rel='stylesheet' href='"+CssFile+"' type='text/css'>");

function Start()
{
   document.write( "<H1 align=center>"+ Title1 + "</H1>");
   document.write( "<H2 align=center>"+ Title2 + "</H2>");
   document.write( "<H2 align=center>"+ Title3 + "</H2>");
   document.write( "<P align=left><TABLE border=0><TR><TD colspan=2>\n");

   if (ShowPopup == false)
   {
      document.write( "<DIV ID=img></DIV>");
   }

   document.write( "<P align=center><TABLE border=0>\n");
   for (i = 0; i<NRows; i++)
   {
      document.write( "<TR>" );
      for (j = 0; j <NCols; j++) 
      {
         A=i*NCols+j;
         document.write("<TD><DIV ID=thumb" + i + "_" + j + ">" +  A + "</DIV></TD>");
      }
      document.write( "</TR>" );
   }
   document.write( "</TABLE>" );
   document.write( "<TR><TD align=left>&nbsp;</TD><TD>&nbsp;</TD></TR>");
   document.write( "</TD></TR><TR><TD align=left><DIV ID=preview></DIV></TD><TD align=right><DIV ID=next></DIV></TD></TR>");
   document.write( "<TR><TD align=left>&nbsp;</TD><TD>&nbsp;</TD></TR>");
   document.write( "<TR><TD align=left>");
   if (ShowGoBack == true)
   {
      document.write( "<A href=javascript:window.history.go(-1)  title='"+HPage+"'>"+HPage+"</A>");
   }
   document.write( "<TD align=right>Erstellt mit <a href=\"http://sourceforge.net/projects/libergie\">libErgie</a></td></tr></TABLE>");
   ShowPage(CurrPage);
}

function ShowInfo()
{
   var stili = "top=80 left=150 width=300, height=120, status=no, menubar=no, toolbar=no scrollbar=no";
   var testo = window.open("", "", stili);
      testo.document.write("<html>\n");
      testo.document.write("<head>\n");
      testo.document.write("<title>"+ShowAbout+"</title>\n");
	  testo.document.write( "<link rel='stylesheet' href='"+CssFile+"' type='text/css'>");
      testo.document.write("</head>\n");
      testo.document.write("<body>\n");
      testo.document.write("<p align=center><B>libErgie</B><BR>Javascript Photo Gallery\n");
	  testo.document.write("<BR><A HREF='http://sourceforge.net/projects/libergie'>Go to the project</A>\n");
      testo.document.write("<p align=center><FORM METHOD=POST ACTION='javascript:window.close()'>\n");
      testo.document.write("<INPUT TYPE='submit' VALUE='"+Close+"'>\n");
      testo.document.write("</FORM>\n");
      testo.document.write("</body>\n");
      testo.document.write("</html>");   
}
function ShowImage(Index)
{
   var stili = "top=80 left=120 width=" + (ImgW[Index] +35)+ ", height=" + (ImgH[Index]+35) + ", status=no, menubar=no, toolbar=no scrollbar=no";
   if (ShowPopup == true)
   {
      var testo = window.open("", "", stili);
      testo.document.write("<html>\n");
      testo.document.write("<head>\n");
      testo.document.write("<title>" + Description[Index] + "</title>\n");
	  testo.document.write( "<link rel='stylesheet' href='"+CssFile+"' type='text/css'>");
      testo.document.write("</head>\n");
      testo.document.write("<body>\n");
      testo.document.write("<p align=center><table border=1><tr><td>");
      testo.document.write("<A href=javascript:window.close()>");
	  testo.document.write("<IMG SRC='" + ImagesPath+'/'+image[Index] + "' border=0 width=" + ImgW[Index]  + " height=" + ImgH[Index] + " title='"+Click+"'>");
	  testo.document.write("</A>\n");
	  testo.document.write("</td></tr></table>");
	  testo.document.write("</body>\n");
      testo.document.write("</html>");   
   }
   else
   {
	  Current=Index;
	  ShowPage(CurrPage);
   }
}

function ShowPage(page)
{
  var offset = page*PSize;

  for (i=0;i<NRows;i++)
  {
     for (j=0;j<NCols;j++)
	 {
	   A = offset+i*NCols+j;
       if (A < NImages)
		  if (ShowDescription)
		   {
             document.getElementById("thumb" + i + "_" + j ).innerHTML = '<span align=center><IMG onclick="ShowImage('+ A +')" SRC="'+ThumbsPath+'/'+image[ A ] + '" BORDER=1 HSPACE=5 VSPACE=5 title="'+Description[A]+'"><br>'+Description[A]+'</span>';
	       }
	       else
           {
             document.getElementById("thumb" + i + "_" + j ).innerHTML = '<span align=center><IMG onclick="ShowImage('+ A +')" SRC="'+ThumbsPath+'/'+image[ A ] + '" BORDER=1 HSPACE=5 VSPACE=5 title="'+Description[A]+'</span>';
		   }
	   else
          document.getElementById("thumb" + i + "_" + j ).innerHTML = '<span align=center><IMG SRC="blank.gif" width=160 height=120 HSPACE=5 VSPACE=5></span>';
     }
   }
   if (ShowPopup == false)
   {
       document.getElementById("img").innerHTML = '<p align=center><IMG SRC="'+ImagesPath+'/'+image[Current] + '" BORDER=1 WIDTH=' + ImgW[Current]  + ' HEIGHT=' + ImgH[Current] + ' HSPACE=5 VSPACE=5 title="'+Description[Current]+'"><br>'+Description[Current];
   }
   if (page > 0 ) 
      document.getElementById("preview").innerHTML = '<A href="#" onclick="PrevPage()" title="'+PPage+'">'+PPage+'</A>';
   else
     document.getElementById("preview").innerHTML = "";

   if (page < NPages ) 
      document.getElementById("next").innerHTML = '<A href="#" onclick="NextPage()" title="'+NPage+'" >'+NPage+'</A>';
   else
     document.getElementById("next").innerHTML = "";
}

function PrevPage()
{
  CurrPage--;
  ShowPage(CurrPage);
}

function NextPage()
{
  CurrPage++;
  ShowPage(CurrPage);
}

function ShowConfig()
{
	var i;
	document.write("<HR><H3>Configuration</H3><P>");
	document.write("<UL>");
	document.write("<LI><B>CSS file = </B>"+CssFile);
	// Language file: it en na
	document.write("<LI><B>Language file = </B>"+Lang);
	// Path for preview and full sized image
	document.write("<LI><B>Thumbals path = </B>"+ThumbsPath);
	document.write("<LI><B>Images path = </B>"+ImagesPath);
	// Title for the web pages
	document.write("<LI><B>Title 1 = </B>"+Title1);
	document.write("<LI><B>Title 2 = </B>"+Title2);
	document.write("<LI><B>Title 3 = </B>"+Title3);
	// Number of rows e culomns in a single page
	document.write("<LI><B>Rows for page = </B>"+NRows);
	document.write("<LI><B>Columns for page = </B>"+NCols);
	// Total number of images in the gallery
	document.write("<LI><B>Number of images in the gallery = </B>"+NImages);

	// Display options
	document.write("<LI><B>Full image in a popup = </B>"+ShowPopup);
	document.write("<LI><B>Show description under preview = </B>"+ShowDescription);
	document.write("<LI><B>Show link to main page = </B>"+ShowGoBack);
	document.write("</UL>")
	document.write("<HR><H3>File descriptions list</H3><P>");
	document.write("<UL>");
	for (i=0;i<NImages ;i++ )
	{
		document.write("<LI>"+Description[i]);
	}
	document.write("</UL>")
}