function okno(nazwa,adres,w,h) {
  NoweOkno = window.open(adres , nazwa, "toolbar=no,menubar=no,resizable=yes,scrollbars=yes,width="+w+",height="+h);
  NoweOkno.focus();
}


function ajax_fetch(datafile) {
 if (!document.all) {
  objXml = new XMLHttpRequest();
  objXml.open("GET","/pub/ajax.php?"+datafile,false);
  objXml.setRequestHeader("Content-Type", "text/plain; charset=UTF-8");
  objXml.send(null);
  output=objXml.responseText;
  } 
  else {
   objXml = new ActiveXObject("Microsoft.XMLHTTP");
   //objXml = new ActiveXObject("Msxml2.XMLHTTP"); older version
   objXml.open("GET","/pub/ajax.php?"+datafile, false);
   objXml.onreadystatechange=function() {
    if (objXml.readyState==4) {
     output=objXml.responseText;
    }
   }
   objXml.setRequestHeader("Content-Type", "text/plain; charset=UTF-8");
   objXml.send(null);
  }
}

var output="";
function showGal(idGal) {
  ajax_fetch("a=isGal&idGal="+idGal);
  var out = JSON.decode(output);
  if(out[0]=="OK") {
    location.href="/"+out[2]+","+out[1]+",4.html";
  }
}

function showPhoto(idPhoto) {
  ajax_fetch("a=isPhoto&idPhoto="+idPhoto);
  var out = JSON.decode(output);
  posY = getScreenCenterY();
  posX = getScreenCenterX();
  if(out[0]=="OK") {
    document.getElementById("photoBox").style.visibility="visible";
    document.getElementById("photoBox").className="photoB1";
    document.getElementById("photoBox").style.top=parseInt(posY-200)+"px";
    document.getElementById("photoBox").style.left=parseInt(posX-250)+"px";
    document.getElementById("photoBox").innerHTML="<span class=\"photoB2\" onclick=\"hidePhoto();return false;\"><img src=\"/pub/zamknij.jpg\" border=\"0\"></span><img src=\"/foto/galeria2/"+out[1]+".jpg\" border=\"0\" /><br /><span>"+out[2]+"</span>";

    document.getElementById("shader").style.visibility="visible";
    document.getElementById("shader").className="photoS1";

    document.getElementById("shader").style.top=parseInt(posY-800)+"px";


  }
}

function showPhoto2(idPhoto) {
  ajax_fetch("a=isPhoto2&idPhoto="+idPhoto);
  var out = JSON.decode(output);
  posY = getScreenCenterY();
  posX = getScreenCenterX();
  if(out[0]=="OK") {
    document.getElementById("photoBox").style.visibility="visible";
    document.getElementById("photoBox").className="photoB1";
    document.getElementById("photoBox").style.top=parseInt(posY-200)+"px";
    document.getElementById("photoBox").style.left=parseInt(posX-250)+"px";

    document.getElementById("photoBox").innerHTML="<span class=\"photoB2\" onclick=\"hidePhoto();return false;\"><img src=\"/pub/zamknij.jpg\" border=\"0\"></span><img src=\"/foto/artykul2/"+out[1]+".jpg\" border=\"0\" /><br /><span>"+out[2]+"</span>";
    document.getElementById("shader").style.visibility="visible";
    document.getElementById("shader").className="photoS1";
    document.getElementById("shader").style.top=parseInt(posY-800)+"px";
  }
}
  var idStr = new Array();
  idStr[6]="ogloszenie";
  idStr[8]="impreza";

function showPhoto3(idPhoto,idStr_i) {

  ajax_fetch("a=isPhoto3&co="+idStr[idStr_i]+"&idPhoto="+idPhoto);
  var out = JSON.decode(output);
  posY = getScreenCenterY();
  posX = getScreenCenterX();
  if(out[0]=="OK") {
    document.getElementById("photoBox").style.visibility="visible";
    document.getElementById("photoBox").className="photoB1";
    document.getElementById("photoBox").style.top=parseInt(posY-200)+"px";
    document.getElementById("photoBox").style.left=parseInt(posX-250)+"px";

    document.getElementById("photoBox").innerHTML="<span class=\"photoB2\" onclick=\"hidePhoto();return false;\"><img src=\"/pub/zamknij.jpg\" border=\"0\"></span><img src=\"/foto/"+idStr[idStr_i]+"/500/"+out[1]+".jpg\" border=\"0\" /><br /><span>"+out[2]+"</span>";
    document.getElementById("shader").style.visibility="visible";
    document.getElementById("shader").className="photoS1";
    document.getElementById("shader").style.top=parseInt(posY-800)+"px";
  }
}


function hidePhoto() {
    document.getElementById("photoBox").style.visibility="hidden";
    document.getElementById("shader").style.visibility="hidden";
    document.getElementById("shader").className="photoS2";
    document.getElementById("photoBox").className="photoB3";
    document.getElementById("photoBox").innerHTML="";
}

function getScreenCenterY() {
  var y = 0;
  y = getScrollOffset()+(getInnerHeight()/2);
  return(y);
}

function getScreenCenterX() {
  return(document.body.clientWidth/2);
}

function getInnerHeight() {
  var y;
  if (self.innerHeight) {
    y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight) {
    y = document.documentElement.clientHeight;  
  }
  else if (document.body) {
    y = document.body.clientHeight;  
  }
  return(y);
}

function getScrollOffset() {
  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 checkZ(id,typ) {
  var Z =  new Array();
  if(typ == "O") {
    Z[0]="id_ogloszenia";
    Z[1]="newOZ";
    Z[2]="ogloszenie";
    Z[3]="O";
  }
  else if (typ == "I") {
    Z[0]="id_imprezy";
    Z[1]="newIZ";
    Z[2]="impreza";
    Z[3]="I";
  }
  if(id > 0) {
    var idQ = "&"+Z[0]+"="+id;
  }
  else {
    var idQ = "";
  }
  ajax_fetch("a="+Z[1]+idQ);
  var out = new JSON.decode(output);
  if(document.getElementById("OglZdjecia")) {
    var OZ = document.getElementById("OglZdjecia");
    OZ.innerHTML="";
    if(out.status=="OK") {
      out.zdjecia.each(function(zdjecie){
        //alert(out[1][i]);
        OZ.innerHTML += "<img src=\"/foto/"+Z[2]+"/150/"+zdjecie+".jpg\" onclick=\"if(window.confirm('Czy na pewno chcesz usunąć to zdjęcie') == true) {usunZ("+zdjecie+",'"+Z[3]+"');checkZ(0,'"+Z[3]+"');}else{return false;}\" /> ";
      });
    }
    else {
      OZ.innerHTML="";
    }
  }
  setTimeout("checkZ("+id+",'"+typ+"')",5000);
}

function usunZ(idZdjecia,typ) {
  ajax_fetch("a=usun"+typ+"Z&idZdjecia="+idZdjecia);
}

function findPosX(obj) {
  var curleft = 0;

  if(obj.offsetParent) {
    while(1) {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
  } else if(obj.x) {
    curleft += obj.x;
  }

  obj.style.position = "static";

  return curleft;
}

function findPosY(obj) {
  var curtop = 0;

  if(obj.offsetParent) {
    while(1) {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
  } else if(obj.y) {
    curtop += obj.y;
  }

  return curtop;
}

function findPos(obj) {
  var left = findPosX(obj);
  var top = findPosY(obj);
  return [left , top];
}

function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

function showmenu(menuid) {
  if (document.getElementById(menuid)) {
    if(document.getElementById("menubox").className == "hide") {
      pos = findPosition(document.getElementById(menuid));
      el_height = document.getElementById(menuid).offsetHeight;
      el_width = document.getElementById(menuid).offsetWidth;
      document.getElementById("menubox").className="show";
      document.getElementById("menubox").style.top=parseInt(pos[1]+parseInt(el_height)+2)+"px";
      document.getElementById("menubox").style.left=pos[0]+"px";
    }
  }
}

function hidemenu() {
  document.getElementById("menubox").className="hide";
}

function showMap(lat,lon) {
  posY = getScreenCenterY();
  posX = getScreenCenterX();
  if(lat && lon) {
    document.getElementById("photoBox").style.visibility="visible";
    document.getElementById("photoBox").className="photoB1";
    document.getElementById("photoBox").style.top=parseInt(posY-200)+"px";
    document.getElementById("photoBox").style.left=parseInt(posX-250)+"px";

    document.getElementById("photoBox").innerHTML="<span class=\"photoB2\" onclick=\"hidePhoto();return false;\"><img src=\"/pub/zamknij.jpg\" border=\"0\"></span><img src=\"http://maps.google.com/staticmap?center=52.021214,19.225512&markers="+lat+","+lon+",red&size=450x440&zoom=6&key=ABQIAAAADLvh-APp1YLipUNahKtrhBQ19OD_qYKqsGLKi_J1C_AwPR8vhRSx2jN2CmySNZFAdCuXclygHGAl-g\" border=\"0\" /><br /><span></span>";
    document.getElementById("shader").style.visibility="visible";
    document.getElementById("shader").className="photoS1";
    document.getElementById("shader").style.top=parseInt(posY-800)+"px";
  }
}

function zobaczWiecej(gwid) {
  location.href="http://www.goscinnawies.pl/wyszukiwarka.html?redir2gw="+gwid;
}

function pobierzGalerie(gwid) {
         new Request.JSON({
           url: "/pub/ajax.php",
           onSuccess: function(out) {
             if(out.status = "OK") {
               if(typeof(out.odp == "object")) {
                 var i =1;
                 var newHead = new Element('h3', {'text':'Galeria zdjęć'});
                 $('galBox').adopt(newHead);
                 $each(out.odp,function(val) {
                   if(i > 5) {
                     var css = "display:none;";
                   }
                   else {
                     var css = "";
                   }
                   i++;
                   var newImg = new Element('a', {'rel':'milkbox:gal1', 'href':"http://www.goscinnawies.pl/foto/500/"+gwid+"/"+val.order+".jpg",'html':"<img src=\"http://www.goscinnawies.pl/foto/100/"+gwid+"/"+val.order+".jpg\" />",'title':" ", 'style':css});
                   $('galBox').adopt(newImg);
                 });
               }
             }
             else {
               alert(out.odp);
             }
           }
         }).get({'gwid': gwid,'a': 'pobierzGalerie'});
}

