/*
  Dynamic Application of Functions to DOM
*/

var _debugMode = false;
Event.observe(document, 'dom:loaded', init);
Event.observe(window, 'load', initAfterImages);

/*
  This is the function that applies the functions to the DOM
*/
function init(){
  
  // REINIT
  function reinit() {
    if(debug) {
      Try.these(function() { console.log("reinit"); });
    }
    
    init();
    
  }
  
  // SLIDE SHOW 
  Try.these(function() {
    if ($('feature')) { new SlideShow('feature', {delay:2}); }
  });

  // GALLERY NAV LAST CLASS
  if($('portfolioSub')) {
    $$('ul#portfolioSub li').last().addClassName('imagelast');
  }
  
  // HIDE IMAGE LIST SO IT WILL RENDER THE THUMBS CORRECTLY
  if($('photoListShell')) {
    $$('ul.photoList li a img').invoke('hide');
  }
  
  // FIND AND EQUALIZE HEIGHTS OF ELEMENTS ON GALLERY
  if($('photoListShell') && $('gallerySidebar')) {
    $$('#photoListShell', '#gallerySidebar').equalizeHeights();
  }

  // insert images on press page
  (function(){
    if ($('pressList')){
      $$('#pressList li a').each(function(a){
        var path =  a.readAttribute('href').split('#').last().gsub('_normal.', '.');
        var pathToThumb = path.replace(/\.(?!.*\.)/, '_thumb.');
        a.insert('<img src="' + pathToThumb + '" />');
      });
    }
  })();

}

function initAfterImages () {
  
  // GALLERY THUMBS
  if($('photoListShell')) { 
    
    // INITIALIZE VARIABLES
    var maxWidth  = 170;
    var ratio     = 0;
    var new_h     = 0;
    var new_w     = 0;
    var w         = 0;
    var h         = 0;
        
    $$('ul.photoList li a img').each(function(node) {
        w = node.getWidth();
        h = node.getHeight(); 
        if(w > h) {
            ratio = (maxWidth/w); 
            new_h = Math.ceil(h*ratio);
            node.setStyle('width: ' + maxWidth + 'px; height: ' + new_h + 'px;');
        } else if(w < h) {
            ratio = (maxWidth/h); 
            new_w = Math.ceil(w*ratio);
            node.setStyle('width: '+ new_w +'px; height: ' + maxWidth + 'px;');
        } else {
            node.setStyle('width: 180px; height: 180px;');
        }
        node.show(); // SHOW HIDDEN NODES ONE BY ONE AS THEY ARE RENDERED
      });
  }

  // PRESS THUMBS
  if($('pressList') && $('pressRight')) {
    $$('#pressThumbs li a').each(function(a) {
      a.observe('click', function(e) {

        // KILL EVENT TO PREVENT THE CLICK FROM CHANGING THE PAGE
        Event.stop(e); 

        var url = a.href.substr(
          a.href.lastIndexOf("#") + 1,
          a.href.length
        );

        var previewImage = url;
        var previewPDF = a.name;
        var content = "<a href=\""+previewPDF+"\" target=\"_blank\"><img src=\"" + previewImage + "\" /></a>";
        $('pressRight').update(content);
      });
    });
  }
    
}

/*
Equalize the heights of columns
*/
Object.extend(Array.prototype, {
  equalizeHeights: function(){
    //equalize the heights of columns
    if (this.any(function(el){
      return Object.isElement(el)
    }))
      var maxHeight = this.map(function(el){
          return el.getHeight();
      }).max();
      this.each(function(el) {
        if (el.getHeight() != maxHeight) {
          el.setStyle({height: 
            (maxHeight - Number(el.getStyle('padding-top').sub('px', '')) -
            Number(el.getStyle('padding-bottom').sub('px', ''))) + 'px'
          });
        }
      });
    return this;
  }
});

