/* Globals */
var gridTO 			= false,
	tooltipTO 		= false,
 	cursorPositionX = 0,
 	cursorPositionY = 0,
	$toolTip		= false;


/**
 * DOM Load
 */

$(document).ready(function() {
	pageEnhance();	
});



function pageEnhance() {
	
	$toolTip = $("#tooltip");
	
	$(document).mousemove(function(e){	
		windowHalfwayX = $(window).width()/2;			
		cursorPositionX = e.pageX;
		cursorPositionY = e.pageY;
		if(cursorPositionX>windowHalfwayX) {
			$toolTip.css({top:(cursorPositionY+10)+"px",left:(cursorPositionX-260)+"px"})
		} else {
			$toolTip.css({top:(cursorPositionY+10)+"px",left:(cursorPositionX+10)+"px"})
		}
		
	})	
		
	$('#grid').isotope({
		animationEngine : 'jquery',
	  	itemSelector : 'li'	
		});
	
	$('.home #footer nav a').click(function(){
		$('.home #footer nav a').removeClass("selected");
		$(this).addClass("selected");
	  	var selector = $(this).attr('data-filter');
	  	$('#grid').isotope({ filter: selector });
	  	return false;
	});
	

	
	$('#grid li').hover(
		function(){
			
			clearTimeout(gridTO);
			clearTimeout(tooltipTO);
			var title = $(this).find(".tooltip h2").html();
			var cats = $(this).find(".tooltip span").html();
			$toolTip.html("<h3>"+title+"</h3><p>"+cats+"</p>").stop(true,true).hide().show()
			$('#grid li').not(this).stop(true,true).fadeTo(500,.2)
			$(this).stop(true,true).fadeTo(500,1)
		},
		function(){		
			gridTO = setTimeout(function(){
				$('#grid li').not(this).fadeTo(500,1)
			},500);
			tooltipTO = setTimeout(function(){
				$toolTip.fadeOut()
			},500);
		}
	)
	
	$("a#logo").hover(
		function(){
			$("header").stop().animate({"padding-top":"75px"});
		},
		function(){
			$("header").stop().animate({"padding-top":"0"});
		}
	)
	
	$(".ldr img").imgpreload({
		each: function() {				
			$(this).fadeIn(1200);
			$(this).closest(".ldr").addClass("loaded");				
		}
	});
	
	// New windows
	newWindowLinks();
	
	// Default text
	defaultText();
	
}





function defaultText() {
	$(".defaultText").unbind("focus,blur").focus(function () {		
		if ($(this).val() == $(this)[0].defaultValue) {
        	$(this).val("");
        }
	 }).blur(function() {
        if ($(this).val() == "") {
        	$(this).val($(this)[0].defaultValue);
     	}
	 });
}



/**	
 * Target new windows
 */
function newWindowLinks(){
	$('a[rel=external]').live("click",function(){
		window.open($(this).attr('href'));
		return false;
	});
}



function jsdebug(message) {
	try {
		if(window.console) {
			console.log(message);			
		}
	} catch(e) {}	
}


