var showed = false;
var linkObj;
function shWriteus(link) {
	if(showed) {
		$($('writeus').style).display = 'none';
		showed = false;
	} else {
		$($('writeus').style).display = '';
		showed = true;
		linkObj = link
	}
}

function sendUs() {
	var browser = navigator.appName + " " + navigator.appVersion;
	
	var prio = jQuery('#support_form input[name="writeusPrio"]:checked').attr('value');
	var pars = '&url='
		+ encodeURIComponent(document.location) + '&prio=' + prio + '&browser='
		+ browser + '&sendText='
		+ encodeURIComponent($('writeusText').value) + '&referrer='
		+ encodeURIComponent(document.referrer);
	if($('writeusText').value.length < 3) {
		alert('Необходимо ввести текст!')
		return
	}
	new Ajax.Request(
		$('support_form').action, {
			method: 'post',
			postBody: pars,
			onLoading: function () {
				shWriteus(linkObj);
			},
			onSuccess: function (transport) {
				$('writeusText').value = '';
				$($('popup_simple').getElementsByTagName('p')[0]).update('Сообщение об ошибке успешно отправлено. Мы всё почитаем и исправим.')
				showPopup(linkObj, 'popup_simple', {left: -200, top: -120})
				setTimeout("closePopup('popup_simple')", 2000);
			},
			onFailure: function (transport) {
				$($('popup_simple').getElementsByTagName('p')[0]).update('Не удалось отправить сообщение об ошибке.')
				showPopup(linkObj, 'popup_simple', {left: -200, top: -120})
				setTimeout("closePopup('popup_simple')", 2000);
			}
		}
	);
	return false;
}

function doEdit(id) {
	var url = '/actions.html?action=edit_support';
	var pars = 'edit=' + id;
	var myAjax = new Ajax.Request(
		url, {
			method: 'post',
			postBody: pars,
			onLoading: function () {
				$($('view_'+id).style).display = 'none';
				$($('edit_'+id).style).display = 'none';
			},
			onSuccess: function (request) {
				answer = eval(request.responseText);
				$('view_'+id).innerHTML = '<textarea id="text_' + id + '">' + answer.text +'</textarea><br /><input type="button" value="save" onclick="doSave(' + id + ')" />';
				$($('view_'+id).style).display = '';
			},
			onFailure: function (request) {
				alert('Не удалось загрузить.');
			}
		}
	);
}
function doSave(id) {
	var url = '/actions.html?action=save_support';
	var pars = 'edit=' + id + '&editText=' + encodeURIComponent($('text_'+id).value);
	var myAjax = new Ajax.Request(
		url, {
			method: 'post',
			postBody: pars,
			onLoading: function () {
				$($('view_'+id).style).display = 'none';
			},
			onSuccess: function (request) {
				answer = eval(request.responseText);
				$('view_'+id).innerHTML = answer.text;
				$($('view_'+id).style).display = '';
				$($('edit_'+id).style).display = '';
			},
			onFailure: function (request) {
				alert('Не удалось сохранить.');
			}
		}
	);
}