$(function() {
	Corners.apply();
	Datepickers.apply();
	InputSelector.apply();
	NewUserNav.init();
	InviteEvents.apply();
	Shadowbox.init({
		enableKeys: false
		,players: ['html', 'swf']
	});
	Tooltips.init();
	Tooltips.apply('.tooltip');
	
	if (show_weighin_reminder) {
		var url = base_url + 'weight/reminder?partial=1';
		$.get(url, function(html) {
			Shadowbox.open({
				content: html
				,player: 'html'
		
				,height: 352
				,width: 280
			});
			$('div#weighin-reminder-buttons input').unbind().live('click', function() {
				var form = $(this).parents('form');
				var redirect_url = base_url + 'weight/create';
				var params = form.serializeArray();
				$.post(redirect_url + '.js', params, function(response) {
					if (!response.weight || !response.weight.errors) {
						return;
					}
					
					for (var key in response.weight.errors) {
						alert(response.weight.errors[key]);
						return;
					}
					
					Shadowbox.close();
					
					if (response.feedback) {
						window.location = base_url + 'goal/feedback';
					}
				}, 'json');
				return false;
			});
			$('div#weighin-reminder-buttons a').live('click', function() {
				Shadowbox.close();
				return false;
			});
		});
	}
});

var Corners = {
	apply: function() {
		$('#nav, #nav ul').corners('20px top');
		$('div#nav ul li a').corners('top 15px');
		$('a.button').corners('15px');
		
		if (!$.browser.msie) {
			$('input.button').corners('15px');
		}
		
		$('#footer').corners('20px bottom');
	}
};

var Datepickers = {
	apply: function() {
		$('input.date').datepicker();
		var currentYear = (new Date()).getFullYear();
		$('input.birthday').datepicker({changeYear: true, yearRange: '1900:' + currentYear});
	}
};

var InputSelector = {
	apply: function() {
		var first_input = $('input[type!=submit][type!=image]:first').not('div#invite input');

		if (first_input.size() > 0) {
			try {
				first_input.get(0).focus();
			} catch (e) {
			}
		}
	}
};

var InviteEvents = {
	apply: function() {
		var default_email = 'friend@example.com';
		var invite_email = $('div#invite input#invite-email').click(function() {
			if (invite_email.val() == default_email) invite_email.val('');
		}).blur(function() {
			if (invite_email.val() == '') invite_email.val(default_email);
		});
		if (invite_email.val() == '') invite_email.val(default_email);
		
		$('div#invite input#invite-link').click(function() {
			$(this).select();
		});
		
		$('div#invite form').submit(function() {
			var email = $('div#invite input#invite-email').val();
			
			if (email == '' || email == default_email) {
				alert('Please enter an email address.');
				return false;
			}
			
			var url = window.location.pathname + '.js' + window.location.search;
			$.post(url, {'invite-email': email}, function(response) {
				if (response.sent) {
					alert('Your invitation has been sent.');
				} else {
					alert('There was an error and your invitation was not sent. Please try again.');
				}
			}, 'json');
			return false;
		});
	}
};

var NewUserNav = {
	init: function() {
		$('span#rad-food').hover(function() {
			$('li#nav-food a').addClass('over');
		}, function() {
			$('li#nav-food a').removeClass('over');
		});
		$('span#rad-weight').hover(function() {
			$('li#nav-weight a').addClass('over');
		}, function() {
			$('li#nav-weight a').removeClass('over');
		});
		$('span#rad-goals').hover(function() {
			$('li#nav-goal a').addClass('over');
		}, function() {
			$('li#nav-goal a').removeClass('over');
		});
	}
};

var Tooltips = {
	apply: function(selector) {
		$(selector).each(function() {
			var text = $(this).attr('title');
			
			if (text.length == 0) {
				if ($(this).data('qtip')) {
					var api = $(this).qtip('api');
					api.destroy();
				}

				return;
			}
			
			text = text.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>');
			text = text.replace(/_(.*?)_/g, '<em>$1</em>');
			text = text.replace(/\[(.*?)\]/g, '<small>$1</small>');
			text = text.replace(/\*(.*?)\*/g, '<strong>$1</strong>');
			text = text.replace(/\n/g, '<br />');

			var adjustX = 0;
			
			if ($(this).is('div#foods table tr')) {
				adjustX = -220;
			}
	
			$(this).qtip({
				content: text
				,position: {
					at: 'topmiddle'
					,my: 'bottommiddle'
					,adjust: {
						x: adjustX
						,y: 0
					}
				}
				,style: {
					background: '#00a0c6'
					,border: {
						radius: 6
						,color: '#666'
						,width: 6
					}
					,color: '#c1d71e'
					,padding: 15
					,textAlign: 'left'
					,tip: true
					,width: {
						min: 0
						,max: 350
					}
				}
				,events: {
					hide: function(e) {
						if (typeof e == 'undefined') {
							return true;
						}
						
						// if we're moving into the tooltip, don't hide the tooltip
						// we might have links in the tooltip that the users need to click
						var element = $(e.currentTarget);
						var offset = element.offset();

						if (
							e.originalEvent.pageX > offset.left
							&& e.originalEvent.pageX < (offset.left + element.outerWidth())
							&& e.originalEvent.pageY > offset.top
							&& e.originalEvent.pageY < (offset.top + element.outerHeight() + 11)
						) {
							var qtip = element.qtip('api');
							
							qtip.elements.tooltip.one('mouseleave', function() {
								qtip.hide();
							});
							return false;
						}
						
						return true;
					}
				}
			});
		});
	},
	
	init: function() {
		var tooltip = $('<div class="tooltip-element"></div>').appendTo(document.body);
	}
};

$.fn.defaultVal = function(value) {
	if ($(this).val() == '') {
		$(this).addClass('default').val(value);
	}
	
	$(this).focus(function() {
		if ($(this).val() == value) {
			$(this).removeClass('default').val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).addClass('default').val(value);
		}
	});
	
	return $(this);
};

