/*jslint browser: true, white: true, continue: true */
(function() {
'use strict';

var Resume =
{
  content: null,
  
  /**
   * Listen for clicks and navigation change events
   */
  listen: function(openLinkIds, closeLinkIds)
  {
    var t = this, i, linkId, link, url, clickOpen, clickClose;
    
    if (!this.supportsHistoryAPI())
    {
      // never mind
      return;
    }
    
    clickOpen = function(e)
    {
      t.appear();
      history.pushState({ showResume: true }, null, url);
      e.preventDefault();
    };
    
    // loop through provided IDs; register listener for each
    for (i in openLinkIds)
    {
      if (openLinkIds.hasOwnProperty(i))
      {
        linkId = openLinkIds[i];
        link = document.getElementById(linkId);
        
        if (!link)
        {
          continue;
        }
        
        url = link.href;
        
        // event called when user clicks link
        link.addEventListener("click", clickOpen, false);
      }
    }
    
    clickClose = function(e)
    {
      history.go(-1);
      e.preventDefault();
    };
    
    // loop through provided IDs; register listener for each
    for (i in closeLinkIds)
    {
      if (closeLinkIds.hasOwnProperty(i))
      {
        linkId = closeLinkIds[i];
        link = document.getElementById(linkId);
        
        if (!link)
        {
          continue;
        }
        
        // event called when user clicks link
        link.addEventListener("click", clickClose, false);
      }
    }
    
    // event called when user clicks browser back/forward button
    window.addEventListener("popstate", function(e)
      {
        if (e.state && e.state.showResume)
        {
          t.appear();
        }
        else
        {
          t.disappear();
        }
      }
    );
  },
  
  appear: function()
  {
    var t = this, req;
    
    this.curtain.lower();
    
    document.getElementById('resume').style.display = 'block';
    
    if (this.content === null)
    {
      req = new XMLHttpRequest();
      req.open('GET', 'resume.html');
      req.onreadystatechange = function()
        {
          var matches;
          
          if (req.readyState === 4)
          {
            // Yes, yes... never parse HTML with a regex. Except, maybe, when it's coming from an extremely limited, trusted source, and um HEY LOOK OVER THERE
            matches = req.responseText.match(/<article>([\s\S]+)<\/article>/);
            
            if (matches && matches.length > 1)
            {
              t.content = matches[1];
            }
            else if (!t.content)
            {
              t.content = req.responseText;
            }
            
            t.spinner.banish();
            document.getElementById('resume_content').innerHTML = t.content;
          }
        };
      req.send();
    }
  },
  
  disappear: function()
  {
    this.curtain.raise();
    
    document.getElementById('resume').style.display = 'none';
  },
  
  /**
   * A curtain to cover up existing content on the page
   */
  curtain:
  {
    zIndexRaised: -1,
    zIndexLowered: 2,
    
    opacityRaised: 0,
    opacityLowered: 0.75,
    
    transitionTime: 250,
    
    timer: null,
    
    get: function()
    {
      return document.getElementById('curtain');
    },
    
    lower: function()
    {
      this.get().style.zIndex = this.zIndexLowered;
      this.get().style.opacity = this.opacityLowered;
    },
    
    raise: function()
    {
      var t = this;
      
      this.get().style.opacity = this.opacityRaised;
      
      // wait for opacity transition animation to complete before restoring z-index
      if (this.timer !== null)
      {
        clearTimeout(this.timer);
      }
      
      this.timer = setTimeout(function()
        {
          t.get().style.zIndex = t.zIndexRaised;
        },
        t.transitionTime
      );
    }
  },
  
  spinner:
  {
    get: function()
    {
      return document.getElementById('spinner');
    },
    
    banish: function()
    {
      this.get().style.display = 'none';
    }
  },
  
  supportsHistoryAPI: function()
  {
    return !!(window.history && history.pushState);
  }
};

// Register event listeners
if (window.addEventListener)
{
  window.addEventListener('load', function()
    {
      Resume.listen(['resume_link'], ['resume_close_link']);
    }
  );
}

}());

