function $(id) { return document.getElementById(id); }
function wri(par1,par2) { if (par2) par1.innerHTML=par2; else document.write(par1); }
function AL_ChkDel(text,link) { if (confirm("Na pewno chcesz kasować:\n"+text+"?")) location.href=link; }

function AL_SendMail(sDom, sUser) { location.href="mail"+"to:"+sUser+"@"+sDom; }

function AL_AddTag(form,tag) {
	lista=form.value;
	pre=(lista)? ", " : "";
	form.value=lista+pre+tag;
}


function AL_TinyToggle(id) {
	if (!tinyMCE.get(id)) tinyMCE.execCommand('mceAddControl', false, id);
	else tinyMCE.execCommand('mceRemoveControl', false, id);
}


function AL_InputNum(form,min,max,nullfl) {
	licz=form.value;
	if (isNaN(licz)) licz="0";
	if (nullfl && licz=="") licz="0";
	if (licz!="") {
		if (licz<min) licz=min;
		if (licz>max && max!=0) licz=max;
	}
	form.value=licz;
}


function AL_WinPop(fname,sz,wy) {
	wid=(sz)? "width="+sz : "";
	hei=(wy)? "height="+wy : "";
	cfg='toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,'+wid+','+hei;
	win=window.open("winpop.php?"+fname,"win",cfg);
	win.focus(); 
}

function AL_GetFlash(name,src,sz,wy,vars) {
	if (!vars) vars="";
	return "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='"+sz+"' height='"+wy+"' id='"+name+"'><param name='movie' value='"+src+"'><param name='quality' value='high'><param name='scale' value='auto'><param name='wmode' value='transparent'><param name='FlashVars' value='"+vars+"'><embed src='"+src+"' quality='high' width='"+sz+"' height='"+wy+"' name='"+name+"' wmode='transparent' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' flashvars='"+vars+"' swLiveConnect='true'></embed></object>";
}


function AL_Tabs(name,layer,title) {
	wri("<div class='Tabs'><div><span class='Menu' id='TabMenu'></span></div></div>");
	var sName=name;
  	var sLayer=layer;
	var sTitle=title.split("|");
	var cCnt=sTitle.length;
	var pLayTab=$("TabMenu");
	var cLast=0;

	for (i=0;i<cCnt;i++) wri("<div class='Text' id='"+sLayer+i+"'></div>");

	this.Change = function(activ) {
		var i, txt, cltxt;
		if (activ==-1) activ=cCnt-1; // OSTATNI
		txt="";
		for (i=0;i<cCnt;i++) {
			if ($(sLayer+i).style.display=="block") cLast=i;
			$(Lay+i).style.display="none";
			cltxt= (i==activ)? "class='Activ'" : "";
			if (cTitle[i]) txt+="<a "+cltxt+" href='JavaScript:"+sName+".Change("+i+");'>"+sTitle[i]+"</a>";
		}
		pLayTab.innerHTML=txt;
		$(sLayer+activ).style.display="block";
	}

	this.Back = function() { this.Change(cLast); }
}


function AL_ZoomLayer(name,dtyp) {
	var sName=name;
	wri("<div id='AL_Bkg' onClick='"+sName+".Close();'></div><div id='AL_Lay'><table height='100%' align='center' border='0'><tr><td align=center valign=middle><table><tr><td><div id='AL_Txt'></div></td></tr></table></td></tr></table></div>");
	var pLay=$("AL_Lay");
	var pBkg=$("AL_Bkg");
	var pTxt=$("AL_Txt");
	var bGalFl=0;
	var pImgNew=new Image();
	var pReq;
	var sLoader="<a href='JavaScript:"+sName+".Close();'><img src='pix/al_loader.gif' border=0></a>";
	var sClose="<div id='AL_CloT'><a href='JavaScript:"+sName+".Close();'>ZAMKNIJ</a></div>";
	var sCloseX="<div id='AL_CloX' onClick='"+sName+".Close();'></div>";
	var aTab;
	var iGalNr;
	var sPath;
	var pdTyp=dtyp;
	var isIE=document.all? 1 : 0;

	document.onmousedown = function(e) {
		e = (e || event);
		var target = e.target || e.srcElement;
		if (target.id!="AL_Tyt") return;

		var ox = pTxt.offsetLeft-e.clientX;
		var oy = pTxt.offsetTop-e.clientY;
		ox+= isIE? ((pTxt.scrollWidth-pLay.scrollWidth)/2) : ((pTxt.clientWidth-pLay.clientWidth)/2);
		oy+= isIE? ((pTxt.scrollHeight-pLay.scrollHeight)/2) : ((pTxt.clientHeight-pLay.clientHeight)/2);

		document.onmousemove = function(e) {
			e = (e || event);
   			pTxt.style.left = (ox+e.clientX) + "px";
			pTxt.style.top  = (oy+e.clientY) + "px";
			return false;
		}
	}

	document.onmouseup = function(e) { document.onmousemove = null; }

	this.getXMLHttpRequest = function() {
		var request = false;

		if(window.XMLHttpRequest) {
			request=new XMLHttpRequest();
			if (request.overrideMimeType) request.overrideMimeType('text/html');
		} else if(window.ActiveXObject) {
			try { request=new ActiveXObject("Msxml2.XMLHTTP"); }
			catch(e) { request=new ActiveXObject("Microsoft.XMLHTTP"); }
		}
		return request;
	}

	this.GetParam = function(form) {
		input=form.elements;
		param="";
		for(i=0;i<input.length;i++) {
			switch(input[i].type) {
				case "radio":
				case "checkbox": if(input[i].checked) param+=input[i].name+"="+input[i].value; break;
				case "select-one":
					sel=input[i].options[input[i].selectedIndex];
					war=(sel.value.length>0)? sel.value : sel.innerHTML;
					param+=input[i].name+"="+war;
					break;
				case "select-multiple":
					for(j=0;j<input[i].options.length;j++) if(input[i].options[j].selected) param+=input[i].name+"="+input[i].options[j].value;
					break;
				default: param+=input[i].name+"="+input[i].value;
			}
			if (i<input.length-1) param+="&";
		}
		return param;
	}

	this.GetData = function(url,div) {
		var lnk="ajaxget.php?"+url;
		pReq.open('GET',lnk,true);
		pReq.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
		pReq.onreadystatechange = function() { if ((pReq.readyState == 4) && (pReq.status == 200)) wri(div,pReq.responseText); }
		pReq.send(null);
	}

	this.PostData = function(url,div,form) {
		var lnk="ajaxget.php?"+url;
		param=this.GetParam(form);
		pReq.open('POST',lnk,true);
		pReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		pReq.onreadystatechange = function() {
			if ((pReq.readyState == 4) && (pReq.status == 200)) {
				div.innerHTML=pReq.responseText;
				LO=$("ListaOver");
				LO.style.height=document.body.clientHeight-60;
			}
		}
		pReq.send(param);
	}

	this.ChngBkg = function(id,grf,pos) { $(id).style.background="url('"+grf+"') no-repeat "+pos; }

	this.Visible = function(vis) {
		var viewsel=(vis==1)? "hidden" : "visible";
		var viewzom=(vis==1)? "block" : "none";
		var x=document.getElementsByTagName("select");
		for(i=0;i<x.length;i++) x[i].style.visibility=viewsel;
		pLay.style.display=viewzom;
		pBkg.style.display=viewzom;
	}

	this.Close = function() {
		this.Visible(0);
		pLay.style.height="0px";
		pBkg.style.height="0px";
		pTxt.style.height="0px";
 		wri(pTxt,"KONIEC");
	}

	this.ImgLoaded = function() {
		var sImg="<a href='#' onClick='"+sName+".Close(); return false;'><img src='"+pImgNew.src+"' border=0></a>";
		var sClo="<div id='AL_Clo' onClick='"+sName+".Close();' onMouseOver='"+sName+".ChngBkg(\"AL_Clo\",\"pix/al_clo22.gif\",\"\");' onMouseOut='"+sName+".ChngBkg(\"AL_Clo\",\"pix/al_clo21.gif\",\"\");'></div>";
		var sPre=""; sFor=""; sPodp="";
		if (bGalFl) {
			if (iGalNr>0) sPre="<div id='AL_GalPrev' onClick='"+sName+".Next(-1);' onMouseOver='"+sName+".ChngBkg(\"AL_GalPrev\",\"pix/al_gall.gif\",\"left center\");' onMouseOut='"+sName+".ChngBkg(\"AL_GalPrev\",\"pix/al_gal.gif\",\"\");'></div>";
			if (iGalNr<aTab.length-1) sFor="<div id='AL_GalForv' onClick='"+sName+".Next(1);' onMouseOver='"+sName+".ChngBkg(\"AL_GalForv\",\"pix/al_galr.gif\",\"right center\");' onMouseOut='"+sName+".ChngBkg(\"AL_GalForv\",\"pix/al_gal.gif\",\"\");'></div>";
			var sPodp="<div id='AL_GalPodp'>("+(iGalNr+1)+"/"+aTab.length+") <b>"+aTab[iGalNr][1]+"</b>";
			if (aTab[iGalNr][2]) sPodp+=" - "+aTab[iGalNr][2];
			sPodp+="</div>";
		}

		wri(pTxt,"<div style='position:relative;'>"+sImg+sPre+sFor+sClo+sPodp+"</div>");

		var szer=(pImgNew.width/3)+"px";
		var wyso=pImgNew.height+"px";

		if (sPre) {
			var z=$("AL_GalPrev");
			z.style.width=szer;
			z.style.height=wyso;
		}

		if (sFor) {
			var z=$("AL_GalForv");
			z.style.width=szer;
			z.style.height=wyso;
		}
		if (sPodp) $("AL_GalPodp").style.width=pImgNew.width+"px";
		this.Position();
	}

	this.Position = function() {
		var ZoomPw=isIE? document.body.clientWidth+"px" : "100%";
		var ZoomPh=document.body.scrollHeight+"px";
		pBkg.style.width=ZoomPw;
		pBkg.style.height=ZoomPh;
	}

	this.Resize = function() {
		var ZoomPw=isIE? document.body.clientWidth+"px" : "100%";
		var ZoomPh=document.body.scrollHeight+"px";
		pBkg.style.width=ZoomPw;
		pBkg.style.height=ZoomPh;

		var ZoomPx=isIE? document.body.scrollLeft : window.pageXOffset;
		var ZoomPy=isIE? document.body.scrollTop : window.pageYOffset;
		var ZoomPw=isIE? document.body.clientWidth+"px" : "100%";
		var ZoomPh=isIE? document.body.clientHeight+"px" : "100%";

		pLay.style.left="0px";
		pLay.style.top=ZoomPy+"px";
		pLay.style.width=ZoomPw;
		pLay.style.height=ZoomPh;
	}

	this.Init = function() {
		pTxt.style.height="auto";
		this.Visible(1);
		pBkg.style.left="0px";
		pBkg.style.top="0px";
		this.Resize();
		window.onscroll = this.Position;
		window.onresize = this.Resize;
	}

	this.Alert = function(title,txt) {
		this.Init();
		wri(pTxt,"<div class='zzz' id='AL_Tyt'>"+title+sCloseX+"</div><div id='AL_Tre'>"+txt+sClose+"</div>");
	}

	this.Video = function(title,name,src,sz,wy,param) {
		this.Init();
		wri(pTxt,"<div id='AL_Tyt'>"+title+sCloseX+"</div><div id='AL_Tre'><div id='klipv'>"+AL_GetFlash(name,src,sz,wy,param)+"</div>"+sClose+"</div>");
	}

	this.Ajax = function(fname) {
		this.Init();
		wri(pTxt,sLoader);
		this.GetData(fname,pTxt);
	}

	this.AjaxPost = function(fname,form) {
		this.Init();
		wri(pTxt,sLoader);
		this.PostData(fname,pTxt,form);
	}

	this.Image = function(fname) {
		bGalFl=0;
		this.Init();
		wri(pTxt,sLoader);
		pImgNew.src=fname;
		pImgNew.onload=this.ImgLoaded;
	}

	this.Next = function(dx) {
		iGalNr+=dx;
		wri(pTxt,sLoader);
		pImgNew.src=sPath+"/"+aTab[iGalNr][0];
		pImgNew.onload=this.ImgLoaded;
	}

	this.Gallery = function(path,tab,nr) {
		bGalFl=1;
		this.Init();
		aTab=tab;
		iGalNr=nr;
		sPath=path;
		this.Next(0);
	}

	pReq=this.getXMLHttpRequest();
}
