// -----------------------------------------------------------------------------------  
  
  // sizes = [
    // "square": "_s",
    // "thumbnail": "_t",
    // "small": "_m",
    // "medium": "",
    // "large": "_b",
    // "original": "_o"
  // ];
  function buildPhotoURL(photo, size) {
    if (size == '_o') {
      url = "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.originalsecret + "_o." + photo.originalformat;
    } else {
      url = "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + size + ".jpg";
    }
    return url;
  }
  
  function xmlHttpCreate() {
    var req = null;
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (sc) {
        req = null;
      }
    }
    if (!req && typeof XMLHttpRequest != "undefined") {
      req = new XMLHttpRequest();
    }
    return req; 
  }
  
  function shorten(s) {
    t = s.substr(0, 15);
    if (t != s)
      return t + '...';
    else
      return s;
  }
  
// -----------------------------------------------------------------------------------  
  
  function main() {
  
    var defgallery = $('defgallery');
    var url = "galerien/phpFlickr/jsFlickr.php";
      
    xmlhttp = xmlHttpCreate();
    xmlhttp.open("GET", url, false);
    xmlhttp.send(""); 

    if (xmlhttp.responseText == "There has been a problem sending your command to the server.")
      return;
    
    sets = eval("(" + xmlhttp.responseText + ")");
    
    if (sets.stat != "ok")
      return;
    
    photosets = sets.photosets.photoset;
    plength = photosets.length;
    if (plength > 4)
    	plength = 4;
    for (i = 0; i < plength; i++) {
      photoset = photosets[i];
      document.write('<div class="gallery"><h2><a href="galerien/index.php?set_id=' + 
        photoset.id + '">' + photoset.title._content + '</a></h2><p>' + shorten(photoset.description._content) + 
        ' | ' + photoset.photos + ' Fotos</p>');
      if (i == 0) {
        xmlhttp = xmlHttpCreate();
        xmlhttp.open("GET", url + '?thumbs=' + photoset.id, false);
        xmlhttp.send(""); 
        photos = eval("(" + xmlhttp.responseText + ")");
        photos = photos.photoset.photo;
        for (j = 0; j < photos.length; j++)
          document.write('<a href="galerien/index.php?set_id?=' + photoset.id + 
            '"><img src="' + buildPhotoURL(photos[j], "_s") + 
            '" border="0" width="38" height="38" alt="" /></a>');
      }
      document.write('</div>');
    }
    
    defgallery.style.display = 'none';
  
  }
  
  main();
