jQuery(document).ready(function(event) {
	jQuery('.buy_item').click(function() {
		if (!jQuery(this).hasClass('add-to-basket-added')) {
			jQuery(this).addClass('add-to-basket-added');
			ID = jQuery(this).attr('element_id');
			OBIVKA = jQuery(this).attr('obivka_id');
			elem = jQuery(this);
			jQuery.ajax({
				type: "GET",
				url: "/catalog/ajax.addtobasket.php",
				data: "ID="+ID+"&OBIVKA="+OBIVKA,
				success: function(result){
					elem.parent().addClass('active');
					jQuery('#basket_top_table').html(result);
				}
			});
		}
		return false;
	});
	jQuery('.good-added-close-link').click(function() {
		jQuery(this).parent().parent().parent().removeClass('active');
	});
	function set_upholstery() {
		ob_where_is_image = jQuery('.photo-center-inner .x-zoom');
		big_image = ob_where_is_image.children('img');
		price = jQuery(".title .price");
		arImgSrc = jQuery(this).attr("longdesc").split('|');
		var new_data = {
			src: arImgSrc[0],
			width: big_image.attr("width"),
			height: big_image.attr("height"),
			alt: img.attr("alt"),
			title: img.attr("title"),
			obivka: img.attr("title"),
			price: (price.attr("title")+' <span>р.</span>'),
			longdesc: arImgSrc[1]
		};
		
		jQuery('.x-zoom img').remove();
		new_image = '<img src="'+new_data.src+'" width="'+new_data.width+'" height="'+new_data.height+'" alt="'+new_data.alt+'" title="'+new_data.title+'" />';
		jQuery('.photo-center-inner .x-zoom .photo-zoom').before(new_image);
		jQuery('.photo-center-inner .x-zoom').attr('href', new_data.longdesc);
		jQuery(".material_obivka").html(new_data.obivka);
		price.html(new_data.price);
	}
	jQuery('.upholstery .block-content .one img').click(function() {
		img = jQuery(this);
		ob_where_is_image = jQuery('.photo-center-inner .x-zoom');
		big_image = ob_where_is_image.children('img');
		price = jQuery(this).next('.price').attr('title');
		if (img.parent().hasClass('one-border')) {
			jQuery('#price_from').hide();
		} else {
			jQuery('#price_from').show();
		}
		arImgSrc = jQuery(this).attr("longdesc").split('|');
		var new_data = {
			src: arImgSrc[0],
			width: big_image.attr("width"),
			height: big_image.attr("height"),
			alt: img.attr("alt"),
			title: img.attr("title"),
			obivka: img.attr("title"),
			price: (price+' <span>р.</span>'),
			longdesc: arImgSrc[1]
		};
		
		
		new_image = '<img src="'+new_data.src+'" width="'+new_data.width+'" height="'+new_data.height+'" alt="'+new_data.alt+'" title="'+new_data.title+'" />';
		
		jQuery('.x-zoom img').remove();
		jQuery('.photo-center-inner .x-zoom .photo-zoom').before(new_image);
		jQuery('.photo-center-inner .x-zoom').attr('href', new_data.longdesc);
		jQuery(".material_obivka").html(new_data.obivka);
		jQuery(".title .price").html(new_data.price);
	});
	
	// Слайдер распродажи на главной
	var salePreviewPause = false;
	var saleInerval = false;
	var check = 0;
	var w = setTimeout(checkcheck, 2000);
	function checkcheck() {
		check = 0;
		//window.setTimeout(salePreviewChangeImg, 1000);
	}
	function salePreviewChangeImg(this_direction) {
		this_direction = this_direction || false;
		//console.log(salePreviewPause +'_'+this_direction);
		if ((check == 0 && !salePreviewPause) || this_direction == 'back' || this_direction == 'next') {
			obj = jQuery('.sales_preview .sales_preview_imgs a[class~="active"]');
			obj.removeClass('active');
			obj.fadeOut("slow");
			//console.log(this_direction);
			if (this_direction == 'back') {
				if (obj.hasClass('first')) {
					obj.parent().children('a:last').fadeIn("slow");
					obj.parent().children('a:last').addClass('active');
				} else {
					obj.prev().fadeIn("slow");
					obj.prev().addClass('active');
				}
			} else {
				if (obj.hasClass('last')) {
					obj.parent().children('a:first').fadeIn("slow");
					obj.parent().children('a:first').addClass('active');
				} else {
					obj.next().fadeIn("slow");
					obj.next().addClass('active');
				}
			}
			window.clearTimeout(w);
			w = setTimeout(checkcheck, 3000); // сбрасываем флаг через две секунды
			//if (check == 0) window.setTimeout(salePreviewChangeImg, 3000);
			check = 1; // ставим флаг о том, что мы только что мы поменяли изображение, чтобы избежать двойного перелистывания по таймауту и по кнопке
			window.setTimeout(salePreviewChangeImg, 3000);
		} else if (salePreviewPause) {
			window.setTimeout(salePreviewChangeImg, 3000);
		}
	}
	//saleInerval = setInterval(salePreviewChangeImg, 3000);
	window.setTimeout(salePreviewChangeImg, 3000);
	jQuery('#sales_nav_pause').click(function() {
		if (salePreviewPause) {
			salePreviewPause = false;
			jQuery('#sales_nav_pause').attr('src', '/images/pause.png');
		} else {
			salePreviewPause = true;
			jQuery('#sales_nav_pause').attr('src', '/images/start.png');
		}
	});
	jQuery('#sales_nav_prev').mousedown(function() {
		jQuery(this).attr('src', '/images/arrow_left_active.png');
	}).mouseup(function() {
		jQuery(this).attr('src', '/images/arrow_left.png');
	}).click(function() {
		salePreviewChangeImg('back');
	});
	jQuery('#sales_nav_next').mousedown(function() {
		jQuery(this).attr('src', '/images/arrow_right_active.png');
	}).mouseup(function() {
		jQuery(this).attr('src', '/images/arrow_right.png');
	}).click(function() {
		salePreviewChangeImg('next');
	});
	jQuery('#green_box_open').click(function() {
		jQuery(this).toggleClass('active');
		jQuery('#rasprodaga_box').toggle();
	});
	
	
	jQuery('.universal_button_red').corner('4px');
	jQuery('.rasprodaga_header').corner('4px');
	
	jQuery('.universal_button_green').corner('4px');
	jQuery('#bestsellers .header').corner('4px');
	jQuery('.shop_detail_name').corner('left 4px');
	
});

