function popUpImg() {
	var popup = document.createElement('div');
	popup.setAttribute('id','popup');
	var zoom = document.createElement('img');
	zoom.setAttribute('id','ImgZoom');
	zoom.style.display = "none";
	var parag = document.createElement('p');
	parag.setAttribute('id','closeImg');
	parag.style.display = "none";
	var close = document.createTextNode('Zamknij');
	parag.appendChild(close);
	popup.appendChild(parag);
	popup.appendChild(zoom);
	popup.style.visibility = 'hidden';
	popup.onclick = function() {
		return Hide(this);
	}
	var content = document.getElementsByTagName("body").item(0);
	content.appendChild(popup);
    
    
    parag.onmouseover = function() {
        this.style.cursor = "pointer";
        this.style.textDecoration = "underline";
    }
    parag.onmouseout = function() {
        this.style.textDecoration = "none";
    }
    
	var lnks = content.getElementsByTagName('a');
	for (i=0;i<lnks.length;i++) {
		var lnkclass = lnks[i].className;
		if (lnkclass == "zoom") {
			lnks[i].onclick = function() {
				return Show(this);
			}
		}
	}
}

function Show(img) {
	if (document.getElementById && document.createElement) {
		var url = img.getAttribute('href');
		var zoom = document.getElementById('ImgZoom');
		zoom.setAttribute('src', url);
		zoom.style.display = "block";
		var popUp = document.getElementById('popup');
		popUp.style.visibility = 'visible';
		popUp.style.position   = 'absolute';	
		popUp.style.padding    = '5px';
		/* parse image href for dimensions
		based on http://www.themaninblue.com/writing/perspective/2004/08/05/ */		
		var paramString = url.replace(/.*\?(.*)/, "$1");
 		var paramTokens = paramString.split("&");
 		var paramList = new Array();

 		for (i = 0; i < paramTokens.length; i++) {
			var paramName = paramTokens[i].replace(/(.*)=.*/, "$1");
			var paramValue = paramTokens[i].replace(/.*=(.*)/, "$1");
			paramList[paramName] = paramValue;
		}
 		imgWidth  = paramList["x"];
 		imgHeight = paramList["y"];
		
		if (imgWidth)  {zoom.style.width  = imgWidth  + 'px';}
 		if (imgHeight) {zoom.style.height = imgHeight + 'px';}
		popUpPosition(popUp);
		window.onscroll = function() {
			popUpPosition(popUp);
		}
		window.onresize = function() {
			popUpPosition(popUp);
		}
		var close = document.getElementById('closeImg');
		close.style.display = "block";
		return false;
	}
	else
		return true;
}

function Hide(popup) {
	if (document.getElementById('ImgZoom'))
		document.getElementById('ImgZoom').src = 'images/blank.gif';
	popup.style.visibility = 'hidden';
	popup.style.padding    = '0';
}

function getScreenHeight() {
	var y;
	if (self.innerHeight) {
		// wszystko poza IE
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		// IE 6 Strict
		y = document.documentElement.clientHeight;
	}
	else if (document.body) {
		// inne IE
		y = document.body.clientHeight;
	}
	return y;
}

function getScreenWidth() {
	var x;
	if (self.innerWidth) {
		// wszystko poza IE
		x = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		// IE 6 Strict
		x = document.documentElement.clientWidth;
	}
	else if (document.body) {
		// inne IE
		x = document.body.clientWidth;
	}
	return x;
}

function getScroll() {
	var y;
	if (self.pageYOffset) {
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) {
		y = document.documentElement.scrollTop;
	}
	else if (document.body) {
		y = document.body.scrollTop;
	}
	return y;
}

function popUpPosition(popUp) {
	var popUpHeight  = popUp.offsetHeight;
	var screenHeight = getScreenHeight();
	var scrolled     = getScroll();
	var top = ((screenHeight - popUpHeight)/2) + scrolled;
	popUp.style.top  = top+"px";
	var ScreenWidth  = getScreenWidth();
	var popUpWidth   = popUp.offsetWidth;
	var left = (ScreenWidth-popUpWidth) / 2;
	popUp.style.left = left + "px";
}

function kontaktDivs() {
    var divs = document.getElementsByTagName("div");
    for (var i = 0; i < divs.length; i++) {
        if (divs.item(i).className == "kontakt-details") {
            divs.item(i).style.width  = "0";
            divs.item(i).style.height = "0";
        }
    }
    divs = null;
    var as = document.getElementsByTagName("a");
    for (var i = 0; i < as.length; i++) {
        if (as.item(i).className.indexOf("kontakt-more") != -1) {
            as.item(i).onmouseover = function(e) {
                if (!e) var e = window.event;
				x = mouseX(e);
				y = mouseY(e);
                var div = document.getElementById("details-" + this.getAttribute("id"));
                div.style.display  = "block";
                div.style.position = "absolute";
                div.style.top      = y + 10 + "px";
                div.style.left     = x + 3  + "px";
                div.style.overflow = "hidden";
                window.zoomDivInterval = window.setInterval("zoomDiv('"+div.getAttribute("id")+"', 240, 110)", 10);
                div =  null;
            }
            as.item(i).onmouseout = function() {
                var div = document.getElementById("details-" + this.getAttribute("id"));
                div.style.display = "none";                
                div.style.width  = "0";
                div.style.height = "0";
                div = null;
                window.clearInterval(window.zoomDivInterval);
            }
        }
    }
    as = null;
}

function zoomDiv(divId, maxWidth, maxHeight) {
    var div = document.getElementById(divId);
    var width  = parseInt(div.style.width.replace("px", ""));
    var height = parseInt(div.style.height.replace("px", ""));
    if (width < maxWidth || height < maxHeight) {
        if (width < maxWidth)
            div.style.width  = (width + 10) + "px";
        if (height < maxHeight)
            div.style.height = (height + 10) + "px";
    } else {
        window.clearInterval(window.zoomDivInterval);
    }
    div = null;
}

function mouseX(evt) {
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX)
   		return evt.clientX + (document.documentElement.scrollLeft ?
   			document.documentElement.scrollLeft :
   			document.body.scrollLeft);
	else return null;
}

function mouseY(evt) {
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
   		return evt.clientY + (document.documentElement.scrollTop ?
   		document.documentElement.scrollTop :
   		document.body.scrollTop);
	else return null;
}

function groupsMenu() {
    var mainMenu = document.getElementById("groupsMenu");
    var as = mainMenu.getElementsByTagName("a");
    for (var i = 0; i < as.length; i++) {
        var urlId = as.item(i).getAttribute("href").substr(as.item(i).getAttribute("href").indexOf("#") + 1);
        var subUl = document.getElementById(urlId);
        if (typeof(subUl) != "undefined" && subUl != null) {
            if (as.item(i).className == "selectedGroup") {
                subUl.setAttribute("selected", "selected");
                subUl.style.display = "block";
            } else {
                subUl.style.display = "none";
            }
        }
        as.item(i).onclick = function() {
            var urlId = this.getAttribute("href").substr(this.getAttribute("href").indexOf("#") + 1);
            var subUl = document.getElementById(urlId);
            if (typeof(subUl) != "undefined" && subUl != null) {
                if (subUl.style.display == "none") {
                    subUl.style.display = "block";
                    var uls = document.getElementsByTagName("ul");
                    for (var j = 0; j < uls.length; j++) {
                        if (uls.item(j).getAttribute("selected") == "selected") {
                            uls.item(j).style.display = "none";
                            uls.item(j).removeAttribute("selected"); 
                        }
                    }
                    uls = null;
                    subUl.setAttribute("selected", "selected");
                } else if (subUl.getAttribute("selected") != "selected") {
                    subUl.style.display = "none";
                }
            }
            subUl = null;
        }
        subUl = null;
    }
    as = null;
    mainMenu = null;
}

function ShowReklama() {
	if (document.getElementById && document.createElement) {
		var popUp = document.getElementById('popup2');
		//var zoom = document.getElementById('ImgZoom');
		var close = document.getElementById('closeZoom');
		close.onclick = function() {
			popUp.style.visibility = 'hidden';
			popUp.style.display = 'none';
		}
		close.onmouseover = function() {
			close.style.cursor = "pointer";
			close.style.textDecoration = "underline";
		}
		close.onmouseout = function() {
			close.style.textDecoration = "none";
		}
		
		popUp.style.position = 'absolute';
		popUp.style.display  = 'none';
 		imgWidth  = '550';
 		imgHeight = '500';
		
		
		popUp.style.padding = "5px";
		var popUpHeight  = 560; //popUp.offsetHeight;
		var screenHeight = getScreenHeight();
		var scrolled     = getScroll();
		var top = ((screenHeight - popUpHeight)/2) + scrolled;
		if (top > 100)
			top = top - 100;
		//alert("wysokosc popupa:" + popUpHeight + " wysokosc ekranu" + screenHeight + " ile przewiniete:" + scrolled + " poz:" + top);
		popUp.style.top = top+"px";
		var ScreenWidth = getScreenWidth();
		var popUpWidth  = '408'; //popUp.offsetWidth;
		var left = (ScreenWidth-popUpWidth)/2;
		popUp.style.left    = left+"px";
		close.style.display = "block";
		close.style.margin  = "0";
		close.style.padding = "0";
		close.style.color   = "#fff";
		popUp.style.cursor  = "pointer";
		popUp.style.display = "block";
	}
}

window.onload = function() {
	if (document.getElementById && document.createElement) {
		if (document.getElementById("images")) {
			popUpImg();
        }
        if (document.getElementById("details")) {
            kontaktDivs();
        }
        if (document.getElementById("groupsMenu")) {
            groupsMenu();
        }
        if (document.getElementById("reklama")) {
            ShowReklama();
        }
	}
}

