Date.format = 'mmmm dd, yyyy';
Date.firstDayOfWeek = 0;

$(document).ready(function() {

	/** ACCOUNT * */
	
	if ( $('#user').length == 1 )
	{
		fn.account.user = $('#user').val();
		fn.account.load();

		if ( $('#empty').length == 1 )
		{
			fn.view.bubble(fn.global.bubbles.newItem);
			delete fn.global.bubbles.newItem;
			$('#newItem').removeClass('hidden');
		}
		else
		{
			$('#' + fn.global.view + 'StatesList a.hidden:not(.scheduled), #newItem, #newState, #newDivider').removeClass('hidden');
			fn.global.showHelpBubbles = false;
		};
		fn.account.active();
	}
	else
	{
		$('#welcome').removeClass('hidden');
	};

	// start keepalive
	fn.global.keepAliveTimer = setInterval(function(){
		fn.global.keepalive();
	}, 5000);

	fn.global.refreshReset();
	
	/** end ACCOUNT **/
	
	
	
	/** HEADER * */

	/**
	 * change global view
	 */
	$('#viewsList a').click(function() {
		fn.global.toggleView.call(this);
	});

	/**
	 * behavior for new user reg modal
	 */
	$('#recoverUser, #newUser').colorbox({
		height: '150px',
		width: '440px',
		speed: 100,
		transition: 'none',
		close: '',
		onComplete: fn.global.refreshReset
	});

	/**
	 * add a new item
	 */
	$('#newItem')
	.click(function() {
		switch (fn.global.view) {
			case 'projects':
				fn.project.add();
			break;
		} // end switch
		
		return false;
	})
	.mouseenter(function(){
		$('span', this).removeClass('hidden');
	})
	.mouseleave(function(){
		$('span', this).addClass('hidden');
	});
	
	/**
	 * add a new divider
	 */
	$('#newDivider')
	.click(function() {
		switch (fn.global.view) {
			case 'projects':
				fn.project.add({
					divider: true
				});
			break;
		} // end switch
		
		return false;
	})
	.mouseenter(function(){
		$('span', this).removeClass('hidden');
	})
	.mouseleave(function(){
		$('span', this).addClass('hidden');
	});

	/**
	 * filter items by state
	 */
	$('#header .filterList')
	.click(function(e) {
		switch (fn.global.view) {
			case 'projects':
				fn.projects.filter.call(e);
			break;
		} // end switch

		return false;
	})
	.dblclick(function(e){
		var $clicked = $(e.target);

		if ( $clicked.is('.all, .scheduled') ) 
		{
			return false;
		};
		fn.state.edit.call(e);
	});

	/**
	 * add a new state
	 */
	$('#newStateForm').submit(function() {
		fn.state.add.call(this);
		return false;
	});
	
	$('#newState')
	.mouseenter(function(){
		$('span', this).removeClass('hidden');
		$(this).css({
			width: '5em'
		});
	})
	.mouseleave(function(){
		$('span', this).addClass('hidden');
		$(this).css({
			width: 'auto'
		});
	})
	.click(function(){
		fn.view.bubble(fn.global.bubbles.newStateForm);
	});
	
	$('#feedbackLink').colorbox();
	
	$('#feedbackForm #submit').live('click', function(){
		fn.global.buttonReplace.call(this);
		setTimeout(function(){
			fn.global.feedback();
		}, 300);
				
		return false;
	});
	
	/** end HEADER * */

	
	
	
	
	/** PROJECT * */

	/**
	 * Show/hide action buttons
	 */
	$('.project')
	.mouseenter(function() {
		switch (fn.global.view) {
		case 'projects':
			$('.project:not(this) .states, .project:not(this) .actions').hide();
			$('.states, .actions', this).show();
			break;
		} // end switch
		}).mouseleave(function() {
		switch (fn.global.view) {
		case 'projects':
			$('.states, .actions', this).hide();
			break;
		} // end switch
	})
	.click(function(){
		fn.global.unedit();
	});

	/**
	 * change project state
	 */
	$('.project .states').click(function(e) {
		switch (fn.global.view) {
		case 'projects':
			fn.project.state(e);
			fn.view.bubble(fn.global.bubbles.firstComment);
			break;
		} // end switch
			return false;
		});

	/**
	 * delete a project
	 */
	$('.project .delete').click(function() {
		var project = $(this).closest('.project');
		fn.project.remove.call(project);
		return false;
	});
	
	/**
	 * put h3 in edit mode
	 */
	$('.project h3 .label').dblclick(function() {
		
		switch (fn.global.view) {
		case 'projects':
			fn.project.editH3.call(this);
			fn.view.bubbleRemove();
			break;
		} // end switch
		return false;
	})
	.click(function(e){
		e.stopPropagation();
	});

	/**
	 * put project comment in edit mode
	 */
	$('.project .comment span').dblclick(function() {
		fn.project.editComment.call(this);
		fn.view.bubbleRemove();
		return false;
	})
	.click(function(e){
		e.stopPropagation();
	});

	/** end PROJECT * */

	
	
	
	
	/** GLOBAL * */

	/**
	 * show/hide based on .toggle triggers
	 */
	$('.toggle').click(function() {
		fn.global.toggle.call(this);
		return false;
	});

	/**
	 * Initialize crayonboxes
	 */
	$('.color').crayonbox();
	
	$('#newUserForm #submit').live('click', function(){
		fn.global.buttonReplace.call(this);
		setTimeout(function(){
			fn.account.add();
		}, 300);
				
		return false;
	});
	
	$('#recoverUserForm #submit').live('click', function(){
		fn.global.buttonReplace.call(this);
		setTimeout(function(){
			fn.account.recover();
		}, 300);
				
		return false;
	});
	
	$('#cboxClose').live('click', function(){
		$.fn.colorbox.close();
	});
	
	$('#projectsList')
	.sortable({
		update: function(event, ui) {
			fn.account.save();
		},
		start: function (event, ui){
			fn.global.unedit();
			$(ui.helper).trigger('mouseleave');
		}
	});

	$('.bubble .close').live('click', function(){
		fn.view.bubbleRemove();
		return false;
	});
	
	$('.bubble .disable').live('click', function(){
		fn.global.showHelpBubbles = false;
		fn.view.bubbleRemove();
		return false;
	});
	/** end GLOBAL * */
	
	
	/** WELCOME **/
	$('#welcome .colorbox').colorbox();

	$('#getStarted').click(function(){
		$('#welcome').fadeOut('fast', function(){
			fn.projects.empty();
			$('#main').removeClass('hidden');
		});
		fn.view.bubble(fn.global.bubbles.newItem);
		delete fn.global.bubbles.newItem;
		$('#newItem').removeClass('hidden');
		return false;
	});
	
	/** end WELCOME **/
});

