﻿$(document).ready(function () {
	if (typeof jQuery !== "undefined" && typeof jQuery.modal !== "undefined") {
		$('#connection_form').click(function (e) {
			e.preventDefault();
			$('#connection-content').modal({
				closeHTML: '<div class="dialog-connection-cancel">&nbsp;</div>',
				overlayId: 'connection-overlay',
				containerId: 'connection-container',
				onOpen: 'Открыть',
				onShow: 'Показать',
				onClose: 'Закрыть',
				//minHeight: 548,
				minWidth: 390
			});
			
		var conectionCategory = '';
		for(var i in ConnectionCategoryArray){
			conectionCategory += '<option value="'+i+'">'+ConnectionCategoryArray[i]+'</option>';
		}	
		var html = '<div class="dialog-connection-head">Обратная связь</div><div class="dialog-connection-body"><form method="post"action="/"onsubmit="return ConnectionDialog.submit()"><table class="connectionform"><tr><td>E-mail:</td><td>Ф.И.О:</td></tr><tr><td width="50%"><input id="dialog_connection_email" class="small" type="text" '+disableConnectionField+' tabindex="1" value="'+CurrentUserEmail+'" /><input id="dialog_connection_person_id" type="hidden" value="'+CurrentUserId+'"/></td><td width="50%"><input id="dialog_connection_fullname" class="small" type="text" tabindex="2" '+disableConnectionField+' value="'+CurrentUserFullName+'" /></td></tr><tr><td colspan="2">Тема:</td></tr><tr><td colspan="2"><select id="dialog_connection_theme"tabindex="3">'+conectionCategory+'</select></td></tr><tr><td colspan="2">Сообщение:</td></tr><tr><td colspan="2"><textarea id="dialog_connection_message"tabindex="4"></textarea></td></tr><tr><td colspan="2"><div id="dialog_connection_wrong"align="right"style="color: red; display: none; padding-right: 10px;"></div></td></tr><tr><td colspan="2"><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="5"/></td><td class="btn-right"></td></tr></table></td></tr></table><div class="clear"></div></form></div>';
		$(html).insertAfter('.dialog-connection-cancel');
		/*$('#login-container .login-message').animate({
				height: '30px'
			}, 'Good!!!');*/
		});
	}
});

var ConnectionDialog = 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', 'ConnectionDialog.hide()');
		document.body.appendChild(wbg);

		wnd = document.createElement('DIV');
		var conectionCategory = '';
		for(var i in ConnectionCategoryArray){
			conectionCategory += '<option value="'+i+'">'+ConnectionCategoryArray[i]+'</option>';
		}
		wnd.style.display = "none";
		wnd.className = 'dialog-connection-wnd';
		var html = '<div class="dialog-connection-cancel"onclick="ConnectionDialog.hide();">&nbsp;</div><div class="dialog-connection-head">Обратная связь</div><div class="dialog-connection-body"><form method="post"action="/"onsubmit="return ConnectionDialog.submit()"><table class="connectionform"><tr><td>E-mail:</td><td>Ф.И.О:</td></tr><tr><td width="50%"><input id="dialog_connection_email" class="small" type="text" '+disableConnectionField+' tabindex="1" value="'+CurrentUserEmail+'" /><input id="dialog_connection_person_id" type="hidden" value="'+CurrentUserId+'"/></td><td width="50%"><input id="dialog_connection_fullname" class="small" type="text" tabindex="2" '+disableConnectionField+' value="'+CurrentUserFullName+'" /></td></tr><tr><td colspan="2">Тема:</td></tr><tr><td colspan="2"><select id="dialog_connection_theme"tabindex="3">'+conectionCategory+'</select></td></tr><tr><td colspan="2">Сообщение:</td></tr><tr><td colspan="2"><textarea id="dialog_connection_message"tabindex="4"></textarea></td></tr><tr><td colspan="2"><div id="dialog_connection_wrong"align="right"style="color: red; display: none; padding-right: 10px;"></div></td></tr><tr><td colspan="2"><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="5"/></td><td class="btn-right"></td></tr></table></td></tr></table><div class="clear"></div></form></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_connection_email').focus();
		},
		
		hide: function(){
			if(isModal){
				modalLayer.style.display = "none";
			}
			wnd.style.display = "none";
			wbg.style.display = "none";
		},
		
		submit: function(){
			document.getElementById('dialog_connection_wrong').style.display = "none";
			var params = {
				email: document.getElementById('dialog_connection_email').value,
				fullname: document.getElementById('dialog_connection_fullname').value,
				message: document.getElementById('dialog_connection_message').value,
				category_id: document.getElementById('dialog_connection_theme').value
			};
			
			$.ajax({
				type: "POST",
				url: "/connection_ajax/main/connection",
				data: params,
				dataType: "json",
				params: params,
				success: function(data, status){
					if(data.created == 1){
						if(redirectUrl){
							window.location.href = redirectUrl;
						}else{
							window.location.reload(true);
						}
					}else{
						if(data.error == 1){
							document.getElementById('dialog_connection_wrong').innerHTML = 'Необходимо заполнить все поля';
							document.getElementById('dialog_connection_wrong').style.display = "block";
						}
						if(data.error == 2){
							document.getElementById('dialog_connection_wrong').innerHTML = 'Некорректный E-mail';
							document.getElementById('dialog_connection_wrong').style.display = "block";
						}
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					
				}
			});
			return false;
		}
	}
}();