﻿
		var jQT = $.jQTouch({
			icon: 'kilo.png',
			statusBar: 'black'
		});
		
		var db;
$(document).ready(function(){
	$('#createEntry form').submit(createEntry);
	$('#settings form').submit(saveSettings);
	$('#settings').bind('pageAnimationStart', loadSettings);
	$('#dates li a').click(function(){
						var dayOffset = this.id;
						var date = new Date();
						date.setDate(date.getDate() - dayOffset);
						sessionStorage.currentDate = date.getDate() + '.' +
													(date.getMonth() + 1) + '.' +
													date.getFullYear();
						sessionStorage.currentDate = date.getDate() + '.' +
														(date.getMonth() + 1) + '.' +
														date.getFullYear();
						refreshEntries();
	});
	var shortName = 'Kilo';
						var version = '1.0';
						var displayName = 'Kilo';
						var maxSize = 65536;
						db = openDatabase(shortName, version, displayName, maxSize);
						db.transaction(
						function(transaction) {
						transaction.executeSql(
						'CREATE TABLE IF NOT EXISTS entries ' + 
						'  (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' + 
						'   date DATE NOT NULL, food TEXT NOT NULL, ' +
						' calories INTEGER NOT NULL );'
						);
						}
						);
});
function loadSettings() {
	$('#age').val(loadStorage.age);
	$('#budget').val(loadStorage.budget);
	$('#weight').val(loadStorage.weight);
}
function saveSettings() {
	localStorage.age = $('#age').val();
	localStorage.budget = $('#budget').val();
	localStorage.weight = $('#weight').val();
	jQT.goBack();
	return false;
}
function refreshEntries() {
	var currentDate = sessionStorage.currentDate;
	$('#date h1').text(currentDate);
	$('#date ul li:gt(0)').remove();
	db.transaction(
		function(transaction) {
			transaction.executeSql(
				'SELECT * FROM entries WHERE date = ? ORDER BY food;',
				
				[currentDate],
				function (transaction, result) {
					for (var i=0; i < result.rows.length; i++) {
						var row = result.rows.item(i);
						var newEntryRow = $('#entryTemplate').clone();
						newEntryRow.removeAttr('id');
						newEntryRow.removeAttr('style');
						newEntryRow.data('entryId', row.id);
						newEntryRow.appendTo('#date ul');
						newEntryRow.find('.label').text(row.food);
						newEntryRow.find('.calories').text(row.calories);
						newEntryRow.find('.delete').click(function(){
							var clickerEntry = $(this).parent();
							var clickedEntryId = clickedEntry.data('entryId');
							deleteEntryById(clickedEntryId);
							clickedEntry.slideUp();
						});
					}
				},
				errorHandler
		);
	}
);					
}

function createEntry() {
	var date = sessionStorage.currentDate;
	var calories = $('#calories').val();
	var food = $('#food').val();
	db.transaction(
		function(transaction) {
			transaction.executeSql(
			'INSERT INTO entries (date, calories, food) VALUES (?, ?, ?);',
			[date, calories, food],
			function(){
				refreshEntries();
				jQT.goBack();
			},
			errorHandler
		);
		},
		transactionErrorHandler,
		transactionSuccessHandler
	);
	return false;
}

function deleteEntryById(id) {
	db.transaction(
	function(transaction) {
		transaction.executeSyl('DELETE FROM entries WHERE id=?;',
		[id], null, errorHandler);
	}
	);
}

function errorHandler(transaction, error) {
	alert('Ups. Der Fehler war '+error.message+' [Code '+error.code+')');
	transaction.executeSql('INSERT INTO errors (code, message) VALUES (?, ?);',
							[error.code, error.message]);
	return true;
}
		
var db;
$(document).ready(function(){
    $('#settings form').submit(saveSettings);
    $('#settings').bind('pageAnimationStart', loadSettings);
    $('#dates li a').click(function(){
        var dayOffset = this.id;
        var date = new Date();
        date.setDate(date.getDate() - dayOffset);
        sessionStorage.currentDate = date.getMonth() + 1 + '/' + 
                                     date.getDate() + '/' + 
                                     date.getFullYear();
        refreshEntries();
    });
    var shortName = 'Kilo';
    var version = '1.0';
    var displayName = 'Kilo';
    var maxSize = 65536;
    db = openDatabase(shortName, version, displayName, maxSize);
    db.transaction(
        function(transaction) {
            transaction.executeSql(
                'CREATE TABLE IF NOT EXISTS entries ' +
                '  (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' +
                '   date DATE NOT NULL, food TEXT NOT NULL, ' +
                ' calories INTEGER NOT NULL );'
            );
        }
    );
});			
