$(document).ready(function() {
	initClickBinding();
	initBinding();
	$("div#nav ul li a img, div#socialBar ul li a img").hover(function() {
		var src = $(this).attr("src");
		var int = src.length -4;
		src = src.substring(0,int);
		src = src + "-hover.png";
		$(this).fadeTo('normal',0.25);
	}, function() {
		$(this).fadeTo('normal',1);
	});
	
	$("a.navHover").anchorAnimate();
	$("a.projBoxLink").anchorAnimate();
	$("a.tLink").anchorAnimate();
	$("a.shuffleProj").anchorAnimate();
	$("a.navLink").anchorAnimate();
	$("a.tclink").anchorAnimate();
	
	$("input.Submit").click(function() {
		$('div#statusMsg').html("<img src=\"img/ajax-loader.gif\" alt=\"Loading\" />");
		$.post("ajax.php", { msg: $("textarea#Message").val(), name: $("input#Name").val(), sub: $("input#Subject").val() }, function(data) {  $('div#statusMsg').html(data); });  
	});
	
	$("a.shuffleProj").click(function() {
		$('div#projGrid').fadeOut();
		$('div#projGrid').html("<img src=\"img/ajax-loader.gif\" alt=\"Loading\" />");
		$.post("ajax.php?r=true", {}, function(data) { $('div#projGrid').html(data); initBinding(); initClickBinding(); }); 
		$('div#projGrid').fadeIn();
	});
	
	
	
});

function initClickBinding() {
	$('a.projBoxLink, a.projBoxLink img').click(function() {
		$("a.projBoxLink").anchorAnimate();
		$("a.tLink").anchorAnimate();
		$('div#projectStuff').html("<img src=\"img/ajax-loader.gif\" alt=\"Loading\" />");
		var title = $(this).attr('title');
		$.post("ajax.php?r=true", { type: title } ,function(data) { $('div#projectStuff').html(data); initBinding(); initClickBinding()});
		$('div#projectStuff').fadeIn();
	});
}

function initBinding(){
	$(".boxgrid.caption").hover(function(){
		$("a.projBoxLink").anchorAnimate();
		$("a.tLink").anchorAnimate();
		$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'220px'},{queue:false,duration:160});
	});
}

jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 800
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
};
