$(document).ready(function() {

	$("#icons img").animate({"opacity":0.5},1);

	$("#icons img").live("mouseenter", function() {
		$(this).animate({"opacity":1.0},150);
	}).live("mouseleave", function() {
		$(this).animate({"opacity":0.5},150);
	});
	
	$(".menu:first").css("padding-left","0px");
	$(".menu:last").css("background","none");
	$(".menu:last").css("padding-right","0px");

	$("a.plus").live("click", function() {
		var next = $(this).parents("li:first").next().next();
		if ($(next).attr("class") == "cat2" || $(next).attr("class") == "cat3")
			{
			$(next).slideUp();
			}
		
		$("a.plus1").trigger("click");
		$(this).removeClass("plus");
		$(this).addClass("minus");
		return false;
		});
	
	$("a.minus").live("click", function() {
		var next = $(this).parents("li:first").next().next();
		if ($(next).attr("class") == "cat2" || $(next).attr("class") == "cat3")
			{
			$(next).slideDown();
			}
			
		$("a.plus").trigger("click");
		$("a.plus1").trigger("click");
		$(this).removeClass("minus");
		$(this).addClass("plus");
		return false;
		});
	
	$("a.plus1").live("click", function() {
		var next = $(this).parents("li:first").next().next();
		if ($(next).attr("class") == "cat2" || $(next).attr("class") == "cat3")
			{
			$(next).slideUp();
			}

		$(this).removeClass("plus1");
		$(this).addClass("minus1");
		return false;
		});
	
	$("a.minus1").live("click", function() {
		var next = $(this).parents("li:first").next().next();
		if ($(next).attr("class") == "cat2" || $(next).attr("class") == "cat3")
			{
			$(next).slideDown();
			}

			$("a.plus1").trigger("click");
		$(this).removeClass("minus1");
		$(this).addClass("plus1");
		return false;
		});
		
	$("a.trigger").live("click", function() {
		var prev = $(this).prev().attr("class");
		if (prev == "minus" || prev == "minus1" || prev == "plus" || prev == "plus1")
			{
			$(this).prev().trigger("click");
			};
		return false;
	});
	
	open1();
		
	$("#catalog tr.product_row_1:even").css("background","#ffffff");
	$("#catalog tr.product_row_2:even").css("background","#ffffff");
	$("#catalog tr.product_row_1:odd").css("background","#f7f9fa");
	$("#catalog tr.product_row_2:odd").css("background","#f7f9fa");
	
	$("a.cart_more").live("click", function() {
		var amount = $(this).prev().attr("value");
		amount = parseFloat(amount) + 1;
		$(this).prev().attr("value", amount);
		return false;
		});
	
	$("a.cart_less").live("click", function() {
		var amount = $(this).next().attr("value");
		if (parseFloat(amount) > 0)
			{
			amount = parseFloat(amount) - 1;
			$(this).next().attr("value", amount);
			};		
		return false;
		});
	
	$(".amount_form").live("submit", function() {
		$(this).parents("tr:first").find("a.cart_add:first").trigger("click");
		return false;
		});
	
	$("a.cart_add").live("click", function() {
		var amount = $(this).parents("td:first").prev().find("input:first").attr("value");
		var product_id = $(this).attr("id");
		var this_obj = $(this);
		
		if (parseFloat(amount) > 0)
			{
			$.post("/catalog/add_to_cart/", {product_id:product_id, amount:amount}, function(data){
				$("#cart").animate({"opacity":0.0},200).animate({"opacity":1.0},200).animate({"opacity":0.0},200).animate({"opacity":1.0},200);
				$(this_obj).parents("td:first").prev().find("input:first").attr("value",0);
				$("#cart").load("/engine/view/_cart.php");
				});
			
				yaCounter11262850.hit('http://catalog.gallop.ru/#add_to_cart', 'Добавление товара в корзину');
			};
				
		return false;
		});
		
	$("#cart").load("/engine/view/_cart.php");
		
	$("form#cat_params select").live("change",function() {
		var key = $(this).attr("name");
		var value = $(this).attr("value");
		$.post("/catalog/cat_params/", {key:key, value:value}, function(data){
			location.reload(true);
			});
		});
		
	$("#quicksearch_go").live("click", function() {
		//var search = $(this).parents("form:first").find("input[name='quicksearch']").attr("value");
		//alert(search);
		$(this).parents("form:first").submit();		
		return false;
		});
		
	$("a#recalc").live("click", function() {
		$("#cart_1").submit();		
		return false;
		});
		
	$("a#cart_confirm").live("click", function() {
		$("#cart_confirm_form").submit();		
		return false;
		});
		
	$("a[href='#open_order']").live("click", function() {
		var div = $(this).nextAll("div.order:first");
		var div_class = $(div).attr("id");
		
		if (div_class == "close")
			{
			
			$(div).slideDown(200);
			$(div).attr("id","open");
			}
		else if (div_class == "open")
			{
			$(div).slideUp(200);
			$(div).attr("id","close");
			};
		
		return false;
		});
	
	$("a[href='#del_order']").live("click", function() {
		var id = $(this).attr("id");
		var ask = confirm("Вы действительно хотите удалить заказ?");
		var cont = $(this).parents("div.cont:first");
		if (ask == 1)
			{
			$.post("/service/order_del/", {id:id}, function(data){
				$(cont).remove();
				});
			}
		return false;
		});
	
	$(".order#close").live("click", function() {
		SlideDown(100);
		$(this).attr("id","open");
		});
		
	$("a[href='#add_range_1']").live("click", function() {
		$("#range_1_frm").css("display","block");		
		return false;
		});
		
	$("a[href='#add_range_2']").live("click", function() {
		$("#range_2_frm").css("display","block");		
		return false;
		});
		
	$("input[name='range_cancel']").live("click", function() {
		$(this).parents("form:first").css("display","none");		
		return false;
		});
		
	
	
		
});

function open1() {
	$("a.plus").trigger("click").trigger("click");
	open2();
	};
	
function open2() {
	$("li.curr").parents("ul.cat3:first").prevAll("li:first").find("a.minus1").trigger("click");
	$("li.curr").find("a.minus1").trigger("click");
	};

function Trimmer(str) {
	str = str.replace(/^\s+/, '');
	str = str.replace(/\s+$/, '');
	return str;
	}


function Render() {
	$(".msg_result").animate({"opacity":1.0},5000).animate({"opacity": 0.0, "height": 0},200, function() { $(this).remove(); });
	
	$("select[name='task_month']").trigger("change");
	
	$("input[name='task_date']").datepicker({
		dateFormat: 'yy-mm-dd',
		dayNamesMin: ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'],
		monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь']
		});
	};

function Serial(form)
	{
	var params = "";
	
	//textarea
	$(form).find("textarea").each( function() {
		var this_name = $(this).attr("name");
		var this_val = $(this).attr("value");
		this_val = encodeURIComponent(this_val);
		params = params + this_name + "=" + this_val + "&";
		});
	
	//input text
	$(form).find("input[type='text']").each( function() {
		var this_name = $(this).attr("name");
		var this_val = $(this).attr("value");
		this_val = encodeURIComponent(this_val);
		params = params + this_name + "=" + this_val + "&";
		});
	
	//input hiddens
	$(form).find("input[type='hidden']").each( function() {
		var this_name = $(this).attr("name");
		var this_val = $(this).attr("value");
		this_val = encodeURIComponent(this_val);
		params = params + this_name + "=" + this_val + "&";
		});
	
	//input password
	$(form).find("input[type='password']").each( function() {
		var this_name = $(this).attr("name");
		var this_val = $(this).attr("value");
		params = params + this_name + "=" + this_val + "&";
		});
	
	//input radio
	$(form).find("input[type='radio']:checked").each( function() {
		var this_name = $(this).attr("name");
		var this_val = $(this).attr("value");
		params = params + this_name + "=" + this_val + "&";
		});
	
	//input checkbox
	$(form).find("input[type='checkbox']").each( function() {
		var this_name = $(this).attr("name");
		var checked = $(this).attr("checked");
		if (checked == true)
			{
			this_val = "on";
			}
		else if (checked == false)
			{
			this_val = "off";
			};
		params = params + this_name + "=" + this_val + "&";
		});
	
	//select
	$(form).find("select option:selected").each( function() {
		var this_name = $(this).parents("select:first").attr("name");
		var this_val = $(this).attr("value");
		this_val = encodeURIComponent(this_val);
		params = params + this_name + "=" + this_val + "&";
		});
	
	//params = params + "enabled=1";
	return params;
	};
