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','closeZoom');
	parag.style.display = "none";
	var close = document.createTextNode('Zamknij');
	parag.appendChild(close);
	popup.style.padding = "0";
	popup.style.margin  = "0";
	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);

	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 = _('ImgZoom');
		zoom.setAttribute('src', url);
		zoom.style.display = "block";
		var popUp = _('popup');
		popUp.style.visibility = 'visible';
		popUp.style.position   = "absolute";
		/* 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';}

		popUp.style.width   = "auto";
		popUp.style.height  = "auto";
		popUp.style.padding = "5px";
		var popUpHeight  = _('popup').offsetHeight;
		var screenHeight = getScreenHeight();
		var scrolled     = getScroll();
		var top = ((screenHeight - popUpHeight)/2) + scrolled;
		//alert("wysokosc popupa:" + popUpHeight + " wysokosc ekranu" + screenHeight + " ile przewiniete:" + scrolled + " poz:" + top);
		popUp.style.top = top+"px";
		var ScreenWidth = getScreenWidth();
		var popUpWidth  = _('popup').offsetWidth;
		var left = (ScreenWidth-popUpWidth)/2;
		popUp.style.left    = left+"px";
		var close = _('closeZoom');
		close.style.display = "block";
		close.style.margin  = "0";
		close.style.padding = "0";
		close.style.color   = "#fff";
		popUp.style.cursor  = "pointer";
		return false;
	}
	else
		return true;
}

function Hide(popup) {
	_('ImgZoom').src = 'images/blank.gif';
	popup.style.visibility = 'hidden';
}

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 ShowElement(x)
{
  var field = _(x);
  field.style.display = 'block';
}

function HideElementAgain(x)
{
  var field = _(x);
  field.style.display = 'none';
}

function ShowHideElement(x) {
	var field = _(x);
	if (field.style.display == 'none')
		ShowElement(x);
    else
		HideElementAgain(x);
};

var menu = function() {
	var toggleMenu = function(e) {
		typeof e.preventDefault === 'function' ? e.preventDefault() : e.returnValue = false;
		var el = typeof this.nextSibling !== 'undefined' ? this.nextSibling.nextSibling : e.srcElement.nextSibling.nextSibling;
		el.id === 'me_shop' ? (el.style.display === 'block' ? el.style.display = 'none' : el.style.display = 'block') : null;
	};
	var getText = function(element) {
		return (element.textContent ? element.textContent : element.innerText ? element.innerText : false).replace(/\n/g, '').replace(/^\s+|\s+$/g, '');
	};
	var menus = _('me_shop').parentNode.getElementsByTagName('a');
	for (var i = 0; i < menus.length; i++) {
		if (menus[i].nextSibling && menus[i].nextSibling.nextSibling && menus[i].nextSibling.nextSibling.id === 'me_shop') {
			if (_('first') && getText(menus[i]) === getText(_('first'))) {
				menus[i].nextSibling.nextSibling.style.display = 'block';
			}
			typeof menus[i].addEventListener === 'function' ? menus[i].addEventListener('click', toggleMenu, true) : menus[i].attachEvent('onclick', toggleMenu);
		}
	}
};

var _ = function(id) {
	return document.getElementById(id);
};

window.onload = function() {
	if (document.getElementById && document.createElement) {
		menu();
		if (_("images")) {
			popUpImg();
		}
	}
}
