var immonet = immonet || {};

immonet.relocationCosts = {
	debug: true,

	// default values
	// in square meters
	surface: 50, 
	// in km
	distance: 90, 
	people: 3,
	rooms: 2,

	maxRooms: 10,
	maxPeople: 10,

	// slider config
	minSurface: 1,
	maxSurface: 450,
	surfaceIncrement: 1,

	minDistance: 5,
	maxDistance: 300,
	distanceIncrement: 5,

	init: function() {
		// setup selectors
		this.surfaceDisplay = jQuery("#surface");
		this.distanceDisplay = jQuery("#distance");
		this.roomDisplay = jQuery("#rooms");
		this.peopleDisplay = jQuery("#people");
		this.promoContainer = jQuery("#promo");
		this.priceDisplay = jQuery("#price");

		// bind behaviour
		var self = this;
		jQuery(document).on("click", ".increment-rooms", function() {
			self.incrementRooms();
		});
		jQuery(document).on("click", ".decrement-rooms", function() {
			self.decrementRooms();
		});
		jQuery(document).on("click", ".increment-people", function() {
			self.incrementPeople();
		});
		jQuery(document).on("click", ".decrement-people", function() {
			self.decrementPeople();
		});

		// build sliders
		jQuery("#surface-selector").slider({
			value: self.surface,
			min: this.minSurface,
			max: this.maxSurface,
			step: this.surfaceIncrement,
			slide: function(event, ui) {
				self.setSurface(ui.value);
				self.calculatePrice();
			}
		});

		jQuery("#distance-selector").slider({
			value: self.distance,
			min: this.minDistance,
			max: this.maxDistance,
			step: this.distanceIncrement,
			slide: function(event, ui) {
				self.setDistance(ui.value);
				self.calculatePrice();
			}
		});

		// display initial values
		this.setSurface(this.surface);
		this.setDistance(this.distance);
		this.roomDisplay.text(this.rooms);
		this.peopleDisplay.text(this.people);
		this.calculatePrice();
	},

	log: function(msg) {
		if (this.debug)  {
			console.log(msg);
		}
	},

	incrementPeople: function() {
		this.people = (this.people < this.maxPeople) ? this.people + 1 : this.maxPeople;
		this.updatePeople();
	},

	decrementPeople: function() {
		this.people = (this.people > 1) ?  this.people - 1 : 1;
		this.updatePeople();
	},

	incrementRooms: function() {
		this.rooms = (this.rooms < this.maxRooms) ? this.rooms + 1 : this.maxRooms;
		this.updateRooms();
	},

	decrementRooms: function() {
		this.rooms = (this.rooms > 1) ? this.rooms - 1 : 1;
		this.updateRooms();
	},

	setSurface: function(value) {
		this.surface = value;
		this.surfaceDisplay.text(value);
		this.setBgColorWidth("surface");
	},

	setDistance: function(value) {
		this.distance = value;
		this.distanceDisplay.text(value);
		this.setBgColorWidth("distance");
		this.showPromo();
	},

	updateRooms: function() {
		this.roomDisplay.text(this.rooms);
		this.calculatePrice();
	},

	updatePeople: function() {
		this.peopleDisplay.text(this.people);
		this.calculatePrice();
	},

	setBgColorWidth: function(elementName) {
		var handle = jQuery(".ui-slider-handle", jQuery("#" + elementName + "-selector"));
		// this is kind of bug: when a user clicks with the mouse, the callback is called before setting the handle position...
		// when using the arrows or dragging the hanlde, the position is updated before calling the callback
		setTimeout(function() {
			jQuery("#" + elementName + "-color").css("width", handle.css("left"));
		}, 0);
	},

	showPromo: function() {
		this.promoContainer.toggleClass("hidden", this.distance < 100);
	},

	calculatePrice: function() {
		var numberBoxes = (this.people * 4) + (this.rooms * 14.7) + (this.people * 0.6);
		var cubicMeters = ((this.rooms * 9 + 12) * 0.4) + ((this.surface / 2) * 0.6) + (numberBoxes * 0.06);
		var price = (cubicMeters * 25) + (this.distance * 0.8) + ((3 * (2 * cubicMeters)) + (cubicMeters * 2));
		price = price * 0.85;
		price = Math.round(price / 25) * 25;
		this.priceDisplay.text(price);
	}

};