function log(msg) { return window.console ? console.log(msg) : null; }

$(function() {

	// form error notification
	$.ui.dialog.defaults.bgiframe = true;
	$("#form-errors").dialog({width:340, modal:true});

	var comparison_table_ = {
		125: [2,2,2,2,2,2,2,2],
		250: [2,2,2,2,3,3,3,3],
		500: [2,3,4,4,4,4,4,4],
		1000: [3,4,5,6,6,7,7,7],
		1500: [4,6,8,10,11,12,12,13],
		2000: [5,8,10,11,12,13,13,14],
		3000: [7,12,15,16,18,18,19,20],
		3500: [8,14,18,20,22,22,23,25],
		4000: [10,16,19,22,24,25,25,27],
		5000: [12,20,24,27,29,31,32,33],
		6000: [14,24,28,33,35,37,37,39],
		7000: [17,27,34,38,41,42,46,46],
		10000: [20,32,38,44,48,49,50,51],
		12500: [24,39,47,54,58,62,64,66],
		15000: [30,48,61,68,73,78,80,81],
		17500: [37,57,70,81,87,92,94,99],
		20000: [48,77,95,107,115,120,127,131],
		25000: [60,96,122,133,146,156,159,162],
		30000: [73,114,141,163,174,184,188,194],
		40000: [95,156,188,217,231,243,249,257],
		50000: [120,192,238,267,290,305,312,322],
		75000: [177,292,357,403,432,455,470,485],
		100000: [239,378,475,515,542,562,583,593]		
	}

	var boxFn = {

		amount_: 0,
		container_: $("input#total_boxes, input[name=num_biobox]"),

		update: function() {
			
			var tp = $("#total_people").val() || null;
			var td = $("#total_hours").val() || null;

			if(tp && td) {
				
				this.amount_ = comparison_table_[tp][td-1] || 0;
				if($("#alcohol:checked").length) { this.amount_ = Math.ceil(this.amount_ * 1.15); }
				this.container_.val(this.amount_);

			}

		}
		
	};

	$("#total_people, #total_hours, #alcohol").bind('change', function() { boxFn.update(); });	
	boxFn.update();


	var totalFn = 
	{
	
	update: function() {

			var total_price = 0;
			
			$.each($("#prices .amount-input"), function() { var price = +$(this).attr('rel'); var amount = +$(this).val(); total_price = total_price + (amount * price); });
			var formatted_price = total_price.toString().replace(/\./g, ',');

			$("#total-price").html("&euro; "+ formatted_price);
			$("#subtotal").val(total_price);

		}	
	};

	$.each($("#prices .amount-input"), function() 
	{
		$(this)
		.defaultValue('0')
		.numeric() // only numeric
		.focus(function() { })		
		.blur(function() { totalFn.update(); });	
	});
	
	totalFn.update();
	

	








	/*
	var amount = $("#amount", "#total");

	var total = {
		
		days: 0,
		people: 0,
		
		update: function() {

			var box_per_people 	= Math.ceil(total.people / 500);
			var box_per_days 	= Math.ceil(total.days / 5) + 1;

			var total_boxes = box_per_people * box_per_days;

			amount.text(total_boxes);
			$("#input-amount").val(total_boxes);
		
		}
	
	};

	$("#slider-people").slider({
		min: 0,
		max: 5000,
		step: 250,
		value: 0,
		slide: function(event, ui) {
		
			$("#people").val(ui.value);
			
			total.people = ui.value;
			total.update();
			
		}
	});
	$("#people").val($("#slider-people").slider("value"));

	$("#slider-days").slider({
		min: 1,
		max: 40,
		value: 1,
		slide: function(event, ui) {
		
			$("#days").val(ui.value);

			total.days = ui.value;
			total.update();				
			
		}
	});
	$("#days").val($("#slider-days").slider("value"));

	total.update();

	/*
	var amount = $("#amount", "#total");
	var slider_people_conf = {
		min: 250,
		max: 100000,
		step: 250,
		value: 250,
		slide: function(event, ui) { $("#people").val(ui.value); total.people = ui.value; total.update(); }
		};
		
	var slider_hours_conf = {
		min: 1,
		max: 8,
		value: 1,
		slide: function(event, ui) { $("#hours").val(ui.value); total.hours = ui.value; total.update(); }
		};

	var steps = [250,500,2000,3000,4000,7000,10000,20000,30000,50000];
	var chunks = [250, 500, 100,500,1000,3000,10000,20000,25000];

	// total calculator
	var total = {

		people: 0,
		hours: 0,

		getStep: function(current) {

			for(i in steps) { if(steps[i] == current) { return +i; } } // unary plus operator - converts i to a number
			return false;

		},	

		update: function() {

			var box_per_people 	= Math.ceil(total.people / 500);
			var box_per_hours 	= Math.ceil(total.hours / 5) + 1;

			var total_boxes = box_per_people * box_per_hours;

			amount.text(total_boxes);
			$("#input-amount").val(total_boxes);
			
			// recalculate steps
			total.recalculate_steps(total.people);

		},
		
		recalculate_steps: function(people) {
		
			/*
			var index = total.getStep(people);

			if(typeof(index) == "number") {

				// log(chunks[index]);

				log(slider_people_conf);

				slider_people_conf.step = +chunks[index];
				slider_people_conf.value = people;
				
				log(slider_people_conf);

				$("#slider-people").slider(slider_people_conf);
				
			} 
			*/ /*

		}
	
	};


	// conf_slider
	$("#slider-people").slider(slider_people_conf);
	$("#people").val($("#slider-people").slider("value"));

	$("#slider-hours").slider(slider_hours_conf);
	$("#hours").val($("#slider-hours").slider("value"));

	total.update();
	*/




	/**
	 *	datepicker func
	 */
	
	var datepicker_daynames = ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'];
	var datepicker_daynames_min = ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'];
	var datepicker_monthnames = ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'];
	var datepicker_monthnames_short = ['Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'];
	
	var datepicker_date = {
		dateFormat: 'dd-mm-yy',
		minDate: new Date(), // no past dates selectable
		dayNames: datepicker_daynames,
		dayNamesMin: datepicker_daynames_min,
		monthNames: datepicker_monthnames,
		monthNamesShort: datepicker_monthnames_short
		};

	$("#start_date").datepicker(datepicker_date);
	$("#end_date").datepicker(datepicker_date);

});
