var PG_SelectedThumbIndex = 0;
var PG_CountOfThumbIndex = 5;

function PG_GetThumbIndex(ID) {
    var index = 0;
    
    try {
        index = parseInt(ID.split("_")[1], 10);
    } catch (error) {
        // ignore it
    }

    return index;
}

function PG_SelectImage(ID,richtung) {
	try
	{
	var newIndex = PG_GetThumbIndex(ID);
    var oldThumb = document.getElementById("th_" + ZeroPadInteger(PG_SelectedThumbIndex));
	var newThumb = document.getElementById("th_" + ZeroPadInteger(newIndex)); 
	
    oldThumb.src = "images/spacer.gif";
	
    newThumb.src = PG_RedBorderImage;

    document.getElementById("pg_largeimage").src = newThumb.name;
    document.getElementById("pg_largeimage").title = newThumb.title;
    
    var caption = document.getElementById("pg_largeimage_caption")
    if (caption)
    	caption.innerHTML = newThumb.title;
    
    PG_SelectedThumbIndex = newIndex;
	}
	catch(error)
	{
		if(richtung=="next")
		{
			ID="th_" + String(parseInt(ID.substr(3,1))+1);
			var newIndex = PG_GetThumbIndex(ID);
    		var oldThumb = document.getElementById("th_" + ZeroPadInteger(PG_SelectedThumbIndex));
			var newThumb = document.getElementById("th_" + ZeroPadInteger(newIndex)); 
	
    		oldThumb.src = "images/spacer.gif";
	
    		newThumb.src = PG_RedBorderImage;

    		document.getElementById("pg_largeimage").src = newThumb.name;
    		document.getElementById("pg_largeimage").title = newThumb.title;
    
    		var caption = document.getElementById("pg_largeimage_caption")
    		if (caption)
    			caption.innerHTML = newThumb.title;
    
    		PG_SelectedThumbIndex = newIndex;
		}
		if(richtung=="prev")
		{
			ID="th_" + String(parseInt(ID.substr(3,1))-1);
			var newIndex = PG_GetThumbIndex(ID);
    		var oldThumb = document.getElementById("th_" + ZeroPadInteger(PG_SelectedThumbIndex));
			var newThumb = document.getElementById("th_" + ZeroPadInteger(newIndex)); 
	
    		oldThumb.src = "images/spacer.gif";
	
    		newThumb.src = PG_RedBorderImage;

    		document.getElementById("pg_largeimage").src = newThumb.name;
    		document.getElementById("pg_largeimage").title = newThumb.title;
    
    		var caption = document.getElementById("pg_largeimage_caption")
    		if (caption)
    			caption.innerHTML = newThumb.title;
    
    		PG_SelectedThumbIndex = newIndex;
		}		
	}	

    return false;
}

function PG_NextIndex(index) {
    if (index >= (PG_CountOfThumbIndex-1))
        index = 0;
    else
        index++;

    return index;
}

function PG_PreviousIndex(index) {
    if (index <= 0)
        index = PG_CountOfThumbIndex;
    else
        index--;

    return index;
}

function PG_NextImage() {
    return PG_SelectImage("th_" + PG_NextIndex(PG_SelectedThumbIndex),"next");
}

function PG_PreviousImage() {
    return PG_SelectImage("th_" + PG_PreviousIndex(PG_SelectedThumbIndex),"prev");
}


