$(document).ready(function(){
	$('.list-questions-answers li a').click(function(){
		if ($(this).parent().find('p').length > 0) {
			$(this).toggleClass('sel');
			$(this).parent().find('p').slideToggle(300);
		}
		return false;
	})

	window.confirm = false;

	/*Check own-question-form,feedback-form*/
	jQuery.fn.check = function(){
		var empty = 0;
		$('.necessary').each(function(){
			if ($(this).hasClass('email')) {
				var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
				var email = $(this).val();
				if (!pattern.test(email)) {
					$(this).parent().addClass('error');
					confirm = false;
				}
				else {
					$(this).parent().removeClass('error');
					confirm = true;
				}
			}
			if ($(this).val() == '' || typeof $(this).val() == 'undefined' || $(this).val() == $(this).attr('longdesc')) {
				$(this).parent().addClass('error');
				empty = empty + 1;
			}
			else {
				$(this).parent().removeClass('error');
				if (confirm == false) {
					$('.email').parent().addClass('error');
					empty = empty + 1;
				}
				else {
					$('.email').parent().removeClass('error');
				}
			}
			if (empty > 0) {
				$(this).parent().parent().parent().find('.send-btn').attr('disabled','disabled');
				//console.log('dis');
			} else {
				$(this).parent().parent().parent().find('.send-btn').removeAttr('disabled');
				//console.log('not dis');
			}
		});
	}

	$('.necessary').change(function(){
		$(this).check();
	}).focus(function(){
		$(this).check();
	}).keyup(function(){
		$(this).check();
	}).click(function(){
		$(this).check();
	}).blur(function(){
		$(this).check();
	});

	/*Sending-data*/
	/*
	$('.own-question .send-btn,.feedback .send-btn').click(function(){
		$('#own-question-form,#feedback-form').submit();
		return false;
	});
	*/

	$('#own-question-form,#feedback-form').submit(function(){
		$.post('?', $(this).find('input,textarea').serialize(),function(data){
			$('.thanks').fadeIn(300);
		});
		return false;
	});

	$('.more a').click(function(){
		count = parseInt($('.more').text().replace(/[^0-9 ]+/ig," "));
		c = 0;
		$('.list .item.dnone').each(function(){
			c = c + 1;
			if (c <= count) {
				$(this).removeClass('dnone');
			}
		})
		return false;
	})

	$('.peoples-search span').click(function(){
		$('.search-row input').val($(this).text());
	})

	$('.target-blank a').each(function(){
		$(this).attr('target','_blank');
	});

	$('.feedback .necessary').each(function(){
		$(this).attr('longdesc',$(this).val());
	});

	$('.feedback .necessary').focus(function(){
		if ($(this).val() == $(this).attr('longdesc')) {
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == '') {
			$(this).val($(this).attr('longdesc'));
		}
	});

	$('.text-editor table').each(function(){
		$(this).find('tr:eq(0)').addClass('first-line');
	});

	$('.text-editor .marker-list,.text-editor .number-list').each(function(){
		$(this).prev('p:eq(0)').addClass('list-title');
	});

	$('#feedback_form').find('.send-btn').click(function(){
		var form = $('#feedback_form');
		var flag = true;
		form.find('.necessary').each(function(){
			if ($(this).val() == $(this).attr('longdesc'))
				flag = false;
		});

		if (!flag) {
			alert('Пожалуйста заполните необходимые поля.')
			return false;
		}
	});

})
