/*
*	home.js
*		JavaScript to manage the DPAC homepage
*	
*	Author:
*		Adam Fisher, DPAC 2008
*/

var execute;
function windowResize() {
	$clear(execute);
	var delay = Browser.Engine.trident ? 200 : 0;
	execute = (function() {
		$('contentTable').setStyle('height', 0);
		$('selectionRightColor').setStyle('height', 120);
		var difference = Browser.Engine.trident ? $('curve').getStyle('height').toInt() : $('curve').getStyle('height').toInt() - 2;
		var height = window.getScrollHeight().toInt() - $('TG-header').getStyle('height').toInt() - $('footer').getStyle('height').toInt();
		$('contentTable').setStyle('height', height);
		$('selectionRightColor').setStyle('height', height - difference);
		$('header').setStyle('width', $('footer').getStyle('width'));
	}).delay(delay);
}
var TopicControl = new Class({
	initialize: function(topics, start) {
		this.topics = $H(topics);
		this.current = start;
		this.contentTimer;
		$('selectionCell').setStyle('background-image', 'none');
		$$('.menuLink').setStyle('cursor', 'pointer');
		this.fx = {
			selectCell: new Fx.Tween('selectionCell', {
				duration: 300,
				link: 'cancel',
				property: 'backgroundColor'
			}),
			topics: new Hash({})
		};
		this.topics.each(function(obj, topic) {
			var li = $(topic);
			var kids = li.getChildren();
			li.store('color', '#' + obj.color);
			this.fx.topics[topic] = {
				topicOver: new Fx.Elements([kids[2], kids[1]], {
					duration: 300,
					link: 'cancel'
				}),
				topicClick: new Fx.Tween(kids[0], {
					duration: 300,
					link: 'cancel',
					property:  'backgroundColor'
				})
			};
			li.addEvents({
				mouseenter: function() {
					this.over(topic);
				}.bind(this),
				mouseleave: function() {
					this.out(topic);
				}.bind(this),
				click: function(e) {
					new Event(e).stop();
					this.click(topic);
				}.bind(this)
			});
		},
		this);
	},
	over: function(topic) {
		this.fx.topics[topic].topicOver.cancel().set({
			'0': {
				color: $(topic).retrieve('color')
			},
			'1': {
				'background-color': $(topic).retrieve('color')
			}
		});
	},
	out: function(topic) {
		this.fx.topics[topic].topicOver.start({
			'0': {
				color: '#000000'
			},
			'1': {
				'background-color': '#ffffff'
			}
		});
	},
	click: function(topic) {
		if (topic != this.current) {
			$clear(this.contentTimer);
			this.fx.topics.each(function(fx) {
				fx.topicClick.cancel().set('#ffffff');
			});
			$('rightImgWrap').setStyle('background-image', 'none');
			this.fx.selectCell.start($(topic).retrieve('color')).chain(function() {
				$('rightImgWrap').setStyle('background-image', 'url(/home_page_contents/rightback/' + $(topic).retrieve('color').substr(1) + '.gif)');
			});
			this.fx.topics[topic].topicClick.start($(topic).retrieve('color'));
			$(this.current + 'Content').setStyle('display', 'none');
			this.contentTimer = (function() {
				$(topic + 'Content').setStyle('display', 'block');
			}).delay(150);
			this.current = topic;
		}
	}
});
window.addEvents({
	'domready': function() {
		$$('selectionCell', 'rightImgWrap').setStyle('background-image', 'none');
		$('pageContents').setStyle('display', 'none');
		$(start + 'Content').setStyle('display', 'block');
		new TopicControl(topics, start);
		windowResize();
	},
	'resize': windowResize,
	load: windowResize
});