$(document).ready(function(){

	$('#category').change(function(){
		if(this.selectedIndex != 0){
			$('#cal-content').html('').css({background:'url(/_img/ajax-loader.gif) no-repeat center center', height:'300px'});
			$.get('/ajax-events.php', { category:$(this).val() }, function(o){
				$('#cal-content').html(o).css({background:'none', height:'auto'}).fadeIn('slow');
			})
		}
	});
	
	$('#month').change(function(){		
		$('#cal-content div').fadeOut('slow');
		$('#month'+$(this).val()).fadeIn('slow');		
		setCookie('month', 'month'+$(this).val());
	});
	
	// tab switcher
	$('#caltabs a').click(function(){
		$('.active').removeClass('active');
		$(this).parent().addClass('active');
		
		// hide all divs
		$('.events-container').hide();		
		
		// slide open selected div
		var div = $(this).attr('id')+'-content';		
		$('#'+div).slideToggle('slow');
		
		// hide filters if not cal-content
		if(div !== 'cal-content') {
			$('#filters').fadeOut('slow');
		} else {
			$('#filters').fadeIn('slow');
		}
		return false;
	});
	
	// category toggle
	$('#categories label').bind('click', showHideCats);
	
});

function showHideCats(e){
	
	// check if the clicked one is all events
	if($(e.target).attr('id') == 'all-categories'){
		
		$('#categories input').attr('checked', 'checked');
		$(e.target).attr('checked', 'checked');
		$('.event').show();
		
	} else {
	
		var cats = '';
		// loop through list of inputs and find the checked ones
		$('#categories input').each(function(){
			if($(this).attr('checked')) cats += '.'+$(this).attr('id')+',';		
		});
				
		// remove trailing comma
		cats = cats.slice(0, -1);
		
		// if no categories selected, show all
		if(cats.length <=0){
			$('.event').fadeIn('slow');
		} else {
			
			// uncheck all categories
			$('#all-categories').attr('checked','');
			
			// hide all events
			$('#listing .event').hide();
	
			// show only items in cats
			$(cats).show();
		}
	}
}

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