$(document).ready(function() {
  var slideNum = 0;
  var handAnimated = false;
  var scrollLimit = 50;
  var bookmarkOpen = false;

  calHeight();
  animateHand();
  
  $(window).resize(function(){calHeight();});
  $('#toolbox2 nav2 li img').removeClass('hover');
  $(window).scroll(function(){
    if($(this).scrollLeft() > scrollLimit) {
      stopHand();
      $('#hand_up').fadeOut(100);
    }
    else {
      if(!bookmarkOpen) {
        $('#hand_up:hidden').fadeIn(100);
        animateHand();
      }
    }
  });
  
  function animateHand() {
    var alt = true;
    if (!handAnimated) {
      handAnimated = true;
      function handMove(posToMove) {
        $('#hand_up').animate({bottom: posToMove}, 400, function(){
          if(handAnimated) {
            alt = !alt;
            if(alt) {
              handMove('-=20');
            } else {
              handMove('+=20');
            }
          }
        });
      }
      handMove('-=20');
    }
  }
  
  function stopHand() {
    handAnimated = false;
    $('#hand_up').css('bottom','0px');
  }
  
  $('#toolbox2 nav2 li').click(function(e){
    //e.preventDefault();
    showTool($(this).attr('id'));
  });
  
  $('#toolbox2 nav2 li img').hover(function(e){
    $(this).addClass('hover');
  }).mouseout(function() {
    $(this).removeClass('hover');
  });
  
  $('#index li').click(function(){
    showTool($(this).attr('class'));
  });
  
  $('.close').click(function(){
    $(this).parent('article').hide();
    if ($(this).parent('article').attr('id') == 'toolcont') {
      $('#index li').removeClass('active');
    }
  });
  
  $('#work nav li').click(function(e) {
    e.preventDefault();
    showWork($(this).attr('class'));
  });
  
  $('#work nav li img').hover(function(){
    $(this).addClass('hover');
  }).mouseout(function() {
    $(this).removeClass('hover');
  })
  
  $('#tool .work div').live('click',function() {
    showWork($(this).children('img').attr('class'));
  });
  
  $('.wtools img').live('click',function(){
    showTool($(this).attr('class'));
  });
  
  $('#bookmark li').click(function(e){
    e.preventDefault();
    scrollTo('#' + $(this).attr('class'),false);
  });
  
  $('#bookmark').hover(function(){
    $(this).animate({top: 0},100);
    $('#hand_up').fadeOut(100, function(){stopHand();});
    bookmarkOpen = true;
  }).mouseleave(function(){
    $(this).animate({top: -143},100);
    bookmarkOpen = false;
    if ($(window).scrollLeft() < scrollLimit) {
      $('#hand_up').fadeIn(100);
      animateHand();
    }
  });
  
  $('.slidenav. span, .wprev, .wnext, .slides img').live('click', function() {
    var newSlideNum = $(this).text() - 1;
    var picNum = $(this).closest('.slides').children('img, iframe').length;
    if ($(this).hasClass('wprev')) {
      newSlideNum = slideNum == 0 ? picNum - 1 : slideNum - 1;
    }
    else if ($(this).hasClass('wnext') || $(this).is('.slides img')) {
      newSlideNum = slideNum == picNum -1 ? 0 : slideNum + 1;
    }
    var pictures = $(this).closest('.slides').children('img, iframe').hide().eq(newSlideNum).show();
    $('.slidenav span').removeClass('active').eq(newSlideNum).addClass('active');
    slideNum = newSlideNum;
  });
  
  $('#fr').click(function(){ filterTools('fr'); });
  $('#fc').click(function(){ filterTools('fc'); });
  $('#fa').click(function(){ filterTools('fa'); });
  
  function showTool(tool_to_show) {
    scrollTo('#toolbox2', true);
    $('#tool').html('<p class="ajax_status">Loading...</p>');
    $('#tool').load('tools/' + tool_to_show + '.php');
    $('#index li').removeClass('active');
    $('#index li.' + tool_to_show).addClass('active');
  }
  
  function showWork(work_to_show) {
    resetSlideshow();
    scrollTo('#work', true);
    $('#workitem').html('<p class="ajax_status">Loading...</p>');
    $('#workitem').load('work/' + work_to_show + '.html');
  }
  
  function filterTools(htclass) {
    $('#toolbox2 nav2 li').fadeTo(0,1);
    if (htclass != 'fa') {
      $('#toolbox2 nav2 li:not(.' + htclass + ')').fadeTo(0,0.5);
    }
    $('#filter li').css('text-decoration','none');
    $('#' + htclass).css('text-decoration','underline');
  }
  
  function calHeight() {
    var width1 = ($('#intro .container').outerWidth(true) > $(window).width()) ? $('#intro .container').outerWidth(true) + 50 : $(window).width();
    var width2 = ($('#toolbox2 .container').outerWidth(true) > $(window).width()) ? $('#toolbox2 .container').outerWidth(true) + 50 : $(window).width();
    var width3 = 0;
    if($('body').hasClass('work')) {
      width3 = ($('#work .container').outerWidth(true) > $(window).width()) ? $('#work .container').outerWidth(true) + 50 : $(window).width();
    }
    var width4 = ($('#agency .container').outerWidth(true) > $(window).width()) ? $('#agency .container').outerWidth(true) + 50 : $(window).width();
    var width5 = ($('#about .container').outerWidth(true) > $(window).width()) ? $('#about .container').outerWidth(true) + 50 : $(window).width();
    $('#toolbox2').css('left', width1 + 'px');
    $('#work').css('left',(width1 + width2) + 'px');
    $('#agency').css('left',(width1 + width2 + width3) + 'px');
    $('#about').css('left',(width1 + width2 + width3 + width4) + 'px');
    
    $(window).height() < 665 ? $('#index').hide() : $('#index').show();
  }
  function resetSlideshow() {
    slideNum = 0;
    $('.slides img').hide().eq(slideNum).show();
  }
  
  function scrollTo(id_to_scroll, appear) {
    targetOffset = $(id_to_scroll).offset().left;
    $("html,body").animate({scrollLeft: targetOffset},500, function(){
      if (appear) {
        switch(id_to_scroll) {
          case '#work':
            $('#workcont').fadeIn();
            break;
          case '#toolbox2':
              $('#toolcont').fadeIn();
            break;
          }
        }
    });
  }

});
