function actions(){
	$('.sl_video a').bind('click', playVideo);	
	$('#category').bind('change', changeCategory);
	$('#month').bind('change', changeMonth);	
}

function playVideo(){
	waspPopup($(this).attr('href'), 330, 220);
	return false;
}

function changeCategory(){
	if(this.selectedIndex != 0){
		var group = $('#switcher li.active a').attr('id').split('-')[0];
		$('#the-calendars').html('').css({background:'url(/images/ajax-loader.gif) no-repeat center center', height:'300px'});
		$.get('/events.php', { category:$(this).val(), group:group }, function(o){
			$('#the-calendars').html(o).css({background:'none', height:'auto'}).fadeIn('slow');
		})
	}
}

function changeMonth(){
	toggle('month'+$(this).val(), 12, 'month', 'Y');
}

$(document).ready(function(){	
	actions();
	
	// events switcher
	$('#switcher a').click(function(){
		var group = $(this).attr('id').split('-')[0];
		var current = '.'+$('#switcher .active a').attr('id');
		
		$.get('/events.php', { group:group, category:'all' }, function(o){
			$('#the-calendars').html(o);
		});
		
		$('#switcher .active').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});


	$('#search_term').focus(function(){
		$(this).val('');
	});
	
	
	$('.play').click(function(){
		var id 	= this.id.split('-')[1];
		var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=2399&sermonid='+id+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
		window.open(url, 'wimpyMP3player','width=350,height=140,title=blah');
		return false;	
	});
	$('#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-->');
			$('#sermon-content').html(response[1]);			
			$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
			$('#current').hide();
			$('.sorter').attr({selectedIndex:0});		
			$('#all-messages').focus();
		    Logos.ReferenceTagging.lbsBibleVersion = "ESV";
		    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
		    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
		    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
		    Logos.ReferenceTagging.tag();
			actions();
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		$('#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-->');
					$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
					$('#current').hide();
					$('.sorter').attr({selectedIndex:0});		
					$('#all-messages').focus();
				    Logos.ReferenceTagging.lbsBibleVersion = "ESV";
				    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
				    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
				    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
				    Logos.ReferenceTagging.tag();			
				    actions();		
				}
			);
		}
	});	
	
	$('#articleLists').change(function(){
		var bod = $('body').attr('id');
		if(bod == 'arlington'){
			if(this.selectedIndex == 1) window.location = '/mcms_page_arlington_articlelist_sort.php?groupby=series';
			if(this.selectedIndex == 2) window.location = '/mcms_page_arlington_articlelist_sort.php?groupby=category';
			if(this.selectedIndex == 3) window.location = '/mcms_page_arlington_articlelist_sort.php?groupby=month';
			if(this.selectedIndex == 4) window.location = '/mcms_page_arlington_articlelist_sort.php?groupby=author';
		} else {
			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');
});

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}