// Non-www redirect
if (window.location.hostname.substr(0, 3) == 'www') window.location = window.location.toString().replace(/www./i, '');

// Mobile redirect
if (screen.width < 500) {
	if (document.cookie.indexOf('mobile=') < 0) {
		window.location = '/mobile';
	}
}

//-------------------------------
// Makes Div equal the same height
//-------------------------------
$.fn.equalCols = function() {
	// Array Sorter
	var sortNumber = function(a,b) {return b - a;},
		heights = [];
	// Push each height into an array
	$(this).each(function() {
		heights.push( $(this).height() );
	});
	heights.sort(sortNumber);
	var maxHeight = heights[0];
	return this.each(function() {
		// Set each column to the max height
		$(this).css({'height': maxHeight});
	});
};

//-------------------------------
// Moves form label to the input
//-------------------------------
$.fn.labelHolder = function() {
	return this.each(function() {
		var holder = $(this).find('label').hide().text(),
			field = $(this).find('input');
		field.blur(function() {
			if ( $(this).val() == '' || $(this).val() == holder ) {
				$(this).val(holder).addClass('holder');
			}
		}).blur();
		field.focus(function() {
			if ( $(this).val() == holder ) {
				$(this).val('').removeClass('holder');
			}
		});
	});
};

//-------------------------------
// Pads the page content to fill viewport
//-------------------------------
function fillViewport() {
	var containerh = $('body').height(),
		viewporth = window.innerHeight;

	if (containerh < viewporth) {
		$('#footer').css('padding-top', viewporth - containerh );
	}
}

//-------------------------------
// Adds class name to body pased on URL
//-------------------------------
function setBodyClass() {
	var urlroot = '/', // Set to the root URL of the site
		url = window.location.pathname,
		dir = url.split(urlroot);
	dir = dir[1];
	if (dir) {
		var file = dir.split('.'); // Removes file extentions
		if (file[0]) {
			dir = file[0];
		}
	} else {
		dir = 'home';
	}
	$('body').addClass(dir);
}

//-------------------------------
// .first & .last clasess for styling
//-------------------------------
function firstLast() {
	$('#content li:first,#content p:first').addClass('first');
	$('#content li:last,#content p:last').addClass('last');
}

$(function() {
	$('body').addClass('js'); // For advanced CSS
	setBodyClass();

	$('#sidebar li:not(.selected)')
		.css( {backgroundPosition: '0 0'} )
		.mouseover(function(){
			$(this).stop().animate(
				{backgroundPosition:'(-70px 0)'}, 
				{duration:500});
			})
		.mouseout(function(){
			$(this).stop().animate(
				{backgroundPosition:'(0 0)'}, 
				{duration:500});
			})
		.mousedown(function(){
			$(this).stop().animate(
				{backgroundPosition:'(-130px 0)'}, 
				{duration:500});
			});

	$('.fjump a').fancyZoom({width:350, directory:'/themes/atherton/js/fancy-zoom'});

	$('#content a[rel*="external"]').addClass('external');

	$('#skipto a').add('#jumptotop a').click(function() {
		var $target = $(this.hash);

		$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');

		if ($target.length) {
			var targetOffset = $target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 1000);
			return false;
		}
	});

	firstLast();
	fillViewport();
	$(window).resize(fillViewport);
	
	// Open gTalk in a sized pop-up
	$('.gtalk a').click(function() {
		return !window.open(this.href, 'gTalk', 'width=250,height=450,status=1,resizable=1,scrollbars=1');
	});
});