var DynamicThumb = function()
{
  
  var thumbIdPrefix = 'thumb_';
  var working = 0 ;
  
  function getThumbIdMax(){
    var thumbnailContainer = document.getElementById('thumbnails');
    var idMax = -1 ;
    for (i=0; i<thumbnailContainer.childNodes.length; i++) {
    	var currentThumb = thumbnailContainer.childNodes[i];
      if (currentThumb.id)
      {
        var currentThumbId = parseInt(currentThumb.id.substring(thumbIdPrefix.length));
        if (idMax == -1 || idMax < currentThumbId)
        {
          idMax = currentThumbId ;
        }
      }      
    }
    return idMax ;
  }
  
  function getThumbIdMin(){
    var thumbnailContainer = document.getElementById('thumbnails');
    var idMin = -1 ;
    for (i=0; i<thumbnailContainer.childNodes.length; i++) {
    	var currentThumb = thumbnailContainer.childNodes[i];
      if (currentThumb.id)
      {
        var currentThumbId = parseInt(currentThumb.id.substring(thumbIdPrefix.length));
        if (idMin == -1 || idMin > currentThumbId)
        {
          idMin = currentThumbId ;
        }      
      }
    }
    return idMin ;
  }
  
  function retrieveHTMLThumb(thumbId, iaf, dir){
    new Ajax.Request('includes/dynamic_thumbs.php',
    {
      method:'get',
      parameters: {thumb: thumbId, dir: dir},
      onSuccess: function(transport){
        var response = transport.responseText ;
        if (response)
        {
          var responseArray = eval(response);
          
          var newdiv = document.createElement("div");
          newdiv.id = responseArray[0] ;
          
          var newAnchor = document.createElement("a");
          newAnchor.href = responseArray[1] ;
          
          var newImg = document.createElement("img");
          newImg.src = responseArray[2] ;
          newImg.title = responseArray[3] ;
          newImg.alt = responseArray[3] ;
          newImg.setAttribute('class', 'thumbnails') ;
          newImg.setAttribute('className', 'thumbnails') ;
          

          newdiv.appendChild(newAnchor);
          newAnchor.appendChild(newImg);
          
          if (iaf)
          {
            // image is by default hidden
            insertAtFirst(newdiv);
            hideOlderThumb();
            
          }
          else 
          {
            // image is by default hidden
            insertAtLast(newdiv);
            hideRecentThumb();
            
          }
        }
        working = 0 ;
      },
      onFailure: function(){ 
        //do nothing
      }
    });

  }
  
  function insertAtFirst(newdiv){
    var thumbnailsContainer = document.getElementById('thumbnails');
    if (thumbnailsContainer){
      thumbnailsContainer.insertBefore(newdiv, thumbnailsContainer.firstChild) ;
    }
  }
  
  function insertAtLast(newdiv){
    var thumbnailsContainer = document.getElementById('thumbnails');
    if (thumbnailsContainer){
      thumbnailsContainer.appendChild(newdiv) ;
    }
  }
  
  function hideRecentThumb(){
    var thumbId = getThumbIdMax();
    if (thumbId > 0)
    {
      var thumbnailsContainer = document.getElementById('thumbnails');
      var elem = document.getElementById(thumbIdPrefix + thumbId) ;
      //Effect.SlideUp(elem);
      thumbnailsContainer.removeChild(elem);
    }
  }
  
  function hideOlderThumb(){
    var thumbId = getThumbIdMin();
    if (thumbId > 0)
    {
      var thumbnailsContainer = document.getElementById('thumbnails');
      var elem = document.getElementById(thumbIdPrefix + thumbId) ;
      //Effect.SlideUp(elem);
      thumbnailsContainer.removeChild(elem);
    }
  }
  
  function showRecendThumb(){
    var thumbId = getThumbIdMax();
    if (thumbId > 0)
      Effect.SlideDown(document.getElementById(thumbIdPrefix + thumbId));
  }
  
  function showOlderThumb(){
    var thumbId = getThumbIdMin();
    if (thumbId > 0)
      Effect.SlideDown(document.getElementById(thumbIdPrefix + thumbId));
  }

return {

  init: function(args)
  {
    
  },

  navigateForward: function()
  {
    if (working ==0){
      working = 1 ;
    }
    else 
    {
      return ;
    }
      
    var thumbId = getThumbIdMax() ;
    if (thumbId > 0)
      retrieveHTMLThumb(thumbId, true, 'more');
  },
  
  navigateBackward: function()
  {
    if (working ==0){
      working = 1 ;
    }
    else 
    {
      return ;
    }
    
    var thumbId = getThumbIdMin() ;
    if (thumbId > 0)
      retrieveHTMLThumb(thumbId, false, 'less');
  }
}
}();
