$(document).ready(function(){

	$(".block-leftmenu-2 .oc>a").click(function(){
		$(this).parents(".oc").toggleClass("oc-close");
		$(this).parents(".oc").toggleClass("oc-open");
		return false;
	});

	if (document.getElementById("trackbar")) {
		$("#trackbar").trackbar({
			onMove : function() {
				document.getElementById("f-rating").value = this.leftValue;
			},
			dual : false, // two intervals
			width : 139, // px
			leftLimit : 60, // unit of value
			leftValue : $("#f-rating").val(), // unit of value
			rightLimit : 100, // unit of value
			rightValue : 100, // unit of value
			hehe : ":-)",
			cid : "f-rating"
		});
	}

	$(".trackbarouter").each(function() {
		var vid = $(this).attr("id");
		$(this).trackbar({
			onMove : function() {
				document.getElementById(vid + "-rating").value = this.leftValue;
			},
			dual : false, // two intervals
			width : 139, // px
			leftLimit : 60, // unit of value
			leftValue : $(this).parent(".wrap").children(".quantity").children("input").val(), // unit of value
			rightLimit : 100, // unit of value
			rightValue : 100, // unit of value
			hehe : ":-)",
			cid : vid + "-rating"
		});
	});


	$("#authlink").click(function(){
		$("#authform").toggle();
		return false;
	});
	
	$("form input").keypress(function(e) {
		if(e.which==13) {
			$(this).parents("form").submit();
		}
	});

	if ($(".block-cardform .section .quantity input").size()) {
		$(".block-cardform .section .quantity input").numeric();
	}
	if ($(".block-basket table td.col-quantity input").size()) {
		$(".block-basket table td.col-quantity input").numeric();
	}
	if ($(".block-header .basket .table table td.c2 input").size()) {
		$(".block-header .basket .table table td.c2 input").numeric();
	}
	if ($(".block-search form .range input.type-text").size()) {
		$(".block-search form .range input.type-text").numeric();
	}

	$(".block-header .search").submit(function(){
		if($(this).find("input[type=text]").val() == "поиск по сайту") {
			return false;
		}
	});


	$(".block-catalog table td.col-photo .toggle a").click(function(){
		return toggle_list_elements(this, null);
	});
    
    $(".block-catalog .common-toggle a").click(function() {
        $(this).parent("p").toggleClass("common-toggle_hide");
        if ($(this).text() == "Свернуть все описания") {
            $(this).text("Развернуть все описания");
            var is_hide = false;
        } else {
            $(this).text("Свернуть все описания");
            var is_hide = true;
        }
        
        $(".block-catalog table td.col-photo .toggle a").each(function() {
            toggle_list_elements(this, is_hide);
        });
        
        return false;
    });
    
    function toggle_list_elements(dom_link, is_hide) {
        
        if (is_hide === null) {
            $(dom_link).parents("tr").find("td.col-info p.text").toggle().end()
							 .find("td.col-photo p.img").toggle().end();
            
            $(dom_link).parent("p").toggleClass("toggle_hide");
            if ($(dom_link).text() == "Свернуть") {
                $(dom_link).text("Развернуть");
            }
            else {
                $(dom_link).text("Свернуть");
            }
        } else {
            if (is_hide) {
                $(dom_link).parents("tr").find("td.col-info p.text").show().end()
							 .find("td.col-photo p.img").show().end();
                
                if (!$(dom_link).parent("p").hasClass("toggle_hide")) {
                    $(dom_link).parent("p").addClass("toggle_hide");
                }
                $(dom_link).text("Свернуть");
            } else {
                $(dom_link).parents("tr").find("td.col-info p.text").hide().end()
							 .find("td.col-photo p.img").hide().end();
                             
                $(dom_link).parent("p").removeClass("toggle_hide");
                $(dom_link).text("Развернуть");
            }
        }
		return false;
    }

/*
	$(".block-personal table.table-corr td.col-3 a.del").click(function(){
		$(this).parents("tr").remove();
		if ($(".block-personal table.table-corr tr").size() == 1) {
			$(".block-personal table.table-corr").before("<p class='empty'>Новых сообщений нет</p>");
			$(".block-personal table.table-corr").css("display", "none");
		}
		return false;
	});
*/

	
/*
	$(".block-leftmenu-1 dl dd").each(function() {
		if($(this).find("ul li").size() > 6) {
			$(this).find("ul.hideall li").hide();
			$(this).find("ul li:gt(4)").hide();
		} else {
			//$(this).find(".detail").hide();
		}
	});
*/

	$(".block-leftmenu-1 dl dd .detail a").click(function(){
		var gt=$(this).parents("dd").children("ul").is(".hideall")?0:3;
		$(this).parents("dd").children("ul").find("li" + (gt ? ":gt(" + gt + ")" : "")).toggle();
		if($(this).text() == "раскрыть") {
			$(this).text("скрыть");
		} else {
			$(this).text("раскрыть");
		}
		return false;
	});

/*
	$(".block-basket .col-del a").click(function(){
		$(this).parents("tr").remove();
		if ($(".block-basket table tbody tr").size() == 0) {
			//$(".block-basket table").before("<p class='empty'>Ваша корзина пуста</p>");
			$(".block-basket table").before("<p class='empty'>Вы ничего не выбрали</p>");
			$(".block-basket table").css("display", "none");
		}
		return false;
	});
*/

	$(".block-header .basket .table table td.c4 .del").click(function(){
		var getsrt = "/~services:basket=action=delete," + $(this).parents("tr").children(".c2").attr("rel");
		var tbl=$("#basket-hdr"), tbl2=$("#basket-tbl");
		$.get(getsrt, {}, function(ans) {
			var cnt=0;
			var price=0;
			$(ans).find("item").each(function() {
				var tmp=parseInt($(this).find("count").text());
				cnt+=tmp>0?tmp:0;
				tmp=parseInt(Math.floor(parseFloat($(this).children("price").text())*100))/* * parseInt($(this).find("count").text())*/;
				price+=tmp>0?tmp:0;
			});
			tbl.find("tr:first td").html("Вы выбрали "+cnt+" товар"+digit_end(cnt)+" на сумму "+(parseInt(price)/100)+" руб.");
			tbl2.find("tr:last .c2").html(cnt);
			tbl2.find("tr:last .c3").text((parseInt(price)/100)+" руб.");
		});
		$(this).parents("tr").remove();
		if ($(".block-header .basket .table table tr").size() == 2) {
			$(".block-header .basket .bhead p").css("display", "none");
			$(".block-header .basket .table").css("display", "none");
			$(".block-header .basket .bhead h2").text("Вы ничего не выбрали");
		}
		return false;
	});

	$(".block-header .basket .table table td.c2 input").keyup(function(){
		if ($(this).val() == 0) { $(this).val("1") }
		var getsrt = "/~services:basket=action=changecount," + $(this).parents("td.c2").attr("rel")  + ",count=" +  $(this).val();
		var tbl=$("#basket-hdr"), tbl2=$("#basket-tbl");
		$.get(getsrt, {}, function(ans) {
			var cnt=0;
			var price=0;
			$(ans).find("item").each(function() {
				var tmp=parseInt($(this).find("count").text());
				cnt+=tmp>0?tmp:0;
				tmp=parseInt(Math.floor(parseFloat($(this).children("price").text())*100))/* * parseInt($(this).find("count").text())*/;
				price+=tmp>0?tmp:0;
			});
			tbl.find("tr:first td").html("Вы выбрали "+cnt+" товар"+digit_end(cnt)+" на сумму "+(parseInt(price)/100)+" руб.");
			tbl2.find("tr:last .c2").html(cnt);
			tbl2.find("tr:last .c3").text((parseInt(price)/100)+" руб.");
		});
		var price_one = $(this).parents("tr").children(".c3").attr("rel");
		var price_all = parseInt( 100 * price_one * ($(this).val()>0?$(this).val():0) ) / 100 + " руб.";
		$(this).parents("tr").children(".c3").text(price_all);
	});

	function updbasket() {
		var cnt=0;
		var price=0;
		$(".block-basket table tr").each(function() {
			if($(this).next().is("tr")) {
				var tmp=parseInt($(this).find("input").val());
				cnt+=tmp>0?tmp:0;
				tmp=parseInt(parseFloat($(this).find(".col-price-total").text())*100);
				price+=tmp>0?tmp:0;
			}
		});
		$(".block-basket table tr:last td.col-quantity").text(cnt);
		$(".block-basket table tr:last td.col-price-total").text(price/100);
	}
	
	$(".block-basket table td.col-quantity input").keyup(function(){
		var inp = $(this).parents(".col-quantity").children("input");
		var im = parseInt(inp.val());
		var price = $(this).parents(".col-quantity").siblings(".col-price");
		var pricetotal = $(this).parents(".col-quantity").siblings(".col-price-total");
		var pt = parseInt(parseFloat(price.text())*im*100)/100;
		pt = pt>0?pt:0 + '';
		pricetotal.text(pt);
		updbasket();
		return false;
	});

	$(".block-basket table td.col-quantity .minus").click(function(){
		var inp = $(this).parents(".col-quantity").children("input");
		var im = parseInt(inp.val());
		if ( im > 0) {
			inp.val(im-1);
		} else {
			inp.val("0");
		}
		var im = parseInt(inp.val());
		var price = $(this).parents(".col-quantity").siblings(".col-price");
		var pricetotal = $(this).parents(".col-quantity").siblings(".col-price-total");
		var pt = parseInt(parseFloat(price.text())*im*100)/100;
		pt = pt + '';
		pricetotal.text(pt);
		updbasket();
		return false;
	});

	$(".block-basket table td.col-quantity .plus").click(function(){
		var inp = $(this).parents(".col-quantity").children("input");
		var ip = parseInt(inp.val());
		inp.val(ip+1);
		var im = parseInt(inp.val());
		var price = $(this).parents(".col-quantity").siblings(".col-price");
		var pricetotal = $(this).parents(".col-quantity").siblings(".col-price-total");
		var pt = parseInt(parseFloat(price.text())*im*100)/100;
		pt = pt + '';
		pricetotal.text(pt);
		updbasket();
		return false;
	});

	$(".block-cardform .section .quantity input").keyup(function(){
		if ($(this).val() == 0) { $(this).val("1") }
	});

	$(".block-cardform .section .quantity .minus").click(function(){
		var inp = $(this).parents(".quantity").children("input");
		var im = parseInt(inp.val());
		if ( im > 1) {
			inp.val(im-1);
		} else {
			inp.val("1");
		}
		return false;
	});

	$(".block-cardform .section .quantity .plus").click(function(){
		var inp = $(this).parents(".quantity").children("input");
		var ip = parseInt(inp.val());
		inp.val(ip+1);
		return false;
	});

	$(".block-search form .range .minus").click(function(){
		var inp = $(this).next("input");
		var im = parseInt(inp.val());
		if ( im > 0) {
			inp.val(im-1);
		} else {
			inp.val("0");
		}
		return false;
	});

	$(".block-search form .range .plus").click(function(){
		var inp = $(this).prev("input");
		var ip = parseInt(inp.val());
		if (isNaN(ip)) { ip = 0; }
		inp.val(ip+1);
		return false;
	});

	$(".block-header .basket .bhead .show").click(function(){
		$(this).parents(".basket").children(".table").toggleClass("table-close");
		$(this).parents(".basket").children(".table").css("z-index", 1000);
		$(this).parents("p").toggleClass("button-hide");
		$(this).parent().parent().parent().parent().find("tr:first").toggle();
		return false;
	});

	$(".block-articles table tbody tr:odd td").addClass("even");


	$(".block-mainsectionindex .menu td a").click(function(){
		$(this).parents(".block-mainsectionindex").children(".g-content").css("display","none");
		$(this).parents("div.menu").find(".el-title").toggleClass("el-title");
		$(this).parents("td").addClass("el-title");
		$("#winesearch-" + $(this).attr("rel")).css("display","block");
		return false;
	});

	if(jQuery.browser.version<=6 && jQuery.browser.msie) {
		$(".select-free").each(function() {
			if($(this).children("ul").height()) {
				$(this).children("iframe").width($(this).children("ul").width()+"px");
				$(this).children("iframe").height(($(this).children("ul").height())+"px");
			}
		});
	}
	
	$(".block-mainmenu table td div").hover(function() {
		$(this).parents("td").addClass("current");
	},function() {$(this).parents("td").removeClass("current");} );
	if(jQuery.browser.version<=6 && jQuery.browser.msie) {
		$(".block-mainmenu table td div").hover(function() {}, function() { $(this).removeClass("ie-hover"); } );
		$(".block-mainmenu table td div").find("*").hover(function() {
			$(this).parents("table").find(".ie-hover").removeClass("ie-hover");
			$(this).addClass("ie-hover");
			$(this).parents().addClass("ie-hover");
		}, function() {
			$(this).removeClass("ie-hover");
		});
	}

    /* Registration: Substitution weeding "login" */
    $('#f-rf-mail').keyup(function(event) {
        var data = $(this).val();
        $('#f-rf-logn').val(data);
    });
    
    $('#f-rf-mail').blur(function(event) {
        var data = $(this).val();
        $('#f-rf-logn').val(data);
    });

    $('#f-rf-basic').submit(function() {
        var data = $('#f-rf-mail').val();
        $('#f-rf-logn').val(data);
    });
    
    if ($('#f-rf-city').length) {
        form_registration_hide_mycity(false);
        $('#f-rf-city').append('<option value="0"' + ($.trim($('#f-rf-mycity').val()) != '' ? ' selected="selected"' : '') + '>-- в списке нет моего города --</option>');
        $('#f-rf-city').change(function(){
            if (this.selectedIndex == this.length - 1) {
                form_registration_show_mycity();
            }
            else {
                form_registration_hide_mycity(true);
            }
        });
    }
    
    function form_registration_hide_mycity(ignore_value) {
        if (ignore_value || $.trim($('#f-rf-mycity').val()) == '') {
            $('#block-f-rf-mycity').hide();
            $('#f-rf-mycity').attr('disabled', 'disabled');
        }
    }
    
    function form_registration_show_mycity() {
        $('#block-f-rf-mycity').show();
        $('#f-rf-mycity').removeAttr('disabled');
    }
	
/*	if($("#f-sf-key").val()=="") {
			$("#f-sf-cat").get(0).selectedIndex=1;
		}
	$("#f-sf-key").keypress(function() {
		if($(this).val()=="") {
			$("#f-sf-cat").get(0).selectedIndex=1;
		}
	});
	$("#f-sf-cat").change(function() {
		if($("#f-sf-key").val()==""&&$("#f-sf-cat").get(0).selectedIndex==0) {
			$("#f-sf-cat").get(0).selectedIndex=1;
		}
	});*/
	//$("#f-sf-region").addClass("bigselect");
});

function digit_end(cnt, r0, r1, r2) {
	r0=r0||'ов';
	r1=r1||'';
	r2=r2||'а';
	cnt=cnt%100;
	if(cnt>10&&cnt<20) {
		return r0;
	} else {
		cnt%=10;
		if(cnt==1) return r1;
		else if(cnt>1&&cnt<5) return r2;
		else return r0;
	}
}

function set_records_per_page(dom_select) {
    var value = parseInt(dom_select[dom_select.selectedIndex].value);
    var pathname = window.location.pathname;
    if (!isNaN(value) && value > 0) {
        window.location = "/tools/catalog.php?rpp=" + value + "&pathname=" + encodeURIComponent(pathname.replace(new RegExp("\\/page\\/[0-9]+\\/?"), "/"));
    }
}
