   function parseParams(url) {
      var params=url;
      if (url.match(/\?(.+)$/)) {
          // in case it is a full query string with ?, only take everything after the ?
          params = RegExp.$1;
      }
   	// split the params
   	var pArray = params.split("&");
   	// hash to store result
   	var pHash = {};
   	// parse each param in the array and put it in the hash
   	for(var i=0;i<pArray.length;i++) {
       	var temp = pArray[i].split("=");
   	    pHash[temp[0]] = unescape(temp[1]);
   	}
   	return pHash;
   }
   
   function actions(){
   	$('.sl_listen a, #mb_jukebox a').bind('click', playAudio);
   	$('.sl_video a, #mb_video a, .popup-video').bind('click', playVideo);	
		$('.pagination a').bind('click', paginateAction);
		$('.post-pagination a').bind('click', articlePaginateAction);
/*		$('li.al_listen.ajax').each(function() {
		   $(this).find('a:first-child').hide();
		});*/
		$('.series').bind('click', sermonSeries);
		// makes styling easier
      $('ul').each(function() {
         $(this).find('li:last-child').addClass('last');
         $(this).find('li:first-child').addClass('first');
      });
      $('a.share-this').each(function() {
         var shareLink = $(this).attr('href');
         var domainTest = shareLink.split(':');
         var domainSeg = domainTest[0];
         var domainName = document.domain;
         if (domainSeg !="http") {
            var shareURL = ("http://"+domainName+shareLink);
         } else {
            var shareURL = shareLink;
         }
         var shareTitle = $(this).attr('title');
         $(this).click(function() {
            return addthis_sendto();
         });
         $(this).hover(function() {
            return addthis_open(this, '', shareURL, shareTitle);
         }, 
         function() {
            addthis_close();
         }    
         );

      });
   }
   
	
	function paginateAction(e){
   	$('#sermon-content').html('').css({background:'url(/_img/ajax-loader.gif) no-repeat center center', height:'300px'});
   	var url = parseParams($(e.target).attr('href'));	
   	$.get('/ajax-sermons.php', url, function(o){			
   		var response = o.split('<!--split-->');
   		$('#content > h4').html(response[0]);
   		$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});							
   		actions();
   	});	
   	return false;	
   }
   
   function articlePaginateAction(e){
   	$('#post-content').html('').css({background:'url(/_img/ajax-loader.gif) no-repeat center center', height:'300px'});
   	var pieces = $(this).attr('href').split('?');
   	var url = pieces[1];
   	var aFilter = $('#post-content').attr('class').split('_');
   	url+="&module="+aFilter[0]+"&series="+aFilter[1]+"&cat="+aFilter[2]+"&howmany="+aFilter[3];
   	$.get('/ajax-media.php', url, function(o){			
   		$('#post-content').html(o).css({height:'auto', background:'none'});							
   		actions();
   	});	
   	return false;	
   }

   function sermonSeries(){
   	$('#sermon-content').html('').css({height:'300px', background:'url(/_img/ajax-loader.gif) no-repeat center center'});
   	$.get('/ajax-sermons.php', {
   		'groupby': 'series',
   		'key'	 : 'series',
   		'val'	 : $(this).attr('href')
   		}, function(o){
   				var response = o.split('<!--split-->');
   				$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
   				$('.sorter').attr({selectedIndex:0});		
   				//$('#sidebar a').focus();
   				actions();
   			}
   		);
   	return false;

   }

   function playAudio(){
   	var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=2669&sermonid='+this.id.split('sermon')[1]+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
   	wimpyPopPlayer(url,'wimpyMP3player','width=350,height=140,title=blah');
   	return false;		 
   };

   function playVideo(){
   	window.open('/video.php?video='+$(this).attr('href'), 'videoPlayer', 'width=560,height=315,scrollbars=0,statusbar=0,address=0');
   	return false;
   }
$(document).ready(function(){
   	actions();

	// Video Embedding
	var vid = 1;
	$('.video').each(function(){
		var vurl = $(this).attr('href');
		if(vurl) vurl = vurl.toString();
		
		$(this).before('<div id="video'+vid+'">'+vurl+'</div>');		
		$(this).remove();
		
		var flashvars = {file:vurl};
		
		var params = { 
			menu: "false",
			wmode:"transparent", 
			allowfullscreen:"true",
			allowscriptaccess:"always",
			allownetworking:"all",
			flashvars: "autostart=false&dock=true&bufferlength=3&frontcolor=ffffff&lightcolor=84a02f&controlbar=over&logo=/_fla/player/watermark-logo.png&plugins=viral-2,googlytics-1&skin=/_fla/player/skin-modieus.swf&viral.onpause=false&viral.allowdock=true&viral.matchplayercolors=true&stretching=exactfit"
			};
		var attributes = {  };
		
		if($(this).find('img').attr('src')){
			flashvars.image = $(this).find('img').attr('src');
		}
		swfobject.embedSWF("/_fla/player/player-licensed-5-1.swf", "video"+vid, "412", "232", "9.0.0", "/_fla/expressInstall.swf", flashvars, params, attributes);		
		$(this).remove();								
		vid++;			
	});
	
	/* SERMON SORTING */

	
	$('#all-messages').click(function(){
		window.location.hash = 'all';
		$('#sermon-content').html('').css({height:'300px', background:'url(/_img/ajax-loader.gif) no-repeat center center'});
		$.get('/ajax-sermons.php', {
			'groupby':'all',
			'key':'current',
			'val':'current'
		},
		function(o){
			var response = o.split('<!--split-->');
			$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
			actions();	
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		window.location.hash = this.id+'_'+this.value;
		if(this.selectedIndex !== 0){
			$('#sermon-content').html('').css({height:'300px', background:'url(/_img/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 })		
					actions();
				}
			);
		}

	});		
	
});
	/* END SERMON SORTING */