
jQuery.fn.ScrollToAbove = function(s, transition) {
	o = jQuery.speed(s);
	return this.queue('interfaceFX',function(){
		new jQuery.fx.ScrollToAbove(this, o, transition);
	});
};

jQuery.fn.BounceText = function(width) {
	return this.queue('interfaceFX',function(){
		var subject=this;
		bounceOut = Animator.apply(subject, 'text-indent: '+width+'px');
		bounceOut.setOptions({
			duration: 300,
			onComplete: function() {
				bounceIn = Animator.apply(subject, 'text-indent: 0');
				bounceIn.setOptions({duration: 800,transition: Animator.makeBounce(2)});
				bounceIn.play();
				jQuery.dequeue(subject,'interfaceFX');
			}
		});
		bounceOut.play();
	});
};

jQuery.fx.ScrollToAbove = function (e, o, transition)
{
	var z = this;
	z.o = o;
	z.e = e;
	z.transition = transition||'original';
	prevsib = $(e).prev(e).get(0);
	p = jQuery.iUtil.getPosition(prevsib);
	p.y = p.y + jQuery.iUtil.getSize(prevsib).h - 100;
	s = jQuery.iUtil.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null;jQuery.dequeue(z.e, 'interfaceFX');};
	z.t=(new Date).getTime();
	s.h = s.h > s.ih ? (s.h - s.ih) : s.h;
	s.w = s.w > s.iw ? (s.w - s.iw) : s.w;
	z.endTop = p.y > s.h ? s.h : p.y;
	z.endLeft = p.x > s.w ? s.w : p.x;
	z.startTop = s.t;
	z.startLeft = s.l;
	z.step = function(){
		var t = (new Date).getTime();
		var n = t - z.t;
		var p = n / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.endTop, z.endLeft)},13);
		} else {
			st = jQuery.fx.transitions(p, n, z.startTop, (z.endTop - z.startTop), z.o.duration, z.transition);
			sl = jQuery.fx.transitions(p, n, z.startLeft, (z.endLeft - z.startLeft), z.o.duration, z.transition);
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t);};
	z.timer=setInterval(function(){z.step();},13);
};

var AnchorClicker = {
	initialize: function() {
		$('a[@href^="#"]').each(function(i) {
				$(this).click(function(){
						var item = $(this.hash);
						item.ScrollToAbove(500, 'easeout').BounceText(60);
						return false;
				});
		});
	}
};

// Initialise on document ready:
$(document).ready(function() {

	if(location.hash) {
		$('#content').css('overflow','visible');
		$(location.hash).ScrollToAbove(500, 'easeout').BounceText(60);
		setTimeout(function(){ console.info('back to hidden'); $('#content').css('overflow','hidden'); }, 500);
	}

	AnchorClicker.initialize();
});
