/////// Copyright Kompan (eo Networks ASP sp z o.o.)
/////// 09/02/2007
/////// Lukasz Jarochowski
///////

	intervalid = 0;

	function addEvent(obj, event, func, params){
		func = new Function(func+"("+params+");");
		if(obj.addEventListener) { obj.addEventListener(event, func, true); }
		else if(obj.attachEvent) { obj.attachEvent('on'+event, func); }
	}

	function showTopLayer(src, type) {
		var prefix = 'kompan_';
		var content = new String();
		switch(type) {
			case 'img':
				// XXX
				content = showTopLayerImage(src);
				break;
		}
		
		target = null;
		try {
			document.getElementById(prefix + 'toplayer').style;
		} catch (e) {
			background = document.createElement("div");
			background.id = prefix + "bg";
			background.style.height = Math.max(getWindowSize()[1], document.body.offsetHeight) + "px";

			div = document.createElement("div");
			div.className = prefix + "hidden";
			div.id = prefix + "toplayer";

			hideDiv = document.createElement("div");
			hideDiv.className = prefix + "hideButton";
			//addEvent(hideDiv, 'click', "this.parentNode.className='"+prefix+"hidden';//", '');
			hideDiv.onclick = new Function("this.parentNode.className='"+prefix+"hidden';document.getElementById('"+prefix+"bg').style.display='none';");
			
			cDiv = document.createElement("div");
			cDiv.className = prefix + "content";

			cDiv.onclick = hideDiv.onclick; //off button

			div.appendChild(hideDiv);
			div.appendChild(cDiv);
			document.body.appendChild(div); //toplayer
			document.body.appendChild(background); //background
		}
		
		target = document.getElementById(prefix + 'toplayer');

		offTop = document.documentElement.scrollTop;
		height = getWindowSize()[1];

		document.getElementById(prefix + 'bg').style.display = 'block';
		document.getElementById(prefix + 'bg').style.backgroundPosition = '50% ' + ((height - 60)/2 + offTop) + 'px';
		// element created so insert content:
		if (target.lastChild.innerHTML != content) {
			target.lastChild.innerHTML = content;
		}

		// finally show target
		target.className = prefix + "loading";
		
		cp = new CheckProgress(target);
		intervalid = setInterval(cp.runCheck, 100);
/*		
		target.style.top = ((ypos = (document.body.clientHeight - target.offsetHeight)/2 + offTop)>0?ypos:0) + "px";
		target.style.left = ((xpos = (document.body.clientWidth - target.offsetWidth)/2)>0?xpos:0) + "px";
		target.className = "";
*/
		//alert(content);
	}

	function showTopLayerImage(src) {
		// auto gallery
		maxH = 600; maxW = 800;
		src = src.replace(/width=\d+/, 'width='+ maxW);
		src = src.replace(/height=\d+/, 'height='+ maxH);
		// mod_rewrite
		src = src.replace(/-mini/, '-maxi');
		content = new String();
		content = '<img src="'+ src + '"/>';

		return content;
	}

//	function topLayerInit(gallery, type) {
//		elements = document.getElementsByTagName(type);
//
//		for (var ii in elements) {
//			if ((gallery.length > 0 && elements[ii].className == gallery) || (gallery.length == 0)) {
//				addEvent(elements[ii], 'click', 'showTopLayer',
//					'"'+ elements[ii].src + '","' + type.toLowerCase() + '"');
//			}
//		}
//
//	}

/// get window size:
function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return new Array(myWidth, myHeight);
}


//// checking progress of image loading

function CheckProgress(target) {
	//construct
	this.target = target;
}

CheckProgress.prototype.runCheck = function() {
	target = this.target;
	offTop = document.documentElement.scrollTop;
	if (target.offsetHeight >= 50) {
		clearInterval(intervalid);
	} else {
		return false;
	}

	tmp = getWindowSize();
	width = tmp[0]; height = tmp[1];


	target.style.width = target.lastChild.firstChild.offsetWidth + "px";


	target.style.top = ((ypos = (height - target.offsetHeight)/2 + offTop)>0?ypos:0) + "px";
	target.style.left = ((xpos = (width - target.offsetWidth)/2)>0?xpos:0) + "px";
	target.className = "";
}

/// testing on load resizeing windows

rowCount = 4;
	function topLayerInit(gallery, type) {
		elements = document.getElementsByTagName(type);

		for (var ii in elements) {
			if ((gallery.length > 0 && elements[ii].className == gallery) || (gallery.length == 0)) {
				addEvent(elements[ii], 'click', 'showTopLayer',
					'"'+ elements[ii].src + '","' + type.toLowerCase() + '"');
			}
		}

	}

resizeIntId = 0;
mWidth = 0;

	function resizeGallery() {
		if (!rowCount) {
			return;
		}
		mWidth = Math.floor(0.9 * document.body.clientWidth / rowCount);

		clearInterval(resizeIntId);
		resizeIntId = setInterval(checkResizeEnd, 1000);

		elements = document.getElementsByTagName(type);
		for (var ii in elements) {
			if ((gallery.length > 0 && elements[ii].className == gallery) || (gallery.length == 0)) {
				elements[ii].width = mWidth;
			}
		}

	}

	checkResizeEnd = function() {
		clearInterval(resizeIntId);
		elements = document.getElementsByTagName(type);

		for (var ii in elements) {
			if ((gallery.length > 0 && elements[ii].className == gallery) || (gallery.length == 0)) {
				elements[ii].width = mWidth;
				src = elements[ii].src.replace(/width=\d+/, 'width='+ mWidth);
				src = src.replace(/height=\d+/, 'height='+ mWidth);
				elements[ii].src = src;
			}
		}
		
	}
		
//addEvent(window, 'load', 'resizeGallery', '');
//addEvent(window, 'resize', 'resizeGallery', '');

function changeVisibility(htmlElement, id) {
	var panel = document.getElementById(id);

	if (!panel || !htmlElement) return;

	if (htmlElement.id == 'order_time2') {
		panel.style.display = 'block';
	} else {
		panel.style.display = 'none';
		document.getElementById('order_del_day').value = '';
		document.getElementById('order_del_time1').value = '';
		document.getElementById('order_del_time2').value = '';
	}
}

