function CGalerie(szGalerie)
{
    this.pGalerie       = null;

    this.pCanvas        = null;
    this.pPrevPhoto     = null;
    this.pNextPhoto     = null;
    this.pPhoto         = null;

    this.pSlider        = null;
    this.pThumbs        = null;
    this.pPrevThumb     = null;
    this.pNextThumb     = null;

    this.pAlbumTitle    = null;
    this.pPhotoTitle    = null;
    this.pComment       = null;
    
    this.pCurrentPhoto  = null;
    this.pCurrentThumb  = null;
    this.pPhotoCount	= null;
    
    this.szAlbumDir     = "";
    this.szPhotoTitle   = "";
    this.szComment      = "";
    
    this.szAlbumRootPath    = "files/galerie/";
    
    this.nWalk          = 462;
    this.nPosition      = 0;
    this.nMaxPhoto      = 3;
    
    if(document.getElementById(szGalerie))
    {
        this.pGalerie = document.getElementById(szGalerie);

        this.pAlbumTitle    = this.pGalerie.getElementsByTagName("h2")["AlbumName"].getElementsByTagName("span")[0];
        this.pPhotoTitle    = this.pGalerie.getElementsByTagName("h3")["PhotoName"];
        
        this.pCanvas    = this.pGalerie.getElementsByTagName("div")["Canvas"];
        this.pPhoto     = this.pCanvas.getElementsByTagName("div")["Photo"];
        this.pPrevPhoto = this.pCanvas.getElementsByTagName("div")["PrevPhoto"].getElementsByTagName("a")[0];
        this.pNextPhoto = this.pCanvas.getElementsByTagName("div")["NextPhoto"].getElementsByTagName("a")[0];
        this.pComment   = this.pPhoto.getElementsByTagName("p")[0];
        
        this.pSlider    = this.pGalerie.getElementsByTagName("div")["Slider"];
        this.pThumbs    = this.pSlider.getElementsByTagName("div")["Thumbs"].getElementsByTagName("ul")[0];
        this.pPrevThumb = this.pSlider.getElementsByTagName("div")["PrevThumb"].getElementsByTagName("a")[0];
        this.pNextThumb = this.pSlider.getElementsByTagName("div")["NextThumb"].getElementsByTagName("a")[0];
        
        this.pCurrentPhoto = this.pGalerie.getElementsByTagName("input")["inCurrentPhotoId"];
        this.pCurrentThumb = this.pGalerie.getElementsByTagName("input")["inCurrentThumbId"];
	this.pPhotoCount = this.pGalerie.getElementsByTagName("input")["inPhotoCount"];

	this.pThumbs.style.left = "0px";
	this.pPhotoCount.value = 1;
    }
}

CGalerie.prototype.Run = function()
{
    var i;
    var thumbs = this.pThumbs.getElementsByTagName("li");
    var self = this;
    
    // set first photo as current
    this.setCurrentPhoto("TID1");
    
    // Thumbs
    for(i = 0; i < thumbs.length; i++)
    {
        // set current photo
        thumbs[i].onclick = function()
        {
            self.setCurrentPhoto(this.id);
	    self.checkPosition();
	    self.pPhotoCount.value = ((parseInt(this.getElementsByTagName("a")[0].id) % self.nMaxPhoto) == 0) ? 3 : (parseInt(this.getElementsByTagName("a")[0].id) % self.nMaxPhoto);
        }
    }
    
    //this.nWalk = document.getElementById(this.getCurrentPhoto().id).offsetWidth + 8;
    
    // navigation
    this.pNextPhoto.onclick = function(){ self.nextPhoto(); self.checkPosition();}
    this.pPrevPhoto.onclick = function(){ self.prevPhoto(); self.checkPosition();}
    this.pNextThumb.onclick = function(){ self.nextThumb(); self.checkPosition();}
    this.pPrevThumb.onclick = function(){ self.prevThumb(); self.checkPosition();}
    
    this.checkPosition();
}

CGalerie.prototype.addClassName = function(pObject, szClass)
{
    if(pObject && szClass)
    {
        pObject.className = pObject.className + " " + szClass;
    }
}

CGalerie.prototype.replaceClassName = function(pObject, szSearch, szReplace)
{
    if(pObject && szSearch && szReplace)
    {
        pObject.className = pObject.className.replace(szSearch, szReplace);
        pObject.className.replace(/^\s/, "");
        pObject.className.replace(/$\s/, "");
    }
}

CGalerie.prototype.removeClassName = function(pObject, szClass)
{
    if(pObject && szClass)
    {
        this.replaceClassName(pObject, szClass, "");
    }
}

CGalerie.prototype.getRealPhotoName = function(nThumbId)
{
    if(nThumbId)
    {
        return this.pThumbs.getElementsByTagName("li")[nThumbId].getElementsByTagName("img")[0].src.replace(/(thumb\/thumb_)/, "");
    }
    
    return null;
}

/*CGalerie.prototype.getPhotoCount = function()
{
    if(this.pPhotoCount)
    {
	alert(this.pPhotoCount.value);
	return this.pPhotoCount.value;
    }

    return null;
}*/

CGalerie.prototype.setPhotoCount = function(nCount)
{
    if((this.pPhotoCount) && (nCount > 0))
    {
	this.pPhotoCount.value = nCount;
    }
}

CGalerie.prototype.getCurrentPhoto = function()
{
    if(this.pCurrentThumb.value != "")
    {
        return document.getElementById(this.pCurrentThumb.value);
    }
    
    return null;
}

CGalerie.prototype.setCurrentPhoto = function(nThumbId)
{
    if(nThumbId)
    {
        this.clear(this.pPhoto);
        this.createPhoto(this.getRealPhotoName(nThumbId));

        if(this.getCurrentPhoto())
        {
            this.setInactive(this.getCurrentPhoto().id);
        }
        
        this.setActive(nThumbId);
        this.pCurrentThumb.value = nThumbId;
    }
}

CGalerie.prototype.clear = function(pObject)
{
    var child = null;

    if(pObject && pObject.hasChildNodes())
    {
        child = pObject.firstChild;
        
        while(child != null)
        {
            pObject.removeChild(child);
            child = child.nextSibling;
        }
    }
}

CGalerie.prototype.createPhoto = function(szPath)
{
    var tagImg = document.createElement("img");
    tagImg.className = "foto";
    tagImg.alt = "Ein Bild";
    tagImg.src = szPath;
    tagImg.width = 450;
    tagImg.height = 338;
    this.pPhoto.appendChild(tagImg);
}

CGalerie.prototype.setActive = function(nThumbId)
{
    if(nThumbId && this.pThumbs.getElementsByTagName("li")[nThumbId])
    {
        this.replaceClassName(this.pThumbs.getElementsByTagName("li")[nThumbId].getElementsByTagName("img")[0], "inactive", "active");
    }
}

CGalerie.prototype.setInactive = function(nThumbId)
{
    if(nThumbId && this.pThumbs.getElementsByTagName("li")[nThumbId])
    {
        this.replaceClassName(this.pThumbs.getElementsByTagName("li")[nThumbId].getElementsByTagName("img")[0], "active", "inactive");
    }
}

CGalerie.prototype.nextPhoto = function()
{
    var current = this.getCurrentPhoto();

    if(current.nextSibling && current.nextSibling.nodeType == 1)
    {
        this.setCurrentPhoto(current.nextSibling.id);
    }

    else
    {
	current = current.nextSibling;
	this.setCurrentPhoto(current.nextSibling.id);
    }

    this.pPhotoCount.value = (parseInt(this.pPhotoCount.value) + 1);

    if(parseInt(this.pPhotoCount.value) > this.nMaxPhoto)
    {
	this.pThumbs.style.left = parseInt(this.pThumbs.style.left) - this.nWalk + "px";
	this.pPhotoCount.value = 1;
    }
}

CGalerie.prototype.prevPhoto = function()
{
    var current = this.getCurrentPhoto();
    
    if(current.previousSibling && current.previousSibling.nodeType == 1)
    {
        this.setCurrentPhoto(current.previousSibling.id);
    }

    
    else
    {
	if(current.previousSibling)
	{
	    current = current.previousSibling;
	}

	this.setCurrentPhoto(current.previousSibling.id);
    }

    this.pPhotoCount.value = (parseInt(this.pPhotoCount.value) - 1);

    if(parseInt(this.pPhotoCount.value) <= 0)
    {
	this.pThumbs.style.left = parseInt(this.pThumbs.style.left) + this.nWalk + "px";
	this.pPhotoCount.value = this.nMaxPhoto;
    }
}

CGalerie.prototype.getCurrentPosition = function(pObject)
{
    if(pObject)
    {
        return pObject.style.left;
    }

    return null;
}

CGalerie.prototype.nextThumb = function()
{
    this.nextPhoto();

    //this.pPhotoCount.value = (parseInt(this.pPhotoCount.value) + 1);

    /*if(this.nPosition > (this.pThumbs.parentNode.offsetWidth - this.pThumbs.offsetWidth))
    {
        this.nPosition -= this.nWalk;
        this.slide(this.pThumbs, this.nPosition);
    }*/
}

CGalerie.prototype.prevThumb = function()
{
    this.prevPhoto();

    //this.pPhotoCount.value = (parseInt(this.pPhotoCount.value) - 1);

    /*if(this.nPosition < 0 )
    {
        this.nPosition += this.nWalk;
        this.slide(this.pThumbs, this.nPosition);
    }*/
}

CGalerie.prototype.slide = function(pObject, nPosition)
{
    if(pObject)
    {
        pObject.style.left = nPosition;
    }
}

CGalerie.prototype.show = function(pObject)
{
    if(pObject)
    {
        pObject.style.visibility = "visible";
    }
}

CGalerie.prototype.hide = function(pObject)
{
    if(pObject)
    {
        pObject.style.visibility = "hidden";
    }
}

CGalerie.prototype.getPhotoCount = function()
{
    if(this.pThumbs)
    {
        return this.pThumbs.getElementsByTagName("img").length;
    }
    
    return null;
}

CGalerie.prototype.checkPosition = function()
{
    if(this.getPhotoCount() == 1)
    {
        this.hide(this.pPrevPhoto);
        this.hide(this.pPrevThumb);
        this.hide(this.pNextPhoto);
        this.hide(this.pNextThumb);
    }
    
    else if(this.getCurrentPhoto().id == "TID1")
    {
        this.hide(this.pPrevPhoto);
        this.hide(this.pPrevThumb);
        this.show(this.pNextPhoto);
        this.show(this.pNextThumb);
    }
    
    else if(this.getCurrentPhoto().id == ("TID" + this.getPhotoCount()))
    {
        this.hide(this.pNextPhoto);
        this.hide(this.pNextThumb);
        this.show(this.pPrevPhoto);
        this.show(this.pPrevThumb);
    }
    
    else
    {
        this.show(this.pPrevPhoto);
        this.show(this.pPrevThumb);
        this.show(this.pNextPhoto);
        this.show(this.pNextThumb);
    }
}








