fn.global.view = 'projects';
fn.global.today = new Date(); // object
fn.global.keepAliveTimer = null;
fn.global.refreshTimer = null;
fn.global.showHelpBubbles = true;

fn.global.bubbles = {
	newItem: {
		html: 		'<h3>Start here</h3><p>Create an item by clicking here. It\'ll add one to the page.</p><p><a href="#" class="disable">Turn off these help bubbles</a></p><a href="#" class="close button"> X </a>',
		appendTo:	'#header .liner',
		css: 		'left: 43px; top: 60px;',
		direction:	'left'
	},
	firstProject: {
		html: 		'<h3>Give your item a title</h3><p>What\'re you working on? Something like &quot;Project with mike&quot; or &quot;Blog about Ultralight Startups&quot;. When you\'re done, press [tab] or click anywhere on the page.</p><p><a href="#" class="disable">Turn off these help bubbles</a></p><a href="#" class="close button"> X </a>',
		appendTo:	'#projectsList .project:first',
		css: 		'left: 490px; top: 20px;',
		direction:	'left'
	},
	newState: {
		html: 		'<h3>Create a status   </h3><p>Click here to create a status. It\'ll open the new status form.</p><p><a href="#" class="disable">Turn off these help bubbles</a></p><a href="#" class="close button"> X </a>',
		appendTo:	'#header .liner',
		css: 		'right: 43px; top: 60px;',
		direction: 	'right'
	},
	newStateForm: {
		html: 		'<h3>Give your status a title</h3><p>What\'s the status of your item? Maybe &quot;In Progress&quot; or &quot;Needs attention&quot;. Then give it a color.</p><p><a href="#" class="disable">Turn off these help bubbles</a></p><a href="#" class="close button"> X </a>',
		appendTo:	'#newStateForm',
		css: 		'left: -330px; top: 30px;',
		direction: 	'right'
	},
	firstState: {
		html: 		'<h3>Finally, update your item</h3><p>Move your mouse over an item to show the list of statuses on the right. Click on the status to assign it to an item.</p><p><a href="#" class="disable">Turn off these help bubbles</a></p><a href="#" class="close button"> X </a>',
		appendTo:	'#projectsList .project:first',
		css: 		'left: 90px; top: 37px;',
		direction:	'left'
	},
	firstComment: {
		html: 		'<h3>Give it a note</h3><p>How about some detail like &quot;Waiting for Nick\'s email&quot; or a next action like &quot;test the homepage&quot;. When you\'re done, press [tab] or click anywhere on the page.</p><p><a href="#" class="disable">Turn off these help bubbles</a></p><a href="#" class="close button"> X </a>',
		appendTo:	'#projectsList .project:first',
		css: 		'left: 600px; top: 57px;',
		direction:	'left'
	},
	firstCompleted: {
		html: 		'<h3>Congrats!</h3><p>You\'ve got your first item. Edit the title or note by double clicking on them. Move your mouse over an item to delete it or schedule it.</p><p><a href="#" class="disable">Turn off these help bubbles</a></p><a href="#" class="close button"> X </a>',
		appendTo:	'#projectsList .project:first',
		css: 		'left: 200px; top: 37px;',
		direction:	'left'
	}
};

/**
 * toggle any item
 */
fn.global.toggle = function()
{
	// get id and show it
	var id = $(this).attr('href');
	$(id).toggle();
	
	// add focus if it's a form
	var inputs = $(':input:first', id);
	if ( undefined !== inputs[0] ) 
	{
		inputs[0].focus();
	};
	
	fn.global.refreshReset();
};

/**
 * Change views betweek projects and tasks
 */
fn.global.toggleView = function()
{
	// if already selected, stop
	if ( $(this).hasClass('selected') ) 
	{
		return false;
	};
	
	// changes classes for nav items 
	$('#viewsList li:not(this)').removeClass('selected');
	$(this).closest('li').addClass('selected');
	
	// get new state
	var styleName = $(this).attr('href').split('#')[1];
	
	// select style sheet
	$('link[rel*=style][title]').each(function() 
	{
		this.disabled = true;
		if (this.getAttribute('title') == styleName) 
		{
			this.disabled = false;
		};
	});
	
	// set global state 
	fn.global.view = styleName;

	return false;
};

/*
 * write alerts to page
 * obj={
 * 		classname: good or bad
 * 		content: html message to display
 * }
 */
fn.global.notify = function(obj)
{
	$('.alert').remove();
	
	var $element = $('<div class="alert ' + obj.classname + '" style="display: none;"><div class="liner">' + obj.content + '</div></div>');
	$element
	.insertAfter('#header')
	.fadeIn('fast', function(){
			var words = obj.content.split(' ');

			setTimeout(function(){
				$element.fadeOut('fast');
			}, words.length*550);
	});
};

/**
 * Restore any fields being edited
 */
fn.global.unedit = function()
{
	$(':input.editing').trigger('blur');
};

/**
 * Hide a form button and show a loading gif.
 */
fn.global.buttonReplace = function ()
{
	$('<img src="/c/img/loading_button.gif" /> ')
	.prependTo(this);
	$(this).attr('disabled', 'disabled');
};

/**
 * Hide a form button and show a loading gif.
 */
fn.global.buttonReplace = function ()
{
	$('<img src="/c/img/loading_button.gif" /> ')
	.prependTo(this);
	$(this).attr('disabled', 'disabled');
};

/**
 * keep connection alive, returns date for showing scheduled projects
 */ 
fn.global.keepalive = function()
{
	$.ajax( {
		url : '/keepalive?' + fn.functions.uniqId(),
		type : 'get',
		dataType : 'json',
		success : function(json) {
			if ( null === json ) return false; // server hiccup on time change
			var today = new Date(json.today);
			
			// convert to local
			var offset = today.getTimezoneOffset();
			fn.global.today = today.addMinutes(-offset);
			fn.projects.checkScheduled();
		}
	});
};

/**
 * refresh the page without creating history 
 */
fn.global.refresh = function()
{
	// redirect to account page
	window.location.replace('/' + fn.account.user);
};

/**
 * if editing something, clear the refresh timer
 */
fn.global.refreshRemove = function()
{
	clearInterval(fn.global.refreshTimer);
};

/**
 * reset the refresh timer to x minutes
 */
fn.global.refreshReset = function()
{
	var minutes = 30;
	clearInterval(fn.global.refreshTimer);
	fn.global.refreshTimer = setInterval(function(){
		fn.global.refresh();
	}, 1000*60*minutes); // 1 minutes
};

fn.global.feedback = function() {
	var $form = $('#feedbackForm');

	var data = $form.serialize();
	var path = $form.attr('action');

	$.ajax( {
		type : 'POST',
		url : path,
		data : data,
		dataType : 'html',
		success : function(html) {
		 	$('#feedbackFormWrapper').replaceWith(html);
//		 	$.fn.colorbox.resize();
		} // success
	}); // ajax
};