$(document).ready(function(){

	// subnav testing
	if(id('snav')){
		var curs = getElementsByClassName(document.getElementById('snav'), 'li', 'current');
		var ULS = id('snav').getElementsByTagName('UL');
		var LIS = id('snav').getElementsByTagName('LI');		
		for(var i=0; i<LIS.length; i++){
			if(curs.length > 2) { 
				if(LIS[i] == curs[1] && LIS[i].getElementsByTagName('UL')[0]) {
					LIS[i].className += ' active';
				}
			} else if(LIS[i].getElementsByTagName('UL')[0]) {
				if(LIS[i].className !== 'current') LIS[i].className = 'active';				
				/*
				LIS[i].firstChild.onclick = function(){
					var self = this;
					if(this.parentNode == curs[0]){ 
						window.location = this.href;
					} else {
						this.style.backgroundImage = 'url(/images/down_arrow.gif)';
					}
					
					this.parentNode.getElementsByTagName('UL')[0].style.display = 'block';
					self.onclick = function(){
						window.location = self.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';		
	});
	
	$('#all-messages').click(function(){
		$('#sermon-content').css({
			background:'url(/images/ajax-loader.gif) no-repeat center center',
			height:'200px'
		}).html('');
		$('#current').hide();		
		$.get('/ajax-sermons.php', {
			'groupby':'all',
			'key':'current',
			'val':'current'
		},
		function(o){
			var response = o.split('<!--split-->');
			$('#content > h2').html(response[0]);
			$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
			$('.sorter').attr({selectedIndex:0});		
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		$('#sermon-content').css({
			background:'url(/images/ajax-loader.gif) no-repeat center center',
			height:'200px'
		}).html('');
		if(this.selectedIndex != 0){
			$('#current').hide();					
			$.get('/ajax-sermons.php', { 
				'groupby':this.id,
				'key':this.id,
				'val':this.value			
				}, 
				function(o){
					var response = o.split('<!--split-->');
					$('#content > h2').html(response[0]);
					if(response[1] == '') {
						$('#sermon-content').html('<p>No sermons could be found for the selected filtering option.</p>')
						.css({
							height:'auto',
							background:'none'
						}).focus();
					} else {
						$('#sermon-content').html(response[1])
						.css({
							height:'auto',
							background:'none'
						});			
					}
					$('.sorter').attr({ selectedIndex:0});
					$('#all-messages').focus().css({outline:'none'});					
				}
			);
		}
	});	
});

addEvent(window, 'load', sermonPlay);
function sermonPlay(){
	var plays = getElementsByClassName(document, 'a', 'play');
	for(var i=0; i<plays.length; i++){
		plays[i].onclick = function(){
			var id = this.id.split('-')[1];
			var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=1765&sermonid='+id+'&useSkin=skin_plain.xml';
			window.open(url, 'wimpyMP3player','width=350,height=140,title=blah');
			return false;
		}		
	}
}

function externo(){
	var exts = getElementsByClassName(document, 'a', 'external');
	for(var i=0; i<exts.length; i++) {
		exts[i].onclick = function(){
			window.open(this.href);
			return false;
		}
	}
}
addEvent(window, 'load', externo);

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;
}
/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com

*/

function getElementsByClassName(oElm, strTagName, strClassName){	
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);	
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");	
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }	
    return (arrReturnElements)
}

// ============ ADD EVENTS ================ //
function addEvent(obj, evType, fn){
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, true);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
	    return false;
    }
}