
// $ ==========

function $(id)
{
  return document.getElementById(id);
}


// Bind ==========

Function.prototype.bind = function(scope /*...*/)
{
  var __method = this;
  var __scope = scope || this;
  var __args = Array.prototype.slice.call(arguments, 1);
  
  return function() {
    __args2 = __args.concat(Array.prototype.slice.call(arguments));
    return __method.apply(__scope, __args2);
  }
}

// Email Bubble ==========

function EmailBubble()
{
  this.toggle = $('link_email')
  this.bubble = $('email_bubble')
  this.hidden = true;
  
  this.toggle.onclick = this.toggleClick.bind(this)
}

EmailBubble.prototype.toggleClick = function(event)
{
  event = event || window.event;
  
  if(this.hidden) {
    this.toggle.className = 'link stick_on';
    Animation(this.bubble).to('opacity', 1).from(0).show().duration(300).go();
    document.onclick = this.toggleClick.bind(this);
    this.hidden = false;
  } else {
    Animation(this.bubble).to('opacity', 0).from(1).hide().duration(300).go();
    this.toggle.className = 'link';
    document.onclick = null;
    this.hidden = true;
  }
  
	event.cancelBubble = true;
	if (event.stopPropagation) event.stopPropagation();
}


// SlideAnimation ==========

function SlideAnimation(container, slideImages, interval, initial_delay)
{
  this.box    = $(container)
  this.slides = [];
  this.mark   = 0;
  this.gap    = interval || 5000;
  
  // Load Images
  for (var i=0; i< slideImages.length; i++) {
    this.slides[i] = new Image();
    this.slides[i].src = 'images/' + slideImages[i];
  }
  
  // Insert Shim Div
  this.shim = document.createElement('div');
  this.shim.style.backgroundPosition = 'bottom left';
  this.box.appendChild(this.shim);
  
  // Run Animation
  initial_delay = initial_delay || 0;
  setTimeout(this.nextSlide.bind(this), initial_delay);
}

SlideAnimation.prototype.nextSlide = function()
{
  var previous = this.mark;
  
  this.mark++;
  if(this.mark >= this.slides.length)
    this.mark = 0;
  
  Animation(this.shim).to('height', '0px').from('55px').ease(Animation.ease.begin).duration(1000).go();
  this.shim.style.backgroundImage = 'url(\'' + this.slides[previous].src + '\')';
  this.shim.style.width = this.box.offsetWidth+'px';
  this.shim.style.height = this.box.offsetHeight+'px';
  this.box.style.backgroundImage = 'url(\'' + this.slides[this.mark].src + '\')';
  
  setTimeout(this.nextSlide.bind(this), this.gap);
}

