function IexShowMessage(id){
    $("#"+id).fadeIn();
    setTimeout("IexHideMessage('"+id+"')", 3000);
}
function IexHideMessage(id){
    $("#"+id).fadeOut();
}
function IexSendRequest(action, method, callback_function)
{
	var req = new JsHttpRequest();

	// Отображаем экран загрузки
	//ShowLoadingScreen();

	// Этот код вызовется автоматически, когда загрузка данных завершится.
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			// Возвращаем обычный курсор
			document.body.style.cursor = '';

			// Убираем затемнение.
			//HideLoadingScreen();

			if (typeof callback_function != 'undefined')
			{
				callback_function(req.responseJS);
			}

			return true;
		}
	}

	req.open(method, action, true);

	// Отсылаем данные в обработчик.
	req.send(null);

	// Курсор ставим на часики.
	document.body.style.cursor = "wait";
}
// Функция обратного вызова для AddIntoCart
function callbackfunction_IexAddIntoCart(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 IexAddIntoCart(shop_path, item_id, item_count, properties)
{
	//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 + '&' + properties;
	
	// Отправляем запрос backend-у
	IexSendRequest(cmsrequest, 'get', callbackfunction_IexAddIntoCart);
	
	return false;
}

function AddToCompare(id){
	if($("#id_compare_id_"+id).attr('checked')){
		$.get('/?compare_id_'+id+'=1', function(){
			
		});
	}else{
		$.get('/?del_compare_id_'+id+'=1&delete_compare=1', function(){
			
		});
	}
}



$(document).ready(function (){
	$('.add_rt').click(function(){
		var id = $(this).attr('id').match(/addcart_([0-9]+)/);
		var commid = 0;
		if (id != undefined && id[1] != undefined) {
			commid = id[1];
		}
		var property_id, properties = '';
		$("select[id^='mod_property_']").each(function(){
			property_id = $(this).attr('id').match(/mod_property_([0-9]+)/);
			if(property_id != undefined && property_id[1] != undefined && property_id[1] != 0){
				if(properties != ''){
					properties += '&';
				}
				properties += 'property_'+property_id[1]+'='+$(this).val();
			}
		});
		IexAddIntoCart('/shop/', commid, document.getElementById('count_'+commid).value, properties);
		$('#addcart_yes_'+commid).fadeIn();
		return false;
	});
	$('.next_add_cart').click(function(){
		$(this).closest('.addcart_yes_y').hide();
		return false;
	});
});


