var TDN = {

	panes:new Object(),
	tabs:new Object(),
	selectedPaneName:String,
	timer:0,
	duration:10000,
	timerItems:Array('top','middle','bottom'),
	currentTimerItem:-1,
	fadeOutDuration:800,
	fadeInDuration:800,
	
	addTab:function(inName,el) {
		TDN.tabs[inName] = el;
	},
	
	addPane:function(inName,el) {
		TDN.panes[inName] = el;
		if (TDN.panesComplete()) {
			TDN.next();
		}
	},
	
	panesComplete:function() {
		return TDN.panes['top'] && TDN.panes['middle'] && TDN.panes['bottom'];
	},
	
	tabsComplete:function() {
		return TDN.tabs['top'] && TDN.tabs['middle'] && TDN.tabs['bottom'];
	},
	
	hidePanes:function() {
		if (!TDN.panesComplete()) return;
		TDN.panes['top'].style.display = 'none';
		TDN.panes['middle'].style.display = 'none';
		TDN.panes['bottom'].style.display = 'none';
	},
	
	resetTabs:function() {
		if (!TDN.tabsComplete()) return;
		TDN.tabs['top'].style.backgroundPosition = '0px 0px';
		TDN.tabs['middle'].style.backgroundPosition = '0px -80px';
		TDN.tabs['bottom'].style.backgroundPosition = '0px -160px';
		twiki.CSS.removeClass(TDN.tabs['top'], 'hover');
		twiki.CSS.removeClass(TDN.tabs['middle'], 'hover');
		twiki.CSS.removeClass(TDN.tabs['bottom'], 'hover');
	},
	
	hilightTab:function(inName) {
		TDN.tabs[inName].style.backgroundPosition = '0px -999px';
		twiki.CSS.addClass(TDN.tabs[inName], 'hover');
	},
	
	showPane:function(inName,isAutoPilot) {
		TDN.selectedPaneName = inName;
		if (isAutoPilot) {
			TDN.changeOpac(0,inName);
			TDN.fadeIn(inName);
		} else {
			TDN.hidePanes();
			TDN.changeOpac(100,inName);
			for (var i=0; i<TDN.timerItems.length; i++) {
				if (TDN.timerItems[i] == TDN.selectedPaneName) {
					TDN.currentTimerItem = i;
				}
			}
		}
		TDN.panes[inName].style.display = 'block';
		TDN.updateTabs();
	},
	
	updateTabs:function() {
		if (!TDN.tabsComplete()) return;
		TDN.resetTabs();
		TDN.hilightTab(TDN.selectedPaneName);
	},
	
	next:function() {
		if (TDN.currentTimerItem != -1) {
			var currentItem = TDN.timerItems[TDN.currentTimerItem%3];
			TDN.fadeOut(currentItem);
		}
		TDN.currentTimerItem++;
		var newItem = TDN.timerItems[TDN.currentTimerItem%3];
		TDN.showPane(newItem, 1);
		TDN.stopTimer();
		TDN.startTimer();
	},
	
	stopTimer:function() {
		clearTimeout(TDN.timer);
		TDN.timer = null;
	},
		
	startTimer:function() {
		if (!TDN.timer) {
			TDN.timer = setTimeout("TDN.next()", TDN.duration);
		}
	},
	
	fadeOut:function(inName) {
		TDN.opacity(inName, 100, 0, TDN.fadeOutDuration);
	},
	
	fadeIn:function(inName) {
		TDN.opacity(inName, 0, 100, TDN.fadeInDuration);
	},
	
	opacity:function(id, opacStart, opacEnd, millisec) {
		//speed for each frame
		var speed = Math.round(millisec / 100);
		var timer = 0;
		
		//determine the direction for the blending, if start and end are the same nothing happens
		if(opacStart > opacEnd) {
			for(i = opacStart; i >= opacEnd; i--) {
				setTimeout("TDN.changeOpac(" + i + ",'" + id + "')",(timer * speed));
				timer++;
			}
		} else if(opacStart < opacEnd) {
			for (i = opacStart; i <= opacEnd; i++) {
				setTimeout("TDN.changeOpac(" + i + ",'" + id + "')",(timer * speed));
				timer++;
			}
		}
	},
	
	changeOpac:function(opacity, id) {
		var el = TDN.panes[id];
		var object = el.style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
};

var rules = {

	'#splash #benefits #lefttabs' : function(el) {
		el.onmouseover = function () {
			TDN.stopTimer();
		}
		el.onmouseout = function () {
			TDN.startTimer();
		}
	},

	'#splash #benefits #lefttabs li#top a.tab' : function(el) {
		TDN.addTab('top', el);
		el.onmouseover = function () {
			TDN.stopTimer();
			TDN.showPane('top');
		}
		el.onmouseout = function () {
			TDN.updateTabs();
		}
		el.onclick = function () {
			return false;
		}
	},
	'#splash #benefits #lefttabs li#middle a.tab' : function(el) {
		TDN.addTab('middle', el);
		el.onmouseover = function () {
			TDN.stopTimer();
			TDN.showPane('middle');
		}
		el.onmouseout = function () {
			TDN.updateTabs();
		}
		el.onclick = function () {
			return false;
		}
	},
	'#splash #benefits #lefttabs li#bottom a.tab' : function(el) {
		TDN.addTab('bottom', el);
		el.onmouseover = function () {
			TDN.stopTimer();
			TDN.showPane('bottom');
		}
		el.onmouseout = function () {
			TDN.updateTabs();
		}
		el.onclick = function () {
			return false;
		}
	},
	'#splash #benefits #lefttabs li#top ul' : function(el) {
		TDN.addPane('top', el);
		el.onmouseover = function () {
			TDN.stopTimer();
		}
	},
	'#splash #benefits #lefttabs li#middle ul' : function(el) {
		TDN.addPane('middle', el);
		el.onmouseover = function () {
			TDN.stopTimer();
		}
	},
	'#splash #benefits #lefttabs li#bottom ul' : function(el) {
		TDN.addPane('bottom', el);
		el.onmouseover = function () {
			TDN.stopTimer();
		}
	}
};
Behaviour.register(rules);