function addLink(name) {
    var div = document.getElementById(name + "Gallery");
    var p = document.createElement("p");
    p.setAttribute("id", name + "MoreLink");
    p.setAttribute("class", "moreinfo");
    var a = document.createElement("a");
    a.setAttribute("title", "Více fotografiií");
    a.setAttribute("href", "#" + name + "Gallery");
    pridejListener(a, "click", function () {
        loadPhotos(name);
    });
    a.innerHTML = "Více...";
    p.appendChild(a);
    div.appendChild(p);
}

function loadPhotos(name) {
    var url = "./xml.php?dir=" + name;

    var rq =(window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    rq.open("GET", url, true);
    rq.onreadystatechange = function() {
        httpResponse(name, rq);
    };
    rq.send(null);
}

function httpResponse(name, xml) {
    if(xml.readyState != 4) {
        return;
    }
    
    parseXMLPhotos(name, xml.responseXML);
}

function parseXMLPhotos(name, xml) {
    var imgs = xml.getElementsByTagName("image");
    var mainDiv = document.getElementById(name + "Gallery");
    var divMore = document.createElement("div");
    var linkMore = document.getElementById(name + "MoreLink");
    divMore.setAttribute("id", name + "MorePhotos");
  
    for(var i = 4; i < imgs.length; i++) {
        divPhoto = document.createElement("div");
        divPhoto.setAttribute("class", "photo");
        divMore.appendChild(divPhoto);

        a = document.createElement("a");
        a.setAttribute("href", "./imgs/photo/" + name + "/" + imgs[i].getAttribute("filename"));
        a.setAttribute("title", "Náhled fotografie - klikněte pro zvětšení");

        img = document.createElement("img");
        img.setAttribute("src", "./imgs/photo/" + name + "/nahled_" + imgs[i].getAttribute("filename"));

        a.appendChild(img);
        divPhoto.appendChild(a);
        divMore.appendChild(divPhoto);

        if((i % 4) == 3) {
            hr = document.createElement("hr");
            hr.setAttribute("class", "clear");
            divMore.appendChild(hr);
        }
    }
    mainDiv.appendChild(divMore);
    linkMore.style.display = "none";
}
