/*
Name:		AIST Awards - JavaScript Enhancement
Author:		RBurnie
Created:	31-07-08
===========================================================
*/

/* = GENERAL ENHANCEMENTS WE WANT TO RUN IN BACKGROUND = */
window.addEvent('domready', function() {
	// = sIFR REPLACEMENTS = ==============================
	if(typeof(sIFR)=='function') {
		var text_white = ['h1', 'h2.post-heading', 'h4', 'h5', 'h6'];
		var text_blue = ['h2', 'h3', 'caption', 'legend'];
		// NB: 	process white first otherwise h2.post-heading will
		//		still come thu blue like other h2's
		$each(text_white,function(selector) {
			sIFR.replaceElement('#main '+selector, named({
				sFlashSrc: '/scripts/awards08/sIFR/helvetica-67.swf', 
				sColor: '#ffffff',
				sBgColor: '#0D456D',
				sCase: 'upper',
				sWmode: 'transparent'
			}));
		});
		$each(text_blue,function(selector) {
			sIFR.replaceElement('#main '+selector, named({
				sFlashSrc: '/scripts/awards08/sIFR/helvetica-67.swf', 
				sColor: '#B8D7F1',
				sBgColor: '#0D456D',
				sCase: 'upper',
				sWmode: 'transparent'
			}));
		});
		sIFR.replaceElement('#sidebar h2', named({
				sFlashSrc: '/scripts/awards08/sIFR/helvetica-67.swf', 
				sColor: '#B8D7F1',
				sBgColor: '#0D456D',
				sCase: 'upper',
				sWmode: 'transparent'
			}));
	}
	
	/* = GENERATE TABLE OF CONTENTS of h2's = */
	
	//new TOC();
	
	/* = MAKE TABLES STRIPEY = */
	new Zebra_stripes();
	
	
});

var TOC = new Class({
	initialize: function() {
		var main = $('main');
		var headings = main.getElements('h2');
		var toc = new Element('div').set('id','contents').adopt(new Element('h2').set('text','Contents'));
		var ul = new Element('ul').addClass('clearfix');
		
		$each(headings, function(heading,i){
			if(!heading.hasClass('post-heading')) {
				var id = heading.get('id');
				if(!$chk(id)) {
					id = 'toc_'+i;
					heading.set('id',id);
				}
				
				var html, text;
				if(heading.hasClass('sIFR-replaced')) {
					var span = heading.getElement('span.sIFR-alternate');
					//html = span.get('html');
					text = span.get('text');
				}else{
					//html = heading.get('html');
					text = heading.get('text');
				}
				
				var li = new Element('li').inject(ul);
				var a = new Element('a').set({
					'href': '#'+id,
					'title': text,
					'text': text
				}).inject(li);
				
			}
		});
		
		if(ul.getChildren().length > 0) {
			if(headings[0].hasClass('post-heading')){
				toc.adopt(ul).inject(headings[0],'after');
			}else{
				toc.adopt(ul).inject(main,'top');
			}
		}
	}
});

/* = ZEBRA STRIPES = ======================================
	Adds class 'odd' or 'even' to tr's in tbody 
	(if tables present)
*/
var Zebra_stripes = new Class({
	initialize: function() {
		var tables = $$('table');
		// Exit if no tables
		if(tables.length==0) return;
		
		$each(tables, function(table) {
			var rows = table.getElement('tbody').getElements('tr');
			$each(rows, function(row, i) {
				row.addClass( (i % 2 == 0 ? 'even' : 'odd') );
			});
		});
	}
});