<commit_before>// TODO: make the 'back >>>' link work more smoothly.
$(document).ready(function()
{
	init();
});

function init()
{
	var $userDetails = $('#userDetails');
	var $main = $('#main');
	var $reminderForm = $('#reminderForm');
	$('#success').hide();

	if (getItem('username') == undefined)
	{
		var $username = $('#username');
		var $useSsl = $('#useSsl');

		$main.hide();
		window.localStorage.clear();
		
		$('#save').click(function(){
			if ($username.val().length > 0)
			{
	    		setItem('username', $username.val().toLowerCase());
				$useSsl.attr('checked') ? setItem('useSsl', true) : setItem('useSsl', false);
				$userDetails.hide();
				$reminderForm.hide();
				$main.show();
				request('ws/pages/all');
				request('me.xml');
			}
			else
			{
				$username.addClass('error');
			}
	    });
	}
	else
	{
		var username = getItem('username');
		$userDetails.hide();
		$reminderForm.hide();
		request('ws/pages/all');
	}
}

function initMainContent(xml)
{
	var username = getItem('username');

	$(xml).find('page').each(function(){
		var $page = $(this);
		var $homeLink = $('#home');

		if ($page.attr('title').toLowerCase() != username + ' home')
		{
			$('#pageList').append('<li id="' + $page.attr('id') + '">' + $page.attr('title') + '</li>');
		}
		else
		{
			$homeLink.html($page.attr('title'));
			$homeLink.attr('id', $page.attr('id'));
		}
	});

	$('#pageList').click(function(event){
		$target = $(event.target);

		if ($target.is('li'))
		{
			chrome.tabs.create({ url: getProtocol() + username + '.backpackit.com/pages/' + $target.attr('id') });
		}
		else
		{
			chrome.tabs.create({ url: getProtocol() + username + '.backpackit.com/pages/' + $target.parent().attr('id') });
		}
	});

	$('#topLinks').click(function(event){
		event.stopPropagation();
		chrome.tabs.create({ url: getProtocol() + username + '.backpackit.com/' + $(event.target).attr('id') });
	});

	$('#addReminder').click(function(event){
		event.stopPropagation();
		initReminders();
	});

	initPageListFilter();
}

function initReminders()
{
	var userId = getItem('userId');
	var $remindAt = $('#remindAt');
	var $reminderText = $('#reminderText');

	$('#addReminder').hide();
	$('#topLinks').hide();
	$('#pageContainer').hide();
	$('#reminderForm').show();
	$remindAt.find('option').eq(2).attr('value', '+' + tomorrow(9));
	$remindAt.find('option').eq(3).attr('value', '+' + tomorrow(14));
	$remindAt.find('option').eq(5).attr('value', '+' + nextMonday());
	$('#loading').addClass('loadingReminder');
	$('#success').addClass('successReminder');

	$('#setReminder').click(function(event){
		var content = '';
		var postData = '';
		
		if ($remindAt.val() == 'specificTime')
		{
			content = $reminderText.val();
			var date = $('#date').val();
			var hour = $('#hour').val();
			var minute = $('#minute').val();
			var amPm = $('#amPm').val();
			
			if (amPm == 'pm')
			{
				hour = parseInt(hour);
				hour += 12;
			}
			
			var reminderDateTime = date + ' ' + hour + ':' + minute + ':' + '00';
			postData = '<reminder><remind_at>' + reminderDateTime + '</remind_at><content>' + content + '</content><remindees type="array"><user_id>' + userId + '</user_id></remindees></reminder>';			
		}
		else
		{
			content = $remindAt.val() + " " + $reminderText.val();
			postData = '<reminder><content>' + content + '</content><remindees type="array"><user_id>' + userId + '</user_id></remindees></reminder>';
		}
		
		if ($reminderText.val().length > 0)
		{	
			request('reminders.xml', postData);
			$reminderText.val('');
			$reminderText.removeClass('error');
			$time.hide();
			resizeReminderUi(115);
		}
		else
		{
			$reminderText.addClass('error');
			$time.hide();
			resizeReminderUi(115);
		}
	});

	resizeReminderUi(115);
	initDateTimePicker();
}

function initDateTimePicker()
{
	var $remindAt = $('#remindAt');
	var $time = $('#time');
	var $humanDate = $('#humanDate');
	var $date = $('#date');
	$time.hide();

	$date.datepicker({
		dateFormat: 'yy-mm-dd',
		beforeShow: function(){
			$humanDate.hide();
			$time.css({ marginTop: '194px' });
		},
		onSelect: function(dateText, inst) {
			var dateSplit = dateText.split('-');
			$humanDate.show().html(new Date(dateSplit[0], dateSplit[1] - 1, dateSplit[2]).toLocaleDateString());
			resizeReminderUi(145);
		},
		onClose: function(){
			$time.css({ marginTop: '0' });
			resizeReminderUi(145);
		}
	});

	$remindAt.change(function(){
		if ($(this).val() == 'specificTime')
		{
			resizeReminderUi(300);<commit_msg>Fixed bug where the time picker stays visible after the datepicker loses focus, without the user having chosen a date.<commit_after>// TODO: make the 'back >>>' link work more smoothly.
$(document).ready(function()
{
	init();
});

function init()
{
	var $userDetails = $('#userDetails');
	var $main = $('#main');
	var $reminderForm = $('#reminderForm');
	$('#success').hide();

	if (getItem('username') == undefined)
	{
		var $username = $('#username');
		var $useSsl = $('#useSsl');

		$main.hide();
		window.localStorage.clear();
		
		$('#save').click(function(){
			if ($username.val().length > 0)
			{
	    		setItem('username', $username.val().toLowerCase());
				$useSsl.attr('checked') ? setItem('useSsl', true) : setItem('useSsl', false);
				$userDetails.hide();
				$reminderForm.hide();
				$main.show();
				request('ws/pages/all');
				request('me.xml');
			}
			else
			{
				$username.addClass('error');
			}
	    });
	}
	else
	{
		var username = getItem('username');
		$userDetails.hide();
		$reminderForm.hide();
		request('ws/pages/all');
	}
}

function initMainContent(xml)
{
	var username = getItem('username');

	$(xml).find('page').each(function(){
		var $page = $(this);
		var $homeLink = $('#home');

		if ($page.attr('title').toLowerCase() != username + ' home')
		{
			$('#pageList').append('<li id="' + $page.attr('id') + '">' + $page.attr('title') + '</li>');
		}
		else
		{
			$homeLink.html($page.attr('title'));
			$homeLink.attr('id', $page.attr('id'));
		}
	});

	$('#pageList').click(function(event){
		$target = $(event.target);

		if ($target.is('li'))
		{
			chrome.tabs.create({ url: getProtocol() + username + '.backpackit.com/pages/' + $target.attr('id') });
		}
		else
		{
			chrome.tabs.create({ url: getProtocol() + username + '.backpackit.com/pages/' + $target.parent().attr('id') });
		}
	});

	$('#topLinks').click(function(event){
		event.stopPropagation();
		chrome.tabs.create({ url: getProtocol() + username + '.backpackit.com/' + $(event.target).attr('id') });
	});

	$('#addReminder').click(function(event){
		event.stopPropagation();
		initReminders();
	});

	initPageListFilter();
}

function initReminders()
{
	var userId = getItem('userId');
	var $remindAt = $('#remindAt');
	var $reminderText = $('#reminderText');

	$('#addReminder').hide();
	$('#topLinks').hide();
	$('#pageContainer').hide();
	$('#reminderForm').show();
	$remindAt.find('option').eq(2).attr('value', '+' + tomorrow(9));
	$remindAt.find('option').eq(3).attr('value', '+' + tomorrow(14));
	$remindAt.find('option').eq(5).attr('value', '+' + nextMonday());
	$('#loading').addClass('loadingReminder');
	$('#success').addClass('successReminder');

	$('#setReminder').click(function(event){
		var content = '';
		var postData = '';
		
		if ($remindAt.val() == 'specificTime')
		{
			content = $reminderText.val();
			var date = $('#date').val();
			var hour = $('#hour').val();
			var minute = $('#minute').val();
			var amPm = $('#amPm').val();
			
			if (amPm == 'pm')
			{
				hour = parseInt(hour);
				hour += 12;
			}
			
			var reminderDateTime = date + ' ' + hour + ':' + minute + ':' + '00';
			postData = '<reminder><remind_at>' + reminderDateTime + '</remind_at><content>' + content + '</content><remindees type="array"><user_id>' + userId + '</user_id></remindees></reminder>';			
		}
		else
		{
			content = $remindAt.val() + " " + $reminderText.val();
			postData = '<reminder><content>' + content + '</content><remindees type="array"><user_id>' + userId + '</user_id></remindees></reminder>';
		}
		
		if ($reminderText.val().length > 0)
		{	
			request('reminders.xml', postData);
			$reminderText.val('');
			$reminderText.removeClass('error');
			$time.hide();
			resizeReminderUi(115);
		}
		else
		{
			$reminderText.addClass('error');
			$time.hide();
			resizeReminderUi(115);
		}
	});

	resizeReminderUi(115);
	initDateTimePicker();
}

function initDateTimePicker()
{
	var $remindAt = $('#remindAt');
	var $time = $('#time');
	var $humanDate = $('#humanDate');
	var $date = $('#date');
	$time.hide();

	$date.datepicker({
		dateFormat: 'yy-mm-dd',
		beforeShow: function(){
			$humanDate.hide();
			$time.css({ marginTop: '194px' });
		},
		onSelect: function(dateText, inst) {
			var dateSplit = dateText.split('-');
			$humanDate.show().html(new Date(dateSplit[0], dateSplit[1] - 1, dateSplit[2]).toLocaleDateString());
			resizeReminderUi(145);
		},
		onClose: function(){
			$time.hide();
			resizeReminderUi(145);
		}
	});

	$remindAt.change(function(){
		if ($(this).val() == 'specificTime')
		{
			resizeReminderUi(300);
{'fraction_non_alphanumeric': 0.24132979898387452, 'fraction_numerical': 0.00828363154406892, 'mean_word_length': 4.16248574686431, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1835032', 'n_tokens_mistral': 3559, 'n_tokens_neox': 3205, 'n_words': 524}