$(function() {

	var dialog;

	// Ensure all links with rel=external open in a new window.  Attach to the
	// click event with live so we catch ajax loaded links as well.
	$('a[rel=external],a[rel=download]').live('click',function() {
		$(this).attr('target', '_blank');
	});


	// Set up modal links.
	$('a.dialog').live( 'click', function(event) {
		var url = $(this).attr('href');
		var title = $(this).attr('title');


		if ( !dialog ) {
			dialog = $('<div>').dialog({
				width: 600,
				height: 420,
				title: title,
				modal: true
			});

		} else {
			dialog
				.empty()
				.dialog('option', 'title', title )
				.dialog('option', 'width', 600 )
				.dialog('option', 'height', 420 )
				.dialog('open');
		}


		$.get(
			url,
			function( data ) {
				dialog.append( data );
			}
		);

		event.preventDefault();
	});

	$('a.refer').live( 'click', function(event) {
		var url = $(this).attr('href');
		var title = $(this).attr('title');


		if ( !dialog ) {
			dialog = $('<div>').dialog({
				width: 710,
				height: 590,
				title: title,
				modal: true
			});

		} else {
			dialog
				.empty()
				.dialog('option', 'title', title )
				.dialog('option', 'width', 710 )
				.dialog('option', 'height', 590 )
				.dialog('open');
		}


		$.get(
			url,
			function( data ) {
				dialog.append( data );
				$('#refer_form').validate();
			}
		);

		event.preventDefault();
	});

	$('a.editSchool').live('click', function(event) {
		var url = $(this).attr('href');
		var title = $(this).attr('title');


		if ( !dialog ) {
			dialog = $('<div>').dialog({
				width: 430,
				height: 510,
				title: title,
				modal: true
			});

		} else {
			dialog
				.empty()
				.dialog('option', 'title', title )
				.dialog('option', 'width', 430 )
				.dialog('option', 'height', 510 )
				.dialog('open');
		}


		$.get(
			url,
			function( data ) {
				dialog.append( data );
			}
		);

		event.preventDefault();
	});

	$('a.action-closeDialog,button.action-closeDialog').live( 'click', function(event) {
		if ( dialog ) {
			dialog.dialog('close');
		}

		event.preventDefault();
	});


	// Default validator error messages.
	$.validator.messages = $.extend( $.validator.messages, {
		required: '*this field is required',
		email: '*sorry, invalid email'
	});

	
	// The login form (visible on every page)
	$('#login_form').validate();

});
