function Planner() {
	this.startDateElmId 					= 'start_date'; 													// Id for date-field
	this.periodElmId 						= 'period'; 														// Id for period-field
	this.defaultPeriod 						= 8;																// Default-period, if none is selected
	this.tourTypesBlockId 					= 'planner_step2_list';												// Id for the element containing tour-types
	this.tourCategoriesBlockId 				= 'planner_step3_list';												// Id for the element containing tour-categories
	this.tourCategoriesDefaultContentId 	= 'planner_step3_default_content'; 									// Id for tour-details default content
	this.tourDetailsBlockId 				= 'planner_step4_content';											// Id for the element containing tour-details
	this.tourDetailsDefaultContentId 		= 'planner_step4_default_content'; 									// Id for tour-details default content
	this.plannerBlockId 					= 'planner_container';												// Id for the element containing the planner
	this.personsBookedId 					= 'no_of_participants';												// Id for the input-field containing the number of persons booked
	this.typeDetailsBlock 					= 'type_details_right_column';										// Id for the table-cell containing type-details
	this.typePicturesBlock 					= 'type_details_picture_container';									// Id for the table-cell containing type-pictures
	this.tourDetailsBlockDefaultContent 	= document.getElementById(this.tourDetailsBlockId) ?
											  document.getElementById(this.tourDetailsBlockId).innerHTML :
											  '';																// Remembers the default-content of step 4
	this.cartIsEmpty 						= false;
	this.fromTypeDetails 					= false;
	this.plannerPageId 						= location.href.indexOf('dev.netimage') > -1 ? 25 : 27; 			// XXX Remember to change this to the proper value for production
	
	this.dayViewDate						= '';																// For remembering the current dayview-date

	this.eventTrigger = function(e) {
	    if (! e) {
	        e = event;
	    }
	    return e.target || e.srcElement;
	}

	this.setStartDate = function(startDate) {
		document.getElementById(this.startDateElmId).value = startDate;
	}

	// helper function to add required zero characters to fixed length fields
	this.zeroPad = function(num, width) {
		num = num.toString();
		while (num.length < width)
		num = "0" + num;
		return num;
	}

	this.getStartDate = function() {
		if(document.getElementById(this.startDateElmId) && document.getElementById(this.startDateElmId).value != '') {
			var formattedStartDate = document.getElementById(this.startDateElmId).value;
			var day = this.zeroPad(formattedStartDate.substring(0, formattedStartDate.indexOf('/')), 2);
			var month = this.zeroPad(formattedStartDate.substring(formattedStartDate.indexOf('/') + 1, formattedStartDate.indexOf(' ')), 2);
			var year = formattedStartDate.substring(formattedStartDate.indexOf('- ') + 2);
			return year+'-'+month+'-'+day;
		}
		/*else {
			var d = new Date();
			var month = d.getMonth()+1;
			var fullDate = d.getDate()+'/'+month+' '+d.getFullYear();
			//this.setDate(fullDate);
			return d.getFullYear()+'-'+month+'-'+d.getDate();
		}*/
		return '';
	}

	this.changeStartDate = function() {
		var getData = {	'eID':			'tx_usernishop_pi1',
						'page_label':	'ajax',
						'mode':			'change_start_date',
						'start_date':	this.getStartDate(),
						'period':		this.getPeriod(),
						'L':			getTypo3Language()};
		$.get('index.php', getData, function(data, textStatus) {
				$("#type_details_choose_type_list a.selected").click();
				if(document.all) { // IE only
					var clickEvent = $("#type_details_choose_type_list a.selected").attr('onclick');
					clickEvent();
				}
				if (textStatus == 'success') {
					//Planner.getPlanner();
					
				}
			}
		);
	}
	
	this.getPeriod = function() {
		if(document.getElementById(this.periodElmId) && parseInt(document.getElementById(this.periodElmId).options[document.getElementById(this.periodElmId).selectedIndex].value, 10) > -1) {
			return parseInt(document.getElementById(this.periodElmId).options[document.getElementById(this.periodElmId).selectedIndex].value, 10);
		}
		else {
			// Feltet er blankt, altså defaulter vi til periode på 8 dage
			//this.setPeriod(this.defaultPeriod);
			return this.defaultPeriod;
		}
	}

	this.getNights = function() {
		var nights = 0;
		if (document.getElementById('no_of_layovers')) {
			nights = document.getElementById('no_of_layovers').value;
		}
		return nights;
	}

	this.getPersonsBooked = function() {
		if (document.getElementById(this.personsBookedId)) {
			return parseInt(document.getElementById(this.personsBookedId).value, 10);
		}
		else {
			return 2;
		}
	}

	this.setPeriod = function(period) {
		var period = parseInt(period, 10);
		var index = period - 1;
		document.getElementById(this.periodElmId).options[index].selected = true;
	}

	this.getTourTypes = function() {
		var postData = {'eID':			'tx_usernishop_pi1',
						'page_label':	'ajax',
						'mode':			'tour_types',
						'L':			getTypo3Language()};
		$.post('/index.php', postData, function(data, textStatus) {
			if(textStatus == 'success') {
				document.getElementById(Planner.tourTypesBlockId).innerHTML = data;
			}
			else {
				Planner.handleError('no_tour_types_found');
			}
		});
	}

	this.cancelBookedTour = function(tourId, cartGroupId) {
		var tourId = parseInt(tourId, 10);
		var cartGroupId = parseInt(cartGroupId, 10);
		var postData = {'eID':			'tx_usernishop_pi1',
						'page_label':	'ajax',
						'mode':			'cancel_booked_tour',
						'tour_id':		tourId,
						'group_id':		cartGroupId,
						'L':			getTypo3Language()};
		$.post('/index.php', postData, function (data, textStatus) {
				if (textStatus == 'success') {
					Planner.getPlanner();
				}
			}
		);
	}

	this.clearClickedListItems = function(parentElmId) {
		$("#"+parentElmId+" li a").removeClass('selected');
	}

	this.getTourCategories = function(tourTypeId, e) {
		var e = this.eventTrigger(e);
		var tourTypeId = parseInt(tourTypeId, 10);
		var postData = {'eID':						'tx_usernishop_pi1',
						'page_label':				'ajax',
						'mode':						'tours',
						'tour_type_id':				tourTypeId,
						'from_type_details_page':	this.fromTypeDetails,
						'L':						getTypo3Language()};
		this.clearClickedListItems(this.tourTypesBlockId);
		e.className = 'selected';

		$.post('/index.php', postData, function(data, textStatus) {
			if(textStatus == 'success') {
				$("#"+Planner.tourCategoriesDefaultContentId).remove();
				document.getElementById(Planner.tourCategoriesBlockId).innerHTML = data;
				if (document.getElementById(Planner.tourDetailsBlockId)) {
					document.getElementById(Planner.tourDetailsBlockId).innerHTML = Planner.tourDetailsBlockDefaultContent; //'<p id="planner_step4_default_content">Vælg en tur<br> i trin 3 først.</p>';
				}

				// Vi vælger automatisk den første entry
				 $("#"+Planner.tourCategoriesBlockId+" li:first a").click().attr('class', 'selected');
				 if(document.all) { // IE only
					var clickEvent = $("#"+Planner.tourCategoriesBlockId+" li:first a").attr('onclick');
					clickEvent();
				}
			}
			else {
				Planner.handleError('no_tour_categories_found');
			}
		});
	}

	this.getTourCategoriesAndLoadType = function(elmClicked, categoryId, tourTypeId) {
		var categoryId = parseInt(categoryId, 10);
		var tourTypeId = parseInt(tourTypeId, 10);
		
		var postData = {'eID':						'tx_usernishop_pi1',
						'page_label':				'ajax',
						'mode':						'tours',
						'tour_type_id':				categoryId,
						/*'from_type_details_page':	this.fromTypeDetails,*/
						'L':						getTypo3Language()};
		this.clearClickedListItems(this.tourCategoriesBlockId);
		this.clearClickedListItems(this.tourTypesBlockId);

		// Vi sætter den korrekte class på det element der bliver klikket på
		elmClicked.className = 'selected';
		
		$.post('/index.php', postData, function(data, textStatus) {
			if(textStatus == 'success') {
				$("#"+Planner.tourCategoriesDefaultContentId).remove();
				document.getElementById(Planner.tourCategoriesBlockId).innerHTML = data;

				// Vi skal markere den valgte type
				/*var type_list_items = $("#"+Planner.tourTypesBlockId+" li a");
				for (var i = 0; i < type_list_items.length; i++) {
					var onclick = type_list_items[i].onclick.toString();
					console.log(onclick);
					if (onclick != '' && onclick.indexOf('('+categoryId+', event)') > -1) {
						type_list_items[i].className = 'selected';
					}
					else if(onclick == '' && type_list_items[i].href.indexOf('id='+categoryId) > -1) {
						type_list_items[i].className = 'selected';
					}
				}*/
				
				// Så skal vi sikre at der også bliver klikket på typen
				
				// Hvis der er et tourTypeId med over, bruger vi det, ellers bare første link
				var jQuerySelector = tourTypeId > 0 ? ' a#type_'+tourTypeId : ' a:first';

				// Klikke og markere korrekt. Klik virker ikke i IE
				$("#"+Planner.tourCategoriesBlockId+jQuerySelector).click().attr('class', 'selected');

				if(document.all) { // IE only
					var clickEvent = $("#"+Planner.tourCategoriesBlockId+jQuerySelector).attr('onclick').toString();
					var typeId = clickEvent.match(/\d+/);
					Planner.getTypeDetails(typeId, null);
				}
			}
			else {
				Planner.handleError('no_tour_categories_found');
			}
		});
	}

	/**
	 * Tilføjer en specifik tour til kurven, kaldes fra quickshop
	 */
	this.addTour = function(tour_id) {
		tour_id = parseInt(tour_id, 10);
		
		var type_id_input		= document.getElementById('quickshop_type_id_' + tour_id);
		var persons_input		= document.getElementById('quickshop_persons_' + tour_id);
		if ( ! (type_id_input && persons_input)) {
			return false;
		}
		
		var postData = {'eID':				'tx_usernishop_pi1',
						'page_label':		'ajax',
						'mode':				'add_type_to_planner',
						'type_id':			parseInt(type_id_input.value, 10),
						'tour_id':			tour_id,
						'personsBooked':	parseInt(persons_input.value, 10),
						'nights':			0,
						'L':				getTypo3Language()};
		if (parseInt(postData.personsBooked, 10) < 1) {
			alert('Der skal minimum være en deltager på turen');
			return false;
		}
		$.post('/index.php', postData, function(data, textStatus) {
				if(textStatus == 'success') {
					Planner.updateDetailsCart();
				}
			}
		);
	} // End addTour()
	
	this.addType = function(type_id) {
		var type_id = parseInt(type_id, 10);

		// Kun én paa slæden?
		var disable_sharing = $("#disable_dogsled_sharing:checked").length;

		var postData = {'eID':				'tx_usernishop_pi1',
						'page_label':		'ajax',
						'mode':				'add_type_to_planner',
						'type_id':			type_id,
						'disable_sharing':	disable_sharing,
						'period':			this.getPeriod(),
						'startDate':		this.getStartDate(),
						'personsBooked':	this.getPersonsBooked(),
						'nights':			this.getNights(),
						'L':				getTypo3Language()};

		if (parseInt(postData.personsBooked, 10) < 1) {
			alert('Der skal minimum være en deltager på turen');
			return false;
		}
		$.post('/index.php', postData, function(data, textStatus) {
				if(textStatus == 'success') {
					/*if (Planner.fromTypeDetails === false) {
						Planner.getPlanner(Planner.getStartDate());
					}
					else {
						location.href = '/index.php?id='+Planner.plannerPageId;
					}*/
					Planner.updateDetailsCart();
				}
			}
		);
	}
	
	this.updateDetailsCart = function() {
		var postData = {'eID':			'tx_usernishop_pi1',
							'page_label':	'ajax',
							'mode':			'details_cart',
							'type_mode':	'ajax',
							'return_mode':	'json',
							'L':			getTypo3Language()};
		$.post('/index.php', postData, function(data, textStatus) {
				if(textStatus == 'success') {
					Planner.insertJSON(data);
					Planner.recalcLeftColumn();
					Cart.updateMiniCart();
				}
			}
		);
	}

	this.getPlanner = function(startDate, direction, dayView, dayViewDate) {
		//var startDate = startDate != undefined ? startDate : this.getStartDate();
		var startDate = startDate != undefined && startDate != '' ? startDate : $("#first_date_entry").text();
		startDate = startDate == '' ? this.getStartDate() : startDate;
		
		var direction = direction != '' && direction != undefined ? direction : '';
		
		var sub_mode = dayView === true ? 'day_view' : '';
		var dayViewDate = dayViewDate != undefined ? dayViewDate : '';
		
		$("div#planner_container").css('position', 'relative'); // if the planner-container isn't positioned relative, the ajax-loader won't be shown correctly

		/* Show the ajax-loading dimmer */
		$("#"+this.plannerBlockId).attr('class', 'white_overlay');
		var blockWidth = parseInt(($("#"+this.plannerBlockId).width() / 2) - 50, 10);
		var blockHeight = $("#"+this.plannerBlockId).height();
		$("#"+this.plannerBlockId).append('<img src="/images/ajax-loader.gif" class="ajax_loader_img" style="top:40px; left:'+blockWidth+'px" />');

		var postData = {'eID':			'tx_usernishop_pi1',
						'page_label':	'ajax',
						'mode':			'planner',
						'startDate':	startDate,
						'direction':	direction,
						'sub_mode':		sub_mode,
						'day_view_day':	dayViewDate,
						'L':			getTypo3Language()};
		$.post('/index.php', postData, function(data, textStatus) {
				$("#"+Planner.plannerBlockId).css('height', 'auto');
				if(textStatus == 'success') {
					$("#"+Planner.plannerBlockId).attr('class', '');
					document.getElementById(Planner.plannerBlockId).innerHTML = data;
					$("th.day_column").css('cursor', 'pointer');
					$("div#planner_container").css('position', 'static'); // if the planner-container is positioned relative, then the day_view_tour_text won't be positioned correctly
					var day_view_tours = $("div.day_view_tour_text");
					if (day_view_tours.length > 0) {
						for (var i = 0; i < day_view_tours.length; i++) {
							var tour_id = day_view_tours[i].id.substring(5);
							var offset = $("div#tour_marker_"+tour_id).offset();
							//$("div#"+day_view_tours[i].id).css('position', 'absolute').css('top', offset.top).css('left', (offset.left - 10)).css('display', 'block');
						}
					}

					// Hvis der er nogle warnings eller oplysninger, bliver de lagt i div#planner_warning
					// i template-håndteringen og så alert'et ud til brugeren.
					if (document.getElementById('planner_warning') && document.getElementById('planner_warning').innerHTML != '') {
						alert(document.getElementById('planner_warning').innerHTML);
					}
					Cart.updateMiniCart();
				}
			}
		);
	}

	this.moveTour = function(tourId, groupId, dayViewDate) {
		var tourId = parseInt(tourId, 10);
		var groupId = parseInt(groupId, 10);
//		this.dayViewDate = dayViewDate != undefined ? dayViewDate : ''
		if (tourId > 0) {
			var postData = {'eID':			'tx_usernishop_pi1',
							'page_label':	'ajax',
							'mode':			'change_tour_in_planner',
							'tour_id':		tourId,
							'group_id':		groupId,
							'dayview_date':	dayViewDate,
							'L':			getTypo3Language()};
			$.post('/index.php', postData, function (data, textStatus) {
					var dayView = dayViewDate != '' ? true : false;
					Planner.getPlanner(dayViewDate, '', dayView, dayViewDate.substring(0, 10));
				}
			);
		}
	}

	this.getDay = function(dayView) {
		var startDate = this.getStartDate();
		var dayView = dayView != undefined ? dayView : startDate;
		this.getPlanner(startDate, '', true, dayView);
	}

	this.changeNoOfParticipants = function(tourTypeId, groupId, action) {
		var tourTypeId = parseInt(tourTypeId, 10);
		var groupId = parseInt(groupId, 10);
		var action = action != undefined ? action : 'add';
		if (tourTypeId > 0 && groupId > 0) {
			//alert('vi kommer her: '+tourTypeId+' '+groupId+' '+action);
			var postData = {'eID':			'tx_usernishop_pi1',
							'page_label':	'ajax',
							'mode':			'change_no_of_participants',
							'action':		action,
							'group_id':		groupId,
							'L':			getTypo3Language()};
			$.post('/index.php', postData, function (data, textStatus) {
					if (document.getElementById(Planner.plannerBlockId)) {
						Planner.getPlanner();
					}
					else {
						Planner.updateDetailsCart();
					}
				}
			);
		}
	}

	this.removeFromPlanner = function(cartGroupId) {
		var cartGroupId = parseInt(cartGroupId, 10);
		if (cartGroupId > 0) {
			var postData = {'eID':				'tx_usernishop_pi1',
							'page_label':		'ajax',
							'mode':				'remove_type_from_planner',
							'cart_group_id':	cartGroupId,
							'L':				getTypo3Language()};
			$.post('/index.php', postData, function(data, textStatus) {
					if(textStatus == 'success') {
						if (Planner.fromTypeDetails === true) {
							Planner.updateDetailsCart();
						}
						else {
							Planner.getPlanner(Planner.getStartDate());
						}
					}
				}
			);
		}
	}

	this.showCalendar = function() {
		var getData = {	'eID':			'tx_usernishop_pi1',
						'page_label':	'ajax',
						'mode':			'is_cart_empty',
						'L':			getTypo3Language()};
		$.get('index.php', getData, function(data, textStatus) {
				var showCalendar = false;
				if (textStatus == 'success') {
					if(data != '') {
						Planner.isCartEmpty = false;
						if(confirm(data)) {
							showCalendar = true;
						}
					}
					else {
						showCalendar = true;
						Planner.isCartEmpty = true;
					}
				}
				if (showCalendar === true) {
					var coordinates = YAHOO.util.Dom.getXY("calendar_pic");
					YAHOO.wog.calendar.plannerCalendar.show();
					YAHOO.util.Dom.setXY('calendar_container', coordinates);
				}
			}
		);
	}

	this.getTypeDetails = function (typeId, e) {
		var typeId = parseInt(typeId, 10);
		if (typeId > 0) {
			this.clearClickedListItems(this.tourCategoriesBlockId);
			if (e != null) {
				// Hvis det er et auto-genereret klik, fanger IE ikke nogen event
				// derfor fanger vi det her
				var e = this.eventTrigger(e);
				e.className = 'selected';
			}
			else {
				// Og laver en alternativ løsning
				// Det er altid den første entry der bliver valgt i et auto-genereret klik
				$("#"+this.tourCategoriesBlockId+" li a#type_"+typeId).attr('class', 'selected');
			}

			var postData = {'eID':			'tx_usernishop_pi1',
							'page_label':	'type_details',
							'mode':			'type_details',
							'type_id':		typeId,
							'type_mode':	'ajax',
							'L':			getTypo3Language()};
			$.post('/index.php', postData, function(data, textStatus) {
					if (textStatus == 'success') {
						Planner.insertJSON(data);
						Planner.updateDetailsCart();
					}
				}
			);
		}
	}
	
	this.recalcLeftColumn = function() {
		if(document.all
			&& document.getElementById('type_details_right_column')) { // Vi vil kun benytte os af metoden på de nødvendige sider
			var right_column = $("#type_details_right_column").height()
							 + $("#type_details_picture_container").height();
			var left_column = $("#left_upper_column").height() 
							+ $("#left_upper_column").height() 
							+ $("#type_details_choose_category_header").height() 
							+ $("#type_details_choose_category_content").height() 
							+ $("#planner_step4_content").height() 
							+ $("#type_details_cart_header").height() 
							+ $("#type_details_cart").height();
			
			var height_diff = right_column > left_column ? right_column - left_column : 1;

			$("#type_details_filler").height(height_diff);	 
			//alert(right_column+ ' - ' +left_column+' = '+height_diff);
		}
	}

	this.insertJSON = function(data) {
		var result_values = eval('(' + data + ')');
		for (var key in result_values) {
			if (document.getElementById(key)) {
				if (result_values[key] != '') {
					$("#"+key).html(result_values[key]);
				}
			}
		}
	}

	this.handleError = function(errorLabel) {
		switch(errorLabel) {
			case 'no_tour_types_found':

			break;
			case 'no_tour_categories_found':

			break;
		}
	}
	
	/**
	 * Tilføjer value til aktuelt heltal i input med id inputId  
	 */
	this.addToIntegerInput = function(inputId, value, noNegative) {
		var input = document.getElementById(inputId);
		if (input) {
			var oldValue = parseInt(input.value, 10);
			var newValue = oldValue + value;
			if (noNegative && newValue < 0) {
				newValue = 0;
			}
			input.value = newValue;
		}
	} // End addToIntegerInput()
}