﻿jQuery.growl.settings.displayTimeout = 7200;
jQuery.growl.settings.dockCss.width = '225px';
jQuery.growl.settings.noticeTemplate = ''
	+ '<table width="225" border="0" cellpadding="0" cellspacing="0">'
	+ '	<tr>'
	+ '		<td style="background-image: url(' + __root + 'content/images/growl/dm_top.png); width: 225px; height: 49px; background-repeat: no-repeat; color: #fff;">'
	+ '			<img src="' + __root + 'content/images/growl/info.png" style="max-width: 25px; max-height: 25px; text-align: center; margin-left: 19px; margin-top: 19px;" />'
	+ '			<h1 style="font-size: 18px; margin: 0pt; margin-left: 5px; margin-bottom: 10px; display: inline;">%title%</h1>'
	+ '		</td>'
	+ '	</tr>'
	+ '	<tr>'
	+ '		<td style="background-image: url(' + __root + 'content/images/growl/dm_repeat.png); width: 225px; background-repeat: repeat-y; color: #ddd;">'
	+ '			<p style="margin: 20px;">%message%</p>'
	+ '		</td>'
	+ '	</tr>'
	+ '	<tr>'
	+ '	<td style="background-image: url(' + __root + 'content/images/growl/dm_bottom.png); background-repeat: no-repeat; width: 225px; height: 27px;" valign="top" align="right" >'
	+ '			<a style="margin-right: 25px; font-size: 10px; color: #fff; text-align: right;" href="" onclick="return false;" rel="close">Close</a>'
	+ '		</td>'
	+ '	</tr>'
'+ </table>';
jQuery.growl.settings.noticeCss = {
    position: 'relative'
};

function PopupForm(f_url, a_url) {
   	this.jcontainer = jQuery('#__popupHolder');
	var th = this;	
    this.formUrl = f_url;
    this.actionUrl = a_url;
    this.onLoad = null;
	this.onSuccess = function(resp) {
		if (resp == "ok") {
			this.onSuccessOk();
		} else {
			var jqh = th.jcontainer;
			jqh.html(resp);
			jqh.jqm();
		}
		th.jcontainer.html(resp);
	};
    this.onSuccessOk = function () {};
}

PopupForm.prototype.initAjaxForm = function() {
	var th = this;
	jQuery('#__popup_form').ajaxForm({
		method: 'post',
		beforeSubmit: function() {
			th.jcontainer.html("<div style='text-align:center;padding:20px;'><img src='" + __root + "Content/images/load_circle.gif' /></div>");
		},
		success: function(resp) {
			th.onSuccess(resp);
			th.initAjaxForm();
		}
	})
	if (th.onLoad)
		th.onLoad();
	var jqh = th.jcontainer;
	var w = (utils.screenWidth() - jqh.width()) / 2 + utils.scrollOffsetX();
	jqh.css('margin-left', 0).css('left', w + 'px');
};

PopupForm.prototype.beforeHide = function() {
	this.jcontainer
		.css('top', (document.documentElement.scrollTop || document.body.scrollTop) + Math.round(17 * (document.documentElement.offsetHeight || document.body.clientHeight) / 100) + 'px')
		.css('width', '650px');
};

PopupForm.prototype.show = function() {
	this.hide();
	var th = this;
	var jph = th.jcontainer;
	jph.jqm({
		ajax: th.formUrl,
		modal: true,
		onShow: function(hash) {
			hash.w.html("<div style='text-align:center;padding:20px;'><img src='" + __root + "Content/images/load_circle.gif' /></div>").show();
		},
		onLoad: function() {
			th.initAjaxForm();
		}
	});
	jph.jqmShow();
	jph.draggable({ handle: '.popup-title' });
	return false;
};

PopupForm.prototype.hide = function() {
	var jph = this.jcontainer;
	if (jph.css('display') != 'none') {
		this.beforeHide();
		jph.jqmHide();
	}
};


function setupSignUpForm(form1) {
	form1.ajaxForm({
		dataType: 'json',
		success: function (result) {
			if (result.status == "ok") {
				if (form1.attr('action').match(/signup/gi)) {
					document.location = __root + "Account/SignUpComplete";
				} else {
					window.location.reload(true);
				}
			} else if (result.status == "first") {
				window.location = __root + 'Profile/FirstLogin';
			} else {
				var inputs = form1.find('input,select');
				inputs.removeClass('validation-error').removeAttr('title');
				form1.find('[name="CaptchaText"]').val('');
				form1.find('[name="CaptchaKey"]').val(result.captchaKey);
				form1.find('img.captcha').attr('src', __root + 'Account/Captcha/' + result.captchaKey);
				var i;
				for (i in result.errors) {
					var pattern = new RegExp('^' + result.errors[i].Field + (result.errors[i].Field == 'BirthDate' ? '' : '$'), 'i');
					for (var j in inputs) {
						if (inputs[j] && typeof (inputs[j].name) != 'undefined' && inputs[j].name.match(pattern)) {
							inputs[j].title = result.errors[i].Message;
							jQuery(inputs[j]).addClass('validation-error');
						}
					}
				}
				form1.find('input.validation-error').first().focus();
			}
		}

	});
}

//// login form
loginForm = {
	cont: null,
	getOverlayContent: function () {
		return "";
	},

	init: function () {
		loginForm.cont = jQuery("#login-popup-holder")
			.jqm({ modal: true });
	},

	show: function () {
		newCaptcha('#p_CaptchaKey', '#p_CaptchaImg');

		loginForm.cont.jqmShow();

		jQuery('#l_Email').focus();

		var form1 = loginForm.cont.find('#form1');
		form1
			.resetForm()
			.find('.validation-error')
			.removeClass('validation-error');
		setupSignUpForm(form1);

		var form2 = loginForm.cont.find('#form2');
		form2
			.resetForm()
			.find('.validation-error')
			.removeClass('validation-error');
		setupSignUpForm(form2);

	},

	hide: function () {
		loginForm.cont.jqmHide();
	}
};

//// forgot password form
forgotPasswordForm = new PopupForm(__root + 'Account/ForgotPassword', __root + 'Account/ForgotPassword');

forgotPasswordForm.onLoad = function() {
	jQuery('#Email').focus();
};
forgotPasswordForm.onSuccessOk = function(resp) {
	window.location = __root + 'Account/PasswordRestoreComplete';
};


//// send message form
msgForm = new PopupForm(null, __root + 'Messages/Run/Send');

msgForm.onLoad = function() {
	var jsbj = jQuery('#Subject');
	var jtext = jQuery('#Text');
	var s = jsbj.val();
	if (s && s.length > 0)
		jtext.focus();
	else
		jsbj.focus();
};

msgForm.onSuccess = function(resp) {
	if (resp.substring(0, 2) == "ok") {
		this.hide();
		jQuery.growl('Successful', 'Your message has been sent to ' + resp.substring(2));
	} else {
		var jqh = jQuery('#__popupHolder');
		jqh.html(resp);
		this.onLoad();
		jqh.jqm();
	}
};

msgForm.send = function(rcvrId, threadID) {
	this.formUrl = __root + 'Messages/Run/SendNew?RecepientID=' + rcvrId;
	if (threadID)
		this.formUrl += '&ThreadID=' + threadID;
	this.show();
};

