document.write('<div id="traildiv"></div>');

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(){
	$('#media #back').bind('click', backAction);
	$('.pagination a').bind('click', paginateAction);
	$('.play, .sl_listen a').bind('click', playAudio);
	$('.series').bind('click', sermonSeries);
}

function backAction(e){
	$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
	$.get('/ajax-sermons.php', { back:1 }, function(o){
		$('#sermon-content').html(o).css({height:'auto', background:'none'});			
		$('#main h2 span').html('');
		actions();
	});
	return false;	
}

function paginateAction(e){
	$('#sermon-content').html('').css({background:'url(/images/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-->');
		$('#main > h2').html(response[0]);
		$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});							
		//$('#sidebar a').focus();
		actions();
	});	
	return false;	
}

function sermonSeries(){
	$('#sermon-content').html('').css({height:'300px', background:'url(/images/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-->');
				$('#main > h2').html(response[0]);
				$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
				$('#current').hide();
				$('.sorter').attr({selectedIndex:0});		
				//$('#sidebar a').focus();
				actions();
			}
		);
	return false;

}

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

$(document).ready(function(){
	actions();	
	
	$('#service-times a img').click(function(){
		$('#shelf').slideToggle();
		if($(this).hasClass('down')){
			$(this).removeClass('down');
			$(this).attr('src','/mediafiles/contact-button.gif');			
		} else {
			$(this).addClass('down').attr('src','/images/contact_up_btn.gif');		
		}
		return false;
	});
	
	$('#media .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-->');
					$('#main > h2').html(response[0]);
					$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
					$('.sorter').attr({selectedIndex:0});		
					//$('#sidebar a').focus();
					//$('#pagination a').bind('click', paginate);					
					actions();
				}
			);
		}
	});	
	
	$('#media-articles .sorter').change(function(){
		var groupby = $(this).attr('id');
		var value 	= $(this).val();
		
		$.get('/ajax-articles.php', { groupby:groupby, value:value }, function(o){
			$('#article-list').html(o).css({height:'auto', background:'none'});							
			actions();
		});
		
	});	
	
	$('#tabs a:first').addClass('active').click(function(){
		$('#tabs a.active').removeClass('active');
		$(this).addClass('active');
		$('#content-one').show();
		$('#content-two').hide();
		return false;
	});
	
	$('#tabs a:last').click(function(){
		$('#tabs a.active').removeClass('active');
		$(this).addClass('active');
		$('#content-one').hide();
		$('#content-two').show();	
		
		sIFR.replace(vhand, {
			selector:'#welcome h2',
			wmode:'transparent',
			css:'.sIFR-root { font-size:44px; color:#483726; }'
		});		
			
		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';	
	});	
	
	$('.external').attr('target', '_blank');
});
