function GalleryApp() {
    
    this.GalleryID
    var args = location.search.substring(1);
    if(args) {
        this.GalleryID = args.match(/gallery=(\d+)/);
        if(!this.GalleryID) {
            this.GalleryID = 0;
        } else {
            this.GalleryID = this.GalleryID[1];
        }
    } else {
        this.GalleryID = 0;
    }

    if (this.GalleryID) {
        this.BaseImagePath = "/images/galleries/" + this.GalleryID + "/images/"
        this.GalleryImages = new Array();
        this.FrontImage = document.getElementById("CurrentImage");
        this.ImageIndex;
        this.ThumbnailList = document.getElementById("ImageThumbnails");
        this.ThumbnailLinks = this.ThumbnailList.getElementsByTagName("A");
        this.ThumbnailImages = this.ThumbnailList.getElementsByTagName("IMG");
        this.PrevLink = document.getElementById("PrevLink");
        this.NextLink = document.getElementById("NextLink");
        this.ImageCaption = document.getElementById("ImageCaption");
        this.ImageCredit = document.getElementById("ImageCredit");
        this.ImageNumber = document.getElementById("ImageNumber");
        this.NumberOfImages = document.getElementById("NumberOfImages");

		var size = this.ThumbnailLinks.length * 62;
		if (size < 744) {
			this.ThumbnailList.style.paddingLeft = (744-size)/2 + 3 + "px";
			this.ThumbnailList.style.width = (744 + size)/2 + "px";
		}

        for(var i = 0; i < this.ThumbnailLinks.length; i++) {
            this.ThumbnailLinks[i].onclick = function(j, o) {
                return function() {
                    o.changeImage(j);
                    return false;
                };
            }(i, this);
            if(this.ThumbnailImages[i].className == "currentThumb") {
                this.ImageIndex = i;
            }
            this.ThumbnailImages[i].onmouseover = function() { if(this.className != "currentThumb") this.className = "hover"; };
            this.ThumbnailImages[i].onmouseout = function() { if(this.className != "currentThumb") this.className = this.className = ""; };
        }
        
        this.PrevLink.onclick = function(obj) { return function() {obj.prevImage(); return false;} }(this);
        this.NextLink.onclick = function(obj) { return function() {obj.nextImage(); return false;} }(this);
    } else {
        var GalleryThumbLIs = document.getElementById("GalleryThumbnails").getElementsByTagName("LI");
        for(var i = 0; i < GalleryThumbLIs.length; i++) {
            GalleryThumbLIs[i].onmouseover = function() { this.className = "hover"; };
            GalleryThumbLIs[i].onmouseout = function() { this.className = ""; };
            GalleryThumbLIs[i].onclick = function() { window.location = this.getElementsByTagName("A")[0].href; };
        }
    }
}

GalleryApp.prototype.loadImage = function(i, func) {
    //first create a new image object
    var img = new Image();
    img.isloaded = false;
    img.onload = function(k, obj) { 
                        return function() { 
                            this.isloaded = true; 
                            obj.changeImage(k); 
                        } 
                    }(i, this);

    this.GalleryImages[i] = "loading";
	var request = getHTTPRequest();
    request.onreadystatechange = function(j, o, img) {
        return function() {
            if(request.readyState == 4 && request.status == 200) {
                o.GalleryImages[i] = eval(request.responseText);
                o.GalleryImages[i].img = img;
			    img.src = o.BaseImagePath + o.GalleryImages[i].src;
            }
        };
    }(i, this, img);
    request.open("GET", "GalleryXML.aspx?img=" + (i+1) + "&gallery=" + this.GalleryID);
    request.send(null);
}

GalleryApp.prototype.changeImage = function(i) {
    if (this.GalleryImages[i] == undefined) {
        this.loadImage(i);
    } else {
        this.FrontImage.src = this.GalleryImages[i].img.src;
        this.ThumbnailImages[this.ImageIndex].className = this.ThumbnailImages[this.ImageIndex].className.replace("currentThumb", "");
        this.ImageIndex = i;
        this.ThumbnailImages[i].className = "currentThumb";
        this.ImageCaption.innerHTML = this.GalleryImages[i].caption;
        this.ImageCredit.innerHTML = this.GalleryImages[i].credit;
        this.ImageNumber.innerHTML = i+1;
        this.NumberOfImages.innerHTML = this.ThumbnailLinks.length;
        
		if (this.ImageIndex == this.ThumbnailImages.length - 1) {
        	this.NextLink.removeAttribute("href");
			this.PrevLink.href="http://www.catholicnewworld.com/gallery.aspx?img=" + (this.ThumbnailImages.length - 1) + "&gallery=1";
		} else if (this.ImageIndex == 0) {
			this.PrevLink.removeAttribute("href");
			this.NextLink.href="http://www.catholicnewworld.com/gallery.aspx?img=2&gallery=1";
		} else {
			this.NextLink.href="http://www.catholicnewworld.com/gallery.aspx?img=" + (this.ImageIndex + 2) + "&gallery=1";
			this.PrevLink.href="http://www.catholicnewworld.com/gallery.aspx?img=" + (this.ImageIndex) + "&gallery=1";
		}
    }
}

GalleryApp.prototype.nextImage = function() {
    if (this.ImageIndex < this.ThumbnailImages.length-1) {
        this.ThumbnailImages[this.ImageIndex + 1].scrollIntoView();
        this.changeImage(this.ImageIndex+1);
    }
}

GalleryApp.prototype.prevImage = function() {
    if (this.ImageIndex > 0) {
        this.ThumbnailImages[this.ImageIndex - 1].scrollIntoView();   
        this.changeImage(this.ImageIndex-1);
    }
}

var App;
function init() { 
    App = new GalleryApp();
}
window.onload = init;
