jQuery(function($) {
	if (window.pub) {
		$('#insert_pub_name').val(pub);
	}
	
	if ($('#latest_tweets').length > 0) {
		var url = {
			base: 'http://api.twitter.com/1/statuses/user_timeline.json',
			screen_name: 'Daniel_Thwaites',
			count: 3,
			callback: 'twitter_response',
			include_rts: true
		}
		document.body.appendChild(document.createElement('script')).src = objectToURLQuery(url);
	}
	
	if ($('#timeline #scroller').length > 0) {
		var count = $('#timeline #scroller .period').length;
		var current = 0;
		var scroll_increment = $('#timeline #scroller .period:first-child').outerWidth();
		var horse_increment = $('#timeline #timeline_overview').outerWidth() / count;
		var num_on_screen = Math.floor($('#timeline').outerWidth() / scroll_increment);
		var scroll_by = 2;
		var current_frame = 0;
		
		window.timelineScrollTo = function() {
			if (arguments[0] === 'prev') {
				if (current > 0) current -= scroll_by;
			} else if (arguments[0] === 'next') {
				if (current < count - num_on_screen) current += scroll_by;
			} else {
				current = parseInt(arguments[0]);
			}
			if (current < 0) current = 0;
			if (current > count - 2) current = count - 2;
			$('#timeline #scroller').clearQueue().delay(500).animate({
				left: '-' + (current * scroll_increment) + 'px'
			},{
				step: function() {move_animate()},
				duration: 500,
				easing: 'easeInOutQuad'
			});
			$('#timeline #timeline_horse').clearQueue().animate({
				left: (current * horse_increment) + 'px'
			},{
				step: function() {move_animate()},
				duration: 1500,
				easing: 'easeInOutQuad'
			});
		}
		
		function move_animate() {
			var pos = parseInt($('#timeline_horse').css('left'));
			
			var frame = (pos * 0.5) % 15;
			
			$('#timeline_horse').css({
				'background-position': '-' + (Math.floor(frame) * 100) + 'px 0px'
			});
		}
		$('#timeline_horse').draggable({
			axis: 'x',
			containment: 'parent',
			start: function(e, ui) {
				$('#timeline_horse').stop(true);
			},
			drag: function(e, ui) {
				move_animate();
			},
			stop: function(e, ui) {
				var val = Math.round(parseInt($(this).css('left')) / horse_increment);
				timelineScrollTo(val);
			}
		});
		
		$('#timeline #controls a:first-child').click(function(e) {
			if (e.preventDefault) e.preventDefault();
			timelineScrollTo('prev');
			return false;
		});
		
		$('#timeline #controls a:last-child').click(function(e) {
			if (e.preventDefault) e.preventDefault();
			timelineScrollTo('next');
			if (e.preventDefault) e.preventDefault();
			return false;
		});
	}
	
	// Beer drop down
	$('#main_navigation li:first-child').hover(function() {
		$('#beer_dropdown').fadeIn(100);
	},function() {
	});
	$('#beer_dropdown, body').hover(function() {
	}, function() {
		$('#beer_dropdown').fadeOut(100);
	})
	
	
	// Homepage drop down(s) (cricket competition)
	
	$.extend($.easing, {
		easeOutBounce: function (x, t, b, c, d) {
			if ((t/=d) < (1/2.75)) {
				return c*(7.5625*t*t) + b;
			} else if (t < (2/2.75)) {
				return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
			} else if (t < (2.5/2.75)) {
				return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
			} else {
				return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
			}
		}
	});
	$('.drop-down-banner h2').delay(1000).animate({
		top: '0'
	}, 800, 'easeOutBounce').bind('mouseenter', function() {
		$(this).siblings('div').animate({
			top: '48px'
		}, 1600, 'easeOutBounce');
		$(this).parent().bind('mouseleave', function() {
			$(this).find('div').animate({
				top: '-227px'
			});
		});
	});
	$('.drop-down-mini h2').delay(1000).animate({
		top: '0'
	}, 800, 'easeOutBounce');
	
	// Pub image switcher
	
	$('#pub_image_switcher').each(function() {
		var $container = $(this);
		var $main_images = $container.children('img');
		$('#pub_image_thumbs img').each(function(i) {
			$(this).hover(function() {
				$main_images.hide();
				$main_images.eq(($main_images.length - 1) - i).show();
			}, function() {});
		});
	});
	
});

function objectToURLQuery(obj) {
	var str = obj.base + '?';
	for (var key in obj) {
		if (key !== 'base') {
			str += key + '=' + obj[key] + '&';
		}
	}
	return str;
}

function time_since(miliseconds) {
	var seconds = parseInt(miliseconds / 1000);
	if (seconds < 60) {
		return 'A few seconds ago';
	} else if (seconds < 60 * 2) {
		return 'About a minute ago';
	} else if (seconds < 60 * 60) {
		return 'About ' + Math.floor(seconds / 60) + ' minutes ago';
	} else if (seconds < 60 * 60 * 2) {
		return 'About an hour ago';
	} else if (seconds < 60 * 60 * 24) {
		return 'About ' + Math.floor(seconds / 60 / 60) + ' hours ago';
	} else if (seconds < 60 * 60 * 24 * 2) {
		return 'Yesterday';
	} else if (seconds < 60 * 60 * 24 * 7) {
		return 'About ' + Math.floor(seconds / 60 / 60 / 24) + ' days ago';
	} else if (seconds < 60 * 60 * 24 * 14) {
		return 'About a week ago';
	} else if (seconds < 60 * 60 * 24 * 30) {
		return 'About ' + Math.floor(seconds / 60 / 60 / 24 / 7) + ' weeks ago';
	} else if (seconds < 60 * 60 * 24 * 30 * 2) {
		return 'About a month ago';
	} else if (seconds < 60 * 60 * 24 * 30 * 12) {
		return 'About ' + Math.floor(seconds / 60 / 60 / 24 / 30) + ' months ago';
	} else if (seconds < 60 * 60 * 24 * 30 * 12 * 2) {
		return 'About a year ago';
	} else {
		return 'About ' + Math.floor(seconds / 60 / 60 / 24 / 30) + ' years ago';
	}
}

function parse_date(str) {
	var v = str.split(' ');
	return new Date(Date.parse(v[1] + " " + v[2] + ", " + v[5] + " " + v[3] + " UTC"));
} 

function parse_links(str) {
	str = str.replace(/(\w+):\/\/[\S]+(\b|$)\/?/gim, "<a href=\"$&\">$&</a>");
	str = str.replace(/(^|\s)@(\w+)/g, "$1<a href=\"http://www.twitter.com/$2\">@$2</a>");
	str = str.replace(/(^|\s)#(\w+)/g, "$1<a href=\"http://search.twitter.com/search?q=%23$2\">#$2</a>");
	return str;
}

function twitter_response(data) {
	var ul = $('#latest_tweets');
	for (var i = 0; i < data.length; i++) {
		var then = Date.parse(parse_date(data[i].created_at));
		var now = Date.parse(new Date());
		
		ul.append('<li><a href="http://twitter.com/#!/Daniel_Thwaites/status/' + data[i].id_str + '">' + data[i].user.name + '</a>' + parse_links(data[i].text) + '<br /><span>' + time_since(now - then) + '</span></li>');
	}
}
