$(function () {

	/* скругление уголков блоков */
	$('.rounded').corner('keep');

	/* исправление прозрачности png */
	$(document).pngFix();

	/* форма обратной связи */
	$('.modal').qtip({
		content:{text:'<form method="post" style="padding:0 20px 10px 10px"><p>Адрес для ответа<span style="color:red">*</span>:<br><input type="text" id="address" name="feedback[address]" style="width:100%" maxlength="128"></p><p>Тема письма<span style="color:red">*</span>:<br><input type="text" name="feedback[subject]" style="width:100%" maxlength="256"></p><p>Текст сообщения<span style="color:red">*</span>:<br><textarea name="feedback[text]" style="width:100%;height:60px" maxlength="1024"></textarea></p><p>Текст с картинки<span style="color:red">*</span>:<br><img src="/captcha/" alt="Captcha" style="vertical-align:middle;cursor:pointer" onclick="kompassCaptcha(this)"/> → <input type="text" id="captcha" name="feedback[captcha]" style="margin:0 0 0 15px" size="9" maxlength="9"></p><input type="submit" value="Отправить сообщение"></form>',title:{text:'Обратная связь',button:'<img src="/img/kompass_close.png" alt="Закрыть">'}},
		show:{when:'click',solo:true},
		hide:false,
		position:{target:$(document.body),corner:'center'},
		style:{width:500,border:{width:9,radius:9,color:'#666666'}},
		api:{
			onShow:function () {
				$('#address').focus();
			}
		}
	});

	/* справка для блоков данных */
	$('.help').each(function () {
		$(this).qtip({
			content:{text:$(this).attr('title'),title:{text:'Справка',button:'<img src="/img/kompass_close.png" alt="Закрыть">'}},
			show:{when:'click',solo:true},
			hide:false,
			position:{corner:{tooltip:'topLeft',target:'bottomRight'}},
			style:{tip:true,width:{min:200,max:450}}
		}).removeAttr('title');
	});

	/* диалог выбора цвета */
	$('.color').ColorPicker({
		onShow:function (picker) {
			$(picker).fadeIn(500);
			return false;
		},
		onBeforeShow:function () {
			$(this).ColorPickerSetColor('#' + $(this).val());
		},
		onHide:function (picker) {
			$(picker).fadeOut(500);
			return false;
		},
		onSubmit:function (hsb, hex, rgb, el) {
			$(el).val(hex);
			$(el).ColorPickerHide();
			kompassPreview(el.id, hex);
		}
	}).change(function () {
		kompassPreview($(this).attr('id'), $(this).val());
	});

});

/* обновление капчи при клике */
function kompassCaptcha(obj)
{
	$(obj).attr('src', '/captcha/?' + Math.random());
	$('#captcha').val('').focus();
}

/* редактирование, отмена, сохранение (старый FCK Editor)*/
function kompassField(action, id)
{
	if (action == 'edit')
	{
		$('#message').slideUp();
		$('#' + id + ',#' + id + '-edit').hide();
		$('#' + id + '-textarea').css('display', 'block').find('textarea:not(.delayedfck)').addClass('delayedfck').each(function () {
			if (!(name = $(this).attr('name')))
				return;

			var width = $(this).get(0).style.width ? $(this).get(0).style.width : ($(this).css('width') ? $(this).css('width') : null);
			var height = $(this).get(0).style.height ? $(this).get(0).style.height : ($(this).css('height') ? $(this).css('height') : null);

			var fck = new FCKeditor(name, width, parseInt(height) + 90);
			fck.Config['CustomConfigurationsPath'] = '/scripts/bbcode.js';
			fck.ReplaceTextarea();
		});
		$('#' + id + '-save,#' + id + '-cancel').css('display', 'inline');
	}
	else if (action == 'cancel')
	{
		$('#' + id + '-textarea,#' + id + '-save,#' + id + '-cancel').hide();
		$('#' + id).css('display', 'block');
		$('#' + id + '-edit').css('display', 'inline');
		$('#' + id + '-textarea').parents('form').get(0).reset();
		kompassCustom(id, true);
	}
	else if (action == 'save')
	{
		$('#' + id + '-textarea').parents('form').submit();
		$('#' + id + '-throbber').show();
		$('#' + id + '-save,#' + id + '-cancel').hide();
		$('body').css('cursor', 'progress');
	}
}

/* редактирование, отмена, сохранение (новый CK Editor) */
function kompassFieldCK(action, id)
{
	if (action == 'edit')
	{
		$('#message').slideUp();
		$('#' + id + ',#' + id + '-edit').hide();
		$('#' + id + '-textarea').css('display', 'block').find('textarea:not(.delayedfck)').addClass('delayedfck').each(function () {
			if (!(name = $(this).attr('name')))
				return;
		});
		$('#' + id + '-save,#' + id + '-cancel').css('display', 'inline');
	}
	else if (action == 'cancel')
	{
		$('#' + id + '-textarea,#' + id + '-save,#' + id + '-cancel').hide();
		$('#' + id).css('display', 'block');
		$('#' + id + '-edit').css('display', 'inline');
		$('#' + id + '-textarea').parents('form').get(0).reset();
		kompassCustom(id, true);
	}
	else if (action == 'save')
	{
		$('#' + id + '-textarea').parents('form').submit();
		$('#' + id + '-throbber').show();
		$('#' + id + '-save,#' + id + '-cancel').hide();
		$('body').css('cursor', 'progress');
	}
}

/* ввод собственного города/банка */
function kompassCustom(id, reset)
{
	if (!$('#' + id + '-select').length)
		return;
	if (!reset && $('#' + id + '-select').css('display') != 'none')
	{
		$('#' + id + '-select').hide();
		$('#' + id + '-custom').show();
	}
	else
	{
		$('#' + id + '-select').show();
		$('#' + id + '-custom').hide();
	}
}

/* просмотр результата изменения цвета */
function kompassPreview(id, hex)
{
	switch(id)
	{
		case 'color-background':
			$('body').css('backgroundColor', '#' + hex);
			$('.rounded').uncorner().corner();
		break;

		case 'color-text':
			$('body').css('color', '#' + hex);
		break;

		case 'color-link':
			$('a').css('color', '#' + hex);
		break;

		case 'color-block':
			$('.block').css('backgroundColor', '#' + hex);
			$('.rounded').uncorner().corner();
		break;

	}
}

/* удаление загруженного файла */
function kompassDelete (index)
{
	if (!confirm('Вы действительно хотите удалить этот файл?'))
		return;
	$('#delete').val(index);
	$('#file-textarea').parents('form').submit();
	$('#file-throbber').show();
	$('#file-save,#file-cancel').hide();
	$('body').css('cursor', 'progress');
}

/* печать данных */
function kompassPrint (type)
{
	window.open('http://' + window.location.hostname + '/' + '?print=' + type);
}
