/* JohnBarbagallo.com JS File
 *
 * Simple module paradigm, nothing that impressive ;)
 *
*/

var JB = (function(){
	
	var _ = {};
	
	_.init=function(){
		// preload the tooltip bg
		_.preloadImages("images/tooltip_box.png");
	
		// create custom animation algorithm for jQuery called "bouncy"
		$.easing.bouncy = function (x, t, b, c, d) {
			var s = 1.70158;
			if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
			return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
		}
		
		// create custom tooltip effect for jQuery Tooltip
		$.tools.tooltip.addEffect("bouncy",
		
			// opening animation
			function(done) {
				this.getTip().animate({top: '+=15', opacity: '1'}, 350, 'bouncy', done).show();
			},
		
			// closing animation
			function(done) {
				this.getTip().animate({top: '-=15', opacity: '0'}, 250, 'bouncy', function()  {
					$(this).hide();
					done.call(); 
				});
			}
		);
		
		
		$("#featured-work li").tooltip({tip: '#featured-tip', offset: [-120, -11], effect: 'bouncy'});
		
		// Contact form stuff
		$('#reach-me span').click(function(){
			var targetID = $(this).attr('class');
			// hide this and show the input form with the ID of this' class
			$(this).fadeOut(150, function(){$('#'+targetID).fadeIn(350).val('').focus()});
			// hacky, simple validation method
			if(targetID == 'themessage') {
				$("#submit-button").fadeIn(500);
			}
		});
		
		$("#success-msg, #error-msg").click(function(){$(this).fadeOut(500, function(){_.doReachMeReset();})});
	};
	
	_.doReachMeReset=function() {
		// Reset all of the fields
		$("#reach-me input#fullname").attr('value', 'Name');
		$("#reach-me input#email").attr('value', 'E-Mail');
		$("#reach-me input#themessage").attr('value', 'Message');
		
		// fade in the spans
		$("#reach-me span").fadeIn(450);
		$("#contact-note").fadeIn(450);
	};
	
	_.doReachMe=function() {
		// hide the form
		$("#reach-me input").slideUp(650);
		$("#reach-me #submit-button").fadeOut(250);
		$("#reach-me #contact-note").fadeOut(450);
		
		// set variables to use for the ajax data
		var fullname = $("#reach-me" + " input#fullname").attr('value');
		var email = $("#reach-me" + " input#email").attr('value');
		var themessage = $("#reach-me" + " input#themessage").attr('value');
		
		//submit it
		$.post("utils/submit.php",{fullname: fullname, email: email, themessage: themessage}, function(data){
				if(data == "success"){
					//show success msg
					$("#reach-me #success-msg").fadeIn(1100);
				} else {
					$("#reach-me #error-msg").fadeIn(1100);
				}					
			}
		);
	};
	
	// Preloads all the images needed, slick stuff
	_.preloadImages=function() {
	    var args_len = arguments.length;
	    var cache = [];
	    for (var i = args_len; i--;) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = arguments[i];
	      cache.push(cacheImage);
	    }
	};
	
	return _;
	
})();
