(function travelGetData () {
	var subHeadingData = $.parseJSON($.cookie("iCuroUser"));
	var workerId = subHeadingData.USER;
	icuro.api.workers.travel.byWorkerID(workerId, travelResponseHandler);
})()

function travelResponseHandler(responseText) {
	var response = JSON.parse(responseText);
	var rendered;
	var today = new Date();
	var loopDate;
	var travelRowHighlight = '<div class="travel-row highlight"><div class="col-md-3 col-sm-3 col-xs-6 travel-list-date">{{PETD_date}}</div><div class="col-md-2 col-sm-2 col-xs-6 transport-mode-{{mode}}">&nbsp;&nbsp;&nbsp;{{transport_name}}</div><div class="col-md-2 col-sm-2 col-xs-6">{{p_source_code}} {{PETD_time}}</div><div class="col-md-2 col-sm-2 col-xs-6">{{p_destination_code}} {{PETA_time}}</div><div class="col-md-3 col-sm-3 col-xs-6">{{status}} <small></small></div></div>';
	var travelRow = '<div class="travel-row"><div class="col-md-3 col-sm-3 col-xs-6 travel-list-date">{{PETD_date}}</div><div class="col-md-2 col-sm-2 col-xs-6 transport-mode-{{mode}}">&nbsp;&nbsp;&nbsp;{{transport_name}}</div><div class="col-md-2 col-sm-2 col-xs-6">{{p_source_code}} {{PETD_time}}</div><div class="col-md-2 col-sm-2 col-xs-6">{{p_destination_code}} {{PETA_time}}</div><div class="col-md-3 col-sm-3 col-xs-6">{{status}} <small></small></div></div>';

	console.log(response);

	$('#travel-wrapper').html('');

	if (response.results) {
		
		for (var i = 0; i < response.results.length; i++) {
			// check if the date passed through is same as today, if so, add class
			loopDate = new Date(response.results[i].PETD_date);
			if (today.isSameDateAs(loopDate)) {
				console.log("Date passed through is today.");
				rendered = Mustache.render(travelRowHighlight, response.results[i]);
				$('#travel-wrapper').append(rendered);
			} else {
				rendered = Mustache.render(travelRow, response.results[i]);
				$('#travel-wrapper').append(rendered);
			}
		}

		$('.transport-mode-Bus').prepend('<i class="fa fa-bus"></i>');
		$('.transport-mode-Plane').prepend('<i class="fa fa-plane"></i>');

	} else {
		$('#error-message').html("You don't have any progress to report. Check back later.")
		$('#error-message').fadeIn();
	}
}