var themeRoot = '/wp-content/themes/johngrimes/';
var contactCardOpen = false;

jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$.preloadImages(themeRoot + 'img/twitter-follow-active.png',
    themeRoot + 'img/twitter-follow.png',
    themeRoot + 'img/delicious-add-active.png',
    themeRoot + 'img/delicious-add.png',
    themeRoot + 'img/github-follow-active.png',
    themeRoot + 'img/github-follow.png',
    themeRoot + 'img/share-twitter-active.png',
    themeRoot + 'img/share-twitter.png',
    themeRoot + 'img/share-facebook-active.png',
    themeRoot + 'img/share-facebook.png',
    themeRoot + 'img/share-delicious-active.png',
    themeRoot + 'img/share-delicious.png',
    themeRoot + 'img/share-digg-active.png',
    themeRoot + 'img/share-digg.png',
    themeRoot + 'img/share-reddit-active.png',
    themeRoot + 'img/share-reddit.png',
    themeRoot + 'img/share-hackernews-active.png',
    themeRoot + 'img/share-hackernews.png',
    themeRoot + 'img/subscribe-active.png',
    themeRoot + 'img/subscribe.png',
    themeRoot + 'img/comment-post-active.png',
    themeRoot + 'img/comment-post.png'
  );

$(document).ready(function() {
  mouseoverFollowButtons();
  mouseoverPostBars();
  mouseoverShareButtons();
  mouseoverSubscribeButton();
  mouseoverPostCommentButton();

  $('.three-col .post-title').click(function() {
    window.location = $(this).find('.post-link').attr('href');
  });
  $('.post-listing .post-footer').click(function() {
    window.location = $(this).find('.post-link').attr('href');
  });

  $('#contact-me-button, .contact-me-link').click(toggleContactCard);
});

function mouseoverFollowButtons() {
  $('#twitter-follow').mousemove(function() {
    $(this).attr('src', themeRoot + 'img/twitter-follow-active.png');
  });
  $('#twitter-follow').mouseout(function() {
    $(this).attr('src', themeRoot + 'img/twitter-follow.png');
  });
  $('#delicious-add').mousemove(function() {
    $(this).attr('src', themeRoot + 'img/delicious-add-active.png');
  });
  $('#delicious-add').mouseout(function() {
    $(this).attr('src', themeRoot + 'img/delicious-add.png');
  });
  $('#github-follow').mousemove(function() {
    $(this).attr('src', themeRoot + 'img/github-follow-active.png');
  });
  $('#github-follow').mouseout(function() {
    $(this).attr('src', themeRoot + 'img/github-follow.png');
  });
}

function mouseoverPostBars() {
  $('.three-col .post-title, .post-listing .post-footer').mousemove(function() {
    $(this).css('opacity', '0.9');
  });
  $('.three-col .post-title, .post-listing .post-footer').mouseout(function() {
    $(this).css('opacity', '1');
  });
}

function mouseoverShareButtons() {
  $('.share-twitter').mousemove(function() {
    $(this).attr('src', themeRoot + 'img/share-twitter-active.png');
  });
  $('.share-twitter').mouseout(function() {
    $(this).attr('src', themeRoot + 'img/share-twitter.png');
  });
  $('.share-facebook').mousemove(function() {
    $(this).attr('src', themeRoot + 'img/share-facebook-active.png');
  });
  $('.share-facebook').mouseout(function() {
    $(this).attr('src', themeRoot + 'img/share-facebook.png');
  });
  $('.share-delicious').mousemove(function() {
    $(this).attr('src', themeRoot + 'img/share-delicious-active.png');
  });
  $('.share-delicious').mouseout(function() {
    $(this).attr('src', themeRoot + 'img/share-delicious.png');
  });
  $('.share-digg').mousemove(function() {
    $(this).attr('src', themeRoot + 'img/share-digg-active.png');
  });
  $('.share-digg').mouseout(function() {
    $(this).attr('src', themeRoot + 'img/share-digg.png');
  });
  $('.share-reddit').mousemove(function() {
    $(this).attr('src', themeRoot + 'img/share-reddit-active.png');
  });
  $('.share-reddit').mouseout(function() {
    $(this).attr('src', themeRoot + 'img/share-reddit.png');
  });
  $('.share-hackernews').mousemove(function() {
    $(this).attr('src', themeRoot + 'img/share-hackernews-active.png');
  });
  $('.share-hackernews').mouseout(function() {
    $(this).attr('src', themeRoot + 'img/share-hackernews.png');
  });
}

function mouseoverSubscribeButton() {
  $('#subscribe').mousemove(function() {
    $(this).attr('src', themeRoot + 'img/subscribe-active.png');
  });
  $('#subscribe').mouseout(function() {
    $(this).attr('src', themeRoot + 'img/subscribe.png');
  });
}

function mouseoverPostCommentButton() {
  $('#submit').mousemove(function() {
    $(this).css('background', '#E6E6E6 url(' + themeRoot + 'img/comment-post-active.png) no-repeat scroll top left');
  });
  $('#submit').mouseout(function() {
    $(this).css('background', '#E6E6E6 url(' + themeRoot + 'img/comment-post.png) no-repeat scroll top left');
  });
}

function toggleContactCard() {
  if (contactCardOpen) {
    $('#contact-me').animate({ 'left': '-307px' }, 500);
    contactCardOpen = false;
  } else {
    $('#contact-me').animate({ 'left': '0' }, 500);
    contactCardOpen = true;
  }
}
