var gallery;

function createGallery(maxSirka, maxVyska, previewCount, showFullPreview, previewWidth, previewHeight)
{
    try
    {
    gallery = new Gallery(maxSirka, maxVyska, previewCount, showFullPreview, previewWidth, previewHeight);
    return true;
    }
    catch (err) { showGalleryError(err); return false;}   
}

function setGalleryParam(paramName, paramValue)
  {
    if (gallery == null) {return true;}
    try
    {
    gallery.setParam(paramName, paramValue);
    }
    catch (err) { showGalleryError(err); }
  }

function addGalleryImage(filename, description)
  {
    if (gallery == null) {return true;}
    try
    {
    gallery.addImage (filename, description);
    }
    catch (err) { showGalleryError(err); }
  }

function getDatailImage()
  {
    if (gallery == null) {return true;}
    try
    {         
    gallery.showFullPreview = false;
    gallery.showImage(Number(this.name));
    }
    catch (err) { showGalleryError(err); }    
  }
  
function getListOfImages()
  {
    if (gallery == null) {return true;}  
    try
    {
    gallery.showFullPreview = true;
    gallery.showImage(Number(this.name));
    }
    catch (err) { showGalleryError(err); }      
  }  
  
function showGalleryImage()
  {
    if (gallery == null) {return true;}
    try
    {
    gallery.showImage(gallery.imgNumber);
    }
    catch (err) { showGalleryError(err); }
     
  }

function previousImage()
  {
    if (gallery == null) {return true;}
    try
    {
     gallery.previousImage();
    }
    catch (err) { showGalleryError(err); }
     
  }

function nextImage()
  {
    if (gallery == null) {return true;}  
    try
    {
     gallery.nextImage();
    }
    catch (err) { showGalleryError(err); }
     
  }


function previousPageImage()
  {
    if (gallery == null) {return true;}  
    try
    {
     gallery.previousPage();
    }
    catch (err) { showGalleryError(err); }
     
  }

function nextPageImage()
  {
    if (gallery == null) {return true;}  
    try
    {
     gallery.nextPage();
    }
    catch (err) { showGalleryError(err); }
     
  }
  
function getActualBigImageName()
  {
    if (gallery == null) {return true;}  
    try
    {
     return gallery.getActualBigImageName();
    }
    catch (err) { showGalleryError(err); }     
  }

function setButtonsTransparency(direction)
 {  
    if (gallery == null) {return true;} 
    try
    {
     
     gallery.buttonsOpacity(direction);
    }
    catch (err) { showGalleryError(err); }
    return true;
 }

function scrollImage(e) 
 {        
    if (gallery == null) {return true;}
    try {
    gallery.move(e);
    }
    catch (err) { showGalleryError(err); }
 }

function showGalleryError(err)
 {
   var txt;
   txt ="There was an error on this page.\n\n";
   txt+="Error description: " + err.description + "\n\n";
   txt+="Click OK to continue.\n\n";
   // alert(txt);
 }
 
// *******************************************************************************
// **************** main object **************************************************
// *******************************************************************************

function Gallery (maxSirka, maxVyska, previewCount, showFullPreview, previewWidth, previewHeight)
{
  this.maxSirka = maxSirka;
  this.maxVyska = maxVyska;
  this.pocetNahledu = previewCount;
  this.showFullPreview = showFullPreview;
  this.previewWidth = previewWidth;
  this.previewHeight = previewHeight;  
  this.unit   = "px";

  // next params can be changed through function setParam ($paramname, $value) or directly too, ofcourse :)
  this.dir = "files/";
  this.dirThrumbs = "small/";
  this.dirFull = "big/";
  this.buttonsDir = "images-web/";
  this.imgNumber = 0;
  this.backgroundColor = "black";
  this.borderWidth = "10";
  this.borderColor = "darkgray";
  this.borderStyleVisible = this.borderWidth + this.unit +this.borderColor    +' solid';
  this.borderStyleHidden  = this.borderWidth + this.unit +this.backgroundColor+' solid';  
  this.showThrumbsIfDetail = "true";
  this.fullPreviewMaxCount = 0;  
  // end of section
  
  // next attributes takse valuse of id attribute of tags from html file. You can change it according the html file id's
  this.photoalbumIdName  = "photogallery";
  this.previewIdName     = "preview";
  this.detailIdName      = "detail";
  this.photoIdName       = "photo";
  this.previousIdName    = "previous";
  this.nextIdName        = "next";
  this.counterIdName     = "counter";
  this.textIdName        = "text";
  this.previewButtonIdName = "list";
  this.loadingText       = "Loading image...";
  // end of section
  
  // don't change next params 
  this.opacityValue = 0;
  this.timer;
  this.isIE = !(typeof window.addEventListener != "undefined");
  this.initializedClass = false;
  this.oldstart     = 0;
  this.oldend       = 0;
 
  this.obrazky = new Array();
  
  this.photoalbum  = null;
  this.preview     = null;
  this.detail      = null;
  this.photo       = null;
  this.previous    = null;
  this.next        = null;
  this.counter     = null;
  this.text        = null;
  this.list        = null;   
  
  // end of section
  
  this.move = function move(e)
  {
    try
    {
    var keynum;
    if(window.event)   {keynum = event.keyCode;} // IE
      else if(e.which) {keynum = e.which;} // Netscape/Firefox/Opera
    if (keynum == 13)     {this.showFullPreview = false;this.showImage(this.imgNumber);}
    if (keynum == 27)     {this.showFullPreview = true;this.showImage(this.imgNumber);}      
    if ((keynum == 39) || (keynum == 40)) {this.nextImage();}
    if ((keynum == 37) || (keynum == 38)) {this.previousImage();};
    }
    catch(err) 
    {
    }
  }
  
  this.initClass = function initClass ()
  {
     var chyba = "";
     try
     {
     var nahledy = document.getElementById(this.previewIdName);
     if (nahledy == null) {chyba = "this.previewIdName"; throw chyba;}     
     var list    = document.getElementById(this.previewButtonIdName);
     if (list == null)   {chyba = "this.previewButtonIdName"; throw chyba;}     
     var pocitadlo = document.getElementById(this.counterIdName);
     if (pocitadlo == null) {chyba = "this.counterIdName"; throw chyba;}     
     var popis = document.getElementById(this.textIdName);
     if (popis == null) {chyba = "this.textIdName"; throw chyba;}     
     var next = document.getElementById(this.nextIdName);
     if (next == null) {chyba = "this.nextIdName"; throw chyba;}     
     var prev = document.getElementById(this.previousIdName);
     if (prev == null) {chyba = "this.previousIdName"; throw chyba;}     
     var foto = document.getElementById(this.photoIdName);
     if (foto == null) {chyba = "this.photoIdName"; throw chyba;}
     var detail = document.getElementById(this.detailIdName);
     if (detail == null) {chyba = "this.detailIdName"; throw chyba;}
     var parent = document.getElementById(this.photoalbumIdName);
     if (parent == null) {chyba = "this.photoalbumIdName"; throw chyba;}
     
     this.photoalbum  = parent;
     this.preview     = nahledy;
     this.detail      = detail;
     this.photo       = foto;
     this.previous    = prev;
     this.next        = next;
     this.counter     = pocitadlo;
     this.text        = popis;
     this.previewButton = list;   
     
     this.addKeyEvent();
       
     return true;
     }
     catch (err)
     {
       //alert("V html dokumentu neexistují všechny potřebné elementy :"+err);
       return false;
     }
  }
  
  this.addImage = function addImage ($filename, $description)
    {
       var obrazek = new Array($filename, $description)
       this.obrazky[this.obrazky.length] = obrazek;
    }
    
  this.setParam = function setParam ($paramname, $value)
  {
    eval("this."+$paramname +"=\""+ $value+"\"");  
  }
  
  this.getActualBigImageName = function getActualBigImageName()
  {
    return this.dirFull + this.obrazky[this.imgNumber][0];
  }
 
  this.showNahledy = function showNahledy(limited)
  {
  var pocetObrazku = 1;
  var nahledy = new Array ();
  var div = this.preview;
  
  var start = this.imgNumber - Math.round((this.pocetNahledu-1) / 2);
  
  if (start<0) {start = 0;}
  if (!limited) 
    {
    start = 0;
    if (Number(this.fullPreviewMaxCount)!=0)
      {
      start = this.imgNumber - (this.imgNumber % Number(this.fullPreviewMaxCount));
      }
    }        
        
  if ((limited && (div.childNodes.length>0)) || 
        ((!limited) && 
          ((this.oldstart != start) 
          || 
            (
            (this.oldend    != (Number(this.fullPreviewMaxCount) + this.oldstart) - 1)
            &&
            (this.oldend != this.obrazky.length-1)
            )
          )           
        )  
       )
    {        
      var pocet = div.childNodes.length;
      for (var x = 0; x<pocet; x++)
        {
        var objekt = div.childNodes[0];
        div.removeChild(objekt);
        }
      //alert(this.oldstart + '' + this.oldend + ' z ' + this.obrazky.length);
    }
                
  this.oldstart = start;
  
  if (limited || (div.childNodes.length==0))
    {   
    for (var x = start; x<this.obrazky.length; x++)
      {
      if ((limited) && (pocetObrazku > this.pocetNahledu)) {break;}        
      if ((!limited) && (Number(this.fullPreviewMaxCount)!=0))
       {       
       if (Number(this.fullPreviewMaxCount)<=(x-start)) // next
         { 
         div.appendChild(document.createElement("br"));         
         var newImageNext = document.createElement ("img");
         newImageNext.setAttribute("src",this.buttonsDir+"next.gif");
         newImageNext.setAttribute("name",x+1);
           newImageNext.onclick = nextPageImage;
         div.appendChild (newImageNext);         
         break;
         }       
       if ((x>0) && (x % Number(this.fullPreviewMaxCount) == 0))
         {
         var newImagePrev = document.createElement ("img");
         newImagePrev.setAttribute("src",this.buttonsDir+"previous.gif");
         newImagePrev.setAttribute("name",x-1);
         newImagePrev.onclick = previousPageImage;
         div.appendChild (newImagePrev);
         div.appendChild(document.createElement("br"));         
         }         
       }      
      
      var innerDiv = document.createElement ("div");
      var newImage = document.createElement ("img");
      
      newImage.setAttribute("src",this.dirThrumbs+this.obrazky[x][0]);
      newImage.setAttribute("name",x);  
      newImage.style.border = this.borderStyleHidden;      
      newImage.onclick = getDatailImage;
      newImage.setAttribute("alt",this.obrazky[x][1]);
      
      
      if (x == this.imgNumber) 
        {newImage.style.border = this.borderStyleVisible;}
        else
        {newImage.style.border = this.borderStyleHidden;}
              
      newImage.height = this.previewHeight;
      newImage.width = this.previewWidth;
      
      div.style.textAlign = "center";
      
      if (limited)
        {    
        div.style.textAlign = "center";     
        }
        else
        {
        div.style.textAlign = "left";        
        }           
      
      newImage.style.verticalAlign = "top";
                  
      innerDiv.width  = this.previewWidth  + 2 * this.borderWidth+this.unit;
      innerDiv.height = this.previewHeight + 2 * this.borderWidth+this.unit;            
      innerDiv.style.display = "inline";
          
      div.appendChild (innerDiv);
      innerDiv.appendChild (newImage);
      pocetObrazku += 1;
      if (x == start) {newImage.focus();}
      }  // end for
      this.oldend = start + pocetObrazku - 2; // posun o 1x kvuli poslednimu pricteni pocetObrazku a 1x kvuli startu na 0
    } // end if limited 
    
    if (!limited && (div.childNodes.length>0))
    {
      for (var x = 0; x<div.childNodes.length; x++)
      {
        if (div.childNodes[x].childNodes.length==0) {continue;}
        newImage = div.childNodes[x].childNodes[0];
        if (newImage.tagName.toUpperCase() == "IMG")
        {
          if (newImage.name == this.imgNumber) 
            {newImage.style.border = this.borderStyleVisible;}
            else
            {newImage.style.border = this.borderStyleHidden;}      
        }
      }    
    }
  }

         
  this.showNacitam = function showNacitam($visible)
    {
    if (this.showFullPreview) {return true;}
    var next = this.next;
    var prev = this.previous;
    var list = this.previewButton;
    var popis = this.text;
    var pocitadlo = this.counter;
    
    if ($visible)
      {
      next.style.visibility = "hidden";
      prev.style.visibility = next.style.visibility;
      list.style.visibility = next.style.visibility;
      popis.style.visibility = next.style.visibility;
      pocitadlo.style.visibility = "visible";      
      pocitadlo.innerHTML = this.loadingText; 
      }
      else
      {      
      next.style.visibility = "visible";
      prev.style.visibility = next.style.visibility;      
      pocitadlo.style.visibility = next.style.visibility;
      popis.style.visibility = next.style.visibility;
      list.style.visibility = next.style.visibility;
      }               
    }
    
  this.showButtons = function showButtons()
    {
    if (this.showFullPreview) {return true;}
    var next = this.next;
    var prev = this.previous;
    var img  = this.photo;    
    var fotografie = this.detailId;
    
    next.style.position = "relative";
    prev.style.position = "relative";
  
    if (this.imgNumber > 0)
      {prev.style.visibility = "visible";}
      else
      {prev.style.visibility = "hidden";}
    if (this.imgNumber < (this.obrazky.length-1))
      next.style.visibility = "visible";
      else
      next.style.visibility = "hidden";
      
    if (this.counter.innerHTML == this.loadingText) 
      {
      prev.style.visibility = "hidden";
      next.style.visibility = "hidden";
      this.previewButton.style.visibility = "hidden";
      } 
    
    prev.style.left = -1*(img.offsetWidth/2 - prev.width - this.borderWidth) + this.unit;
    prev.style.top  = -1*(img.offsetHeight/2 + prev.height/2) + this.unit;
    next.style.top  = prev.style.top;
    next.style.left = (img.offsetWidth/2 - next.width - this.borderWidth) + this.unit;
    prev.style.lineHeight = "0";        
    }

   
  this.buttonsOpacity = function buttonsOpacity(value)
    {
    var next = this.next;
    var prev = this.previous;
                                              
    if (value ==1)
      {
      if (this.opacityValue<=40) {this.opacityValue = 40; clearTimeout(this.timer);}
      this.opacityValue += 5;
      }
      else
    if (value == 0)
      {      
      if (this.opacityValue>=40) {this.opacityValue = 40; clearTimeout(this.timer);}
      this.opacityValue -= 10;
      }
    if (value == -1)
      {
      this.opacityValue = 0;
      next.style.visibility = "hidden";  
      prev.style.visibility = "hidden";
      }  
      
    next.style.opacity = this.opacityValue / 100;
    prev.style.opacity = this.opacityValue / 100;
    next.style.filter = "alpha(opacity="+this.opacityValue+")";
    prev.style.filter = "alpha(opacity="+this.opacityValue+")";
    
    if (this.opacityValue>100) {this.opacityValue = 100;}
    if (this.opacityValue<0)   {this.opacityValue = 0;}     
       
    if (!(this.opacityValue == 100) && !(this.opacityValue == 0))
      {
        this.timer = setTimeout("gallery.buttonsOpacity("+ value +")",100);      
      }
    }
          
  this.paintImage = function paintImage($cislo)
    {    
    this.imgNumber = $cislo;    
    if (this.showThrumbsIfDetail != "false")
      {
        this.showNahledy(!this.showFullPreview);
      }
    var parent = this.detail;
    var soubor = this.dir+this.obrazky[this.imgNumber][0];
    var img = new Image();
    img.src = soubor;
    var pomer = img.width / img.height;
    if (img.width >this.maxSirka) {img.width = this.maxSirka; img.height = this.maxSirka / pomer;} 
    if (img.height>this.maxVyska) {img.height = this.maxVyska; img.width = this.maxVyska * pomer;}
        
    var obrazek = this.photo;   
    if (obrazek.src != img.src) 
      {
      this.buttonsOpacity(-1);
      obrazek.style.border = "0"+this.unit+" "+this.backgroundColor + " solid";
      this.showText(false);
      obrazek.src = img.src;
      }
      else
      { // nacetl jsem to
      this.showNacitam(false);
      obrazek.style.border = this.borderStyleVisible;
      obrazek.width  = img.width;
      obrazek.height = img.height;
      this.showNumber();
      this.showText(true);
      this.showButtons();                  
      }        
    }

   this.showNumber = function showNumber()
    {
    if (this.showFullPreview) {return true;} 
    var span = this.counter;
    var cislo = this.imgNumber+1;
    var cislo1 = this.obrazky.length;
    span.innerHTML  = cislo+"/"+cislo1;
    }
    
   this.showText = function showText($show)
    {
    if (this.showFullPreview) {return true;} 
    var span = this.text;
    span.style.visibility = "visible";
    var cislo = this.obrazky[this.imgNumber][1];
    if ($show)        
      {span.innerHTML  = cislo;}
      else
      {span.innerHTML  = "&nbsp;";}
    }
    
   this.previousImage = function previousImage()
    {
    if (this.imgNumber<=0) {this.imgNumber = 0;} else {this.imgNumber = this.imgNumber - 1 ;}        
    this.showImage(this.imgNumber);  
    }
    
   this.nextImage = function nextImage()
    {     
    if (this.imgNumber>=this.obrazky.length-1) {this.imgNumber = this.obrazky.length-1;} else {this.imgNumber = this.imgNumber + 1 ;}        
    this.showImage(this.imgNumber);
    }
       
   this.previousPage = function previousPage()
    {
    var start = this.imgNumber - Number(this.fullPreviewMaxCount) - (this.imgNumber % Number(this.fullPreviewMaxCount));   
    this.imgNumber = start;                
    this.showImage(this.imgNumber);  
    }
    
   this.nextPage = function nextPage()
    { 
    var start = this.imgNumber + Number(this.fullPreviewMaxCount) - (this.imgNumber % Number(this.fullPreviewMaxCount));   
    this.imgNumber = start;    
    this.showImage(this.imgNumber);
    }

   this.showHideDetail = function showHideDetail(style)
   {     
     var nahledy = this.preview; 
     var list    = this.previewButton;
     var pocitadlo = this.counter;
     var popis = this.text;
     var next = this.next;
     var prev = this.previous;
     var foto = this.photo;
     var detail = this.detail;  
     var parent = this.photoalbum;
     
     parent.style.width = "99%";                                
     
     foto.style.visibility = style;
     list.style.visibility = style;
     pocitadlo.style.visibility = style;    
     popis.style.visibility = style; 
     next.style.visibility = style;
     prev.style.visibility = style;  
     
     nahledy.style.display = "inline-block";
     detail.style.display = "inline-block";
     detail.style.cssFloat = "right";
               
     if (popis.style.visibility == "visible")
     {                
     popis.style.position = "relative";
     popis.style.top      = -1 * next.height + this.unit;
     pocitadlo.style.position = "relative";
     pocitadlo.style.top  = -1 * next.height + this.unit;
     list.style.position = "relative";
     list.style.top = popis.style.top;
     }    
              
     if (this.showFullPreview)     
       {
       nahledy.style.visibility = "visible";
       detail.style.width = "0";
       detail.style.height = "0";
       detail.style.cssFloat = "";
       }
       else
       {
         if (this.showThrumbsIfDetail != "false")
         {
         nahledy.style.visibility = "visible";         
         }
         else
         {
         nahledy.style.visibility = "hidden";
         }         
       detail.style.width  = (parent.offsetWidth - this.previewWidth - this.borderWidth*5) + this.unit;    
       detail.style.height = (this.maxVyska + Number(popis.style.lineHeight) + this.borderWidth * 2)+ this.unit;
       }                     
     if (style == "visible")
     {  
        if (nahledy.style.visibility == "visible")
          {
          nahledy.style.width = this.previewWidth+this.borderWidth*2+this.unit;
          }
          else
          {nahledy.style.width = 0;}        
        detail.style.textAlign = "center";
        foto.width  = this.maxSirka+this.unit;
        foto.height = this.maxVyska+this.unit;
        parent.style.height  = detail.style.height;
        nahledy.style.height = detail.style.height;
        nahledy.style.width = (this.previewWidth + this.borderWidth*3)+this.unit;        
     }
     else
     {              
        var pocetNahledu = Math.round(parent.offsetWidth / (this.previewWidth + this.borderWidth * 2));
        var pocetRad = 0; 
        if (Number(this.fullPreviewMaxCount)!=0)
          {pocetRad     = Math.ceil(this.fullPreviewMaxCount / pocetNahledu);}
          else
          {pocetRad     = Math.ceil(this.obrazky.length / pocetNahledu);}
        nahledy.style.width  = "99%";     
        nahledy.style.height = pocetRad * (this.previewWidth + this.borderWidth * 3) + "px";
        parent.style.height  = nahledy.style.height;
        foto.width  = "0"+this.unit;
        foto.height = "0"+this.unit;         
     }
   }
  
 
   this.loadImage = function loadImage($cislo)
    {
    this.showNacitam(true);   
    this.paintImage($cislo);
    }
    
   
   this.showImage = function showImage(cisloObrazku)
   {
     if (!this.initializedClass) {this.initializedClass = this.initClass();}
     if (!this.showFullPreview)
       {    
        this.showHideDetail("visible");       
        this.loadImage(cisloObrazku);        
        this.showButtons();
       }
       else
       {
        this.showHideDetail("hidden");
        this.showNahledy(false);
       }       
   }

   
   this.addKeyEvent = function addKeyEvent()
   {
      document.onkeydown = null;
      document.onkeydown = scrollImage;    
   }   
        
}
