$(document).ready(function(){
    $('.thickbox').click(function(){
        $(document).scrollTo( 0,0 );
    });
});

//var Photogallery = Class.create();

//function Photogallery(){}

var myPhotogallery =
{
   initialize : function()
   {

      var objImageParameters = document.getElementById( 'ImageParameters' );
//      var objImageParameters = $('#ImageParameters');

      this.Anchors = objImageParameters.getElementsByTagName( 'a' );

      this.Images = new Array();
      this.currentImage;
      this.currentImageId;
      this.currentImageThumbId = 0;

      this.setImages();
      this.setCurrentImage( '0' );
      
      this.thumbs = new Array();

      this.showImage();
      
   }
   ,

   setImages : function()
   {
      var item = new Array();
      for ( i = 0; i < this.Anchors.length; i ++ )
      {
         item["thumb"] = $(this.Anchors.item( i )).attr( 'rel' );
         item["detail"] = $(this.Anchors.item( i )).attr( 'href' );
         item["description"] = $(this.Anchors.item( i )).attr('alt');
         item["name"] = $(this.Anchors.item( i )).attr('name');
         item["name_thumb"] = $(this.Anchors.item( i )).attr('title');
         this.Images.push( item );
         item = new Array();
      }
   }
   ,

   getImages : function()
   {
      return this.Images;
   }
   ,

   setCurrentImage : function( id )
   {
      this.currentImage = this.Images[id];
      this.currentImageId = id;
      id +=1;
      if (id<this.Images.length)
      { 
         $("#next-link").replaceWith('<a id="next-link" style="display: inline;" onclick="myPhotogallery.event_image_click('+ id +');return false;">' + $("#next-link").text() + '</a>');
         $("#next-link").attr('href','#');
//         $("#next-link").show();
      }
      else
      {
        $("#next-link").replaceWith('<span id="next-link">' + $("#next-link").text() + '</span>');
      }
      id -= 2;
      
      if (id>-1)
      { 
         $("#prev-link").replaceWith('<a id="prev-link" style="display: inline;" onclick="myPhotogallery.event_image_click('+ id +');return false;">' + $("#prev-link").text() + '</a>');
         $("#prev-link").attr('href','#');
//         $("#prev-link").show();
      }
      else
      {
         $("#prev-link").replaceWith('<span id="prev-link">' + $("#prev-link").text() + '</span>');
      }
      
   }
   ,

   showImage : function()
   {
      //$("#image_detail").hide('fast');
      var src = this.currentImage['detail'];
      $("#image_detail").attr('src',src);
      $("#image_detail").attr('alt',this.currentImage['name']);
      $("#image_detail").attr('title',this.currentImage['name']);
      document.getElementById('image-label').innerHTML = this.currentImage['name'];
      document.getElementById('image-description').innerHTML = this.currentImage['description'];
      this.setThumbs();
      //$("#image_detail").show('fast');
   }
   ,

   setThumbs : function()
   {
      var super_max = this.Images.length;
      
      var currentImageId = this.currentImageThumbId;
      var objThumbs = window.document.getElementById( 'thumbnails' );
      objThumbs.innerHTML = "";

      var anchorThumb = new Array();
      var imgThumb = new Array();
      var spanThumb = new Array();
      
      var divOfThumbnail = new Array();
      
      for ( i = currentImageId; i < this.Images.length; i ++ )
      {
         imgThumb[i] = $('<img src="'+ this.Images[i]['thumb'] +'" id="img_thumb_'+ i +'" class="thickbox" alt="'+this.Images[i]['name_thumb']+'" title="'+this.Images[i]['name_thumb']+'" onclick="javascript: myPhotogallery.event_image_click('+ i +')" style="cursor: pointer;" />');
         
         spanThumb[i] = $('<span>'+this.Images[i]['name_thumb']+'</span>');
          divOfThumbnail[i] = $('<div class="thumbnail"></div>');
         if (!((i+1)%5))
         {
            $(divOfThumbnail[i]).addClass('last');
         }
         
         $(divOfThumbnail[i]).append( imgThumb[i] );
         $(divOfThumbnail[i]).append( spanThumb[i] );
         
         $(objThumbs).append( divOfThumbnail[i] );
      }

      this.setPrev( currentImageId );
      this.setNext( currentImageId );

   }
   ,

   setNext : function( currentImageId )
   {
        
   }
   ,
   setPrev : function( currentImageId )
   {
        
   }
   ,

   event_image_click : function( imageId )
   {
      this.setCurrentImage( imageId );
      this.showImage();
   }
   ,

   event_next_click : function( imageId )
   {
      if ( imageId + 1 < this.Images.length )
      {
         this.setCurrentImage(imageId + 1)
      }
   }
   ,

   event_prev_click : function( imageId )
   {
      if ( imageId - 1 >= 0 )
      {
         this.setCurrentImage(imageId - 1);
      }
   }
}

//  ---------------------------------------

// function initPhotogallery() { myPhotogallery = new Photogallery(); }
/*
$(document)
    .ready(function() {
//        initPhotogallery();
        myPhotogallery.initialize();
    });
    */
// Event.observe( window, 'load', initPhotogallery, false );

