// detect if nav items have sub items and give htem a class of more
$(document).ready(function(){
	var lis = id('topbar').getElementsByTagName('LI');
	for(var i=0; i<lis.length; i++){
		if(lis[i].getElementsByTagName('UL')[0]){			
			$(lis[i]).addClass('more');
		}
	}
});

$(document).ready(function(){

	$('#articleLists').change(function(){
		if(this.selectedIndex == 1) window.location = '/mcms_page_articlelist_sort.php?groupby=series';
		if(this.selectedIndex == 2) window.location = '/mcms_page_articlelist_sort.php?groupby=category';
		if(this.selectedIndex == 3) window.location = '/mcms_page_articlelist_sort.php?groupby=month';
		if(this.selectedIndex == 4) window.location = '/mcms_page_articlelist_sort.php?groupby=author';		
	});
	
	$('#all-messages').click(function(){
		//$('#loading').show();
		$.get('/ajax-sermons.php', {
			'groupby':'all',
			'key':'current',
			'val':'current'
		},
		function(o){
			var response = o.split('<!--split-->');
			$('#primaryContent > h2').html(response[0]);
			$('#sermon-content').html(response[1]);			
			$('#current').hide();
			//$('#loading').hide();
			$('.sorter').attr({selectedIndex:0});		
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		//$('#loading').show();
		if(this.selectedIndex != 0){
			$.get('/ajax-sermons.php', { 
				'groupby':this.id,
				'key':this.id,
				'val':this.value			
				}, 
				function(o){
					var response = o.split('<!--split-->');
					$('#primaryContent > h2').html(response[0]);
					if(response[1] == '') {
						$('#sermon-content').html('<p>No sermons could be found for the selected filtering option.</p>');
					} else {
						$('#sermon-content').html(response[1]);			
					}
					$('#current').addClass('hidden');
					//$('#loading').hide();
					$('.sorter').attr({ selectedIndex:0});
				}
			);
		}
	});	
});

$(document).ready(function(){
	$('.external').click(function(){
		window.open(this.href);
		return false;
	});
});

function id() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}