jQuery(document).ready(function(){

	jQuery(".block-leftmenu-2 .oc>a").click(function(){
		jQuery(this).parents(".oc").toggleClass("oc-close");
		jQuery(this).parents(".oc").toggleClass("oc-open");
		return false;
	});

	if (document.getElementById("trackbar")) {
		jQuery("#trackbar").trackbar({
			onMove : function() {
				document.getElementById("f-rating").value = this.leftValue;
			},
			dual : false, // two intervals
			width : 139, // px
			leftLimit : 60, // unit of value
			leftValue : jQuery("#f-rating").val(), // unit of value
			rightLimit : 100, // unit of value
			rightValue : 100, // unit of value
			hehe : ":-)",
			cid : "f-rating"
		});
	}

	jQuery(".trackbarouter").each(function() {
		var vid = jQuery(this).attr("id");
		jQuery(this).trackbar({
			onMove : function() {
				document.getElementById(vid + "-rating").value = this.leftValue;
			},
			dual : false, // two intervals
			width : 139, // px
			leftLimit : 60, // unit of value
			leftValue : jQuery(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"
		});
	});


	jQuery("#authlink").click(function(){
		jQuery("#authform").toggle();
		return false;
	});
	
	jQuery("form input").keypress(function(e) {
		if(e.which==13) {
			jQuery(this).parents("form").submit();
		}
	});

	if (jQuery(".block-cardform .section .quantity input").size()) {
		jQuery(".block-cardform .section .quantity input").numeric();
	}
	if (jQuery(".block-basket table td.col-quantity input").size()) {
		jQuery(".block-basket table td.col-quantity input").numeric();
	}
	if (jQuery(".block-header .basket .table table td.c2 input").size()) {
		jQuery(".block-header .basket .table table td.c2 input").numeric();
	}
	if (jQuery(".block-search form .range input.type-text").size()) {
		jQuery(".block-search form .range input.type-text").numeric();
	}

	jQuery(".block-header .search").submit(function(){
		if(jQuery(this).find("input[type=text]").val() == "поиск по сайту") {
			return false;
		}
	});


	jQuery(".block-catalog table td.col-photo .toggle a").click(function(){
		return toggle_list_elements(this, null);
	});
    
    jQuery(".block-catalog .common-toggle a").click(function() {
        jQuery(this).parent("p").toggleClass("common-toggle_hide");
        if (jQuery(this).text() == "Скрыть все описания") {
            jQuery(this).text("Показать все описания");
            var is_hide = false;
        } else {
            jQuery(this).text("Скрыть все описания");
            var is_hide = true;
        }
        
        jQuery(".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) {
            jQuery(dom_link).parents("tr").find("td.col-info p.text").toggle().end()
							 .find("td.col-photo p.img").toggle().end();
            
            jQuery(dom_link).parent("p").toggleClass("toggle_hide");
            if (jQuery(dom_link).text() == "скрыть описание") {
                jQuery(dom_link).text("показать описание");
            }
            else {
                jQuery(dom_link).text("скрыть описание");
            }
        } else {
            if (is_hide) {
                jQuery(dom_link).parents("tr").find("td.col-info p.text").show().end()
							 .find("td.col-photo p.img").show().end();
                
                if (!jQuery(dom_link).parent("p").hasClass("toggle_hide")) {
                    jQuery(dom_link).parent("p").addClass("toggle_hide");
                }
                jQuery(dom_link).text("скрыть описание");
            } else {
                jQuery(dom_link).parents("tr").find("td.col-info p.text").hide().end()
							 .find("td.col-photo p.img").hide().end();
                             
                jQuery(dom_link).parent("p").removeClass("toggle_hide");
                jQuery(dom_link).text("показать описание");
            }
        }
		return false;
    }

/*
	jQuery(".block-personal table.table-corr td.col-3 a.del").click(function(){
		jQuery(this).parents("tr").remove();
		if (jQuery(".block-personal table.table-corr tr").size() == 1) {
			jQuery(".block-personal table.table-corr").before("<p class='empty'>Новых сообщений нет</p>");
			jQuery(".block-personal table.table-corr").css("display", "none");
		}
		return false;
	});
*/

	
/*
	jQuery(".block-leftmenu-1 dl dd").each(function() {
		if(jQuery(this).find("ul li").size() > 6) {
			jQuery(this).find("ul.hideall li").hide();
			jQuery(this).find("ul li:gt(4)").hide();
		} else {
			//jQuery(this).find(".detail").hide();
		}
	});
*/

	jQuery(".block-leftmenu-1 dl dd .detail a").click(function(){
		var gt=jQuery(this).parents("dd").children("ul").is(".hideall")?0:3;
		jQuery(this).parents("dd").children("ul").find("li" + (gt ? ":gt(" + gt + ")" : "")).toggle();
		if(jQuery(this).text() == "раскрыть") {
			jQuery(this).text("скрыть");
		} else {
			jQuery(this).text("раскрыть");
		}
		return false;
	});

/*
	jQuery(".block-basket .col-del a").click(function(){
		jQuery(this).parents("tr").remove();
		if (jQuery(".block-basket table tbody tr").size() == 0) {
			//jQuery(".block-basket table").before("<p class='empty'>Ваша корзина пуста</p>");
			jQuery(".block-basket table").before("<p class='empty'>Вы ничего не выбрали</p>");
			jQuery(".block-basket table").css("display", "none");
		}
		return false;
	});
*/

	jQuery(".block-header .basket .table table td.c4 .del").click(function(){
		var getsrt = "/~services:basket=action=delete," + jQuery(this).parents("tr").children(".c2").attr("rel");
		var tbl=jQuery("#basket-hdr"), tbl2=jQuery("#basket-tbl");
		jQuery.get(getsrt, {}, function(ans) {
			var cnt=0;
			var price=0;
			jQuery(ans).find("item").each(function() {
				var tmp=parseInt(jQuery(this).find("count").text());
				cnt+=tmp>0?tmp:0;
				tmp=parseInt(Math.floor(parseFloat(jQuery(this).children("price").text())*100))/* * parseInt(jQuery(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)+" руб.");
		});
		jQuery(this).parents("tr").remove();
		if (jQuery(".block-header .basket .table table tr").size() == 2) {
			jQuery(".block-header .basket .bhead p").css("display", "none");
			jQuery(".block-header .basket .table").css("display", "none");
			jQuery(".block-header .basket .bhead h2").text("Вы ничего не выбрали");
		}
		return false;
	});

	jQuery(".block-header .basket .table table td.c2 input").keyup(function(){
		if (jQuery(this).val() == 0) { jQuery(this).val("1") }
		var getsrt = "/~services:basket=action=changecount," + jQuery(this).parents("td.c2").attr("rel")  + ",count=" +  jQuery(this).val();
		var tbl=jQuery("#basket-hdr"), tbl2=jQuery("#basket-tbl");
		jQuery.get(getsrt, {}, function(ans) {
			var cnt=0;
			var price=0;
			jQuery(ans).find("item").each(function() {
				var tmp=parseInt(jQuery(this).find("count").text());
				cnt+=tmp>0?tmp:0;
				tmp=parseInt(Math.floor(parseFloat(jQuery(this).children("price").text())*100))/* * parseInt(jQuery(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 = jQuery(this).parents("tr").children(".c3").attr("rel");
		var price_all = parseInt( 100 * price_one * (jQuery(this).val()>0?jQuery(this).val():0) ) / 100 + " руб.";
		jQuery(this).parents("tr").children(".c3").text(price_all);
	});

	function updbasket() {
		var cnt=0;
		var price=0;
		var c=0;
		$(".block-basket table tr").each(function() {
			if(($(this).next()).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;
				c++;
			}
		});
		jQuery(".block-basket table tr:last td.col-quantity").text(cnt);
		var skidka=0;
		if (price>1000000) skidka=.05;
		if (price>2500000) skidka=.10;
		if (price>5000000) skidka=.15;
		skidka=Math.floor(skidka*price/100);
		if (skidka>0) $(".block-basket table tr:eq("+(c+1)+")").show("slow");
		else $(".block-basket table tr:eq("+(c+1)+")").hide("slow");
		jQuery(".block-basket table tr:eq("+(c+1)+") td.col-price-total").text(skidka);
		jQuery(".block-basket table tr:last td.col-price-total").text(price/100-skidka);
	}
	
	jQuery(".block-basket table td.col-quantity input").keyup(function(){
		var inp = jQuery(this).parents(".col-quantity").children("input");
		var im = parseInt(inp.val());
		var price = jQuery(this).parents(".col-quantity").siblings(".col-price");
		var pricetotal = jQuery(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;
	});

	jQuery(".block-basket table td.col-quantity .minus").click(function(){
		var inp = jQuery(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 = jQuery(this).parents(".col-quantity").siblings(".col-price");
		var pricetotal = jQuery(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;
	});

	jQuery(".block-basket table td.col-quantity .plus").click(function(){
		var inp = jQuery(this).parents(".col-quantity").children("input");
		var ip = parseInt(inp.val());
		inp.val(ip+1);
		var im = parseInt(inp.val());
		var price = jQuery(this).parents(".col-quantity").siblings(".col-price");
		var pricetotal = jQuery(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;
	});

	jQuery(".block-cardform .section .quantity input").keyup(function(){
		if (jQuery(this).val() == 0) { jQuery(this).val("1") }
	});

	jQuery(".block-cardform .section .quantity .minus").click(function(){
		var inp = jQuery(this).parents(".quantity").children("input");
		var im = parseInt(inp.val());
		if ( im > 1) {
			inp.val(im-1);
		} else {
			inp.val("1");
		}
		return false;
	});

	jQuery(".block-cardform .section .quantity .plus").click(function(){
		var inp = jQuery(this).parents(".quantity").children("input");
		var ip = parseInt(inp.val());
		inp.val(ip+1);
		return false;
	});

	jQuery(".block-search form .range .minus").click(function(){
		var inp = jQuery(this).next("input");
		var im = parseInt(inp.val());
		if ( im > 0) {
			inp.val(im-1);
		} else {
			inp.val("0");
		}
		return false;
	});

	jQuery(".block-search form .range .plus").click(function(){
		var inp = jQuery(this).prev("input");
		var ip = parseInt(inp.val());
		if (isNaN(ip)) { ip = 0; }
		inp.val(ip+1);
		return false;
	});

	jQuery(".block-header .basket .bhead .show").click(function(){
		jQuery(this).parents(".basket").children(".table").toggleClass("table-close");
		jQuery(this).parents(".basket").children(".table").css("z-index", 1000);
		jQuery(this).parents("p").toggleClass("button-hide");
		jQuery(this).parent().parent().parent().parent().find("tr:first").toggle();
		return false;
	});

	jQuery(".block-articles table tbody tr:odd td").addClass("even");

    /*
	 * Автоматическое переключение вкладок
	 */
    /*
	var trels_pattern = /#rel([0-9]+)/g;
    if (trels_pattern.test(window.location.hash)) {
        change_tab(jQuery(".block-mainsectionindex .menu td a[rel=" + window.location.hash.replace(/#rel/, "") + "]"), false);
    }
    */
	jQuery(".block-mainsectionindex .menu td a").click(function(){
		return change_tab(jQuery(this), true);
	});
	
	function change_tab(jQueryj_link, jQueryis_change_href) {
		jQueryj_link.parents(".block-mainsectionindex").children(".g-content").css("display","none");
		jQueryj_link.parents("div.menu").find(".el-title").toggleClass("el-title");
        jQueryj_link.parents("td").addClass("el-title");
        jQuery("#winesearch-" + jQueryj_link.attr("rel")).css("display","block");
        if (jQueryis_change_href) {
            window.location.href = window.location.href.replace(/#rel[0-9]+/, "") + "#rel" + jQueryj_link.attr("rel");
        }
        return false;
    }

	if(jQuery.browser.version<=6 && jQuery.browser.msie) {
		jQuery(".select-free").each(function() {
			if(jQuery(this).children("ul").height()) {
				jQuery(this).children("iframe").width(jQuery(this).children("ul").width()+"px");
				jQuery(this).children("iframe").height((jQuery(this).children("ul").height())+"px");
			}
		});
	}
	
	jQuery(".block-mainmenu table td div").hover(function() {
		jQuery(this).parents("td").addClass("current");
	},function() {jQuery(this).parents("td").removeClass("current");} );
	if(jQuery.browser.version<=6 && jQuery.browser.msie) {
		jQuery(".block-mainmenu table td div").hover(function() {}, function() { jQuery(this).removeClass("ie-hover"); } );
		jQuery(".block-mainmenu table td div").find("*").hover(function() {
			jQuery(this).parents("table").find(".ie-hover").removeClass("ie-hover");
			jQuery(this).addClass("ie-hover");
			jQuery(this).parents().addClass("ie-hover");
		}, function() {
			jQuery(this).removeClass("ie-hover");
		});
	}

    /* Registration: Substitution weeding "login" */
    jQuery('#f-rf-mail').keyup(function(event) {
        var data = jQuery(this).val();
        jQuery('#f-rf-logn').val(data);
    });
    
    jQuery('#f-rf-mail').blur(function(event) {
        var data = jQuery(this).val();
        jQuery('#f-rf-logn').val(data);
    });

    jQuery('#f-rf-basic').submit(function() {
        var data = jQuery('#f-rf-mail').val();
        jQuery('#f-rf-logn').val(data);
    });
    
    if (jQuery('#f-rf-city').length) {
        form_registration_hide_mycity(false);
        jQuery('#f-rf-city').append('<option value="0"' + (jQuery.trim(jQuery('#f-rf-mycity').val()) != '' ? ' selected="selected"' : '') + '>-- в списке нет моего города --</option>');
        jQuery('#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 || jQuery.trim(jQuery('#f-rf-mycity').val()) == '') {
            jQuery('#block-f-rf-mycity').hide();
            jQuery('#f-rf-mycity').attr('disabled', 'disabled');
        }
    }
    
    function form_registration_show_mycity() {
        jQuery('#block-f-rf-mycity').show();
        jQuery('#f-rf-mycity').removeAttr('disabled');
    }
	
/*	if(jQuery("#f-sf-key").val()=="") {
			jQuery("#f-sf-cat").get(0).selectedIndex=1;
		}
	jQuery("#f-sf-key").keypress(function() {
		if(jQuery(this).val()=="") {
			jQuery("#f-sf-cat").get(0).selectedIndex=1;
		}
	});
	jQuery("#f-sf-cat").change(function() {
		if(jQuery("#f-sf-key").val()==""&&jQuery("#f-sf-cat").get(0).selectedIndex==0) {
			jQuery("#f-sf-cat").get(0).selectedIndex=1;
		}
	});*/
	//jQuery("#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]+\\/?"), "/"));
    }
}


jQuery.fn.addtocopy=function(d){
	var e={
		htmlcopytxt:'<br>More: <a href="'+window.location.href+'">'+window.location.href+'</a><br>',
		minlen:25,
		addcopyfirst:false
	};
	$.extend(e,d);
	var f=document.createElement('span');
	f.id='ctrlcopy';
	f.innerHTML=e.htmlcopytxt;
	return this.each(
		function(){
			$(this).mousedown(function(){$('#ctrlcopy').remove()});
			$(this).mouseup(
				function(){
					if(window.getSelection){
						var a=window.getSelection();
						var b=a.toString();
						if(!b||b.length<e.minlen)
							return;
						var c=a.getRangeAt(0);
						b=c.cloneRange();
						b.collapse(e.addcopyfirst);
						b.insertNode(f);
						if(!e.addcopyfirst)
							c.setEndAfter(f);
						a.removeAllRanges();
						a.addRange(c)
					} else if(document.selection){
						var a=document.selection;
						var c=a.createRange();
						var b=c.text;
						if(!b||b.length<e.minlen)
							return;
						b=c.duplicate();
						b.collapse(e.addcopyfirst);
						b.pasteHTML(f.outerHTML);
						if(!e.addcopyfirst){
							c.setEndPoint("EndToEnd",b);
							c.select();
						}
					}
				}
			);
		}
	);
}

$(document).ready(function() {
$('#bodyid').addtocopy({
htmlcopytxt: '<br><br>Читать полностью: <a href="'+window.location.href+'">'+window.location.href+'</a>',
                minlen:35,
                addcopyfirst: false
        });
});
