(function($) {
  var cache = [];
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


$(document).ready(function() {
	jQuery.preLoadImages("../images/skype-hover.png", "../images/twitter-hover.png", "../images/project-hover.png");

	//external link
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
	
	$(".box").colorbox();
	
	$('#cboxContent').live('mousemove', function(e) {
		$("#cboxNext, #cboxPrevious").css('top', e.pageY - $(this).offset().top);
	});
	
	$('#cboxContent').live('mouseenter', function(e) {
		$("#cboxNext, #cboxPrevious").fadeIn("fast");
	});
	
	$('#cboxContent').live('mouseleave', function(e) {
		$("#cboxNext, #cboxPrevious").fadeOut("fast");
	});
	
	$("#nav a, a.scroll").click(function(e) {
		var dest = $(e.currentTarget).attr('href');
		$.scrollTo(dest, 800);	
	});
	
	$('.item-image a')
		.removeClass('highlight')
		.append('<span class="hover" />').each(function () {
			var $span = $('> span.hover', this).css('opacity', 0);
			$(this).hover(function () {
				$span.stop().fadeTo(300, 1); // on hover
			}, function () {	
				$span.stop().fadeTo(300, 0); // off hover
			});
		});
	
	// submit newsletter form
	$("#newsletterForm").validate({
		submitHandler: function(form) {
			$.ajax({
				type: "POST",
				url: $(form).attr("action"),
				data: $(form).serialize(),
				success: function(msg){
					alert("Thank you!")
				}
			});
			$(form)[0].reset();
		},
		errorPlacement: function(error, element) {
		}
	});
		
	$("#inquiryForm").validate({
		submitHandler: function(form) {
			$.ajax({
				type: "POST",
				url: $(form).attr("action"),
				data: $(form).serialize(),
				success: function(msg){
					$("#inquiryForm + .success").fadeIn(800);
				}
			});
			$(form)[0].reset();
		 },
		errorPlacement: function(error, element) {
			error.insertBefore(element);
		}
	});
		
	$("#helloForm").validate({
		submitHandler: function(form) {
			$.ajax({
				type: "POST",
				url: $(form).attr("action"),
				data: $(form).serialize(),
				success: function(msg){
					$("#helloForm + .success").fadeIn(800);
				}
			});
			$(form)[0].reset();
		 },
		errorPlacement: function(error, element) {
			error.insertBefore(element);
		}
	});
		
	
});
