// Disable caching of AJAX responses
// $.ajaxSetup ({ cache: false });
ripple_class = function(will_add, span) {
  if(will_add) span.addClass('appear');
  else span.removeClass('appear');
  setTimeout(function(){
    var next_span = span.next();
    if(next_span.length !== 0) ripple_class(will_add, next_span);
  }, 100);
}


// /*===  onload  =================================================================*/
$(function(){
  window.load_project_from_hash = (function(){
    var $next_link = $('#work-next'),
        $prev_link = $('#work-prev'),
        $thumbs = $('.work-thumbs > a')
        
    return function (hash, scroll_to_proj) {
      var proj_name = hash.replace(/#work-/, ''),
          $proj_thumb = $("#" + proj_name);
          $('#work-nav').fadeIn();

      $proj_thumb 
        .addClass('active')
        .siblings()
        .removeClass('active');

      var full_url = '_includes/work/work.php?project=' + proj_name;
      if (scroll_to_proj) $.scrollTo($('#work'), 400);

      $.get(full_url, function(data) {
        var $inject = $(data);
        setTimeout(function(){

          /* Inject new links */
          if ($proj_thumb.next().length) {
            $next_link.attr('href', $proj_thumb.next().attr('href'));
          } else {
            $next_link.attr('href', $thumbs.first().attr('href'));
          }

          if ($proj_thumb.prev().length) {
            $prev_link.attr('href', $proj_thumb.prev().attr('href'));
          } else {
            $prev_link.attr('href', $thumbs.last().attr('href'));
          }

          new ImageFlip({
            div: $('.slider', $inject),
            exclude: ['.vjs-play-control', '.vjs-big-play-button', '.vjs-button'],
            width: 960,
            height: 540
          });
        }, 50);
        $('#work-content').trigger('inject_html', [ $inject ]);
        VideoJS.setupAllWhenReady();
      })
    }
  })()

  var win_hash = window.location.hash;
  if (window.location.hash.substr(1, 5) === 'work-') {
    load_project_from_hash(window.location.hash, true);
  } else {
    if (win_hash !== '') {
      setTimeout(function() {
        $.scrollTo($(window.location.hash), 400);
      }, 400);
    }
  }

  $.localScroll({
    target: 'body', 
    hash: true,
    duration: 800,
    onBefore: function(obj, element){         
      $('nav ul li').removeClass('active');
    }
  });
  $('nav ul li').localScroll({
    onBefore: function(obj, element){         
      var active = '#' + $(element).attr('id');
    }
  });

  //Checking active state while scrolling
  $(window).scroll(function() {
    var windowPos = $(this).scrollTop();
    $('nav ul li a').each(function() {
      var href = $(this).attr('href');
      var sectionPos = $(href).offset().top - windowPos;
      if (sectionPos < 10 && windowPos > 100) {
        $('nav ul li').removeClass('active');
        $(this).parent('li').addClass('active');
      } else if (windowPos <= 270) { 
        $('nav ul li').removeClass('active');
      }
    });
  });

  var underline_phrases = [
	'Interactive Addicts. ',
    'Brand Junkies. ',
    'Creative Monsters. ',
    'Design Fanatics. ',
	''
  ]

  for (var i=0; i < underline_phrases.length; i++) {
    var $appear = $("<p class='appearing'></p>");
    $('#appearing_text').prepend($appear);
    $appear.text(underline_phrases[i]).lettering();
    
    $('span', $appear).each(function(i, item) {
      setTimeout(function() {
        if ($(item).length && $(item).text() === ' ') $(item).html("&nbsp;");
      }, 50);
    });
  };

  swap_phrase = function(index) {
    $current = $phrases.eq(index);
    // if (index === underline_phrases.length) index = 0;
	index++;
	if (index === underline_phrases.length) return;
    $next = $phrases.eq(index);

    setTimeout(function() {
      ripple_class(false, $('span:first', $current));
    }, 40);
    setTimeout(function() {
      ripple_class(true, $('span:first', $next));
    }, 300);

    setTimeout(function() {
      swap_phrase(index);
    }, 3500)
  }
  
    
  setTimeout(function() {
    $phrases = $('.appearing');
    swap_phrase(0);
  }, 100);

  // Work click
  $('.work-thumbs a, .project-link').live('click', function(clicked) {
    load_project_from_hash(this.hash, true);
  })

  $('#work-content').bind('inject_html', function(event, $inject) {
    var $self = $(this);
    $self.html($inject);
    setTimeout(function() {
      $self.animate({height: $inject.height()}, 300);
    }, 50);
  });

  $.each($('.slider'), function(i, image) {
    new ImageFlip({
      div: $(image),
      exclude: ['.vjs-play-control', '.vjs-big-play-button', '.vjs-button'],
      width: 960,
      height: 540
    });
  })

  getTwitters('tweets', {
    id: 'theprogrampdx', 
    count: 4, 
    withFriends: true,
    enableLinks: true, 
    ignoreReplies: false,
    newWindow: true,
    template: '<p class="">%text%</p> <em class=""><a href="http://twitter.com/%user_screen_name%/statuses/%id%" target="blank">%time%</a></em>'
  });
})

