function fixPlaceholders()
{
  $$('input, textarea').each(
    function(input)
    {
      var placeholder = input.get('title');
      input.erase('title');
      
      //Get the REAL value, instead of the placeholder when shown
      $ext(input,
        {
          getRealValue: function()
          {
            var value = this.getValue();
            if(value == this.fakeplaceholder)
            {
              return '';
            }
            return value;
          }
        }      
      );
      
      //Empty placeholder?
      if(!placeholder)
      {
        input.fakeplaceholder = '';
        return false;
      } 
      
      //Native placeholder-ing in WebKit
      if(Browser.WebKit)
      {
        input.set('placeholder', placeholder);
        input.fakeplaceholder = '';
        return true;
      }
      
      //Functions to show and hide the placeholder
      $ext(input,
        {
          showPlaceholder: function()
          {
            if(this.getValue().blank())
            {
              this.setValue(this.fakeplaceholder);
            }
          },
          
          hidePlaceholder: function()
          {
            if(this.getValue() == this.fakeplaceholder)
            {
              this.setValue('');
            }
          }
        }
      );
      
      //And here are the events related
      input.on(
        {
          focus:  'hidePlaceholder',
          blur:   'showPlaceholder',
          change: 'showPlaceholder'
        }
      );
			    
		  //First time init
      input.fakeplaceholder = placeholder;
      input.showPlaceholder();
    }
  );
}

function followHashLinks() {
  $$('a[href^="#"]').each(
    function(link) {      
      link.on('click',
        function(e) {
          e.stop();  
          
          var hash = link.getAttribute('href').substr(1);
          
          goToHash(hash);
        }
      );
    }
  );
}

function goToHash(hash) {
  if($$('#main > section#section-' + hash)[0]) {
    goToSection(hash);
  }
  else if($(hash)) {
    $(hash).scrollThere({ duration: 600 });
  }
}

var goingToSection = false;

function goToSection(hash) {
  if(goingToSection) return;
  
  if(!$$('#main > section#section-' + hash)[0]) return;
  
  var oldMenuItem = $$('nav > ul > li.current')[0];
  var newMenuItem = $$('nav > ul > li.' + hash)[0];
  var nipple = $$('nav > #nipple')[0];
  
  var oldSection = $$('#main > section.current')[0];
  oldSection._$pd = 'block';
  var newSection = $$('#main > section#section-' + hash)[0];
  newSection._$pd = 'block';
  
  if(oldSection == newSection) return;
  
  goingToSection = true;
  
  oldMenuItem.removeClass('current');
  newMenuItem.addClass('current');
  
  oldSection.fade('out', 
    {
      duration: 300,
      
      onFinish: function() {
        document.location.hash = hash;
        document.title = 'Daniel Waldron - ' + hash.capitalize();
  
        this.element.removeClass('current');
        
        newSection.addClass('current');
        newSection.fade('in', 
          { 
            duration: 300,
            
            onFinish: function() {
              newSection.select('img').each(
                function(image) {
                  adjustImageMargin(image);
                }
              );
              
              goingToSection = false;
            }
          }
        );
      }
    }
  );
  
  var newNippleLeft = (newMenuItem.position().x - $$('nav')[0].position().x + 35 - 12);
  nipple.setStyle('left', newNippleLeft + 'px');
}

function adjustImageMargin(image) {
  if(!image.getStyle('max-width') || !image.getStyle('max-height')) return;
  
  if(!image.width || !image.height) {
    image.on('load', function() { adjustImageMargin(this); });
    return;
  }
  
  var maxHeight = parseInt(image.getStyle('max-height'));
  var maxWidth  = parseInt(image.getStyle('max-width'));
  
  var height    = image.height;
  var width     = image.width;
  
  var marginTop  = (height < maxHeight) ? ((maxHeight - height) / 2) : 0;
  var marginLeft = (width < maxWidth)   ? ((maxWidth - width) / 2)   : 0;
  
  image.setStyle('margin-top', marginTop + 'px');
  image.setStyle('margin-left', marginLeft + 'px');
}

document.on('ready', 
  function() {
    var currentSection = $$('#main > section.current')[0];
    currentSection.select('img').each(
      function(image) {
        adjustImageMargin(image);
      }
    );
    
    if(document.location.hash.substr(1)) {
      goToHash(document.location.hash.substr(1));
    }
    
    fixPlaceholders();
  
    followHashLinks();
  }
);  
