$(document).ready(function(){
	setup();
	extend();
	topLinks();
	newWindow();
	listToggle();
	contactForm();
	mailTo();
	stats();
});

this.setup = function(){
	$('#home #projectview').removeClass('hide');
	if(location.href.indexOf('refresh=true') > -1){
		$('head').append('<link href="/css/refresh.css" media="screen" rel="stylesheet" type="text/css" />');
		var header = $('#header').clone(true).remove();
		$('#header').remove();
		$('body').prepend('<div id="head_wrap"><div class="container_12"></div></div>').find('#head_wrap .container_12').append(header);
		$('#header a').click(function(){
			window.location = $(this).attr('href') + '?refresh=true';
			return false;
		});
		$('#title span').wrap('<span>');
	}
}

this.extend = function(){
	$.extend($.expr[':'],{
		external: function(a, i){
			if(!a.href) {return false;}
			return a.hostname && a.hostname !== window.location.hostname;
		},
		pdf: function(a, i){
			if(!a.href) {return false;}
			return a.href.indexOf('.pdf') > -1;
		}
	});
}

this.topLinks = function(){
	$('#top_link a').click(function(){
		$('html, body').animate({scrollTop: 0}, 'normal');
		return false;
	});
}

this.newWindow = function(){
	$('a:external').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	$('a:pdf').click(function(){
		tracker._trackPageview($(this).attr('href'));
		window.open($(this).attr('href'));
		return false;
	});
	
	$('a[href*="mailto"]').click(function(){
		tracker._trackPageview('/mailto');
	});
	
	$('a[href="#mailto"]').click(function(){
		window.location = 'mailto:joshua.kemmerling@gmail.com';
		return false;
	});
}

this.listToggle = function(){
	$('#projectview a:not(.active)').live('click', function(){
		$('#projectview a').toggleClass('active');
		$('#content').css({
			height: $('#content ul').height()
		}).find('ul li').css({
			position: 'static'
		}).parent().fadeTo(500, 0.0, function(){
			$('#content').toggleClass('listview').animate({
				height: $('#content ul').height()
			});
			$('#content ul').fadeTo(500, 1.0);
		});
		return false;
	});
	
	$('#projectview a.active').live('click', function(){
		return false;
	});
}

this.contactForm = function(){
	$('#submit').click(function(){
		var emailregex = /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/;
		var validemail = false;
		var validnospam = false;
		var validall = false;
		
		var name = $('form #name').val();
		var email = $('form #email').val();
		var nospam = $('form #nospam').val();
		var message = $('form #message').val();
		
		$('form #email, form #nospam').removeClass('error');
		
		if(emailregex.test(email))
			validemail = true;
		else
			$('form #email').addClass('error');
			
		if(nospam == '7')
			validnospam = true;
		else
			$('form #nospam').addClass('error');
		
		if(validemail && validnospam)
			validall = true;
		else
			validall = false;
		
		if(validall){
			$.post(
				'/contact/',
				{name: name, email: email, nospam: nospam, message: message, submit: 'submit'},
				function(data){
					$('#contact_form').css({
						height: $('#contact_form').height()
					}).find('*').fadeOut(500, function(){
						$('#contact_form').empty().append('<div style="display: none;">' + $(data).find('#contact_form').html() + '</div>');
						$('#contact_form').find('> div').fadeIn(500);
					});
				}
			);
		}
		
		return false;
	});
}

this.mailTo = function(){
	var email = 'moc.liamg@gnilremmek.auhsoj';
	var output = "";
	
	$('a[href*="mailto"]').click(function(){
		var i = email.length; 
		
		for(i; i > 0; i--){ 
			output += email.substring(i - 1, i); 
		}
		
		window.location = "mailto:" + output;
		
		return false;
	});
}

this.stats = function(){
	var src = '';
	$('.area.pie ul li').hover(function(){
		src = $('#pie').attr('src');

		var i = 0;
		var lis = $('.area.pie ul li');
		var index = $(lis).index($(this));
		var hoverSrc = src.substring(0, src.lastIndexOf('=') + 1);
		
		while(i < $(lis).length){
			if(i == index)
				hoverSrc += 'aa0000,';
			else
				hoverSrc += 'e8e8e8,';
			
			i++;
		}
		
		hoverSrc = hoverSrc.substring(0, hoverSrc.lastIndexOf(','));
		
		$('#pie').attr('src', hoverSrc);
	}, function(){
		$('#pie').attr('src', src);
		src = '';
	});
}