function $(id) { return document.getElementById(id); }
function wri(par1,par2) { if (par2) par1.innerHTML=par2; else document.write(par1); }

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_ChkDel(text,link) { if (confirm("Na pewno chcesz kasować:\n"+text+"?")) location.href=link; }

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) { return "<object name='"+name+"' width="+sz+" height="+wy+"><param name='movie' value='"+src+"' /><param name='wmode' value='transparent' /><param name='flashvars' value='"+vars+"'><embed name='"+name+"' src='"+src+"' wmode='transparent' width="+sz+" height="+wy+" flashvars='"+vars+"' type='application/x-shockwave-flash'></object>"; }


function AL_ZoomLayer(name,dtyp) {
  wri("<div id='AL_Lay'><div id='AL_Bkg'></div><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 sName=name;
  var pLay=$("AL_Lay");
  var pLayBkg=$("AL_Bkg");
  var pLayTxt=$("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 class='AClo'><a href='JavaScript:"+sName+".Close();'>ZAMKNIJ</a></div>";
  var aTab;
  var iGalNr;
  var sPath;
  var pdTyp=dtyp;

  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) {
    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) {
    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) {
    viewsel=(vis==1)? "hidden" : "visible";
    viewzom=(vis==1)? "block" : "none";
    x=document.getElementsByTagName("select");
    for(i=0;i<x.length;i++) x[i].style.visibility=viewsel;
    pLay.style.display=viewzom;
  }

  this.Close = function() {
    this.Visible(0);
    pLay.style.height="0px";
    pLayBkg.style.height="0px";
    pLayTxt.style.height="0px";
  }

  this.ImgLoaded = function() {
    sImg="<a href='#' onClick='"+sName+".Close(); return false;'><img src='"+pImgNew.src+"' border=0></a>";
    sClo="<div id='AL_Clo' onClick='"+sName+".Close();' onMouseOver='"+sName+".ChngBkg(\"AL_Clo\",\"pix/al_clo2.gif\",\"\");' onMouseOut='"+sName+".ChngBkg(\"AL_Clo\",\"pix/al_clo1.gif\",\"\");'></div>";
    sPre=""; sFor=""; sPodp="";
    if (bGalFl) {
      if (iGalNr>0) sPre="<div id='GalPrev' onClick='"+sName+".Next(-1);' onMouseOver='"+sName+".ChngBkg(\"GalPrev\",\"pix/al_gall.gif\",\"left center\");' onMouseOut='"+sName+".ChngBkg(\"GalPrev\",\"pix/al_gal.gif\",\"\");'></div>";
      if (iGalNr<aTab.length-1) sFor="<div id='GalForv' onClick='"+sName+".Next(1);' onMouseOver='"+sName+".ChngBkg(\"GalForv\",\"pix/al_galr.gif\",\"right center\");' onMouseOut='"+sName+".ChngBkg(\"GalForv\",\"pix/al_gal.gif\",\"\");'></div>";
      sPodp="<div id='GalPodp'>("+(iGalNr+1)+"/"+aTab.length+") <b>"+aTab[iGalNr][1]+"</b>";
	if (aTab[iGalNr][2]) sPodp+=" - "+aTab[iGalNr][2];
	sPodp+="</div>";
    }

    wri(pLayTxt,"<div style='position:relative;'>"+sImg+sPre+sFor+sClo+sPodp+"</div>");

    szer=(pImgNew.width/3)+"px";
    wyso=pImgNew.height+"px";

    if (sPre) {
      z=$("GalPrev");
      z.style.width=szer;
      z.style.height=wyso;
    }

    if (sFor) {
      z=$("GalForv");
      z.style.width=szer;
      z.style.height=wyso;
    }
    if (sPodp) $("GalPodp").style.width=pImgNew.width+"px";

	pLayBkg.style.height=(pdTyp=="strict")? document.documentElement.scrollHeight+"px" : document.body.scrollHeight+"px";
  }

  this.Position = function() {
	pLayBkg.style.height=(pdTyp=="strict")? document.documentElement.scrollHeight+"px" : document.body.scrollHeight+"px";
}

  this.Resize = function() {
    ZoomPx=(document.all&&!window.opera)? document.body.scrollLeft : window.pageXOffset;
    ZoomPy=(document.all&&!window.opera)? document.body.scrollTop : window.pageYOffset;
    ZoomPw=(document.all&&!window.opera)? document.body.clientWidth : "100%";
    ZoomPh=(document.all&&!window.opera)? document.body.clientHeight : "100%";

    pLay.style.left="0px";
    pLay.style.top=ZoomPy+"px";
    pLay.style.width=ZoomPw;
    pLay.style.height=ZoomPh;
    pLayBkg.style.left="0px";
    pLayBkg.style.top=-ZoomPy+"px";
    pLayBkg.style.width=ZoomPw;
	pLayBkg.style.height=(pdTyp=="strict")? document.documentElement.scrollHeight+"px" : document.body.scrollHeight+"px";
  }

  this.Init = function() {
    pLayTxt.style.height="auto";
    this.Resize();
    this.Visible(1);
    window.onscroll = this.Position;
    window.onresize = this.Resize;
  }

  this.Alert = function(title,txt) {
    this.Init();
    wri(pLayTxt,"<div class='ATyt'>"+title+"</div><div class='ATre'>"+txt+sClose+"</div>");
  }

  this.Ajax = function(fname) {
    this.Init();
    wri(pLayTxt,sLoader);
    this.GetData(fname,pLayTxt);
  }

  this.AjaxPost = function(fname,form) {
    this.Init();
    wri(pLayTxt,sLoader);
    this.PostData(fname,pLayTxt,form);
  }

  this.Image = function(fname) {
    bGalFl=0;
    this.Init();
    wri(pLayTxt,sLoader);
    pImgNew.src=fname;
    pImgNew.onload=this.ImgLoaded;
  }

  this.Next = function(dx) {
    iGalNr+=dx;
    wri(pLayTxt,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();
}
