$(document).ready(function() {
  if($('#slideshow').length > 0)
  {
    $('#pane').scrollable({size: 1, onBeforeSeek: onSlideScrollHandler}).circular().navigator({

      // select #tabs to be used as navigator
      navi: "#tabs",
  
      // select A tags inside the navigator to work as items (not direct children)
      naviItem: 'a',
  
      // assign "current" class name for the active A tag inside navigator
      activeClass: 'current'
    });
    
    $('#tabs').before('<div id="tabBg"><div/></div>');
    adjustTabBg();
    
    // adjust the tab bg when window is resized
    $(window).resize(function() {
      adjustTabBg();
    });
  }
    
  // add the bottom for the smaller stylish blurbs
  $(".stylishBlurb").append('<div class="bottom"/>');
  
  // add span for any button links (ie. contact page)
  $("a.button").wrapInner('<span/>');
  
  // hover action for the nav
  $('#nav div').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });
  
  initClientImages();
  initContactPage();
  initLocalScroll();
});

// called by scrollable plugin before the slides scrolls
// use this to move the tab background
function onSlideScrollHandler(event, index)
{
  adjustTabBg(index);
}

// animate the tab background on the homepage slideshow 
function adjustTabBg(index)
{
  var current;
  if(index == undefined)
    current = $('#tabs a.current');
  else
    current = $($('#tabs a').get(index-1));
    
  $('#tabBg').animate({
    left: current.offset().left - 35,
    width: current.width() + 70
  });
}

// setup the client portfolio page
var startHeight;
var startPos;
function initClientImages()
{
  // only if we're on the work page
  if($("#workContent").length == 0) return false;
  
  // set the height of the middle div so it doesnt shift during animations
  $('#middle').css('height', $('#middle').height());
  
  $('#clientImages img').wTooltip();

  startHeight = $('#workContent').height();
  $("#workContent").append('<div id="clientViewer"/>');
  
  $('#clientImages a').click(function(e) {
    e.preventDefault();
    showClientImage($(this).attr('href'));
  });
}

function showClientImage(id)
{
  // set the height so it doesnt shift while we fade things
  $('#workContent').css('height', startHeight);
  
  // copy the hardcoded div and stick it in the clientViewer div
  var clientDiv = $(id).clone();
  $("#clientViewer").html(clientDiv);
  
  // fade out all the client thumbnails
  $("#clientImagesTitle").fadeOut();
  $("#clientImages").fadeOut(function() {
    
    // make the div invisible but still take up space
    clientDiv.css('opacity', 0);
    clientDiv.show();
    
    // animate the content box up
    startPos = $("#content").offset().top; // set the startPos so we know what to go back to
    $("#content").css({
      'top': startPos,
      'position': 'absolute',
      'z-index': 1000
    });
    
    var top = ($(window).height() / 2) - (clientDiv.height() / 2); // make sure the div is centered vertically
    if(top < 25) 
    {
      top = 25;
    }  
    $("#content").animate({'top': top});
    
    // animate the box height
    $("#workContent").animate({'height': clientDiv.height()}, function() {
      $(document).click(function() {
        hideClientImage();
      });
      
      //clientDiv.css('position', 'static'); // reset position to center image
      // fade in the clientViewer
      clientDiv.animate({'opacity': 1});
      clientDiv.find('a').click(function(e) {
        e.stopPropagation(); // stop the hide event from happening here
        //window.open($(this).find('a').attr('href')); // open the page
      });
    });
  });
}

function hideClientImage()
{
  var clientDiv = $('#clientViewer div');
  clientDiv.fadeOut(function() {
    //move the content box back down
    $("#content").animate({'top': startPos});
    
    //shrink the box back to starting height, show the thumbs, remove the click
    $("#workContent").animate({'height': startHeight}, function() {
      $("#clientImagesTitle").show();
      $("#clientImages").fadeIn();
      $(document).unbind('click');
    });    
  });
}

function initContactPage()
{
  if($("#contactForm").length > 0)
  {
    $('input, textarea').focus(function(){
      $(this).addClass('focus');
    });
    
    $('input, textarea').blur(function(){
      $(this).removeClass('focus');
    });
  }
}

function initLocalScroll() {
  $('a[href*=#]').each(function() {
  if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
  && location.hostname == this.hostname
  && this.hash.replace(/#/,'') ) {
    var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
    var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
     if ($target) {
     var targetOffset = $target.offset().top;
     $(this).click(function() {
       $('html, body').animate({scrollTop: targetOffset}, 600);
       return false;
     });
    }
  }
  });
}