function loadXMLDoc(dname)                                                                  //Default function to important an XML document
	{
	if (window.XMLHttpRequest)
		{
		xhttp=new XMLHttpRequest();
		}
	else
		{
		xhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	xhttp.open("GET",dname,false);
	xhttp.send();
	return xhttp.responseXML;
	}

function galleryObject(folderLocation, delay, random)
    {
        this.folderLocation = folderLocation;
        this.delay = delay;
        this.random = random;
        
        this.loadGallery = loadGallery;
        this.loopGallery = loopGallery;
        this.nextImage = nextImage;
        this.previousImage = previousImage;
    }

function loadGallery()
    {
        var currentGallery = loadXMLDoc("/" + this.folderLocation + "/galleryIndex.xml");   //Load the XML list of images
        var imageList = currentGallery.getElementsByTagName("image");
        
        this.fileNameList = new Array();                                                    //Create an array of file names
        this.orderedFileNameList = new Array();                                             //Create an ordered array of file names
        for (var i=0; i<imageList.length; i++)
            {
                var fileName = "/" + this.folderLocation + "/" + imageList[i].getAttribute("fileName");
                this.fileNameList.push(fileName);
                this.orderedFileNameList.push(fileName);
            }
        
        this.randomisedFileNameList = new Array();                                          //Create a random array of file names
        for (var i=0; this.fileNameList.length>0; i++)
            {
                var randomNumber = Math.floor(Math.random()*this.fileNameList.length);
                this.randomisedFileNameList.push(this.fileNameList[randomNumber]);
                this.fileNameList.splice(randomNumber, 1);
            }
        
        document.write("<div class='galleryFrame' id='" + this.folderLocation + "'>");      //Write out the elements
        if (this.random == true)                                                            //Write random images
            {
                for (var i=0; i<this.randomisedFileNameList.length; i++)
                    {
                        document.write("<img src='" + this.randomisedFileNameList[i] + "' class='galleryImage' id='" + this.folderLocation + i + "' />");
                    }
            }
        else                                                                                //Write ordered images
            {
                for (var i=0; i<this.orderedFileNameList.length; i++)
                    {
                        document.write("<img src='" + this.orderedFileNameList[i] + "' class='galleryImage' id='" + this.folderLocation + i + "' />");
                    }
            }
        document.write("</div>");
        
        this.currentImageNumber = 0;
        document.getElementById(this.folderLocation + this.currentImageNumber).style.display = "block";
        
        if (this.delay != 0)
            {
                var that = this;
                var t = setTimeout(function(){that.loopGallery();}, this.delay);
            }
    }

function loopGallery()
    {
        this.nextImage();
        
        var that = this;
        var t = setTimeout(function(){that.loopGallery();}, this.delay);
    }

function nextImage(transitionDuration)
    {
        var that = this;
        $("#" + this.folderLocation + this.currentImageNumber).animate({
            opacity:0
            },
            transitionDuration/2
            ,
            function(){
            $("#" + that.folderLocation + that.currentImageNumber).hide();
            if (that.currentImageNumber == (that.randomisedFileNameList.length - 1)){
                that.currentImageNumber = 0;
            }
            else{
                that.currentImageNumber++;
            }
            $("#" + that.folderLocation + that.currentImageNumber).css("opacity", 0);
            $("#" + that.folderLocation + that.currentImageNumber).css("display", "block");
            $("#" + that.folderLocation + that.currentImageNumber).animate({
                opacity:1
            },
            transitionDuration/2
            );
            }
        );
    }

function previousImage(transitionDuration)
    {
        var that = this;
        $("#" + this.folderLocation + this.currentImageNumber).animate({
            opacity:0
            },
            transitionDuration/2
            ,
            function(){
            $("#" + that.folderLocation + that.currentImageNumber).hide();
            if (that.currentImageNumber == 0)
                {
                    that.currentImageNumber = (that.randomisedFileNameList.length - 1);
                }
            else
                {
                    that.currentImageNumber--;
                }
            $("#" + that.folderLocation + that.currentImageNumber).css("opacity", 0);
            $("#" + that.folderLocation + that.currentImageNumber).css("display", "block");
            $("#" + that.folderLocation + that.currentImageNumber).animate({
                opacity:1
            },
            transitionDuration/2
            );
            }
        );
    }
