

function btn_author_filter(calendar,header){
	
	return false;
}

btn_author_filter.split = function(str){
	return ['btn_author_filter'];
}



jQuery(document).ready(function($){
	$('.rhcalendar').on('rhc_loaded', function(e){
		var calendar = $(this);
		var fc = $(this).find('.fullCalendar');
		var data = $(this).data('Calendarize');
		//author filter
		$(this).find('.fc-button-btn_author_filter').each(function(i,el){
			
			//data.modes.view.options.rhcadd.authors
			dropdown = $("<select class='selectpicker'></select>");
			dropdown
				.append("<option value=''>" +$(el).html() + "</option>")
			;
			$.each( data.modes.view.options.rhcadd.authors, function(j,o){
				dropdown
					.append("<option value='" + o.value+ "'>" + o.label + "</option>")
				;
			});
			
			$(this).replaceWith( dropdown );

			$(dropdown).wrap("<div class='tax_filter_holder fc-button fc-state-default rh-flat-ui fc-head-control tax_filter_menu_medium'></div>");
			
			$(dropdown).selectpicker({
				style: 'btn-small btn-taxfilter btn_tax',
				size: 10
			});

			function removeUrlParameter(url, parameter) {
				var urlParts = url.split('?');

			  	if (urlParts.length >= 2) {
					var urlBase = urlParts.shift();
					var queryString = urlParts.join('?');

					var prefix = encodeURIComponent(parameter) + '=';
					var parts = queryString.split(/[&;]/g);

					for (var i = parts.length; i-- > 0; ) {
				  		if (parts[i].lastIndexOf(prefix, 0) !== -1) {
							parts.splice(i, 1);
				  		}
					}

					url = urlBase + '?' + parts.join('&');
				}

			  	return url;
			}
			
			function author_filter_change(args){				
				fc = args.data.fc;
				data = args.data.data;
				
				selected_author = $(this).val();

				var current_view = fc.fullCalendar('getView');
				var fc_options = data.modes[data.mode].options;
				fc_options.events_source_query_original = fc_options.events_source_query_original?fc_options.events_source_query_original:fc_options.events_source_query;
				
				filter='';
				if( selected_author!='' ){
					filter = '&author=' + selected_author;
				}
	
				if(''!=filter){
					fc.fullCalendar('removeEventSources');
					url = fc_options.events_source_query_original;
					url = removeUrlParameter(url, 'author');
					fc_options.events_source_query = url + filter;
				}else{
					fc.fullCalendar('removeEventSources');
					fc_options.events_source_query = fc_options.events_source_query_original;
				}				
				
				var new_source = function(start, end, callback) {
					$.fn.Calendarize.events_source(start, end, callback, fc_options);
				};
				fc.fullCalendar('addEventSource',new_source);
				

				$(this).parents('.fullCalendar').find('.fc-button-rhc_search').trigger('click');			
			}
			
			dropdown.unbind('change', author_filter_change).bind('change', {data:data,fc:fc,calendar:calendar}, author_filter_change);

		});
		
		
		

	});
	//
});