

function showImage() {
  Element.show('loading');
  
  imgPreloader = new Image();
  imgPreloader.onload=function(){
    new Effect.Fade('currentPhoto', {duration: 1});
    new Effect.Appear('nextPhoto', {duration: 1});
  }
  
  imgPreloader.src = '/images/galleries/inny.jpg';
  
  anchors = $('photos-gallery').getElementsByTagName('a');
  alert(anchors.length);
}

function navigation_fade(e) {
	new Effect.Fade('galleryNextLink', 
    { from: 0.6, to: 0.01, duration: 0.5 });
}

function navigation_appear(e) {
  new Effect.Appear(e, 
    { from: 0.01, to: 0.6, duration: 0.5 });
}

var Gallery = Class.create();

Gallery.prototype = {
        
	initialize: function() {	
		if (!document.getElementsByTagName || !$('photos-gallery')){ return; }
		this.anchors = $('photos-gallery').getElementsByTagName('a');
                
                //this.activeImage = 'photo'
                this.current = 0;
                //$('galleryPrevLink').hide();
                
                this.currentImage = 'photo';
                this.secondaryImage = 'secondaryPhoto';
                
                this.changeImage(0);

				/*$('galleryNextLink').onmouseout = function (e) {
					new Effect.Fade('galleryNextLink', 
				    { from: 0.6, to: 0.01, duration: 0.5 });
				}
				
				$('galleryPrevLink').onmouseout = function (e) {
					new Effect.Fade('galleryPrevLink', 
				    { from: 0.6, to: 0.01, duration: 0.5 });
				}
				$('galleryNextLink').onmouseover = function (e) {
					new Effect.Appear('galleryNextLink', 
				    { from: 0.01, to: 0.6, duration: 0.5 });
				}	
				
				$('galleryPrevLink').onmouseover = function (e) {
					new Effect.Appear('galleryPrevLink', 
				    { from: 0.01, to: 0.6, duration: 0.5 });
				}	*/
															
                //Event.observe('photoPreview', 'mouseover', showNavigation);
                //Event.observe('photoPreview', 'mouseout', hideNavigation);
                //Event.observe('galleryNavigation', 'mouseover', showNavigation);
                //Event.observe('galleryNavigation', 'mouseout', hideNavigation);
	},

	changeImage: function(direction) {
                if(this.lock) return;
                this.lock = true;
                this.changeNumber(direction);
                
                $('photoIndicator').show();
  
		imgPreloader = new Image();
		
                imgPreloader.onload=function(){
                  myGallery.swapImages(imgPreloader.src);    
              //this.swapImages();
                }
                
                imgPreloader.src = this.anchors[this.current].getAttribute('href');
	},
        
        swapImages: function(imageSrc) {
            image = $(this.secondaryImage + 'Image');
            image.src = imageSrc;
             $('photoIndicator').hide();
            //this.changePosition(this.secondaryImage);
            new Effect.Appear(this.secondaryImage, {afterFinish:function() { myGallery.lock = false;}});
            new Effect.Fade(this.currentImage, {afterFinish:function() { myGallery.lock = false; }});
            
            var temp = this.secondaryImage;
            this.secondaryImage = this.currentImage;
            this.currentImage = temp;
        },
        
        stopSlideshow: function(imageSrc) {
            clearTimeout(this.timeout);
        },
        
        startSlideshow: function(imageSrc) {
            this.changeImage('forward');
            this.timeout = setTimeout("myGallery.startSlideshow()", 15000);
        },
        
        changeNumber: function(direction) {
            if(direction == 'forward') {
              if(this.current < this.anchors.length-1)
                this.current++;
            } else {
              if(this.current > 0)
                this.current--;
            }
            
            if(this.current == 0) {
              $('galleryPrevLink').hide();
            } else {
              $('galleryPrevLink').show();
            }
            if(this.current == this.anchors.length-1 ) {
              $('galleryNextLink').hide();
            } else {
              $('galleryNextLink').show();
            }
        }
}


function initGallery() { myGallery = new Gallery(); }

function showNavigation() {
    new Effect.Appear('galleryNavigation', {duration: 0.3});
}

function hideNavigation() {
    new Effect.Fade('galleryNavigation', {duration: 0.3});
}

Event.observe(window, 'load', initGallery, false);