var load_count = 0;

var web_root = "http://www.renovacar.cz/";

fotopanel = new FotoPanelXML(0, web_root);

addr = location.href;

function ScrollToTitle() {
  div = document.getElementById("ref_nabidka");
  if (div != undefined)
    div.scrollIntoView(true);
}

/* check page to scroll down */
  DocGen.doOnLoad(ScrollToTitle);


  gallery_addr = 0;


function GalleryLoader(base_path, url, x_onload_handler) {

  var loadcount = 0, gallery_count = -1;
  var def_gallery = 0;
  var handler = function() { }

  if (arguments.length > 2) handler = x_onload_handler;

  AcquireGallery = function(url, order) {
    acquirefunc = function(request) {
      if (request.status == 200) {
        fotolist = CreateFotoListXML(request.responseXML, base_path);
        if (fotolist == null) {
          alert("Invalid XML in file #" + loadcount + ": '" + url + "'");
          alert(request.responseXML.getElementsByTagName("images").length);
        } else {
          fotopanel.addCategory(fotolist, order);
          if (++loadcount == gallery_count) {
            fotopanel.setCategory(def_gallery);
            fotopanel.createFotoBanner(document.getElementById("reference"));
            handler();
          }
        }
      }
    }
    var ajax = new Ajax("GET", base_path + url, acquirefunc);
    ajax.setRequestHeader("Connection", "close");
    ajax.send();
  }

  order = 1000;
  acquirefunc = function(request) {
    if (request.status == 200 && request.responseXML) {
      xml = request.responseXML;
      galleries = xml.getElementsByTagName("galleries")[0];
      if (galleries != undefined) {
        tag = galleries.getElementsByTagName("default")[0];
        if (tag != undefined) {
          value = tag.childNodes[0];
          if (value != undefined)
            def_gallery = value.nodeValue;
        }
        galarray = galleries.getElementsByTagName("gallery");
        gallery_count = galarray.length;
        for (i = 0; i < gallery_count; i++) {
          file_full = 0;
          tag = galarray[i].getElementsByTagName("path")[0];
          if (tag != undefined) {
            value = tag.childNodes[0];
            if (value != undefined) {
              file_full = value.nodeValue;
            }
          }
          tag = galarray[i].getElementsByTagName("priority")[0];
          if (tag != undefined) {
            value = tag.childNodes[0];
            if (value != undefined)
              priority = value.nodeValue;
            else
              priority = order++;
          }
          if (file_full != 0)
            AcquireGallery(file_full, priority);
        }
      }
    }
  }
  var ajax = new Ajax("GET", base_path + url, acquirefunc);
  ajax.setRequestHeader("Connection", "close");
  ajax.send();
}

var RefGalerie = {
  show: function(gallery) {
      
    cont = document.getElementById("ref_galerie");
    DocGen.clearElement(cont);
    cont.appendChild(fotopanel.drawGallery(gallery));
    nabidka = document.getElementById("ref_nabidka");
    nabidka.scrollIntoView(true);
    
  },

  empty: function() {
    cont = document.getElementById("ref_galerie");
    DocGen.clearElement(cont);
    DocGen.putString(cont, "Fotografie nejsou k dispozici.");
    nabidka = document.getElementById("ref_nabidka");
    nabidka.scrollIntoView(true);
  },

  autoShow : function() {
    url = document.location.href;
    var idx, tail, args, key, val;
    idx = url.lastIndexOf("/");
    if (idx < 0) return;
    tail = url.substr(idx+1);
    idx = tail.indexOf("#");
    if (idx < 0) return;
    args = tail.substr(idx+1);
    tail = tail.substr(0, idx);
    idx = args.indexOf("=");
    if (idx < 0) return;
    val = args.substr(idx+1);
    key = args.substr(0, idx);
    if (key == "show") {
      gal_id = parseInt(val);
      if (!isNaN(gal_id)) RefGalerie.show(gal_id);
    }
  }
};

function LoadGalleries() {
/*  AcquireGallery("xml/autofolie.xml", 1);
  AcquireGallery("xml/reklama.xml", 2);
  AcquireGallery("xml/okennifolie.xml", 3);
  AcquireGallery("xml/cisteni.xml", 4);
  AcquireGallery("xml/fullgallery.xml", 5);*/
  new GalleryLoader(web_root, "xml/g_index.xml",
    RefGalerie.autoShow);
}

DocGen.doOnLoad(LoadGalleries);



