var prev_comment = 0;

// Ответ на комментарий
function cr(comment_id)
{
	if (prev_comment && prev_comment != comment_id)
	{
		document.getElementById(prev_comment).style.display = 'none';
	}

	var div = document.getElementById(comment_id);

	if (div.style.display != 'block')
	{
		div.style.display = 'block';
	}
	else
	{
		div.style.display = 'none';
	}

	prev_comment = comment_id;
}

var temp_ChildId = '';
var temp_CurrenElementId = '';
var menu_timeout_id = 0;
var filter_timeout_id = 0;

// обработчик наведения мыши на меню
function TopMenuOver(CurrenElementId, ChildId)
{
	clearTimeout(menu_timeout_id);

	if (temp_CurrenElementId != ''
	&& temp_CurrenElementId != CurrenElementId)
	{
		var oTemp_ChildId = document.getElementById(temp_ChildId);

		if (oTemp_ChildId)
		{
			oTemp_ChildId.style.display = "none";
		}
	}

	temp_ChildId = ChildId;
	temp_CurrenElementId = CurrenElementId;

	if (CurrenElementId == undefined)
	{
		return false;
	}

	if (ChildId != '')
	{
		var oChildId = document.getElementById(ChildId);

		if (oChildId)
		{
			oChildId.style.display = "block";
			//oChildId.style.opacity = 0.4;
			//	oChildId.style.filter = 'alpha(opacity=100, style=1, finishopacity=60)';
			//oChildId.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0, style=0)";
		}
	}
}

// обработчик уведения мыши с меню
function TopMenuOut(CurrenElementId, ChildId)
{
	if (CurrenElementId == undefined)
	{
		return false;
	}

	if (ChildId != '')
	{
		var oChildId = document.getElementById(ChildId);
		if (oChildId)
		{
			menu_timeout_id = setTimeout(function (){oChildId.style.display = "none"}, 300);
		}
	}
}

// Функция обратного вызова для AddIntoCart
function callbackfunction_AddIntoCart(responseJS)
{
	// Результат принят
	sended_request = false;

	if (typeof responseJS != 'undefined')
	{
		// Данные.
		if (typeof responseJS.cart != 'undefined')
		{
			var little_cart = document.getElementById('little_cart');

			if (little_cart)
			{
				// Создадим скрытый SPAN для IE, в который поместим текст + скрипт.
				// Если перед <script> не будет текста, нехороший IE не увидит SCRIPT
				var span = document.createElement("span");
				span.style.display = 'none';
				span.innerHTML = "Stupid IE. " + responseJS.cart;

				runScripts(span.getElementsByTagName('SCRIPT'));

				little_cart.innerHTML = responseJS.cart;
			}
			else
			{
				alert('Ошибка! Краткая корзина не найдена');
			}
		}
	}
}

function AddIntoCart(shop_path, item_id, item_count)
{
	//location.href = shop_path + 'cart/?ajax_add_item_id=' + item_id + '&count=' + item_count;
	cmsrequest = shop_path + 'cart/?ajax_add_item_id=' + item_id + '&count=' + item_count;

	// Отправляем запрос backend-у
	sendRequest(cmsrequest, 'get', callbackfunction_AddIntoCart);

	return false;
}

/**
* Склонение после числительных
* int number числительное
* int nominative Именительный падеж
* int genitive_singular Родительный падеж, единственное число
* int genitive_plural Родительный падеж, множественное число
*/
function declension(number, nominative, genitive_singular, genitive_plural)
{
	var last_digit = number % 10;
	var last_two_digits = number % 100;

	if (last_digit == 1 && last_two_digits != 11)
	{
		var result = nominative;
	}
	else
	{
		if ((last_digit == 2 && last_two_digits != 12) || (last_digit == 3 && last_two_digits != 13) || (last_digit == 4 && last_two_digits != 14))
		{
			var result = genitive_singular;
		}
		else
		{
			var result = genitive_plural;
		}
	}

	return result;
}

// массив для хранения текущих рейтингов звезд
var curr_rate = new Array();

// функция работы со звездами рейтинга
function set_rate(id, new_rate)
{
	// устанавливаем атрибуты
	curr_star = document.getElementById(id);
	parent_id = parseInt(curr_star.parentNode.id);

	// при первом пересчете ставим рейтинг для группы звезд в 0
	if (!curr_rate[parent_id])
	{
		curr_rate[parent_id] = 0;
	}

	// устанавливаем новый рейтинг в массив рейтингов и значение скрытого поля
	if (new_rate != curr_rate[parent_id] && parseInt(new_rate) > 0)
	{
		curr_rate[parent_id] = new_rate;

		curr_form = document.getElementById('comment_form_0' + (parent_id != 0 ? parent_id : ''));
		curr_form.comment_grade.value = curr_rate[parent_id].charAt(curr_rate[parent_id].length - 1);
	}

	// пересчет стилей для звезд
	for (i = 1; i < 6; i++)
	{
		if (parent_id != 0)
		{
			j = parent_id + '' + i + '_star_' + i;
		}
		else
		{
			j = i + '_star_' + i;
		}

		temp_obj = document.getElementById(j);

		if (new_rate == 0)
		{
			id = curr_rate[parent_id];
		}

		if (parseInt(j) > parseInt(id))
		{
			temp_obj.className = '';
		}
		else
		{
			temp_obj.className = 'curr';
		}
	}
}

// Функция обратного вызова для CheckBlogUrl
function callbackfunction_CheckBlogUrl(responseJS)
{
	// Результат принят
	sended_request = false;

	if (typeof responseJS != 'undefined')
	{
		// Данные.
		if (typeof responseJS.check_url_result != 'undefined')
		{
			var check_url = document.getElementById('check_url');

			if (responseJS.check_url_result == 0)
			{
				check_url.className = 'error';
				html = 'Адрес занят.';
			}
			else
			{
				check_url.className = 'green';
				html = 'Адрес свободен.';
			}

			if (check_url)
			{
				check_url.innerHTML = html;
			}
			else
			{
				alert('Ошибка! Блок для вывода результатов запроса не найден');
			}
		}
	}
}

function CheckBlogUrl(blog_path, blog_url, parent_id, id)
{
	cmsrequest = blog_path + '?ajax_check_blog_url=' + blog_url + '&group_parent_id=' + parent_id + '&group_id=' + id;

	var check_url = document.getElementById('check_url');

	if (check_url)
	{
		check_url.innerHTML = '<img src="/hostcmsfiles/images/ajax_loader_mini.gif"/>';
	}

	// Отправляем запрос backend-у
	sendBackgroundRequest(cmsrequest, 'get', callbackfunction_CheckBlogUrl);

	return false;
}

// Установка или снятие всех флажков для checkbox'ов элементов.
function SelectAllItemsByPrefix(ASelect, prefix)
{
	element_array = document.getElementsByTagName("input");
	if (element_array.length > 0)
	{
		for (var i = 0; i < element_array.length; i++)
		{
			if (element_array[i].name.search(prefix) != -1)
			{
				// Устанавливаем checked
				element_array[i].checked = ASelect;
			}
		}

	}
}

