var fae = {};

fae.log = function (obj) {
	if (typeof console != 'undefined')
	{
		console.log(obj);
	}
};

fae.start_clock = function (now, end_datetime) {
	return;
	var $clock = $('#clock');
	var client_now = new Date();
	var client_diff = (client_now.getTime() - now.getTime()) / 1000;
	var old_diff = null;
	var is_manupulated = false;
	function handle_tick()
	{
		if (is_manupulated)
		{
			return;
		}
		var client_now = new Date();
		var diff = Math.floor(((end_datetime.getTime() - client_now.getTime()) / 1000) + client_diff);
		if (old_diff && (diff > old_diff || old_diff - diff > 30)) /* Manipulation */
		{
			$clock.text('- Tage - Stunden - Minuten');
			is_manupulated = true;
			return;
		}
		if (diff <= 0)
		{
			$clock.text('0 Tage 0 Stunden 0 Minuten');
			return;
		}
		old_diff = diff;
		var _temp = diff;
		var days = Math.floor(_temp / 86400);
		_temp = _temp % 86400;
		var hours = Math.floor(_temp / 3600);
		_temp = _temp % 3600;
		var minutes = Math.floor(_temp / 60);
		_temp = _temp % 60;
		var seconds = _temp;
		$clock.text(days + ' Tage ' + hours + ' Stunden ' + minutes + ' Minuten');
		window.setTimeout(handle_tick, 2500);
	}
	window.setTimeout(handle_tick, 2500);
	$(document).bind('keypress click', handle_tick);
};

fae.notifications = (function ($) {
	var $notifications_wrapper;
	var $notifications = null;
	
	function clear()
	{
		if ($notifications && $notifications.length)
		{
			$notifications.remove();
		}
		$notifications = null;
	}
	
	function add_close_button()
	{
		var $close_button = $('<div id="notifications-close">X</div>');
		$notifications.append($close_button);
		$close_button.bind('click', clear);
	}
	
	function add(type, text)
	{
		if (!$notifications || !$notifications.length)
		{
			$notifications = $('<div id="notifications">');
			$notifications_wrapper.append($notifications);
			add_close_button();
		}
		if (!$notifications.find('ul').length)
		{
			$notifications.append($('<ul>'));
		}
		var $ul = $notifications.find('ul');
		var $new_li = $('<li>');
		$new_li.addClass(type);
		$new_li.text(text);
		$ul.append($new_li);
	}
	
	function init()
	{
		$notifications_wrapper = $('#notifications-wrapper');
		if ($notifications_wrapper.find('#notifications'))
		{
			$notifications = $notifications_wrapper.find('#notifications');
			add_close_button();
		}
	}
	
	return {
		add: add,
		init: init
	};
})(jQuery);

fae.game = (function ($) {
	var lock_got_fortune = false; // TODO: Remove this workaround (Flash-Bug)
	function handle_save_result(result)
	{
		fae.log(['fae.game.handle_save_result', result]);
		if (result == 'got_fortune' && !lock_got_fortune)
		{
			fae.notifications.add('success', 'Herzlichen Glückwunsch, durch dieses Spiel haben Sie ein Los erhalten.');
			lock_got_fortune = true;
		}
	}
	
	return {
		handle_save_result: handle_save_result
	};
})(jQuery);

$(document).bind('ready', function() {
		fae.notifications.init();
		
		$('#sidebar-right-player .login input').each(function (i, input) {
			var $input = $(input);
			$input.bind('click change keypress', function () { $input.css('background-image', 'none'); });
			try {
				if ($input.val()) { $input.css('background-image', 'none'); }
			} catch (e) { /* nix */ }
		});
});
