var maxW;// =730;
var maxH;// =470;
var RIGHT=true;
var LEFT=true;


//====================  sprawdzające (nie moje) =======================//

function $(id) {
    return !id || id.nodeType === 1 ? id : document.getElementById(id);
}
//-------------------------

function isType(o,t) {    
  return (typeof o).indexOf(t.charAt(0).toLowerCase()) === 0;
}
//-------------------------

function image(src,cfg) {    var img, propw, proph, target;
    cfg = cfg || (isType(src,'o') ? src : {});

    img = $(src);
    if (img) {
        src = cfg.src || img.src;
    } else {
        img = document.createElement('img');
        src = src || cfg.src;
    }

    if (!src) {
        return null;
    }

    propw = isType(img.naturalWidth,'u') ? 'width' : 'naturalWidth';
    proph = isType(img.naturalHeight,'u') ? 'height' : 'naturalHeight';
    img.alt = cfg.alt || img.alt;

    // Add the image and insert if requested (must be on DOM to load or
    // pull from cache)
    img.src = src;

    target = $(cfg.target);
    if (target) {
        target.insertBefore(img, $(cfg.insertBefore) || null);
    }

    // Loaded?
    if (img.complete) {
        if (img[propw]) {
            if (isType(cfg.success,'f')) {
                cfg.success.call(img);
            }
        } else {
            if (isType(cfg.failure,'f')) {
                cfg.failure.call(img);
            }
        }
    } else {
        if (isType(cfg.success,'f')) {
            img.onload = cfg.success;
        }
        if (isType(cfg.failure,'f')) {
            img.onerror = cfg.failure;
        }
    }

    return img;
}
//-------------------------



//================  Pokazuje/Chowa/Sprawdza strzalki (next/prev) ===================//


function Nr(sciezka){

  var imgP = sciezka;
  var locarray = imgP.split("/");
  var pic = decodeURI(locarray[(locarray.length-1)]);
 

  for(var i=0;i<galleryarray.length;i++)
  {
    if(pic==galleryarray[i])
      return i;
  }

  return -1;
}
//-------------------------


function PokazStrz(){

  var nr = Nr(top.document.getElementById('imname').src);
  //top.document.getElementById('inputtest').value+=nr+" "+LEFT+" "+RIGHT;

  if(nr!=-1)
  {
    if(nr>0 && LEFT==true)
      top.document.getElementById('arrow_left').style.display='block';
    if(nr<galleryarray.length-1 && RIGHT==true)
      top.document.getElementById('arrow_right').style.display='block';
  }

}
//-------------------------

function SchowajStrz(){

  top.document.getElementById('arrow_left').style.display='none';
  top.document.getElementById('arrow_right').style.display='none';

}
//-------------------------

function SprawdzStrz(){

  var nr = Nr(top.document.getElementById('imname').src);

  if(nr!=-1)
  {
    if(nr>0)
      LEFT=true;
    else 
      LEFT=false;

    if(nr<galleryarray.length-1)
      RIGHT=true;
    else
      RIGHT=false;
  }

  //top.document.getElementById('inputtest').value="  "+LEFT+" = "+RIGHT;

}
//-------------------------



//====================  Wyświetla zdjęcie (mini->full)=======================//

//Wyswietla obrazek jako duży jeśli miniaturka jest w folderze wyżej
function WyswietlMini(imgP){

  var locarray = imgP.split("/");
  var picc = locarray[(locarray.length-1)];
  delete locarray.splice(locarray.length-2,2);
  var imgP2 = locarray.join("/")+"/"+picc;
  Wyswietl(imgP2);

}
//-------------------------

//Wyswietla obrazek jako duży jeśli miniaturką jest ten sam plik w divie id="im"
function WyswietlMidi(){

  var picm = document.getElementById("im").src;
  Wyswietl(picm);

}
//-------------------------



function Wyswietl(imgP){


  image(imgP,{					//ustawienie wymiarow obrazka do wyswietlenia
    success : function () { 

      var screenH = parent.window.innerHeight;
      var screenW = parent.window.innerWidth;

      maxH = screenH-150;
      maxW = screenW-150;

      if(this.width>=maxW && this.height>=maxH) {	//jesli caly obrazek jest za duzy

        if(this.width/this.height>maxW/maxH) {
          imgW=maxW;
	  var proc = (maxW*100)/this.width;
          imgH=(this.height*proc)/100;
        }
        else{
          imgH=maxH;
	  var proc = (maxH*100)/this.height;
          imgW=(this.width*proc)/100;
        }

      }
      else if(this.width>=maxW && this.height<maxH) {	//jesli szerokosc obrazka jest za duza
        imgW=maxW;
	var proc = (maxW*100)/this.width;
        imgH=(this.height*proc)/100;
      }
      else if(this.width<maxW && this.height>=maxH) {	//jesli wysokosc obrazka jest za duza
        imgH=maxH;
	var proc = (maxH*100)/this.height;
        imgW=(this.width*proc)/100;
      }

      else{						//jesli obrazek mniejszy niz max wymiary
        imgW=this.width;
	imgH=this.height;
      }



      var cornerT = ((screenH/2) - (imgH/2)) - 10;
      var cornerL = ((screenW/2) - (imgW/2)) - 10;


      top.document.getElementById('fade').style.top = 0;
      top.document.getElementById('fade').style.left = 0;
      top.document.getElementById('fade').style.width = screenW;
      top.document.getElementById('fade').style.height = screenH;

      top.document.getElementById('light').style.top = cornerT+"px";
      top.document.getElementById('light').style.left = cornerL+"px";
      top.document.getElementById('light').style.width = imgW+20+"px";
      top.document.getElementById('light').style.height = imgH+40+"px";

      top.document.getElementById('image').style.top = cornerT+10+"px";
      top.document.getElementById('image').style.left = cornerL+10+"px";
      top.document.getElementById('image').style.width = imgW;
      top.document.getElementById('image').style.height = imgH;
      top.document.getElementById('image').onmouseover = PokazStrz;
      top.document.getElementById('image').onmouseout = SchowajStrz;

      top.document.getElementById('arrow_right').style.top = cornerT+imgH/5+10+"px";
      top.document.getElementById('arrow_right').style.left = cornerL+imgW-12+"px";
      top.document.getElementById('arrow_right').style.width = 32;
      top.document.getElementById('arrow_right').style.height = 32;
      top.document.getElementById('arrow_right').onmouseover = PokazStrz;
      top.document.getElementById('arrow_right').onmouseout = SchowajStrz;

      top.document.getElementById('arrow_left').style.top = cornerT+imgH/5+10+"px";
      top.document.getElementById('arrow_left').style.left = cornerL+"px";
      top.document.getElementById('arrow_left').style.width = 32;
      top.document.getElementById('arrow_left').style.height = 32;
      top.document.getElementById('arrow_left').onmouseover = PokazStrz;
      top.document.getElementById('arrow_left').onmouseout = SchowajStrz;

      top.document.getElementById('bottomclose').style.top = cornerT+imgH+10+"px";
      top.document.getElementById('bottomclose').style.left = cornerL+10+"px";
      top.document.getElementById('bottomclose').style.width = imgW;
      top.document.getElementById('bottomclose').style.height = 30;


      top.document.getElementById('closeimg').onclick = function() {Zamknij()};
      top.document.getElementById('rightimg').onclick = function() {Nastepny()};
      top.document.getElementById('leftimg').onclick = function() {Poprzedni()};


      top.document.getElementById('image').style.display='block';
      top.document.getElementById('bottomclose').style.display='block';
      top.document.getElementById('light').style.display='block';
      top.document.getElementById('fade').style.display='block';


      var imname="imname";
      top.document.getElementById('image').innerHTML = '<img id='+imname+' src=\"'+imgP+'\" height='+imgH+' width='+imgW+' />';
      SprawdzStrz();
    }
  });
}
//-------------------------





//====================  Zdjęcie - zamknij, nastepne, poprzednie =======================//


function Zamknij(){

  top.document.getElementById('image').style.display='none';
  top.document.getElementById('bottomclose').style.display='none';
  top.document.getElementById('light').style.display='none';
  top.document.getElementById('fade').style.display='none'
}
//-------------------------


function Nastepny(){

  var sciezka2 = top.document.getElementById('imname').src;
  var sciezka = decodeURI(sciezka2);
  var locarray = sciezka.split("/");
  var imgP = locarray[(locarray.length-1)];

  for(var i=0;i<galleryarray.length-1;i++)
  {
    if(galleryarray[i]==imgP)
    {
      if(i<galleryarray.length-1)
      {
        var pic = galleryarray[i+1];

        if(i+1 == galleryarray.length-1){
          top.document.getElementById('arrow_right').style.display='none';
          RIGHT=false;
        }

        if(i+1>0){
          top.document.getElementById('arrow_left').style.display='block';
          LEFT=true;
        }
      }

      //top.document.getElementById('imname').src=directory+"/"+pic;
      Wyswietl(directory+"/"+pic);
      //top.document.getElementById('inputtest').value=galleryarray.length+"  "+directory+"/"+pic;
      break;
    }
  }
  
}
//-------------------------


function Poprzedni(){

  var sciezka2 = top.document.getElementById('imname').src;
  var sciezka = decodeURI(sciezka2);
  var locarray = sciezka.split("/");
  var imgP = locarray[(locarray.length-1)];

  for(var i=0;i<galleryarray.length;i++)
  {
    if(galleryarray[i]==imgP)
    {
      if(i>0)
      {
        var pic = galleryarray[i-1];

        if(i-1 == 0){
          top.document.getElementById('arrow_left').style.display='none';
          LEFT=false;
        }

        if(i-1<galleryarray.length-1){
          top.document.getElementById('arrow_right').style.display='block';
          RIGHT=true;
        }
      }

      //top.document.getElementById('imname').src=directory+"/"+pic;
      Wyswietl(directory+"/"+pic);
      break;
    }
  }

}
//-------------------------




//====================  Powiększa (mini -> midi) =======================//



function Powieksz(imgP){
 
  var maxH = 245;
  var maxW = 290;

  image(imgP,{success : function () { 
      
        if(this.width/this.height>maxW/maxH) {		//obraz jest szeroki
          imgW=maxW;
	  var proc = (maxW*100)/this.width;
          imgH=(this.height*proc)/100;
          document.getElementById('im').height=imgH;
          document.getElementById('im').width=imgW;
          document.getElementById('im').style.marginTop=10+(maxH-imgH)/2;
          document.getElementById('im').style.marginLeft=10;
        }
        else{						//obraz jest wysoki
          imgH=maxH;
	  var proc = (maxH*100)/this.height;
          imgW=(this.width*proc)/100;
          document.getElementById('im').height=imgH;
          document.getElementById('im').width=imgW;
          document.getElementById('im').style.marginTop=10;
          document.getElementById('im').style.marginLeft=10+(maxW-imgW)/2;
        }

      document.getElementById('im').src = imgP;
    }
  });
}
//-------------------------




//====================  Tworzy galerie =======================//



function Galeria(){

  var mouseover="this.style.cursor='pointer';";
  var klasa="sig";
  galleryarray.sort();  

  for(var i=0;i<galleryarray.length;i++)
  {
    if(i==0)
      document.getElementById('galeria').innerHTML += '<img class='+klasa+' src='+directory+'/Mini/'+galleryarray[i]+' onMouseOver='+mouseover+' onclick=WyswietlMini(this.src) />';
    else
      document.getElementById('galeria').innerHTML += '&nbsp;<img class='+klasa+' src='+directory+'/Mini/'+galleryarray[i]+' onMouseOver='+mouseover+' onclick=WyswietlMini(this.src) />';
  }
  
}
//-------------------------


function GaleriaKonia(){

  var pointer="this.style.cursor='pointer'";
  var num;
  galleryarray.sort();

  for(var i=0;i<=5;i++)
  {
    num="simg"+i;

    if(i==0)
      if(galleryarray.length==0)
        document.getElementById('im').src = "../../Obrazki/brak.png";
      else
        Powieksz(directory+"/"+galleryarray[i]);

    if(i<galleryarray.length)
      document.getElementById(num).src = directory+"/"+galleryarray[i];
    else
      document.getElementById(num).style.visibility="hidden";

  }
  
}
//-------------------------




