$(document).ready(function(){

/*
$(".ul_tovari .name").hover(
	function(){
		$(this).css("text-decoration", "underline");
	},
	function(){
		$(this).css("text-decoration", "none");
	}
);

$(".ul_tovari img").hover(
	function(){
		$(this).css("border", "solid 1px #990000");
	},
	function(){
		$(this).css("border", "solid 1px #fff");
	}
);
*/

// Галерея
$(".sm_img img:first").addClass("active");
$(".big_img img:first").css("display", "block");
$(".sm_img img").hover(function(){
	$(".sm_img img").removeClass("active");
	$(this).addClass("active");
	var i = $(".sm_img img").index(this);
	$(".big_img img").css("display", "none")
		.eq(i).css("display", "block");
});

// lightBox
$('div.gallery a').lightBox({
	imageLoading: '/images/lightbox/loading.gif',
	imageBtnClose: '/images/lightbox/close.gif',
	imageBtnPrev: '/images/lightbox/prev.gif',
	imageBtnNext: '/images/lightbox/next.gif',
	imageBlank: '/images/lightbox/blank.gif',
	txtImage: 'Изображение',
	txtOf: 'из'
});

/* set height */
var name_h = 0;
var li_h = 0;
var cnt_row = $("div.promo ul.set_h").is(".ul_category") ? 5 : 4;
var num_row = 1;
var n = 0;
$("ul.set_h li").height("auto");
$("ul.set_h li .name").height("auto").each(function(i){
	if (n >= cnt_row) {
		$("ul.set_h li .name").filter(".row"+num_row).height(name_h)
			.parentsUntil("li").filter(".p_img").height(li_h + name_h);
		num_row++;
		name_h = 0;
		li_h = 0;
		n = 0;
	}
	var h = $(this).height();
	if (h > name_h) {
		name_h = h;
	}
	$(this).addClass("row"+num_row);
	// with .dsc height
	var h2 = $(this).parentsUntil("li").filter(".p_img").height();
	if (h2 - h > li_h) {
		li_h = h2 - h;
	}
	n++;
});
$("ul.set_h li .name").filter(".row"+num_row).height(name_h)
	.parentsUntil("li").filter(".p_img").height(li_h + name_h);

});


$(document).ready(function(){

/* button buy - start - */

$(".buy").click(
	function()
		{
            $(this).load("/basket/button_buy/ajax.php?id="+$(".buy").attr("id"), {code: 1}, function (){$(this).removeClass("buy");});
		}
);

/* button buy - end - */



itogo();
$(".count").each(function(i) {set_sum(this);});

$(".count").keyup(
	function(event)
		{
			set_sum(this);
			itogo();
		}
	);

function set_sum(this_)
	{
        count=$(this_).attr("value");
		cena=$(this_).parents(".parent").find(".cena").html();

		count=count.replace(/[^0-9]/gi,"");
		for (i=0; i < count.length; i++) {if (count.charAt(i)!=0) {count=count.substr(i); break;}};
		if (count.length>3) {count=count.substr(0, 3)}

		$(this_).attr("value", count);

		summa=count*cena;
		summa=number_format(summa, 3, ".", "'");

		$(this_).parents(".parent").find(".summa").html(summa);

	}

function itogo()
	{
        itogo_=0;
		$(".count").each(function(i) {
		    count=$(this).attr("value");
			cena=$(this).parents(".parent").find(".cena").html();
			itogo_+=cena*count;
		});
		itogo_=number_format(itogo_, 3, ".", "'");
		$(".itogo").html(itogo_);
	};

function number_format(str, count, razd, probel) // строка, кол-во отделяемых знаков, разделитель десятичных знаков, разделитель между группами
	{
		str=String(str);
		str_len=str.length;
		ostatok="00";
		point_pos=str.search(/\./);
		if (point_pos!="-1") { str1=str.substring(0,point_pos); ostatok=str.substr((point_pos+1), 2); str=str1; if (ostatok.length<2) {ostatok+="0";};};
		new_str="";	for (i=0; i <= str_len; i++) {new_str+=str.substr((str_len-i), 1);}; str=new_str;
		new_str="";
		for (i=0; i < str_len; i=i+3)
			{
				new_str+=str.substr(i, 3);
				if (i+3<str_len) {new_str+=probel;};
			}
        str=new_str; new_str="";	for (i=0; i <= str_len; i++) {new_str+=str.substr((str_len-i), 1);}; str=new_str;
		new_str+=razd+ostatok;
		return new_str;
	}
/*
$(".del").click(function(){

	opacity=0.2;

	if ($(this).parents(".parent").find("td").css("opacity")==opacity)
		{
			$(this).parents(".parent").find("td").css("opacity", 1);
			$(this).removeClass("restore");
			$(this).attr("title", "Удалить");
		}
	else
		{
			$(this).parents(".parent").find("td").css("opacity", opacity);
			$(this).parent().css("opacity", 1);
			$(this).addClass("restore");
			$(this).attr("title", "Восстановить");
			count=$(this).parents(".parent").find(".count");
			$(count).attr("value", 0);
			set_sum($(this).parents(".parent").find(".count"));
			itogo();
		}
});
*/
$(".del").click(function(){


			count=$(this).parents(".parent").find(".count");
			$(count).attr("value", 0);

});


});



function radio(li, input, val) {
	var obj=getElementsByClass('active',null,'li'); var len=obj.length;
	for (j=0; j<len; j++)
		{
			obj[j].className="radio"
		}
	li.className="active";
	document.getElementById(input).value=val;
}

// Функция взятия элементов по классу и их обработка


// Обработка массива элементов, взятых по классу



// Функция взятия элементов по классу, возвращает массив элементов
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if (node == null) {node = document;}
	if (tag == null) {tag = '*';}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

