$(document).ready(function () {
	if (typeof jQuery !== "undefined" && typeof jQuery.modal !== "undefined") {
		$('.user_login').click(function (e) {
			e.preventDefault();
			// display the login form
			//<a href="#" title="Close" class="modalCloseX simplemodal-close">x</a>
			$('#login-content').modal({
				closeHTML: '<div class="dialog-login-cancel">&nbsp;</div>',
				overlayId: 'login-overlay',
				containerId: 'login-container',
				onOpen: 'Открыть',
				onShow: 'Показать',
				onClose: 'Закрыть',
				//minHeight: 208,
				minWidth: 300
			});
			
		var html = '<div class="dialog-login-head">Вход в систему</div>';
		html += '<div class="dialog-login-body">';
		html += '<form method="post" action="/" onsubmit="return LoginDialog.submit()">'
		html += '<div class="row"><div class="col_a">E-mail:</div><div class="col_b"><input id="dialog_login_email" class="text" type="text" tabindex="1"></input></div></div>';
		html += '<div class="row"><div class="col_a">Пароль:</div><div class="col_b"><input id="dialog_login_password" class="password" type="password" tabindex="2"></input></div></div>';
		html += '<div id="dialog_login_wrong" align="right" style="color: red; display: none; padding-right:10px;">Неправильный логин или пароль!</div>';
		html += '<div style="padding-right:10px; text-align:right"><a href="/remember_pass/main" style="font-size:11px; text-decoration:underline;" tabindex="5">Забыли пароль?</a></div>';
		html += '<div class="clear"></div>';
		html += '<div class="padTop10"><div class="col_a"></div><div class="col_b"><input type="checkbox" name="remember" id="remember_user_login" tabindex="3" /><label for="remember_user_login">Запомнить меня</label></div></div>';
		html += '<div class="clear"></div>';
		html += '<div class="padTop10"><div class="col_a"></div><div class="col_b">';
		html += '<table style="display: inline;" cellspacing="0" cellpadding="0"><tr><td class="btn-left"></td><td class="btn-center" align="center"><input type="submit" value="Войти" class="CButton" tabindex="4" /></td><td class="btn-right"></td></tr></table>';
		html += '</div></div><div class="clear"></div>';
		html += '</form>';
		html += '</div>';
		$(html).insertAfter('.dialog-login-cancel');
		/*$('#login-container .login-message').animate({
				height: '30px'
			}, 'Good!!!');*/
		});
	}
});


var LoginDialog = function(){
	
	var wnd = null;
	var wbg = null;
	var isModal = false;
	var modalLayer = null;
	var addParam = "";
	var redirectUrl = false;
	
	var createWnd = function(){
		
		modalLayer = document.createElement('DIV');
		modalLayer.style.display = "none";
		modalLayer.className = 'modal-overlay';
		document.body.appendChild(modalLayer);

		wbg = document.createElement('DIV');
		wbg.style.display = "none";
		wbg.className = 'dialogBG';
		wbg.setAttribute('onClick', 'LoginDialog.hide()');
		document.body.appendChild(wbg);

		wnd = document.createElement('DIV');
		wnd.style.display = "none";
		wnd.className = 'dialog-login-wnd';
		var html = '<div class="dialog-login-cancel" onclick="LoginDialog.hide();">&nbsp;</div><div class="dialog-login-head">Вход в систему</div>';
		html += '<div class="dialog-login-body">';
		html += '<form method="post" action="/" onsubmit="return LoginDialog.submit()">'
		html += '<div class="row"><div class="col_a">E-mail:</div><div class="col_b"><input id="dialog_login_email" class="text" type="text" tabindex="1"></input></div></div>';
		html += '<div class="row"><div class="col_a">Пароль:</div><div class="col_b"><input id="dialog_login_password" class="password" type="password" tabindex="2"></input></div></div>';
		html += '<div id="dialog_login_wrong" align="right" style="color: red; display: none; padding-right:10px;">Неправильный логин или пароль!</div>';
		html += '<div style="padding-right:10px; text-align:right"><a href="/remember_pass/main" style="font-size:11px; text-decoration:underline;" tabindex="5">Забыли пароль?</a></div>';
		html += '<div class="clear"></div>';
		html += '<div class="padTop10"><div class="col_a"></div><div class="col_b"><input type="checkbox" name="remember" id="remember_user_login" tabindex="3" /><label for="remember_user_login">Запомнить меня</label></div></div>';
		html += '<div class="clear"></div>';
		html += '<div class="padTop10"><div class="col_a"></div><div class="col_b">';
		html += '<table style="display: inline;" cellspacing="0" cellpadding="0"><tr><td class="btn-left"></td><td class="btn-center" align="center"><input type="submit" value="Войти" class="CButton" tabindex="4" /></td><td class="btn-right"></td></tr></table>';
		html += '</div></div><div class="clear"></div>';
		html += '</form>';
		html += '</div>';
		wnd.innerHTML = html;
		document.body.appendChild(wnd);
	}
	
	return {
		show: function(focusElId, authRedirectUrl){
			if(authRedirectUrl){
				redirectUrl = authRedirectUrl;
			}else{
				redirectUrl = false;
			}
		
			if(focusElId != undefined){
				addParam = "#"+focusElId;
			}else{
				addParam = "";
			}
			
			if(wnd == null){
				createWnd();
			}
			if(isModal){
				modalLayer.style.height = document.height;
				modalLayer.style.width = document.width;
				modalLayer.style.display = "block";
			}
			
			wnd.style.top = Math.round(document.body.scrollTop+document.body.clientHeight/3)+"px";
			wbg.style.display = "block";
			wnd.style.display = "block";
			document.getElementById('dialog_login_email').focus();
		},
		
		hide: function(){
			if(isModal){
				modalLayer.style.display = "none";
			}
			wnd.style.display = "none";
			wbg.style.display = "none";
		},
		
		submit: function(){
			var params = {
				email: document.getElementById('dialog_login_email').value,
				password: document.getElementById('dialog_login_password').value,
				remember: document.getElementById('remember_user_login').checked
			};
			
			$.ajax({
				type: "POST",
				url: "/login_ajax/main/login",
				data: params,
				dataType: "json",
				params: params,
				success: function(data, status){
					if(data.logged == 1){
						if(redirectUrl){
							window.location.href = redirectUrl;
						}else{
							var url = window.location.href;
							var foundOffset = url.indexOf("#");
							if (foundOffset != -1) {
								url = url.substring(0,foundOffset-1)
							}
							window.location.href = url+addParam;
							window.location.reload(true);
						}
					}else{
						document.getElementById('dialog_login_wrong').style.display = "block";
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert(zzz);
				}
			});
			return false;
		}
	}
}();