$(document).ready(function() {
	
	// Move "Featured" tag to front
	$('#featured').addClass('selected').parent().prependTo($('#categories'));
	
	// Open external links in new Window/Tab
	$("a[href^='http:']").not("[href*='" + window.location.host + "']").attr('target','_blank');
	
	// Lightbox
	$(".fancybox").fancybox({
		'hideOnContentClick': true,
		'titleShow': false
	});
	
	// Contact Link
	$("a[href='/contact/']").click(function (e) {
		e.preventDefault();
		$.post("/responder", { linkData: "contact" },
		function(data) {
			$('body > p').css('display', 'none').html(data);
			$('body > p').fadeIn(1000);
			processInputs();
		}, "html");
	});
	
	// Social Link
	$("a[href='/social/']").click(function (e) {
		e.preventDefault();
		$.post("/responder", { linkData: "social" },
		function(data) {
			displayProjectList(data);
		}, "html");
	});
	
	// Tag Selection
	$('#categories li a').click(function (e) {
		e.preventDefault();
		if($(this).attr('id') == "all") {
			$('#categories li a').removeClass('selected');
		} else {
			$('#all').removeClass('selected');
		}
		$(this).toggleClass('selected');
		var divList = $("#categories")
             .find(".selected")
             .map(function() { return this.id; })
             .get();
		$.post("/responder", { tagData: divList },
		function(data) {
			displayProjectList(data);
		}, "html");
	});
	
	// Display Project List
	function displayProjectList(data) {
		$('#project-list').html(data);
		var d= 0;
		$('#project-list li a').css('display', 'none').each(function() {
			$(this).delay(d).fadeIn(1000);
			d += 100;
		});
	}
	
	// Project Selection
	$('#project-list > li > a').live('click', function(e) {
		e.preventDefault();
		
		$('#project-list li').removeClass('selected');
		$(this).parent().addClass('selected');
		var project = $(this).attr('id');
         
		$.post("/responder", { projectData: project },
		function(data) {
			displayProject(data);
		}, "html");
	});
	
	// Display Project
	function displayProject(data) {
		$('html,body').animate({scrollTop: 0}, 1000);
		$('#project').css('display', 'none').html(data);
		$('#project').fadeIn(1000);
		$(".fancybox").fancybox({
			'hideOnContentClick': true,
			'titleShow': false
		});
		$("a[href^='http:']").not("[href*='" + window.location.host + "']").attr('target','_blank');
	}
	
	// Contact Form Submission
	$("#contact-form").live('submit', function(e){
		e.preventDefault();
		name = $('#d_name').val();
		email = $('#d_email').val();
		message = $('#d_message').val();
		$.post("/responder", { d_name: name, d_email: email, d_message: message },
		function(data) {
			$('body > p > p').css('display', 'none').html(data);
			$('body > p > p').fadeIn(1000);
		}, "html");
	});
	
	// Input value switchers
	function processInputs() {
		$('#d_name').focus(function () {
			if ($(this).val() == 'Name') {
				$(this).val('').css('color', '#484848');
			}
		});
		
		$('#d_name').blur(function () {
			if ($(this).val() == '') {
				$(this).val('Name').css('color', '#848484');
			}
		});
		
		$('#d_email').focus(function () {
			if ($(this).val() == 'Email') {
				$(this).val('').css('color', '#484848');
			}
		});
		
		$('#d_email').blur(function () {
			if ($(this).val() == '') {
				$(this).val('Email').css('color', '#848484');
			}
		});
		
		$('#d_message').focus(function () {
			if ($(this).val() == 'Message') {
				$(this).val('').css('color', '#484848');
			}
		});
		
		$('#d_message').blur(function () {
			if ($(this).val() == '') {
				$(this).val('Message').css('color', '#848484');
			}
		});
	}
});

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-6131606-1']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
