$(document).ready(function(){
	$('#all-messages').click(function(){
		$('#sermon-content').html('').css({height:'300px',background:'url(/images/ajax-loader.gif) no-repeat center center'});
		$.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]).css({height:'auto', background:'none'});	
			$('#current').hide();
			$('.play').click(function(){
				var id 	= this.id.split('-')[1];
				var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=1673&sermonid='+id+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
				window.open(url, 'wimpyMP3player','width=350,height=140,title=blah');
				return false;	
			});
			
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		if(this.selectedIndex != 0){
			$('#sermon-content').html('').css({height:'300px',background:'url(/images/ajax-loader.gif) no-repeat center center'});
			
			$.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]);
					$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});	
					$('#current').hide();
					
					// reset sorter selects to first value
					$('.sorter').attr({selectedIndex:0});		
					$('.play').click(function(){
						var id 	= this.id.split('-')[1];
						var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=1673&sermonid='+id+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
						window.open(url, 'wimpyMP3player','width=350,height=140,title=blah');
						return false;	
					});

				}
			);
		}
	});	
	
	$('.external').click(function(){
		window.open(this.href);
		return false;
	});		
	
	$('#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';
	});
	
	$('.play').click(function(){
		var id 	= this.id.split('-')[1];
		var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=1673&sermonid='+id+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
		window.open(url, 'wimpyMP3player','width=350,height=140,title=blah');
		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;
}
