 
function showPage() {
   window.location.hash = this.identifier;
   var photo = photos[this.identifier];
   
   var func = "removeOld('" + photo + "')";
   setTimeout(func, 500);
   new Fx.Style('main', 'opacity').custom(1,0.3);
}

function removeOld(photo) {
   getEXIF(photo);
   var main = aaElem('main');

   // Usuwa bieżącą zawartość
   var current = aaElem('photo');
   if (current) main.removeChild(current);

   var img = document.createElement("img");
   img.src=photo;
   img.id='photo';
   main.appendChild(img);

   new Fx.Style('main', 'opacity').custom(0.3,1);
}


function getEXIF(photo) {

   // Usuwanie starych danych
   if (aaElem('exifid'))
     document.getElementsByTagName('head')[0].removeChild(aaElem('exifid'));

   var url = 'get_photo.php?photo=' + photo;

   var script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = url + '&callback=printEXIF';
   script.id = 'exifid';
   document.getElementsByTagName('head')[0].appendChild(script);
}

function createListItem(text,ul) {
   var li = document.createElement('li');
   var txt = document.createTextNode(text);
   li.appendChild(txt);
   ul.appendChild(li);
   return ul;
}

function printEXIF(exifObj) {

   // access recipe element and clear
   var index;
   var hash = window.location.hash;
   if (hash) {
      index = hash.split("#")[1];
   }

   var meta = aaElem('meta');
   var metacontent =aaElem('metacontent');
   var permalink = aaElem('permalink'); 
   if (metacontent) meta.removeChild(metacontent);
   if (permalink) meta.removeChild(permalink);

   var p = document.createElement('p');
   p.id = 'permalink';
   var a = document.createElement('a');
   var href = window.location.pathname + "?photo=" + index;
   a.setAttribute('href', href);
   a.appendChild(document.createTextNode('Trwały odnośnik do strony'));
   p.appendChild(a);
   meta.appendChild(p);

   var ul = document.createElement('ul');
   ul=createListItem(exifObj.Model,ul); 
   ul=createListItem(exifObj.ISOSpeedRatings,ul);
   ul=createListItem(exifObj.ExposureTime,ul);
   ul=createListItem(exifObj.DateTimeOriginal,ul);
   ul=createListItem(exifObj.COMPUTED.ApertureFNumber,ul); 
   ul.id="metacontent";
   meta.appendChild(ul);
   
}

aaManageEvent(window,'load',function() {
   var home;
   var current;
   var index;
   var past;

   var hash = window.location.hash;

   // Pobieranie wartości skrótu
   if (hash) {
      index = hash.split("#")[1];
   }
   
   // Tworzenie węzłów nawigacyjnych
   for (var id = 0; id < photos.length; id++) {
      var nav = Navigation.addNavigationNode(past,id,showPage);
      if (!past) {
        home = nav;
      }
      past = nav;
      if (index && id == index)
        current = nav;
   }

   // Ustawianie stron: startowej i bieżącej
   Navigation.setHome(home);
   if (!current) {
      Navigation.setCurrentPage(home);
   } else {
      Navigation.setCurrentPage(current);
   }

   // Dodawanie zdarzeń do przycisków do obsługi pokazu
   aaManageEvent(aaElem('next'),'click',aaBindObjMethod(Navigation,Navigation.nextPage));
   aaManageEvent(aaElem('previous'),'click',aaBindObjMethod(Navigation,Navigation.prevPage));
   aaManageEvent(aaElem('home'),'click', Navigation.returnHome);
});
