$(document).ready(function(){
   Cufon.replace('#nav > li a, #primaryContent h2');
   $('#quicklinks li:last').addClass('last');
   $('ul').each(function() {
      $(this).find('li:first-child').addClass('first');
      $(this).find('li:last-child').addClass('last');
   });
	/* SERMON SORTING */		
	// see if there is something in the query string to initially load
	var sort = window.location.hash;
	// assign topbar parents a special class
   $('#topbar li').each(function() {
      var kids = $(this).children().size();
      if (kids > 1) {
         $(this).addClass('parent');
      };
   });
   if ($('#banner').length == 0) {
      $('body').addClass('nobanner');
   };
	// Video Embedding
	var vid = 1;
	$('.video').each(function(){
		var vurl = $(this).attr('onclick');
		if(vurl) vurl = vurl.toString();
		
		vurl = vurl.split('waspPopup')[1];
		vurl = vurl.split('\',')[0].replace('\'', '').replace('(', '');
		$(this).before('<div id="video'+vid+'">'+vurl+'</div>');		
		$(this).remove();
		
		var flashvars = { 
			file: vurl,
			autostart:'false',
			frontcolor: 'ffffff',
			lightcolor: 'cc9900',
			skin: 'http://www.longtailvideo.com/jw/upload/overlay.swf',
			controlbar: 'over'
		};
		
		var params = { wmode:'transparent' };
		var attributes = {  };
		
		if($(this).find('img').attr('src')){
			flashvars.image = $(this).find('img').attr('src');
		}
		swfobject.embedSWF("/js/player.swf", "video"+vid, "620", "440", "9.0.0", "/_js/expressInstall.swf", flashvars, params, attributes);		
		$(this).remove();								
		vid++;			
	});
	
	if(sort){
		$('#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();
			$('#all-messages').focus();
			window.location.hash = 'all';
		}
		);
	}
	
	$('#all-messages').click(function(){
		window.location.hash = 'all';
		$('#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();
			$('#all-messages').focus();
		    Logos.ReferenceTagging.lbsBibleVersion = "NIV";
		    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
		    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
		    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
		    Logos.ReferenceTagging.tag();
			
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		window.location.hash = this.id+'_'+this.value;
		$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
		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]);
					$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
					$('#current').hide();					
					
					// reset sorter selects to first value
					$('.sorter').attr({ selectedIndex:0 })		
					$('#all-messages').focus().css('outline', 'none');
				    Logos.ReferenceTagging.lbsBibleVersion = "NIV";
				    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
				    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
				    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
				    Logos.ReferenceTagging.tag();
					
				}
			);
		}
	});		
	/* END SERMON SORTING */
	
	$('.play').click(function(){
		var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=2067&sermonid='+this.id.split('-')[1]+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
		wimpyPopPlayer(url,'wimpyMP3player','width=350,height=140,title=blah');
		return false;		 
	});
});

addEvent(window, 'load', sermonjump);
function sermonjump() {	
	if(id('articleLists')) { 
		id('articleLists').onchange = 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';
		}
	}
}

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;
    }
}