Accordion = {
	currentTab:1,
	maxTab: 4,
	animating:false,
	
	init: function () {
		$$('#tabblock li>span, #accordion>.cell>div').invoke('hide');
		$$('#tab1>span, #backdrop1').invoke('show');
		
		$('tabblock').observe('click', function (event) {
			var element = $(event.element());
			
			if (element.match('li *, li')) {
				
				while (element.tagName.toUpperCase() != 'LI' && element.parentNode) {element = element.parentNode;}
				var tabindex = element.getAttribute('tabindex');
				Accordion.selectTab(tabindex);
				event.stop();return false;
			}
		});
		
		$$('#accordion .left').first().observe('click', function () {
			if (Accordion.currentTab == 1) Accordion.selectTab(Accordion.maxTab);
			else Accordion.selectTab(parseInt(Accordion.currentTab,10)-1);
		});
		$$('#accordion .right').first().observe('click', function () {
			if (Accordion.currentTab == Accordion.maxTab) Accordion.selectTab(1);
			else Accordion.selectTab(parseInt(Accordion.currentTab,10)+1);
		});
	},
	
	selectTab: function (tabid) {
		if (tabid==this.currentTab || this.animating) return;
		var previousBackdrop = $('backdrop'+this.currentTab);
		var previousTab = $('tab'+this.currentTab);
		var previousTabHilight = previousTab.select('span').first();
		// var previousContent = $('content'+this.currentTab);
		
		var nextBackdrop = $('backdrop'+tabid);
		var nextTab = $('tab'+tabid);
		var nextTabHilight = nextTab.select('span').first();
		// var nextContent = $('content'+tabid);
		
		var w = $('accordion').getWidth();
		
		previousTab.className = 'selected';			
		nextTab.className = '';
		if (tabid>this.currentTab) { 	//moving left
			nextBackdrop.show().setStyle({left:w+'px'});
			nextBackdrop.show();
			new Effect.Parallel([
				new Effect.Move(previousBackdrop, { sync: true, x:-w, mode: 'absolute' }),
				new Effect.Move(nextBackdrop, { sync: true, x:0, mode: 'absolute' }),
				new Effect.Fade(previousTabHilight, {sync:true}),
				new Effect.Appear(nextTabHilight, {sync:true}),
				new Effect.Morph(previousTab, {style:'normal'}),
				new Effect.Morph(nextTab, {style:'selected'})
			], { 
				duration: 0.8,
				transition: Effect.Transitions.easeOutQuad,
				beforeStart: function () {Accordion.animating = true;},
				afterFinish: function () {
					previousBackdrop.hide();
					previousTab.className = '';
					nextTab.className = 'selected';
					Accordion.animating = false;
				}
			});
		} else { 						//moving right
			nextBackdrop.setStyle({left:-w+'px'}).show();
			new Effect.Parallel([
				new Effect.Move(previousBackdrop, { sync: true, x:w, mode: 'absolute' }),
				new Effect.Move(nextBackdrop, { sync: true, x:0, mode: 'absolute' }),
				new Effect.Fade(previousTabHilight, {sync:true}),
				new Effect.Appear(nextTabHilight, {sync:true}),
				new Effect.Morph(previousTab, {style:'normal'}),
				new Effect.Morph(nextTab, {style:'selected'})
			], { 
				duration: 0.8,
				transition: Effect.Transitions.easeOutQuad,
				beforeStart: function () {Accordion.animating = true;},
				afterFinish: function () {
					previousBackdrop.hide();
					previousTab.className = '';
					nextTab.className = 'selected';
					Accordion.animating = false;
				}
			});
		}
		this.currentTab = tabid;
	}
};


NewsSlider = {
	current:null,
	isAnimating: false,
	next: function () {
		if (NewsSlider.isAnimating) return;
		
		var a = NewsSlider.current;
		var b = a.next();
		if (!b) b = a.up().down();
		if (a==b) return; //we've only got one news post
		
		NewsSlider.current = b;
		
		NewsSlider.isAnimating = true;
		new Effect.Parallel([
			new Effect.BlindUp(a, {sync:true}),
			new Effect.BlindDown(b, {sync:true})
		], {
			duration:.5,
			afterFinish: function () {
				NewsSlider.isAnimating = false;
			}
		});
	},
	prev: function () {
		if (NewsSlider.isAnimating) return;

		var a = NewsSlider.current;
		var b = a.previous();
		if (!b) b = a.up().childElements().last();
		if (a==b) return; //we've only got one news post
		
		NewsSlider.current = b;
		
		NewsSlider.isAnimating = true;
		new Effect.Parallel([
			new Effect.BlindUp(a, {sync:true}),
			new Effect.BlindDown(b, {sync:true})
		], {
			duration:.5,
			afterFinish: function () {
				NewsSlider.isAnimating = false;
			}
		});
	}
	
}
Event.observe(document, 'dom:loaded', function () {
	NewsSlider.current = $$('#newsboxes>a').first();
	
});
