/*
---------------------------------------------------------------------------
Departika jQuery
Author: Jeremy Boles
--------------------------------------------------------------------------- */

$(document).ready(function() {
  
  /* Main Navigation
  ------------------------------------------------------------------------- */
  $('#main-nav li a').each(function(index, link) {
    switch($(this).attr('class')) {
      case 'work':
        var text = 'Our blood, sweat, smiles and tears';
        break;
      case 'services':
        var text = 'How we help our clients succeed';
        break;
      case 'about':
        var text = 'Who the heck are these people';
        break;
      case 'contact':
        var text = 'We would love to hear from you';
        break;
    }
    
    $(this).append('<div class="hover">' + text + '</div>');
  }).hover(function() {
    $(this).find('.hover').animate({ top : -75 }, 250, 'swing');
  }, function() {
    $(this).find('.hover').animate({ top : 0 }, 250, 'swing');
  });
  
  /* Slideshow Racket
  ------------------------------------------------------------------------- */
  var slideshow = $('.slideshow ul.images li')
  slideshow.find('.image, .description h1, .description p').css('opacity', 0);
  $('.slideshow ul.images li:first-child').addClass('current').find('.image, .description h1, .description p').css('opacity', 1);
  
  $('.slideshow iframe, .slideshow object').css('display', 'none');
  
  $('.slideshow').append('<ul class="pages"></ul>');
  $('.slideshow ul.images').each(function(i, list) {
    $(list).find('li').each(function(index, item) {
      $(this).parents('.slideshow').find('ul.pages').append('<li><a href="#">' + (index + 1) + '</a></li>');
    });
  });
  
  $('.slideshow ul.pages li:first-child a').addClass('current');
  $('.slideshow ul.pages a').click(function() {
    window.clearInterval($(this).parents('ul').data('timer'));
    
    if ($(this).parents('ul').find('a.current').html() != $(this).html()) {
      var current = $(this).parents('.slideshow ').find('ul.images li.current');
      current.removeClass('current')
             .find('.image, .description h1, .description p')
             .animate({ 'opacity' : 0 }, 500)
             .find('iframe, object').css('display', 'none');

      var next = $(this).parents('.slideshow ').find('ul.images li:nth-child(' + $(this).html() + ')');
      next.addClass('current');
      next.find('.image').animate({ 'opacity' : 1 }, 500, 'linear', function() {
        $(this).find('iframe, object').css('display', 'block');
      });
      next.find('.description h1, .description p')
          .css('left', -15)
          .each(function(index, el) {
            $(el).delay(index * 75).animate({ 'left' : 0, 'opacity': 1 }, 500, 'swing');
          });

      $(this).parents('ul.pages').find('a').removeClass('current');
      $(this).addClass('current');
    }
    
    if ($(this).parents('.slideshow').hasClass('auto-start')) {
      $(this).parents('ul').data('timer', window.setTimeout(function(self) {
        var siblings = $(self).parents('ul').find('a');
        if (siblings.index(self) == (siblings.size() - 1)) {
          $(siblings.get(0)).click();
        } else {
          $(siblings.get(siblings.index(self) + 1)).click();
        }
      }, 5000, this));
    }
    return false;
  });
  
  $('.slideshow.arrows').each(function(index, slideshow) {
      $(slideshow).append('<ul class="arrows"><li class="prev"><a href="#">Previous</a></li><li class="next"><a href="#">Next</a></li></ul>');
  });
  $('.slideshow.arrows .prev a').click(function() {
    var current = $(this).parents('.slideshow').find('ul.pages a.current');
    var links = $(this).parents('.slideshow').find('ul.pages a');
    if (links.index(current) == 0) {
      $(links.get(links.length - 1)).click();
    } else {
      $(links.get(links.index(current) - 1)).click();
    }
    return false;
  });
  $('.slideshow.arrows .next a').click(function() {
    var current = $(this).parents('.slideshow').find('ul.pages a.current');
    var links = $(this).parents('.slideshow').find('ul.pages a');
    if (links.index(current) == (links.size() - 1)) {
      $(links.get(0)).click();
    } else {
      $(links.get(links.index(current) + 1)).click();
    }
    return false;
  });

  $('.slideshow ul.pages a.current').click();
  
  /* Success Messages
  ------------------------------------------------------------------------- */
  $('.success').delay(3000).animate( { 'opacity' : 0 }, 500, function() { $(this).hide(); });
  
  /* View All Projects on the Work Page
  ------------------------------------------------------------------------- */
  $('#all-projects').click(function() {
    $(this).toggleClass('expanded');
  });
  
  /* Scroll Anchor Links
  ------------------------------------------------------------------------- */
  $('#all-projects a').click(function(e) {
    e.preventDefault()  ;
    var target = $(this).attr('href');
    $('html,body').animate({ scrollTop: $(target).offset().top }, 500, function(){ location.hash = target; });
  });
});
