(function profileAutoFill () {
	var subHeadingData = $.parseJSON($.cookie("iCuroUser"));
	var workerId = subHeadingData.USER;

	icuro.api.workers.retrieveSingle(workerId, profileAutoFillResponse);

	profileGetPhoto(workerId);
})()

function profileGetPhoto (workerId) {
	//TODO
	icuro.api.workers.photos.byWorkerID(workerId, profileGetPhotoResponse);
}

function profileGetPhotoResponse (responseText) {
	var response = JSON.parse(responseText);
	console.log(response.results[0].token);
	// $('.content-top-photo img').attr('src', '/img/worker/profile/person-sample.png');
	$('.content-top-photo img').attr('src', icuro.baseUrl + '?token='+response.results[0].token);
	$('.content-top-photo').fadeIn();
}

function profileAutoFillResponse (responseText) {
	var response = responseText;
	var responseObj = JSON.parse(response);
	var template = $('#profileTemplate').html();
	var rendered;
	Mustache.parse(template);

	for (var i = 0; i < responseObj.results.length; i++) {
	    console.log(responseObj.results[i]);
	    rendered = Mustache.render(template, responseObj.results[i]);
	}
	$('#profile-edit').html(rendered);

	//check box if value is 1
	if (document.getElementById('drivers-licence').value == 1) { document.getElementById('drivers-licence').setAttribute("checked", "checked"); }
	if (document.getElementById('postal-different-to-residential-checkbox').value == 1) { document.getElementById('postal-different-to-residential-checkbox').setAttribute("checked", "checked"); }

	// DATEPICKER

	var datepicker = $('.datepicker');

    datepicker.datepicker({
        format : 'yyyy-mm-dd',
        autoclose: true,
	    todayHighlight: true
    });
}

(function onProfileEditFormSubmit () {
	$('#profile-edit').submit(function (event, workerId) {
	  event.preventDefault();

	  var subHeadingData = $.parseJSON($.cookie("iCuroUser"));
	  var workerId = subHeadingData.USER;

	  var data = JSON.stringify($(this).serializeObject());
	  var newData = JSON.parse(data);

	  console.log(newData);

	  var postalDifferent;
	  var driversLicence;

	  // if checkbox checked, send through 1
	  if (document.getElementById('postal-different-to-residential-checkbox').checked) { postalDifferent = 1; } else { postalDifferent = 0; }
	  if (document.getElementById('drivers-licence').checked) { driversLicence = 1; } else { driversLicence = 0; }

	  icuro.api.workers.replace(
	  	workerId,
	  	newData['title'],
	  	newData['fullname'],
	  	newData['gender'],
	  	newData['dob'],
	  	newData['mobile-number'],
	  	newData['home-phone'],
	  	newData['other-phone'],
	  	newData['residential-street-address'],
	  	newData['residential-city'], 
	  	newData['residential-postcode'],
	  	postalDifferent,
	  	newData['postal-street-address'],
	  	newData['postal-city'], 
	  	newData['postal-postcode'],
	  	newData['drivers-licence'],
	  	newData['passport-number'],
	  	newData['passport-issue-location'],
	  	newData['passport-issue-date'],
	  	newData['passport-expiry-date'],
	  	newData['emergency-contact-name'],
	  	newData['emergency-contact-relationship'],
	  	newData['emergency-contact-number'],
	  	newData['emergency-contact-email'],
	  	newData['emergency-contact-street-address'],
	  	newData['emergency-contact-city'],
	  	newData['emergency-contact-postcode'],
	  	onProfileEditFormSubmitHandler
	  );
	});
})()

function onProfileEditFormSubmitHandler (responseText) {
	//parse response to json
	var response = JSON.parse(responseText);
	console.log(response);

	if (response.status == "success") {
		//if success, get object from cookie, get response progress, then put back into cookie
		var jsonCookieObject = $.parseJSON($.cookie("iCuroUser"));
		jsonCookieObject['progress'] = response.progress;
		$.removeCookie('iCuroUser');
		$.cookie("iCuroUser", JSON.stringify(jsonCookieObject));

		console.log(jsonCookieObject);
		
		$('#error-message').hide();
		$('#success-message').fadeIn();

		//scroll to top of page
		$('html, body').animate({ scrollTop: 0 }, 'fast');
	} else {
		$('#success-message').hide();
		$('#error-message').text("Error! Please try again.");
		$('#error-message').fadeIn();
	}
}