function refresh(_this) {
	
	var error = false;
	$("input.number").each(function() {
		if (!/^[0-9]*$/.test($(this).val())) {
			$(this.parentNode).addClass("error");
			$(this.parentNode).attr("title", "Ошибка");
			error = true;
		} else {
			$(this.parentNode).removeClass("error");
			$(this.parentNode).removeAttr("title");
		}
	});
	
	if (!error) {
		start();
		$.post("/basket/refresh/", $("form").serialize(), 
			function(data) {
				if (data.length > 0) $("#basket").html(data);
				stop();
		});
	}
	return false;
}

function start() {
	var div = document.createElement("DIV");
	
	$("body").append("<div id='TB_basket_overlay'></div");
	$("#basket").append(div);
	
	div.id = "wait_window_div";
	div.innerHTML = "Обновление";
	div.className = "waitwindow";
	
	var offset = $(".design-tocart-table").offset();
	div.style.left	= document.body.scrollLeft + offset.left + "px";
	div.style.top	= $(".design-tocart-table")[0].clientHeight/2 + "px";
}
	
function stop() {
	$("#wait_window_div").remove();
	$("#TB_basket_overlay").remove();
}

var hint_id = null;
var color	= null;
var cache = [];

var imgPreview = {
	div: null,
	
	init: function() {
		$("a.preview, a.prw").each(function() {
			$(this).bind("mouseover", function(){imgPreview.show(this)});
			$(this).bind("mouseout", function(){imgPreview.hide(this)});
			$(this).bind("click", function(){return false;});
		});
	},
	
	show: function(obj) {
		hint_id = obj.id;
		color	= $(obj).attr("color");
		
		var cache_data = imgPreview.cache_get(hint_id, color);
		
		imgPreview.div = document.createElement('div');
		imgPreview.div.style.position = 'relative';
		
		if (!cache_data) {
			$.ajax({type: "GET", url: "/show-preview/", data: "task=image&design_id="+hint_id+"&color_id="+color,async: false,
	   			success: function(data){
					if (obj.id == hint_id && $(obj).attr("color") == color) {
						imgPreview.div.innerHTML = data;
						$(imgPreview.div).find("div > span:has(span)").css("left", (obj.clientWidth+15)+"px");
					}
					cache.push({id:obj.id, color:$(obj).attr("color"), value:data});
	   			}, dataType: "html"});

		} else {
			imgPreview.div.innerHTML = cache_data;
			$(imgPreview.div).find("div > span:has(span)").css("left", (obj.clientWidth+15)+"px");
		}
		obj.appendChild(imgPreview.div);
	},
	
	hide: function(obj) {
		if (imgPreview.div) {
			obj.removeChild(imgPreview.div);
		}
		imgPreview.div = null;
	},
	
	cache_get: function(id, color) {
		if (id) {
			for(_idx in cache) {
				if (cache[_idx].id == id && cache[_idx].color == color)
					return cache[_idx].value;
			}
		}
		return false;
	}
}