var Agenda = function(){
	
	var resultContainer = null;
	var currentCategory = 0;
	var selectedDate = null;
	var filterList = null;
	var currentTab = '';
	var currentLevel = 'week'; // day/week/month
	var currentId = 0;
	var firsttime = false;
	
	function init(){
	
		resultContainer = $('#agenda-results');
		
		// Use the datepicker we already use in searchbox
		SearchBox.initDatePicker('agenda-datepicker', function(e) {
			if (!firsttime) {
				firsttime = true;
				return false;
			}
			openTab("day");
			update(getDate());
		});
			
		// Trigger the update function when date has changed
		$('#agenda-form select').change(function() {
			update(getDate());
		});
		
		filterList = $('#filterlist'); 
		// update when category has changed   
		filterList.find('a').click(function(){
			
			// if somebody changes the filter, he/she doenst want to look at 1 id anymore
			if(currentId != 0) currentLevel = 'week';
			currentId = 0; 
			if ( !$(this).parent('li').hasClass('applied')){
				setCategory( this.id.split('-')[1] );		
			}else{
				setCategory(0);
			}
			
			update( getDate() );
			return true;
		});
		
		initTabs();

		if (document.location.hash.indexOf('#') > -1) {
			var category = document.location.hash.substring(1);
			$('#filterlist a').each(function() {
				var obj = $(this);
				if (obj.html() == category) {
					if ( !obj.parent('li').hasClass('applied')) {
						setCategory( this.id.split('-')[1] );
					} else {
						setCategory(0);
					}		
					update(getDate());
					return false;
				}
			});
		}
	}
	
	function getDate(){
		return $('#pick-year').val() + '-' + $('#pick-month').val() + '-' + $('#pick-day').val();
	}
	
	function setCategory( id ){
		currentCategory = id;
		
		// Show which cat is selected
		filterList.find('li').removeClass('applied');
		if ( id > 0 ){
			$('#cat-' + currentCategory).parent('li').addClass('applied');	
		}
	}
	
	function initTabs(){
		$('#opentab-day, #opentab-week, #opentab-month').live( 'click', function(){
			openTab( this.id.split('-')[1]);
			update( getDate() );
			return false;
		});
	}
	
	function openTab( tabName ){
		currentLevel = tabName;
		currentId = 0;
		$('ul.tabs li').removeClass('active');
		$('div.tab').removeClass('tabActive');
		$('#opentab-' + tabName).parent('li').addClass('active');
		return false; 
	}
	
	function update( date ){

		if ( date == undefined ){
			date = getDate();
		}
		
		util.setLoading(true);
		var params  = {
			date: date ,
			categoryId : currentCategory,
			level: currentLevel,
			id: currentId
		};
		Ajax.doRequest('agenda',params, handleUpdate );

	}
	
	function handleUpdate( resp ){
		resultContainer.html(resp.data.html);
		util.setLoading(false);
	} 

	function setCurrentId( id ){
		currentId = id;
		currentLevel = 'item';
	} 
	
	return {
		init:init,
		getDate: getDate,
		update: update,
		setCurrentId : setCurrentId
	};
	
}();
