FotoPanelXML_root = "fotopanelxml";

FotoPanelXML_instances = [];
FotoPanelXML_instance = 0;

/*s = '<script type="text/javascript" language="JavaScript" src="' +
  FotoPanelXML_root + '/hh.js"></script>';

document.write(s);*/

function FotoPanelXML(size, base_path) {
  this.instance = FotoPanelXML_instance++;
  var root;
  if (arguments.length < 2)
    root = FotoPanelXML_root;
  else
    root = base_path + FotoPanelXML_root;
  this.root = root;

  FotoPanelXML_instances[this.instance] = this;

  var iname = "FotoPanelXML_instances[" + this.instance + "]";

/*  var hh = new HistoryHelper();*/
  
  var imgdisplay = 0, gallerydisplay = 0;
  var gallery_url = "";
  var enablegallery = 1;

  var categories = [];
  var catorder = [];
  var curcategory = 0;

  this.setGalleryUrl = function(url) { gallery_url = url; }
  this.enableGallery = function() { enablegallery = 1; }
  this.disableGallery = function() { enablegallery = 0; }

  this.addCategory = function(category, order) {
    if (arguments.length < 2) order = Infinity;
    for (i = 0; i < catorder.length && order > catorder[i]; i++);
    for (j = categories.length; j > i; j--) {
      categories[j] = categories[j - 1];
      catorder[j] = catorder[j - 1];
    }
    categories[j] = category;
    catorder[j] = order;
    return j;
  }

  this.categoryCount = function() { return categories.length; }
  this.getCategory = function(index) { return categories[index]; }
  this.getCategoryTitle = function(index) { return categories[index].title; }

  var bannersize = arguments.length >= 1 ? size > 0 ? size : 4 : 4;
  var start = 0;

  this.setCategory = function(category) {
    curcategory = category;
    start = 0;
  }

  this.prev = function() {
    if (start > 0) start--;
    this.draw();
  }

  this.next = function() {
    if (start + bannersize < categories[curcategory].fotolist.length) start++;
    this.draw();
  }

  this.draw = function() {
    var classname, aname;
    var img, href;
    for (i = 0; i < bannersize; i++) {
      imgname = "foto" + i;
      aname = imgname + "a";
      img = document.getElementById(imgname);
      href = document.getElementById(aname);
      idx = start + i;
      img.setAttribute("src", categories[curcategory].fotolist[idx][1]);
      href.setAttribute("href", "JavaScript:" + iname + ".displayImage(" + idx + ")");
//      href.setAttribute("href", categories[curcategory].fotolist[start + i][0]);
    }
  }

  this.createFotoBanner = function(upcontainer) {
    container = document.createElement("DIV");
    container.className = "fotoxml fotobanner";
    upcontainer.appendChild(container);

    a = document.createElement("A");
    a.href = "JavaScript:" + iname + ".prev()";
    container.appendChild(a);
    img = document.createElement("IMG");
    img.src = root + "/leftarrow.png";
    img.className = "leftarrow";
    a.appendChild(img);

    for (i = 0; i < bannersize; i++) {
      idx = start + i;
      a = document.createElement("A");
//      a.href = categories[curcategory].fotolist[start + i][0];
      a.href = "JavaScript:" + iname + ".displayImage(" + idx + ")";
      a.id = "foto" + i + "a";
      container.appendChild(a);
      img = document.createElement("IMG");
      img.src = categories[curcategory].fotolist[start + i][1];
      img.className = "foto";
      img.id = "foto" + i;
      a.appendChild(img);
    }

    a = document.createElement("A");
    a.href = "JavaScript:" + iname + ".next()";
    container.appendChild(a);
    img = document.createElement("IMG");
    img.src = root + "/rightarrow.png";
    img.className = "rightarrow";
    a.appendChild(img);
  }

  function closeImage() {
    if (imgdisplay != 0) {
      DocGen.clearElement(imgdisplay);
      document.getElementsByTagName("BODY")[0].removeChild(imgdisplay);
      imgdisplay = 0;

      if (gallerydisplay == 0) {
        DocGen.showAllElements("OBJECT");
        DocGen.showAllElements("EMBED");
      }
    }
  }

  this.closeImage = closeImage;

  this.displayImage = function(idx, cat) {
    direct = false;
    if (arguments.length < 2) {
      cat = curcategory;
    } else {
      direct = true;
    }
    ii = new Image();
    ii.onload = function() {
      if (imgdisplay != 0)
        closeImage();

      border = 10;
      buttonwidth = 100;
      buttonheight = 40;

      DocGen.hideAllElements("OBJECT");
      DocGen.hideAllElements("EMBED");

      width = ii.width + 2 * border;
      height = ii.height + 2 * border;
      toppos = (document.documentElement.clientHeight - height)/2;
/*      alert(document.documentElement.clientWidth + 'x' + document.documentElement.clientHeight + ";"
        + ii.width + 'x' + ii.height + ";" + toppos);*/

      body = document.getElementsByTagName("BODY")[0];

      imgdisplay = document.createElement("DIV");
      imgdisplay.className = "fotoxml fotodisplay";

      p = document.createElement("DIV");
      p.className = "wallpaper";
      imgdisplay.appendChild(p);

      cont = document.createElement("DIV");
      cont.className = "content";
      cont.style.top = toppos + "px";
      cont.style.width = width + "px";
      cont.style.height = height + "px"
      imgdisplay.appendChild(cont);

      bg = document.createElement("DIV");
      bg.className = "background";
      cont.appendChild(bg);

      a = document.createElement("A");
      a.className = "imgblock";
      a.href = "JavaScript:" + iname + ".closeImage()";
      a.style.width = ii.width + "px";
      a.style.left = border + "px";
      a.style.top = border + "px";
      cont.appendChild(a);


/*      img = document.createElement("IMG");
      img.src = categories[cat].fotolist[idx][0];
      a.appendChild(img);*/
      a.appendChild(ii);

      /* here come the navigation buttons (prev/next/gallery/close) */
      div = document.createElement("A");
      if (idx > 0) {
        div.href = "JavaScript:" + iname + ".displayImage(" + (idx - 1)
          + (direct ? "," + cat : "") + ")";
      }
      div.className = "button";
      div.style.display = "block";
      div.style.left = border + "px";
      div.style.top = (ii.height - buttonheight + border) + "px";
      div.style.height = buttonheight + "px";
      div.style.width = buttonwidth + "px";
      cont.appendChild(div);
     
      img = document.createElement("IMG");
      img.src = root + "/prev.png";
      div.appendChild(img);

      div = document.createElement("A");
      if (idx < categories[curcategory].fotolist.length)
        div.href = "JavaScript:" + iname + ".displayImage(" + (idx + 1)
          + (direct ? "," + cat : "") + ")";
      div.className = "button";
      div.style.left = (ii.width - buttonwidth + border) + "px";
      div.style.top = (ii.height - buttonheight + border) + "px";
      div.style.height = buttonheight + "px";
      div.style.width = buttonwidth + "px";
      cont.appendChild(div);
     
      img = document.createElement("IMG");
      img.src = root + "/next.png";
      div.appendChild(img);

      if (gallerydisplay == 0 && !direct && enablegallery) { /* do not show when the gallery is open */
        div = document.createElement("A");
        if (gallery_url == "")
          div.href = "JavaScript:" + iname + ".displayGallery()";
        else
          div.href = gallery_url;
        div.className = "button";
        div.style.left = border + "px";
        div.style.top = border + "px";
        div.style.height = buttonheight + "px";
        div.style.width = buttonwidth + "px";
        cont.appendChild(div);
       
        img = document.createElement("IMG");
        img.src = root + "/gallery.png";
        div.appendChild(img);
      }

      div = document.createElement("A");
      div.href = "JavaScript:" + iname + ".closeImage()";
      div.className = "button";
      div.style.left = (ii.width - buttonwidth + border) + "px";
      div.style.top = border + "px";
      div.style.height = buttonheight + "px";
      div.style.width = buttonwidth + "px";
      cont.appendChild(div);
     
      img = document.createElement("IMG");
      img.src = root + "/close.png";
      div.appendChild(img);
      /* end of navigation buttons */

      body.appendChild(imgdisplay);
      
      location.href = "#foto";
    }
    ii.src = categories[cat].fotolist[idx][0];
  }

  function closeGallery() {
    if (gallerydisplay != 0) {
      DocGen.clearElement(gallerydisplay);
      document.getElementsByTagName("BODY")[0].removeChild(gallerydisplay);
      gallerydisplay = 0;

      DocGen.showAllElements("OBJECT");
      DocGen.showAllElements("EMBED");
    }
  }

  this.closeGallery = closeGallery;

  this.drawGallery = function(category_num) {
    p = document.createElement("P");
    p.className = "gallery";
    for (i = 0; i < categories[category_num].fotolist.length; i++) {
      foto = categories[category_num].fotolist[i];
      a = document.createElement("A");
      a.href = "JavaScript:" + iname + ".displayImage(" + i + "," + category_num + ")";
      a.className = "pic";
      img = document.createElement("IMG");
      img.src = foto[1];
      a.appendChild(img);
      p.appendChild(a);
    }
    return p;
  }

  this.displayGallery = function(category_num) {
    if (imgdisplay != 0)
      this.closeImage();
    if (gallerydisplay != 0)
      this.closeGallery();

    DocGen.hideAllElements("OBJECT");
    DocGen.hideAllElements("EMBED");

    if (arguments.length <= 0)
      category_num = curcategory;
    body = document.getElementsByTagName("BODY")[0];

    gallerydisplay = document.createElement("DIV");
    gallerydisplay.className = "fotoxml fotogallery";

    p = document.createElement("DIV");
    p.className = "wallpaper";
    gallerydisplay.appendChild(p);

    main = document.createElement("DIV");
    main.className = "content";
    p = document.createElement("P");
    p.className = "menu";
    for (i = 0; i < categories.length; i++) {
      a = document.createElement("A");
      a.href = "JavaScript:" + iname + ".displayGallery(" + i + ")";
      a.style.padding = "0px 15px 0px 15px";
      a.appendChild(document.createTextNode(categories[i].title));
      p.appendChild(a);
/*      if (fotokategorie[i+1][0] != 0)
        p.appendChild(document.createTextNode("   ")); /* 2x NBSP */
    }
    a = document.createElement("A");
    a.href = "JavaScript:" + iname + ".closeGallery()";
    a.appendChild(document.createTextNode("zavřít galerii"));
    p.appendChild(a);
    main.appendChild(p);

    main.appendChild(this.drawGallery(category_num));

    gallerydisplay.appendChild(main);

    body.appendChild(gallerydisplay);
    body.scrollIntoView(true);
  }

}

