$(function(){
  $("abbr.timeago").timeago();
  if(document.getElementById('gallery')) { homeGallery(); }
  if(document.getElementById('crossFadeGallery')) { crossFadeGallery(); }
  if(document.getElementById('projectGallery')) { projectGallery(); }
  if(document.getElementById('showHide')) { jobAccordion(); }
  if(document.getElementById('projectWindow')) { setSlider(); }
  if(document.getElementById('blogSearchButton')) { blogSearch(); }
  if(document.getElementById('tweetList')) { tweetScroll(); }
  if(document.getElementById('error404')) { setFace(); }
  contactInquiryApplicant();
  if(document.getElementById('postCommentForm')) { postComment(); }
  if(document.getElementById('sendContactForm')) { sendContact(); }
  
  $('input.placeholder, textarea.placeholder').placeholder();
  
  if(getCookie('mobileSwitch') == "full") {
    $('#switchMobile').show();
  }
});

window.onload = function() {
  createBW();
  if(document.getElementById('projectWindow') || document.getElementById('projectBlock')){ projectThumbs(); }
};

$.fn.placeholder = function(defaultValue) {
  return this.each(function() {
    var val = this.defaultValue;
    if(defaultValue) { val = defaultValue; }
    if(this.value != val) {
      $(this).removeClass('placeholder').addClass('userinput');
    }
    $(this).focus(function() {
      if(val == this.value) {
        this.value = "";
      }
      $(this).removeClass('placeholder').addClass('userinput');
    });
    $(this).blur(function() {
      if(this.value.replace(/\s/g,"").length < 1) {
        this.value = val;
        $(this).addClass('placeholder').removeClass('userinput');
      }
    });
  });
};

function setSlider(){
  var slider = $('ul.projects'),
  li = slider.find('li'),
  prev = $('a.slideLeft'),
  next = $('a.slideRight'),
  pos = -11,
  max = ((li.length-3) * 325)+11,
  newPos,
  inMotion = false;
  
  next.click(function(e){
    e.preventDefault();
    if(pos == -max || inMotion){
      return;
    }
    inMotion = true;
    newPos = pos - 325;
    slider.animate({left: newPos}, function(){
      pos = newPos;
      if(pos == -max){
        next.addClass('off');
      }
      else {
        prev.removeClass('off');
      }
      inMotion = false;
    });
  });
  
  prev.click(function(e){
    e.preventDefault();
    if(pos == -11 || inMotion){
      return;
    }
    inMotion = true;
    newPos = pos + 325;
    slider.animate({left: newPos}, function(){
      pos = newPos;
      if(pos == -11){
        prev.addClass('off');
      }
      else {
        next.removeClass('off');
      }
      inMotion = false;
    });
  });
  
}

function projectGallery(){
  var panel = $('ul.galleryWindow'),
  next = $('div.projectGallery a.next'),
  prev = $('div.projectGallery a.prev'),
  slides = panel.find('li.slide'),
  count = slides.length,
  inMotion = false,
  start = -956,
  endPoint = start + 574,
  end = -(((count+2) * 574) - 192),
  pos = start,
  dist = 574,
  on = 0,
  controlUL = $('ul.galleryControls'),
  controls,
  controlStr = '',
  popoutDiv = $('div.popout'),
  popImg,
  timer,
  imgAnchors;
  
  // cloning nodes for 'infinite' looping effect
  slides.eq(0).clone().appendTo(panel);
  slides.eq(1).clone().appendTo(panel);
  
  slides.eq(-1).clone().prependTo(panel);
  slides.eq(-2).clone().prependTo(panel);
  
  imgAnchors = panel.find('li.slide').find('img');
  
  panel.css('left', start);
  
  slides.each(function(i){
    var li = '<li><a href="#" title="Slide ' + (i+1) + '"></a></li>';
    controlStr += li;
  });
  controlUL.html(controlStr);
    
  controls = controlUL.find('a');
  controls.eq(on).addClass('selected');
  
  popOutImage();
  
  timer = setInterval(function(){
    if(!inMotion){
      closeImage(moveToNext);
    }
  }, 7000);
  
  imgAnchors.click(function(e){
    e.preventDefault();
    var imgClicked = $.inArray(this, imgAnchors);
    clearInterval(timer);
    if(imgClicked > (on+2)){
      if(!inMotion){
        closeImage(moveToNext);
      }
    }
    if(imgClicked < (on+2)){
      if(!inMotion){
        closeImage(moveToPrev);
      }
    }
  });
  
  next.click(function(e){
    e.preventDefault();
    clearInterval(timer);
    if(!inMotion){
      closeImage(moveToNext);
    }
  });
  prev.click(function(e){
    e.preventDefault();
    clearInterval(timer);
    if(!inMotion){
      closeImage(moveToPrev);
    }
  });
  controls.click(function(e){
    e.preventDefault();
    clearInterval(timer);
    var clicked = $.inArray(this, controls);
    if(clicked != on && !inMotion){
      closeImage(goToSlide, clicked);
    }
  });
  
  function goToSlide(thisSlide){
    if(!inMotion){
      inMotion = true;
      newPos = start - (thisSlide * 574);
      panel.animate({left: newPos}, function(){
        pos = newPos;
        on = thisSlide;
        setIndicator();
        popOutImage();
        inMotion = false;
      });
    }
  }
  
  function moveToNext(){
    if(!inMotion){
      inMotion = true;
      newPos = pos - dist;
      panel.animate({left: newPos}, function(){
        if(newPos == end){
          panel.css('left',start);
          pos = start;
          on = 0;
        }
        else {
          pos = newPos;
          on++;
        }
        setIndicator();
        popOutImage();
      });
    }
  }
  
  function moveToPrev(){
    if(!inMotion){
      inMotion = true;
      newPos = pos + dist;
      panel.animate({left: newPos}, function(){
        if(newPos == endPoint){
          panel.css('left', end+574);
          pos = end+574;
          on = count-1;
        }
        else {
          pos = newPos;
          on--;
        }
        setIndicator();
        popOutImage();
      });
    }
  }
  
  function setIndicator(){
    controls.removeClass('selected').eq(on).addClass('selected');
  }
  
  function popOutImage(){
    var html = slides.eq(on).html();
    popoutDiv.html(html);
    popImg = popoutDiv.find('img');
    popImg.animate({
      height: 402,
      width: 608
    }, 'fast');
    popoutDiv.animate({
      left: 176,
      top: 16
    }, 'fast', function(){
      inMotion = false;
    });
  }
  
  function closeImage(callback, params){
    popImg.animate({
      height: 285,
      width: 432
    }, 'fast');
    popoutDiv.animate({
      left: 263,
      top: 62
    }, 'fast', function(){
      popoutDiv.html('');
      callback(params);
    });
  }
  
}

function setFace(){
  var span = $('h1.yikes span');
  
  $('a').hover(
    function(){span.addClass('hover');},
    function(){span.removeClass('hover');}
  );
}

function crossFadeGallery(){
  var slides = $('li.slide div.galleryImg'),
  current = 0,
  next = 1,
  count = slides.length;
  
  slides.eq(current).css({
    opacity: 1,
    zIndex: 1
  });
  slides.eq(current)[0].style.filter = '';
  
  setInterval(function(){
    slides.eq(next).css('z-index', 2).animate({opacity: 1}, function(){
      $(this)[0].style.filter = '';
    });
    slides.eq(current).animate({opacity: 0}, function(){
      $(this).css('z-index', 1);
    });
    current = next;
    next++;
    if(next == count){ next = 0;}
  }, 5000);
}

function projectThumbs(){
  $('ul.projects span.projectThumb img').each(function(){
    setBW(this, 298, 153);
  });
}

function jobAccordion(){
  var a = $('ul.showHide h2 a'),
  details = $('ul.showHide div.showDetails'),
  clicked;
  
  //have to set heights to fix jumping bug.
  function setHeights(){
    details.each(function(){
      var _$this = $(this),
          _display = _$this.css('display'),
          _visible = _$this.css('visibility');
      _$this.css({
        display: 'block',
        visibility: 'hidden',
        height: 'auto'
      });
      _$this.css('height', _$this.height()+1);
      _$this.css({
        display: _display,
        visibility: _visible
      });
    });
  }
  setHeights();
  
  a.click(function(e){
    e.preventDefault();
    clicked = $.inArray(this, a);
    if(!$(this).hasClass('open')){
      $(this).addClass('open');
      details.eq(clicked).slideDown();
    }
    else {
      $(this).removeClass('open');
      details.eq(clicked).slideUp();
    }
  });
}

function createBW(){
  //need to wait for images to load before being able to create BW version
  $('a.bwHover img').each(function(){
    var $this = $(this);
    setBW(this, $this.width(), $this.height());
  });
}

// function will insert a black/white canvas version of the image passed in right after the image in the DOM
// code below is pulled/modified from:
// http://net.tutsplus.com/tutorials/javascript-ajax/how-to-transition-an-image-from-bw-to-color-with-canvas/
var setBW = function(s, wid, hei){
  if(!!document.createElement('canvas').getContext){
    var canvas = document.createElement('canvas'),
        ctx,
        imageData, 
        px, 
        len, 
        i=0, 
        gray;
    
    canvas.width = wid;
    canvas.height = hei;
    $(canvas).insertAfter($(s));
    ctx = $(s).parent().find('canvas')[0].getContext('2d');
    
    ctx.drawImage(s, 0, 0);
    
    imageData = ctx.getImageData(0, 0, wid, hei);
    px = imageData.data;
    len = px.length;
    
    for( ; i<len; i+= 4){
      gray = px[i] * .3 + px[i+1] * .59 + px[i+2] * .11;
      px[i] = px[i+1] = px[i+2] = gray;
    }
    ctx.putImageData(imageData, 0, 0);
  }
};

function tweetScroll(){
  var ul = $('#tweetList'),
  li = ul.find('li'),
  up = $('div.tweetControls a.up'),
  down = $('div.tweetControls a.down'),
  count = li.length,
  inMotion = false,
  max = (count+1) * -44,
  min2 = count * -44,
  min = -44,
  dist = -44,
  timer = null;
  
  if(count > 1){
    
    li.eq(0).clone().appendTo(ul);
    li.eq(count-1).clone().insertBefore(li.eq(0));
    
    ul.css('top', min);
    
    timer = setInterval(function(){
      moveDown();
    }, 5000);
    
    down.click(function(e){
      clearInterval(timer);
      e.preventDefault();
      moveDown();
    });
    
    up.click(function(e){
      clearInterval(timer);
      e.preventDefault();
      moveUp();
    });
    
    ul.find('span.tweetText').each(function() {
      this.innerHTML = this.innerHTML.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
        return '<a href="'+url+'" target="_blank">'+url+'</a>';
      }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
        return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
      });
    });
  }
    
  function moveDown(){
    if(!inMotion){
      inMotion = true;
      dist -= 44;
      ul.animate({top: dist}, function(){
        if(dist == max){
          dist = min;
          ul.css('top', min);
        }
        inMotion = false;
      });
    }
  }
  
  function moveUp(){
    if(!inMotion){
      inMotion = true;
      dist += 44;
      ul.animate({top: dist}, function(){
        if(dist == 0){
          dist = min2;
          ul.css('top', min2);
        }
        inMotion = false;
      });
    }
  }

  
}

function homeGallery(){
  var gal = $('div.gallery'),
  prevButton = gal.find('a.prev'),
  nextButton = gal.find('a.next'),
  slides = gal.find('li.slide'),
  imgs = gal.find('div.galleryImg'),
  text = gal.find('div.textContent'),
  controlUL = $('ul.galleryControls'),
  controls = null,
  current = 0,
  next = 1,
  inMotion = false,
  controlStr = '',
  timer;
  
  if(slides.length > 1){
    
    slides.css({
      opacity: 0,
      left: 960
    });
    text.css('opacity', 0);
    
    slides.eq(current).css({
      opacity: 1,
      left: 0
    });
    text.eq(current).css('opacity', 1);
    
    // For IE to remove filter 
    if(text.length){
      text.eq(current)[0].style.filter = '';
      slides.eq(current)[0].style.filter = '';
    }
    
    slides.each(function(i){
      var li = '<li><a href="#" title="Slide ' + (i+1) + '"></a></li>';
      controlStr += li;
    });
    controlUL.html(controlStr);
    
    controls = controlUL.find('a');
    controls.eq(current).addClass('selected');
    
    timer = setInterval(function(){
      slideNext();
    }, 7000);
    
    controls.click(function(e){
      clearInterval(timer);
      e.preventDefault();
      var clicked = $.inArray(this, controls);
      if(clicked > current){
        slideNext(clicked);
      }
      if(clicked < current){
        slidePrev(clicked);
      }
    });
    
    nextButton.click(function(e){
      clearInterval(timer);
      e.preventDefault();
      if(!inMotion){
        slideNext();
      }
    });
    
    prevButton.click(function(e){
        clearInterval(timer);
        e.preventDefault();
        if(!inMotion){
          slidePrev();
        }
    });
    
    gal.find('a.seeWorkButton').click(function(e) {
      e.preventDefault();
      window.location = slides.eq(current).find('a').attr('href');
    });
    
  }
  else {
    prevButton.add(nextButton).css({
      cursor: 'default',
      visibility: 'hidden'
    });
  }
  
  function slideNext(nextSlide){
    inMotion = true;
    slides.eq(current).animate({left: -960, opacity: 0});
    current++;
    if(current == slides.length){
      current = 0;
    }
    current = nextSlide || current;
    slides.eq(current).css({
      opacity: 0,
      left: 960
    });
    controls.removeClass('selected').eq(current).addClass('selected');
    text.eq(current).css('opacity', 0);
    slides.eq(current).animate({left: 0, opacity: 1}, function(){
      $(this)[0].style.filter = ''; // for IE    
      text.eq(current).animate({opacity: 1}, function(){
        $(this)[0].style.filter = ''; // for IE
      });
      inMotion = false;
    });
  }
  
  function slidePrev(nextSlide){
    inMotion = true;
    slides.eq(current).animate({left: 960, opacity: 0});
    current--;
    if(current == -1){
      current = slides.length - 1;
    }
    if(nextSlide == 0){
      current = 0;
    }
    else {    
      current = nextSlide || current;
    }
    slides.eq(current).css({
      opacity: 0,
      left: -960
    });
    controls.removeClass('selected').eq(current).addClass('selected');
    text.eq(current).css('opacity', 0);
    slides.eq(current).animate({left: 0, opacity: 1}, function(){
      $(this)[0].style.filter = ''; // for IE    
      text.eq(current).animate({opacity: 1}, function(){
        $(this)[0].style.filter = ''; // for IE
      });
      inMotion = false;
    });
  }
  
}

function contactInquiryApplicant() {
  var sendContactInquiry = $(document.getElementById('sendContactInquiry')),
    sendContactApplicant = $(document.getElementById('sendContactApplicant'));
  
  if (!sendContactInquiry.length || !sendContactApplicant.length) { return false; }

  sendContactInquiry.change(function () {
    var selectedText = sendContactInquiry.find('option:selected').text().replace(/^\s+/, '').replace(/\s+$/, '');
    if (selectedText === 'Job Inquiry') {
      sendContactApplicant.show();
    } else {
      sendContactApplicant.hide();
    }
  });
}

function postComment() {
  var postCommentForm = $(document.getElementById('postCommentForm')),
    postCommentSuccess = $(document.getElementById('postCommentSuccess')),
    postCommentError = $(document.getElementById('postCommentError')),
    postCommentPostID = $(document.getElementById('postCommentPostID')),
    postCommentName = $(document.getElementById('postCommentName')),
    postCommentNameDefault = postCommentName.val(),
    postCommentEmail = $(document.getElementById('postCommentEmail')),
    postCommentEmailDefault = postCommentEmail.val(),
    postCommentWebsite = $(document.getElementById('postCommentWebsite')),
    postCommentWebsiteDefault = postCommentWebsite.val(),
    postCommentComment = $(document.getElementById('postCommentComment')),
    postCommentCommentDefault = postCommentComment.val(),
    postCommentSubmit = $(document.getElementById('postCommentSubmit'));

  if (!postCommentForm.length || !postCommentSuccess.length ||
      !postCommentError.length || !postCommentPostID.length ||
      !postCommentName.length || !postCommentEmail.length ||
      !postCommentWebsite.length || !postCommentComment.length ||
      !postCommentSubmit.length) {
    return false;
  }

  postCommentSubmit.click(function (e) {
    e.preventDefault();
    e.stopPropagation();

    postCommentError.hide();

    var postID = postCommentPostID.val(),
      name = postCommentName.val(),
      email = postCommentEmail.val(),
      website = postCommentWebsite.val(),
      comment = postCommentComment.val(),
      captchaChallenge = $('#recaptcha_challenge_field').val(),
      captchaResponse = $('#recaptcha_response_field').val(),
      errors = [],
      valid = true;

    if (!name.length || name === postCommentNameDefault) {
      valid = false;
      errors.push('Your name is required.');
    }

    if (!email.length || email === postCommentEmailDefault) {
      valid = false;
      errors.push('Your email is required.');
    } else if (email.Length < 6 || email.indexOf('@') == -1 || email.indexOf('.') == -1) {
      valid = false;
      errors.push('Your email is invalid.');
    }

    if (website === postCommentWebsiteDefault) {
      website = '';
    }

    if (!comment.length || comment === postCommentCommentDefault) {
      valid = false;
      errors.push('Your comment is required.');
    }
    
    if(!captchaResponse.length) {
      valid = false;
      errors.push('Captcha is required.');
    }

    if (valid) {
      $.ajax({
        url: '/Base/Comment/Create.aspx',
        data: {
          postID: postID,
          name: name,
          email: email,
          website: website,
          comment: comment,
          captchaChallenge: captchaChallenge,
          captchaResponse: captchaResponse
        },
        success: function (data) {
          if (data && data.length) {
            postCommentError.text(data.split(',').join(' ')).show();
          } else {
            postCommentForm.hide();
            postCommentSuccess.show();
          }
        },
        error: function () {
          postCommentError.text('An error occurred.').show();
        }
      });
    } else {
      postCommentError.text(errors.join(' ')).show();
    }

    return false;
  });
}

function sendContact() {
  var sendContactForm = $(document.getElementById('sendContactForm')),
    sendContactSuccess = $(document.getElementById('sendContactSuccess')),
    sendContactError = $(document.getElementById('sendContactError')),
    sendContactName = $(document.getElementById('sendContactName')),
    sendContactNameDefault = sendContactName.val(),
    sendContactEmail = $(document.getElementById('sendContactEmail')),
    sendContactEmailDefault = sendContactEmail.val(),
    sendContactInquiry = $(document.getElementById('sendContactInquiry')),
    sendContactPosition = $(document.getElementById('sendContactPosition')),
    sendContactMessage = $(document.getElementById('sendContactMessage')),
    sendContactMessageDefault = sendContactMessage.val(),
    sendContactSubmit = $(document.getElementById('sendContactSubmit'));

  if (!sendContactForm.length || !sendContactSuccess.length ||
      !sendContactError.length || !sendContactName.length ||
      !sendContactEmail.length || !sendContactInquiry.length ||
      !sendContactPosition.length || !sendContactMessage.length ||
      !sendContactSubmit.length) {
    return false;
  }

  sendContactSubmit.click(function (e) {
    e.preventDefault();
    e.stopPropagation();

    sendContactError.hide();

    var name = sendContactName.val(),
      email = sendContactEmail.val(),
      inquiry = sendContactInquiry.val(),
      position = sendContactPosition.val(),
      message = sendContactMessage.val(),
      errors = [],
      captchaChallenge = $('#recaptcha_challenge_field').val(),
      captchaResponse = $('#recaptcha_response_field').val(),
      valid = true;
    
    if (!name.length || name === sendContactNameDefault) {
      valid = false;
      errors.push('Your name is required.');
    }
    
    if (!email.length || email === sendContactEmailDefault) {
      valid = false;
      errors.push('Your email is required.');
    } else if (email.Length < 6 || email.indexOf('@') == -1 || email.indexOf('.') == -1) {
      valid = false;
      errors.push('Your email is invalid.');
    }

    if (!message.length || message === sendContactMessageDefault) {
      valid = false;
      errors.push('Your message is required.');
    }
    
    if(!captchaResponse.length) {
      valid = false;
      errors.push('Captcha is required.');
    }

    if (valid) {
      $.ajax({
        url: '/Base/Contact/Send.aspx',
        data: {
          name: name,
          email: email,
          inquiry: inquiry,
          position: position,
          message: message,
          captchaChallenge: captchaChallenge,
          captchaResponse: captchaResponse
        },
        success: function (data) {
          if (data && data.length) {
            sendContactError.text(data.split(',').join(' ')).show();
          } else {
            sendContactForm.hide();
            sendContactSuccess.show();
          }
        },
        error: function () {
          sendContactError.text('An error occurred.').show();
        }
      });
    } else {
      sendContactError.text(errors.join(' ')).show();
    }

    return false;
  });
}

function blogSearch() {
  var $bsButton = $('#blogSearchButton'),
      $bsInput = $('#blogSearchInput');
  
  $bsButton.click(function(e) {
    e.preventDefault();
    searchBlogs();
  });
  $bsInput.keypress(function(e) {
    if(e.which == 13) {
      e.preventDefault();
      searchBlogs();
    }
  });
  
  function searchBlogs() {
    var sValue = $.trim($bsInput.val());
    if(sValue.length > 0) {
      window.location = $bsButton.attr('href') + "?search=" + sValue;
    }
  }
}

function getCookie( name ) {
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
    return null;
  }
  if ( start == -1 ) return null;
  var end = document.cookie.indexOf( ';', len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}



jQuery.extend( jQuery.easing,
{
  easeInQuad: function (x, t, b, c, d) {
    return c*(t/=d)*t + b;
  },
  easeOutQuad: function (x, t, b, c, d) {
    return -c *(t/=d)*(t-2) + b;
  },
  easeInOutQuad: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t + b;
    return -c/2 * ((--t)*(t-2) - 1) + b;
  },
  easeInCubic: function (x, t, b, c, d) {
    return c*(t/=d)*t*t + b;
  },
  easeOutCubic: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t + 1) + b;
  },
  easeInOutCubic: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t + b;
    return c/2*((t-=2)*t*t + 2) + b;
  },
  easeInQuart: function (x, t, b, c, d) {
    return c*(t/=d)*t*t*t + b;
  },
  easeOutQuart: function (x, t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
  },
  easeInOutQuart: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
    return -c/2 * ((t-=2)*t*t*t - 2) + b;
  },
  easeInQuint: function (x, t, b, c, d) {
    return c*(t/=d)*t*t*t*t + b;
  },
  easeOutQuint: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t*t*t + 1) + b;
  },
  easeInOutQuint: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
    return c/2*((t-=2)*t*t*t*t + 2) + b;
  },
  easeInSine: function (x, t, b, c, d) {
    return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  },
  easeOutSine: function (x, t, b, c, d) {
    return c * Math.sin(t/d * (Math.PI/2)) + b;
  },
  easeInOutSine: function (x, t, b, c, d) {
    return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  },
  easeInExpo: function (x, t, b, c, d) {
    return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  },
  easeOutExpo: function (x, t, b, c, d) {
    return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  },
  easeInOutExpo: function (x, t, b, c, d) {
    if (t==0) return b;
    if (t==d) return b+c;
    if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
    return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
  },
  easeInCirc: function (x, t, b, c, d) {
    return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
  },
  easeOutCirc: function (x, t, b, c, d) {
    return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
  },
  easeInOutCirc: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
    return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  },
  easeInElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  },
  easeOutElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
  },
  easeInOutElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
    return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
  },
  easeInBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c*(t/=d)*t*((s+1)*t - s) + b;
  },
  easeOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  },
  easeInOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158; 
    if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
  },
  easeInBounce: function (x, t, b, c, d) {
    return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
  },
  easeOutBounce: function (x, t, b, c, d) {
    if ((t/=d) < (1/2.75)) {
      return c*(7.5625*t*t) + b;
    } else if (t < (2/2.75)) {
      return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
    } else if (t < (2.5/2.75)) {
      return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
    } else {
      return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
    }
  },
  easeInOutBounce: function (x, t, b, c, d) {
    if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
    return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
  }
});

(function($) {
  $.fn.enterKey = function(target) {
    var $target = $(target),
      domTarget = $target[0],
      href = $target.attr('href'),
      isA = domTarget.tagName == 'A',
      isInput = domTarget.tagName == 'INPUT',
      isButton = domTarget.tagName == 'BUTTON',
      isForm = domTarget.tagName == 'FORM';
    if (!isA && !isInput && !isButton && !isForm) {
      throw new Error('unsupported target');
    }
    return this.keypress(function(e) {
      if (e.keyCode == 13) {
        e.preventDefault();
        if (isA) {
          if (!domTarget.onclick || domTarget.onclick()) {
            if (href.indexOf('javascript') >= 0) {
              eval(unescape(href.replace(/^javascript:/i,'')));
            } else {
              window.location = href;
            }
          }
        } else if (isInput || isButton) {
          $target.click();
        } else if (isForm) {
          $target.submit();
        }
      }
    });
  };
}(jQuery));
