/*!
 *	Script for julianwachholz.ch
 *	Copyright (c) 2010 Julian Wachholz
 */

jQuery.extend( jQuery.easing, {
	easeInOutCubic: function(x,t,b,c,d){
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	}
});

$(function(){

	$("<div/>", { id: "top" }).prependTo("body");
	$("<a/>", { className: "top", href: "#top", title: "Go to top" }).appendTo("section");

	$("#contact form").children("input,textarea").each(function(){
		var placeholder = this.value;

		$(this).focus(function(){
			if(this.value == placeholder) {
				this.value = "";
				this.className = "";
			}
		}).blur(function(){
			if(this.value == "") {
				this.value = placeholder;
				this.className = "placeholder";
			}
		});
	});

	$("a[href^=#]").click(function(e){
		e.preventDefault();

		var target = $(this).attr("href");

		$("html,body").animate({
			scrollTop: $(target).offset().top
		}, 1250, "easeInOutCubic", function(){
			window.location.hash = target;
		});
	});

	$.getJSON("tweets.json", function(obj){
		var tweets = $("#tweets"),
			twl = tweets.children("ul"),
			maxHeight = 0, i;

		for(i = 0; i < obj.length; i++) {
			$("<li/>")
				.html(obj[i].text + "<span class=\"meta\">" + getMetadata(obj[i]) + "</span>")
				.css({opacity:0})
				.appendTo(twl);
		}

		twl.children().each(function(){
			if($(this).height() > maxHeight)
				maxHeight = $(this).height();
		});

		twl.css({
			height: maxHeight
		});

		twl.children().first().animate({
			opacity: 0,
			left: 16
		}, 500, function(){
			var next = $(this).next().addClass("current");
			next.animate({
				opacity: 1,
				left: 0
			}, 500, function(){
				setTimeout(toggleListItems, 6000, twl);
			});
			$(this).remove();
		});
	});

	$.getJSON("tracks.json", function(obj){
		var tracks = $("#tracks"),
			trl = tracks.children("ul"),
			i;

		for(i = 0; i < obj.length; i++) {
			$("<li/>")
				.html("<span class=\"wrap\">" + obj[i].track + "</span><span class=\"meta\">" + getMetadata(obj[i]) + "</span>")
				.css({opacity:0})
				.appendTo(trl);
		}

		trl.children().first().animate({
			opacity: 0,
			left: 16
		}, 500, function(){
			$(this).next().addClass("current").animate({
				opacity: 1,
				left: 0
			}, 500, function(){
				setTimeout(toggleListItems, 6000, trl);
			});
			$(this).remove();
		});
	});

	var form = $("#contact form"),
		note = $("#contact-note"),
		cmail = $("#cmail"),
		cname = $("#cname"),
		cmessage = $("#cmessage");

	form.children("input,textarea,#contact-submit").removeAttr("disabled");

	form.submit(function(){

		$("#contact form").children("input,textarea,#contact-submit").attr("disabled", true);

		if(cmail.val() == "" || cmail.val() == "your mail") {
			note.children("span").html("Please fill in your e-mail.");
			note.children("button.submit").hide();
			note.fadeIn(360).children("button.cancel").focus();
		} else if(!/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(cmail.val())) {
			note.children("span").html("I think you’ve made a mistake, that email is not valid.");
			note.children("button.submit").hide();
			note.fadeIn(360).children("button.cancel").focus();
		} else if(cmessage.val() == "" || cmessage.val() == "your message") {
			note.children("span").html("Please supply a message.");
			note.children("button.submit").hide();
			note.fadeIn(360).children("button.cancel").focus();
		} else if(cname.val() == "" || cname.val() == "your name") {
			note.children("span").html("Please fill in your name.");
			note.children("button.submit").show();
			note.fadeIn(360).children("button.cancel").focus();
		}

		return false;
	});

	$("#contact-note button.submit").click(function(){
		contact_submit();
	});

	$("#contact-note button.cancel").click(function(){
		note.fadeOut(250);
		form.children("input,textarea,#contact-submit").removeAttr("disabled");
		if(!/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(cmail.val())) {
			cmail.focus();
		} else if(cmessage.val() == "" || cmessage.val() == "your message") {
			cmessage.focus();
		} else {
			form.children("input[name=name]").focus();
		}
	});

});

function contact_submit() {
	var note = $("#contact-note"),
		form = $("#contact form");

	note.children("span").html("Please wait&hellip;");
	note.children("button").blur().attr("disabled", true);

	form.children("input,textarea,#contact-submit").removeAttr("disabled");
	var formdata = form.serialize();
	form.children("input,textarea,#contact-submit").attr("disabled", true);

	$.ajax({
		url: "mail.php",
		type: "post",
		data: formdata,
		success: function(d){
			note.children("span").html(d)
		},
		error: function(){
			note.children("span").html("Oops, something went wrong, please try it again later.");
		},
		complete: function(d){
			note.children("button").removeAttr("disabled").filter(".submit").hide();
			note.children("button.cancel").html("OK").focus();
		}
	});
};

function getMetadata(obj) {
	var meta = "",
		time = new Date(obj.time),
		offset = (new Date().getTime() - time.getTime()) / 1000;

	if(!!obj.id) {
		meta += "<a href=\"http://twitter.com/julianwachholz/status/"+obj.id+"\">";
	}

	if(offset <= 3600) {
		meta += Math.floor(offset/60) + " minutes ago";
	} else if(offset <= 24*3600) {
		var hours = Math.floor(offset/3600);
		if(hours == 1) {
			meta += "about an hour ago";
		} else {
			meta += "about " + hours + " hours ago";
		}
	} else {
		var str = (time.getHours() < 13 ?
			time.getHours() + ":" + time.getMinutes() + " AM "
			:(time.getHours() - 12) + ":" + (time.getMinutes()<10?0:"") + time.getMinutes() + " PM "
		), daySuffix = time.getDate()[time.getDate()-1];

		switch(time.getMonth())
		{
			case 0: str += "Jan"; break;
			case 1: str += "Feb"; break;
			case 2: str += "Mar"; break;
			case 3: str += "Apr"; break;
			case 4: str += "May"; break;
			case 5: str += "Jun"; break;
			case 6: str += "Jul"; break;
			case 7: str += "Aug"; break;
			case 8: str += "Sep"; break;
			case 9: str += "Oct"; break;
			case 10: str += "Nov"; break;
			case 11: str += "Dec"; break;
		}

		str += " " + time.getDate() + (daySuffix == 1 ?
			"st"
			:(daySuffix == 2 ?
				"nd"
				:(daySuffix == 3 ?
					"rd"
					:"th"
				)
			)
		);

		meta += str;
	}

	if(!!obj.id) {
		meta += "</a>";
	}

	if(!!obj.in_reply_to_status_id) {
		meta += " <a href=\"http://twitter.com/" + obj.in_reply_to_screen_name + "/status/"
			 + obj.in_reply_to_status_id + "\">in reply to " + obj.in_reply_to_screen_name
			 + "</a>";
	}

	return meta;
};

function toggleListItems(list){
	var current = list.children(".current"),
		next = current.next().size() == 1 ? current.next() : list.children().first();

	current.removeClass("current").animate({
		opacity: 0,
		left: 16
	}, 500, function(){
		if(next.height() > list.height()) {
			list.animate({
				height: next.height()
			}, 500);
		}
		next.addClass("current").css({left:-16}).animate({
			opacity: 1,
			left: 0
		}, 500, function(){
			setTimeout(toggleListItems, 6000, list);
		});
	});
};