(function () {
	var $, window, showUpdatePassForm, data, resetPassword, closeModal, showProfileUpdate, profileUpdateCheck, loader, openModal, ajaxError, setModalHtml;
	
	window = this;
	$ = window.jQuery;
	
	loader = function (status) {
		if (status == "show") {
			$('#upw-modal').append('<div id="upw-modal-loader" style="width:100%;height:100%;background:url(/images/ajax-loader.gif) no-repeat center;"><div>');
		}
		if (status == "hide") {
			$('#upw-modal-loader').remove();
		}
	}
	
	showUpdatePassForm = function () {
		data = $(this).attr('rel').split(',');
		// load form
		openModal(
			'Update Password',
			380, 300,
			function () {
				$.ajax({
					url: '/user-profile',
					dataType: 'html',
					data: {'ulid':data[0], 'redirect_loc':data[1]},
					success: function (res) {
						loader('hide');
						setModalHtml(res);
						registerFormSubmit();
					},
					error: ajaxError
				});
			}
		);
	}
	
	resetPassword = function () {
		var sure = confirm('Are you sure you want to reset the user password?');
		if (sure) {
			data = $(this).attr('rel');
			// show temp password
			openModal(
				'Reset Password',
				300, 250,
				function () {
					$.ajax({
						url: '/user-profile/reset/',
						dataType: 'html',
						type: 'post',
						data: {'ulid':data},
						success: function (res) {
							loader('hide');
							setModalHtml(res);
							registerFormSubmit();
						},
						error: ajaxError
					});
				}
			);
		}
	}
	
	showProfileUpdate = function () {
		openModal(
			'Update User Profile',
			330, 430,
			function () {
				$.ajax({
					url: '/user-profile/profile/',
					dataType: 'html',
					success: function (res) {
						loader('hide');
						setModalHtml(res);
						registerFormSubmit();
					},
					error: ajaxError
				});
			}
		);
	}
	
	profileUpdateCheck = function () {
		$.ajax({
			url: '/user-profile/profile-update-check/',
			dataType: 'html',
			success: function (res) {
				if (res.match(/\w+/)) {
					openModal(
						'Update User Profile',
						330, 130,
						function () {
							loader('hide');
							setModalHtml(res);
							$('#surebtn').live('click', function () {
								closeModal();
								showProfileUpdate();
							});
							$('#notnowbtn').live('click', function () {
								$.ajax({url: '/user-profile/profile-update-not-now/',dataType:'html'});
								closeModal();
							});
						}
					);
				}
			}
		});
	}
	
	setModalHtml = function (html) {
		if (html.match(/Web Site Maintenance/)) {
			ajaxError();
		}else{
			$('#upw-modal').html(html);
		}
	}
	
	openModal = function (title, width, height, openCallback) {
		$('body').append('<div id="upw-modal" style="display:none;">');
		loader('show');
		$('#upw-modal').dialog({
			width: width,
			height: 'auto',
			modal:true,
			resizable:false,
			title: title,
			open: openCallback,
			close: closeModal
		});
	}
	
	closeModal = function () {
		$('#upw-modal').remove();
	}
	
	function registerFormSubmit () {
		$('#upw-modal form').submit(function () {
			$.ajax({
				url: this.action,
				data: $(this).serialize(),
				type: 'post',
				success: function (res) {
					setModalHtml(res);
					if (res.match(/autoclosemodal/)) {
						$('#upw-modal').children('form').fadeOut(500, function () {
							$('#upw-modal').animate({height:150}, 500);
						});
						setTimeout(closeModal, 1800);
					}
				},
				error: ajaxError
			});
			return false;
		});
	}
	
	ajaxError = function () {
		setModalHtml('An error occurred, please close and try again.');
	}
	
	function init() {		
		$('#update-pw-link').click(showUpdatePassForm);
		$('#reset-pw-link').live('click', resetPassword);
		
		$('#logInInfo li i').css(
			{'color':'blue','textDecoration':'underline','fontWeight':'bold','cursor':'pointer'}
		).attr('title', 'Update Profile').click(showProfileUpdate);
		
		if (window.location.pathname.match(/adminsitesel/)) {
			profileUpdateCheck();
		}
		
		$('.ui-widget-overlay').live('click', closeModal);
	}
	
	$(window.document).ready(init);
})();
