var HoverTitle = Class.create();
HoverTitle.prototype = {
  initialize: function( options ) {
    this.options = options;
    this.isActive = false;
    this.options.elements.each( function( element ) {
      Event.observe( element, 'mouseover',  this.onMouseOver.bindAsEventListener( this ) );
      Event.observe( element, 'mouseout',   this.onMouseOut.bindAsEventListener( this ) );
    }.bind(this) );
  },
  onMouseOver: function( event ) {
    if( this.isActive ) return;
    this.isActive = true;
    this.options.elements.each( function( element ) {
      $(element.id).className += ' hoverable_on';
    }); 
  },
  onMouseOut: function( event ) {
    this.isActive = false;
    this.hoverOff();
    //setTimeout( this.hoverOff.bindAsEventListener(this), 100 );
  },
  hoverOff: function() {
    if( this.isActive ) return;
    this.options.elements.each( function( element ) { 
      $(element.id).className = $(element.id).className.replace(' hoverable_on', '', 'g'); 
    });
  }
};


var Swipe = Class.create();
Swipe.prototype = {
  initialize: function( element, image, title, link, offset ) {
    this.element = $(element);
    this.image = image;
    this.title = title;
    this.link = link;
    this.offset = offset;
    window.currentProject = link;
    this.start();
  },
  start: function() {
    this.setup();
    new Effect.SlideDown( this.clone, { duration: .5, afterFinish: this.finished.bindAsEventListener(this) } );
  },
  finished: function() {
    this.element.style.background = 'url(' + this.image + ')';
    setTimeout(function() {this.clone.parentNode.removeChild(this.clone)}.bind(this), 500);
  },
  setup: function() {
    if($("currentProjectTitle")) $("currentProjectTitle").innerHTML = this.title;
    this.clone = this.element.cloneNode( true );
    if(this.element.offsetTop)
      this.clone.style.top = this.element.offsetTop + 'px';
    this.clone.style.height = this.element.offsetHeight + 'px';
    var spacer = document.createElement( "div" );
    spacer.style.height = this.element.offsetHeight + 'px';
    this.clone.appendChild( spacer );
    Object.extend( this.clone.style, {
      background: 'url(' + this.image + ')',
      position:   'absolute',
      display:    'none'
    });
    this.element.parentNode.insertBefore( this.clone, this.element );
  }
};

function projectDetails() {
  if(window.currentProject) document.location.href='/portfolio/' + window.currentProject;
}

function scrollToStaffMember(id) {
  $('internalContent').scrollTop = ($('staff_' + id).offsetTop - $('internalContent').offsetTop);
}

function scrollToActiveLink() {
  $('internalSidebar').scrollTop = ($('activeLink').offsetTop - $('internalSidebar').offsetTop);
}