(function($) {
	GS.require(['gs.ui.modal']);
	GS.extend('homepageModal', function($tie, $template, $show, $hide) {
		var tie = $tie,
			template = $template,
			show = $show || function() {},
			hide = $hide || function() {},
			modal;

		modal = new GS.ui.modal(template);

		$('a.modalCloseImg', modal.getElement()).bind('click', HandleClose);

		tie.bind('click', function() {
			HandleOpen();
			$('.gs-ui-modal-overlay').bind('click', HandleClose);
			$('.gs-home-cancel a').bind('click', HandleClose);
			return false;
		});

		function HandleOpen() {
			modal.show();
			show();
			$('input:first', modal.getElement()).focus();
		}
		function HandleClose() {
			modal.hide();
			hide();
		}

	});
	GS.extend('homepageModalThrobber', function() {
		$('.gs-ui-modal-content:visible form').bind('submit', function() {
			var parent = $(this).parents('.gs-ui-modal-content'),
				view = $('div:first-child', parent);
			$('*', view).css('visibility', 'hidden');
			$('.modalCloseImg', parent).hide();
			$('h3', view).html('Loading…').css({
				'visibility' : 'inherit',
				'text-align' : 'center'
			});
		});
	});
})(jQuery);
