window.addEvent('domready', function() {
	top_menu();
	news_scroller.init();
	table_sort.init();
	table_sort_options.init();
	front_videoplayer.init();
	
	// Link "submit buttons"
	$(document.body).getElements('.searchsubmit').each(function(elem) {
		elem.addEvent('click', function(e) {
			e.stop();
			this.getParent().getParent().submit();
		});
	});
	
	// Autocomplete search fields
	$(document.body).getElements('.search_text').each(function(elem) {
		new Autocompleter.Request.HTML(elem, '/wp-content/themes/glow/autocomplete.php', {
			'indicatorClass': 'autocompleter-loading'
		});
	});
});

front_videoplayer = {
	init : function() {
		var videoplayer = $(document.body).getElement('#videoplayer');
		if (!videoplayer) { return false; }
		
		$(document.body).getElements('.grid a').each(function(elem) {
			elem.addEvent('click', function(e) {
				e.stop();
				front_videoplayer.update_ajax(elem.get('rel'))
			});
		});
		
		video_player = $(document.body).getElement('#theplayer');
		video_player_fx = new Fx.Morph(video_player, {transition:Fx.Transitions.Quad.easeInOut,link:'chain'});
	},
	update_ajax : function(video_id) {
		var req = new Request.HTML({ 
			url: '/wp-content/themes/glow/ajax_video.php', 
			method: 'get', 
			update: video_player,
			onComplete: function() {
				video_player_fx.start({'opacity': 1});
			}
		});
		video_player_fx.start({
			'opacity': 0
		}).chain(function() { req.send('id='+video_id); });
	}
};

function top_menu() {
	$('header').getElements('li.has_sub_menu').each(function(elem) {
		var ul = elem.getElement('ul');
		elem.addEvents({
			'mouseenter' : function(){
				ul.reveal({duration: 300, mode: 'vertical', link: 'cancel'});
				elem.toggleClass('active_sub_menu');
			},
			'mouseleave' : function(){
				ul.dissolve();
				elem.toggleClass('active_sub_menu');
			}
		});
	});
}

news_scroller = {
	init : function() {
		if (!$('news_scroller')) { return false; }
		var scrollFx = new Fx.Scroll($('news_scroller'), {
			wheelStops:false,
			link: 'chain',
			duration: 750,
			transition: 'quad:in:out'
		});
		
		var next_news = $('news_scroller').getFirst();
		
		var periodicalID;
		var begin = function() {
			periodicalID = (function() {
				next_news.clone().inject('news_scroller','bottom');
				next_news = next_news.getNext();
				scrollFx.toElement(next_news);
			}).periodical(5000);
		}

		begin();

		$('news_scroller').addEvents({
			mouseenter: function() {
				$clear(periodicalID);
			},
			mouseleave: begin
		});
		
		
	}
};

table_sort = {
	init : function() {
		if (!$(document.body).getElement('.table')) { return false; }
		
		var sortIndexTh = $(document.body).getElement('.table thead .sort');
		
		if(sortIndexTh) {
			var sortIndex = $(document.body).getElements('.table thead th').indexOf(sortIndexTh);
		} else {
			var sortIndex = null;
		}

		console.log(sortIndex);
		new HtmlTable($(document.body).getElement('.table'), {
			zebra: true,
			classZebra: 'odd',
			sortable:true,
			classSortable: '',
			classHeadSort: 'sort',
			classHeadSortRev: 'sortRev',
			classSortSpan: 'sortSpan',
			classGroup: 'group',
			classGroupHead: 'grouphead',
			classCellSort: 'sort',
			sortIndex: sortIndex
		});		
	}
};

table_sort_options = {
	init : function() {
		sort_options = $(document.body).getElement('.table_sort_options');
		if (!sort_options) { return false; }

		the_form = sort_options.getElement('form');
		ajax_container = $(document.body).getElement('.ajax');
		ajax_container_fx = new Fx.Morph(ajax_container, {transition:Fx.Transitions.Quad.easeInOut,link:'chain'});

		the_form.addEvent('submit', function(e) {
			new Event(e).stop();
		});

		sort_options.getElements('.alphabet li a').each(function(letter) {
			letter.addEvent('click', function(e){
				new Event(e).stop();
				table_sort_options.update_ajax();
				try {
					letter.getParent().getParent().getElement('.current').className = '';
				}
				catch(err) {}
				letter.getParent().className = 'current';
				$(document.body).getElement('#letter').value = letter.getParent().id;
			});
		});
		
		sort_options.getElements('select, input[type=checkbox]').each(function(element) {
			element.addEvent('change', function(e){
				table_sort_options.update_ajax();
			});
		});
		
		
		new Observer(sort_options.getElement('input[type=text]'), function() { table_sort_options.update_ajax() },{'delay':1000});
	},
	update_ajax : function() {
		var req = new Request.HTML({ 
			url: '/wp-content/themes/glow/ajax_table.php', 
			method: 'get', 
			update: ajax_container,
			onComplete: function() {
				ajax_container_fx.start({'opacity': 1});
				table_sort.init();
			}
		});
		ajax_container_fx.start({
			'opacity': 0
		}).chain(function() { req.get(the_form); });
	}
};