function initPager() {
	window.myPager = new Pager();
	window.myPager.init(12);
}

function Pager(){
	var that = this;
	var bilder;
	var anzahlProSeite;
	var anzahlGesamt;
	var pos;
	var isIe = true;
	var isMac = false;
	var changed = false;
	var currentSelection;
	var lastSelection;
	
	this.init = function (anzahlProSeite) {
		that.anzahlProSeite = anzahlProSeite;
		that.bilder = document.getElementById("galeriebilder").getElementsByTagName('div');
		that.anzahlGesamt = that.bilder.length-1;
		that.pos = 0;
		navigator.userAgent.indexOf("Mozilla/4.0") == -1 ? that.isIe = Browser && Browser.ie : false;
		//navigator.userAgent.indexOf("Mozilla/4.0") == -1 ? that.isIe = false : that.isIe = true;
		navigator.userAgent.indexOf("Macintosh; U; PPC Mac OS X Mach-O") == -1 ? that.isMac = false : that.isMac = true;
		
		pagerLinks = "";
		pagerLinks += "<a href='#' id='pagerBack' onclick='myPager.switchPage(-1); return false;'>&laquo;</a> | ";
		for(i=0; i < Math.ceil(that.anzahlGesamt/that.anzahlProSeite);i++){
			pagerLinks += "<a href='#' id='pagerBtn"+(i+1)+"' onclick='myPager.switchPage(\""+(i+1)+"\", this); return false;'>"+(i+1)+"</a> | ";
		}
		pagerLinks += "<a href='#' id='pagerForward' onclick='myPager.switchPage(1); return false;'>&raquo;</a>";
		if(i == 1)
			document.getElementById("blaetterfunktion").style.display ="none";
		
		document.getElementById("blaetterfunktion").innerHTML = pagerLinks;
		that.currentSelection = that.lastSelection = document.getElementById("pagerBtn1");
		
		that.highlightElement(that.currentSelection);
		that.showGallery(0);
	}
	
	this.highlightElement = function() {
		that.lastSelection.style.fontWeight = "normal";
		that.lastSelection.style.textDecoration = "underline";
		that.currentSelection.style.fontWeight = "bold";
		that.currentSelection.style.textDecoration = "none";
	}
	
	this.hideGallery = function() {
		for(i=0; i<that.bilder.length; i++) {
			if(that.bilder[i].className.indexOf("galleriebild") != -1) {
					that.bilder[i].style.display = "none";				
			}
		}
	}
	
	this.showGallery = function(startPos) {
		that.hideGallery();
		imagesToShow = startPos + that.anzahlProSeite;
		if (imagesToShow > that.anzahlGesamt)
			imagesToShow = that.anzahlGesamt;
		
		for(i=startPos; i<imagesToShow; i++) {
			if(that.bilder[i].className.indexOf("galleriebild") != -1) {
					if (that.isIe){
						that.bilder[i].style.display = "block";
						that.bilder[i].style.cssFloat="left";
					/**	that.bilder[i].style.width="150px";
						that.bilder[i].style.border="red solid 1px";
						that.bilder[i].style.clear="none";**/
					}else if(that.isMac) {
						that.bilder[i].style.display = "block";
						that.bilder[i].style.cssFloat = "left";
						}
					else 
						that.bilder[i].style.display = "inline-table";
			}
		}
	}
	
	this.switchPage = function(range, elm) {
	
		if (typeof range == "number" && range == 1 && that.pos >= Math.ceil((that.anzahlGesamt/that.anzahlProSeite)-1)*that.anzahlProSeite) {
			return;
		}
		
		if (range == -1 && that.pos == 0)
			return;
	
		if (!elm) {
			PagerElemArray = that.currentSelection.id.split("pagerBtn");
			PagerCurrent = parseInt(PagerElemArray[1])+range;
			that.currentSelection = document.getElementById("pagerBtn"+PagerCurrent);
			this.highlightElement();
			that.lastSelection = document.getElementById("pagerBtn"+PagerCurrent);	
		}else{
			that.currentSelection = elm;
			this.highlightElement();
			that.lastSelection = elm;
		}

		if (typeof range == "number") {
				if (range == -1) {
					if(that.pos > 0){
						that.pos -= that.anzahlProSeite;
						that.changed = true;
					}
				}
				else if (range == 1) {
					if(that.pos < Math.ceil((that.anzahlGesamt/that.anzahlProSeite)-1)*that.anzahlProSeite){
						that.pos += that.anzahlProSeite;
						that.changed = true;
					}
				}
				if(that.changed){
					that.showGallery(that.pos);
					that.changed = false;
				}	
		}
		else {
				that.showGallery((range-1)*that.anzahlProSeite);
				that.pos = (range-1)*that.anzahlProSeite;
		}
	}
}

var isVisible = false;
function onoff(id, element)
{
	if(isVisible)
	{
		document.getElementById(id).style.display="none";
		isVisible = !isVisible;
		document.getElementById(element).src = "templates/leander_mergener/images/btn_mehr_16x16.png";
	}
	else
	{
		document.getElementById(id).style.display="block";
		isVisible = !isVisible;
		document.getElementById(element).src = "templates/leander_mergener/images/btn_weniger_16x16.png";
	}
}
