
var InfoViaKort = new Class({
	
	tabs: null,
	search: null,
	//rejseplanen: null,
	
	moduleAction: null,
	moduleName: null,
	
	initialize: function()
	{
		this.search = new InfoViaKortSearch();
		//this.rejseplanen = new Rejseplanen();
	
		this.tabs = $$('div#searchbar div.tabs a');
		this.tabs.each(function(tab) {tab.addEvent('click', this.toggleTab.bind(this,tab))}, this);
		
		// preselect tab based on action
		if (this.getModuleAction().contains("route")) {
			this.toggleTab(this.tabs[0]);
		} else {
			this.toggleTab(this.tabs[1]);
		}
		
		// preselect displayMode based on module and add event to toggle displayMode
		$$('div#displayMode input').each(function(input) {
			input.addEvent('click', this.toggleDisplayMode.bind(this,input));
			if (input.id == "displayModeMap" && this.getModuleName() == "map") input.checked = "checked"; 
			if (input.id == "displayModeText" && this.getModuleName() == "text") input.checked = "checked"; 
		}, this);
		
		this.initCalendar();
	},
	
	initCalendar: function()
	{
		var start = this.search.dateSelect.intervalStart;
		var end = this.search.dateSelect.intervalEnd;
		
		var days = ['Søndag', 'Mandag', 'Tirsdag', 'Ondsdag', 'Torsdag', 'Fredag', 'Lørdag'];
		var months = ['Januar', 'Februar', 'Marts', 'April', 'Maj', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'December'];
		
		var myCal = new Calendar({calendarDate:'Ymd'}, {classes:['fynbuscalendar'], offset:1, direction:0, tweak:{x: 0, y: 0}, draggable:0, boundsInterval:[start,end], 'days':days, 'months':months});
		
		myCal.addEvent('onHideComplete', this.search.updateSelectionFromInput.bind(this.search, $('calendarDate')));
	},

	toggleTab: function(tab)
	{
		//this.search.searchInput.value = '';
		
		var searchLabel = $('searchLabel');
		if (!searchLabel.initialValue) searchLabel.initialValue = searchLabel.get('html');
		
		if (tab.title == 'stop') {
			searchLabel.set('html', searchLabel.initialValue);
			this.search.autoCompleter.selectMode = 'type-ahead';
			this.search.autoCompleter.typeAhead = true;
			this.search.autoCompleter.request.options.url = 'text/suggestpoint';
			this.search.searchForm.action = 'text/stops';
			this.moduleAction = 'stops';
		} else {
			searchLabel.set('html', searchLabel.title);
			this.search.autoCompleter.selectMode = 'pick';
			this.search.autoCompleter.typeAhead = false;
			this.search.autoCompleter.request.options.url = 'text/suggestroute';
			this.search.searchForm.action = 'text/routes';
			this.moduleAction = 'routes';
		}
		
		this.search.searchInput.focus();
		
		this.tabs.each(function (item,index) {item.getParent().removeClass('active');});
		tab.getParent().addClass('active');
	},
	
	toggleDisplayMode: function(displayMode)
	{
		var mode = displayMode.value;
		var currentModule = infoViaKort.getCurrentModule();
		var currentAction = infoViaKort.getCurrentAction();
		var currentParameter = infoViaKort.getCurrentParameter();
		
		if (mode == "map" && currentAction != "stop" && currentAction != "route") {
			infoViaKort.redirect("map/show");
		} else if (mode == "text" && !currentAction.contains("route") && !currentAction.contains("stop")) {
			infoViaKort.redirect("text/index");
		} else {
			infoViaKort.redirect(mode + "/" + currentAction + "/" + currentParameter);
		}
	},
	
	getModuleName: function()
	{
		if (this.moduleName) return this.moduleName;
		this.moduleName = this.getCurrentModule(); 
		return this.moduleName;
	},
	
	getModuleAction: function()
	{
		if (this.moduleAction) return this.moduleAction;
		this.moduleAction = this.getCurrentAction();
		return this.moduleAction;
	},
	
	getCurrentModule: function()
	{
		return window.location.pathname.contains('text') ? "text" : "map";
	},
	
	getCurrentAction: function()
	{
		var p = window.location.pathname;
		var start = p.indexOf('/',1)+1;
		var end = p.indexOf('/',start);
		return (end==-1) ? p.substring(start) : p.substring(start,end);
	},
	
	getCurrentParameter: function()
	{
		var matches = window.location.pathname.match(/\/.+\/.+\/(.+)/);
		return (matches) ? matches[1] : '';
	},
	
	redirect: function(relativeUrl)
	{
		var url = window.location.protocol + '//' + window.location.host + '/' + relativeUrl;
		if (getSearchDate()) url += "?searchDate=" + getSearchDate();
		window.location.href = url;
	},

	redirectClick: function(url)
	{
		url = url.replace(/\/+$/, '');
		if (getSearchDate()) url += "?searchDate=" + getSearchDate();
		window.location.href = window.location.protocol + '//' + window.location.host + '/' + url;
		return false;
	}
});

var infoViaKort;
window.addEvent('domready', function() {
	infoViaKort = new InfoViaKort();
});