function switchInfo(_object) {
	var ID = 0;
	var ID_current = $(_object).parent().attr('id').match(/^link(\d+)$/i)[1];
	var flagCurrent = Boolean(false);

	$('div.advantages div[id^="link"][class="link-style"]').each(function() {
		ID = $(this).attr('id').match(/^link(\d+)$/i)[1];

		flagCurrent = Boolean(false);
		if (parseInt(ID) == parseInt(ID_current)) {
			flagCurrent = Boolean(true);
		}

		if (flagCurrent) {
			$(this).next().toggle();
		} else {
			$(this).next().hide();
		}
	});
}

function jQueryAJAXRequest(_form, _function, _arguments) {
	var URL = '/admin/ajax.php?controller=myplugins' + _function;

	var POST = '';
	for (var field in _arguments) {
		POST += (POST ? '&' : '') + field + '=' + encodeURIComponent(_arguments[field]);
	}

	var ajaxResult = {
	  status: -1,
		response: {
			code: 3
		},
		errors: {}
	};
	$.ajax({
		url: URL,
		type: 'POST',
		cache: false,
		async: false,
		data: POST,
		dataType: 'json',
		success: function(ajaxResponse, textStatus, jqXHR) {
			ajaxResult['status'] = parseInt(jqXHR.status);

			if (ajaxResponse != null) {
				ajaxResult['response'] = ajaxResponse['status'];

				if (typeof(ajaxResponse['errors']) != 'undefined') {
					ajaxResult['errors'] = ajaxResponse['errors'];
				}
			}
		},
		error: function(jqXHR, textStatus, errorThrown) {
			ajaxResult['status'] = parseInt(jqXHR.status);
		}
	});

	return ajaxResult;
}

function p502_AskQuestion() {
	var form = '#form_askquestion';
	var fields = {};

	$(form + ' div.status').addClass('style_info').html('Производится проверка введенных данных...');
	$(form + ' input#submit').attr('disabled', 'disabled');

	fields['name'] = $(form + ' input[name="name"]').attr('disabled', 'disabled').val();
	fields['telephone'] = $(form + ' input[name="telephone"]').attr('disabled', 'disabled').val();
	fields['email'] = $(form + ' input[name="email"]').attr('disabled', 'disabled').val();
	fields['question'] = $(form + ' textarea[name="question"]').attr('disabled', 'disabled').val();

	var ajaxResult = jQueryAJAXRequest(form, '&pluginid=502&action=ajax_askquestion', fields);

	$(form + ' div.status, ' + form + ' div.error').empty();
	$(form + ' div.status').removeClass('style_info style_success style_fail');

	if ((ajaxResult['status'] == 200) && (parseInt(ajaxResult['response']['code']) == 0)) {
		$(form + ' div.status').addClass(ajaxResult['response']['class']).html(ajaxResult['response']['msg']);

		$(form + ' div.header, ' + form + ' table.form').hide();
	} else {
		if ((ajaxResult['status'] != 200) || (parseInt(ajaxResult['response']['code']) == 3)) {
			$(form + ' div.status').addClass('style_fail').html('В ходе выполнения операции произошла ошибка.<br />Попробуйте еще раз через несколько минут');

			$(form + ' div.header, ' + form + ' table.form').hide();
		} else if (parseInt(ajaxResult['response']['code']) == 1) {
			$(form + ' input, ' + form + ' textarea').each(function() {
				$(this).removeAttr('disabled');
			});

			var flagFirstField = true;
			for (var field in ajaxResult['errors']) {
				$(form + ' ' + ajaxResult['errors'][field]['selector']).html(ajaxResult['errors'][field]['msg']);

				if (flagFirstField) {
					$(form + ' [name="' + field + '"]').focus();
					flagFirstField = false;
				}
			}
		} else if (parseInt(ajaxResult['response']['code']) == 2) {
			$(form + ' div.status').addClass(ajaxResult['response']['class']).html(ajaxResult['response']['msg']);

			$(form + ' div.header, ' + form + ' table.form').hide();
		}
	}
}

function p502_Call() {
	var form = '#form_call';
	var fields = {};

	$(form + ' div.status').addClass('style_info').html('Производится проверка введенных данных...');
	$(form + ' input#submit').attr('disabled', 'disabled');

	fields['name'] = $(form + ' input[name="name"]').attr('disabled', 'disabled').val();
	fields['address'] = $(form + ' input[name="address"]').attr('disabled', 'disabled').val();
	fields['telephone'] = $(form + ' input[name="telephone"]').attr('disabled', 'disabled').val();
	fields['email'] = $(form + ' input[name="email"]').attr('disabled', 'disabled').val();
	fields['comment'] = $(form + ' textarea[name="comment"]').attr('disabled', 'disabled').val();

	var ajaxResult = jQueryAJAXRequest(form, '&pluginid=502&action=ajax_call', fields);

	$(form + ' div.status, ' + form + ' div.error').empty();
	$(form + ' div.status').removeClass('style_info style_success style_fail');

	if ((ajaxResult['status'] == 200) && (parseInt(ajaxResult['response']['code']) == 0)) {
		$(form + ' div.status').addClass(ajaxResult['response']['class']).html(ajaxResult['response']['msg']);

		$(form + ' div.header, ' + form + ' table.form').hide();
	} else {
		if ((ajaxResult['status'] != 200) || (parseInt(ajaxResult['response']['code']) == 3)) {
			$(form + ' div.status').addClass('style_fail').html('В ходе выполнения операции произошла ошибка.<br />Попробуйте еще раз через несколько минут');

			$(form + ' div.header, ' + form + ' table.form').hide();
		} else if (parseInt(ajaxResult['response']['code']) == 1) {
			$(form + ' input, ' + form + ' textarea').each(function() {
				$(this).removeAttr('disabled');
			});

			var flagFirstField = true;
			for (var field in ajaxResult['errors']) {
				$(form + ' ' + ajaxResult['errors'][field]['selector']).html(ajaxResult['errors'][field]['msg']);

				if (flagFirstField) {
					$(form + ' [name="' + field + '"]').focus();
					flagFirstField = false;
				}
			}
		} else if (parseInt(ajaxResult['response']['code']) == 2) {
			$(form + ' div.status').addClass(ajaxResult['response']['class']).html(ajaxResult['response']['msg']);

			$(form + ' div.header, ' + form + ' table.form').hide();
		}
	}
}

$(document).ready(function() {
	$('a[href$=".jpg"], a[href$=".png"], a[href$=".gif"]').fancybox({
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		speedIn: 350,
		speedOut: 200,
		cyclic: true,
		overlayShow: true,
		titlePosition: 'inside'
	});

	$("a#modal_askquestion, a#modal_call").bind('click', function() {
		var form = $(this).attr('href');

		$(form + ' div.button_close').click(function() {
			$(form).hide();
		});

		$(form).css({right: '-25px', top: '-25px'});
		
		$(form + ' div.status, ' + form + ' div.error').empty();
		$(form + ' div.status').removeClass('style_info style_success style_fail');
		$(form + ' div.header,' + form + ' table.form').show();

		$(form + ' input, ' + form + ' textarea').each(function() {
			$(this).removeAttr('disabled');
			if ($(this).attr('type') != 'button') {
				$(this).attr('value', '');
			}
		});

		$(form).show();

		return false;
	});
});

