function BookerUtils(){
	this.external = false;
	this.testing = (window.location.href.indexOf('?test=1') > -1) ? true : false;
	this.busRoutesEnabled = true;
	this.extTest = (window.location.href.indexOf('/jsp/extbooker.jsp') > -1) ? true : false;
	//this.extTest = false;
	this.visibleRoutes = [];
	this.context = 'page';
	this.contextVars = {};
}

(function($, booker){
	$.extend(BookerUtils.prototype, {
	
		// Parse date strings returned by the backend
		parseDate: function(dateStr){
			var parts = dateStr.split("-");
			var retval = false;
			
			if(parts.length === 3){
				var ints = [];
				var i;
				while(i = parts.pop()){
					ints.push((i.substr(0, 1) === '0') ? parseInt(i.substr(1)) : parseInt(i));
				}
				
				/*
					We'll just assume the numbers are sane.
					If they aren't... well, who are we to judge.
				*/
				retval = new Date(ints[2], (ints[1] - 1), ints[0]);
			}
			
			return retval;
		},
		
		
		// <div class="stand" onclick="matkavaraus.selectStand(2497, ' Alapitkä pikavuoropysäkki');"> Alapitkä pikavuoropysäkki</div>
		
		
		parseStandData: function(dataArray){
			var objArray = [];
			for(var i = dataArray.length; i--;){
				if(dataArray[i].length > 3){
					objArray.push({name: dataArray[i].substr(dataArray[i].indexOf(" ")), id: dataArray[i].substr(0, dataArray[i].indexOf(" "))});
				}
			}
			
			return $oq(objArray).orderBy('name').toArray();
		},
		
		
		
		// Create zero padded string representations of (positive) numbers and strings
		padValue: function(val, length){
			return ((length <= (val += '').length) ? val : (Math.pow(10, length - val.length) + val).substr(1)); // Nice jackassery...
		},
		
		
		/*
			Order a data array with ObjectQuery and return distinct values in ascending order.
			This method hilights some ObjectQuery peculiarities and weaknesses:
				- ObjectQueryCollections can't be instantiated using arrays of primitives (i.e. strings, numbers...)
				- The distinct method returns an array of distinct values in reverse order
			
			NOTICE: Only DISTINCT values are returned.
		*/
		orderData: function(data){
			var tmpArray = []; // An array of objects to be used as an ObjectQueryCollection source
			
			for(var i = data.length; i--;){
				tmpArray.push({data: data[i]});
			}
			
			return $oq(tmpArray).orderBy('data').distinct('data').reverse();
		},
		
		
		// Set the date field values
		setDate: function(d){
			var pv = this.padValue,
				dda = [d.getDate(), (d.getMonth() + 1), d.getFullYear()];
			$('#dateDisplay').val(dda.join('.')); // Looks less retarded than it actually is. The original value is actually set using the wrong date format.
			booker.selectDate([pv(dda[1], 2), pv(dda[0], 2), dda[2]].join('-'));
		},
		
		// Returns true if the date string (YYYY-MM-DD) is in future
		dateStringInFuture: function(dateStr){			
			return (new Date() < this.parseDate(dateStr));
		},
		
		
		// Handle submit button enabling and disabling
		enableSubmit: function(tabIdx){
			$('#btn_submit' + tabIdx + '_inactive').hide();
			$('#btn_submit' + tabIdx).show();
		},
		
		disableSubmit: function(tabIdx){
			$('#btn_submit' + tabIdx).hide();
			$('#btn_submit' + tabIdx + '_inactive').show();
		},
		
		shiftDatepicker: function(coords){
			$('.ui-datepicker:first').css({
				"left" : coords.left + 'px',
				"top" : coords.top + 'px'
			});
		},
		
		
		displayVisibleRoutes: function(){
			for(var i = this.visibleRoutes.length; i--;){
				document.getElementById(this.visibleRoutes[i]).style.display = 'block';
			}
		}
	});
	
	// Extend the original implementation and initialize
	$(document).ready(function(){
		booker.utils = new BookerUtils();
		matkavaraus.utils.disableSubmit(1);
	});
})(jQuery, matkavaraus);
