/* All HTML, CSS and JavaScript code is free to copy and use for any purpose,
but the graphics are Copyright 2009 by Jacob Rask unless stated otherwise. */

$(function(){
	$('.post-time, .comments time').relDate();

	elseLinks = $('.else-choose a');
	elseLinks.click(function () {
		$('.else').hide();
		clicked = $(this);
		elseLinks.removeClass('active');
		theID = clicked.attr('href');
		$(theID).slideDown('slow');
		clicked.addClass('active');
		return false;
	});

	$('#comment-post input, #comment-post textarea, #contact-form input, #contact-form textarea').each(function() {
		el = $(this);
		this.def = el.attr('placeholder'); // default placeholder text to show in inputs
		this.t = el.attr('id');

		if (el.val() == '') {
			el.
				removeClass('invalid changed').
				val(this.def);
		} else {
			el.
				addClass('changed');
			if (this.t == "email" && !validEmail(el.val()))
				el.addClass('invalid');
			else if (this.t == "url" && !validURL(el.val()))
				el.addClass('invalid');
		}

		el.
			focus(function() {
				if ($(this).val() == this.def)
					$(this).val('');
			}).
			blur(function() {
				if ($(this).val() == '') // if empty, remove classes and set to default
					$(this).
						removeClass("invalid changed").
						val(this.def);
				else
					$(this).validateForm();
			});
	});

	$('#comment-post form, #contact-form form').each(function() {
		$(this).submit(function() {
			$(this).find('input, textarea').each(function() {
				if ($(this).val() == $(this).attr('placeholder'))  // will add validation functions here later
					$(this).val('');
			});
		});
	});
	$("#comment-post label, #contact-form label").hide();

});

$.fn.relDate = function() {
	var el, dt, dtt;
	return $(this).each(function() {
		el = jQuery(this);
		dt = el.attr("datetime");
		dtt = relDate_text(dt);
		if (dt && dtt) {
			el.children("a").text(dtt);
			el.attr("title",(dt.replace(/[TZ]/g," ")));
		}
	});
}

function relDate_text(dt){

	var r, s, date = new Date((dt || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		sec = (((new Date()).getTime() - date.getTime()) / 1000),
		min = Math.floor(sec / 60);

	if (min >= 483840) {
		s = (min >= 967680 ) ? 's' : '';
		r = Math.floor(min / 483840) + ' year' + s + ' ago';
	}
	else if (min >= 40320) {
		s = (min >= 80640 ) ? 's' : '';
		r = Math.floor(min / 40320) + ' month' + s + ' ago';
	}
	else if (min >= 10080) {
		s = (min >= 20160 ) ? 's' : '';
		r = Math.floor(min / 10080) + ' week' + s + ' ago';
	}
	else if (min >= 1440) {
		s = (min >= 2880 ) ? 's' : '';
		r = Math.floor(min / 1440) + ' day' + s + ' ago';
	}
	else if (min >= 60) {
		s = (min >= 120 ) ? 's' : '';
		r = Math.floor(min / 60) + ' hour' + s + ' ago';
	}
	else if (min >= 1) {
		s = (min > 1) ? 's' : '';
		r = Math.floor(min) + ' minute' + s + ' ago';
	}
	else
		r = 'moments ago';

	return r;
}

function validEmail(e) {
	var re = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i
	return re.test(e);
}

function validURL(u) {
	var re = /(?:[\w-]+\.)+/i
	return re.test(u);
}

$.fn.validateForm = function() {
	el = $(this);
	el.addClass("changed");
	this.t = $(this).attr('id');
	if (this.t == "email") {
		if (validEmail(el.val()))
			el.removeClass('invalid');
		else
			el.
				addClass('invalid').
				hint();
	} else if (this.t == 'url') {
		if (validURL(el.val()))
			el.removeClass('invalid');
		else
			el.
				addClass('invalid').
				hint();
	}
}

$.fn.hint = function() {
	var hintText = this.attr("title");
	var pos = new Array();
	pos.l = $(this).position().left-200;
	pos.t = $(this).position().top;
	$('<span></span>').
		addClass('hint').
		appendTo('body').
		text(hintText).
		css({ left: pos.l + 'px', top: pos.t + 'px' }).
		fadeIn('slow').
		animate({opacity: 1.0}, 2000).
		fadeOut('slow');
}
