function create_cav()
    {
	var canvas = document.createElement("canvas");
	canvas.style.width=100+"%";
	canvas.style.height=100+"%";
	canvas.style.position="absolute";
	canvas.style.zIndex="0";
	canvas.style.display="none";
	canvas.style.top="0px";
	canvas.style.left="0px";
	canvas.style.bottom="0px";
	canvas.style.right="0px";
	if(canvas.getContext)
		{
		$(canvas).insertAfter('div.cheme img');
		return canvas.getContext('2d');
		}
	return;
    }
function get_coord(input)
    {
    coords=[];
    coords[0]=[];
    j=0;
    dc=0;
	scale=scale_x;
    dy=0;
    dx=0;
    var str = input.attr('coords').toString().replace('"', "");
    for (var cord in str.split(','))
        {
        var corda=parseInt(str.split(',')[cord])*scale;
        coords[j][(cord%2)*1]=corda+dc;
        dc=dy;
        scale=scale_y
        if (cord%2*1)
            {
            j++;
            coords[j]=[];
            dc=dx;
            scale=scale_x
            }
        }
	
    return coords
    }
function show_cav(input, con)
    {
    coords=get_coord(input);
	con.lineWidth = 1;
    con.beginPath();
	con.moveTo(coords[0][0], coords[0][1]);
    for (var point in coords)
        {
        if (point!=0)
            con.lineTo(coords[point][0], coords[point][1]);
        }
    con.lineTo(coords[0][0], coords[0][1]);
    con.fillStyle = 'rgba(255, 255, 255, .5)';
    con.strokeStyle = 'rgba(200, 200, 255, .5)';
    con.closePath();
    con.fill();
    $('canvas').fadeIn(200);
    }
function fill_canv(map, con, image, cats)
	{
	con.clearRect (0, 0,  1000, 1000);
	scale_x=1;
    scale_y=1;
	var poly_list=[];
	map.find('area').each(function()
		{
		if(find_cats(cats, $(this).attr('alt')))
			{
			var id=$(find_cats(cats, $(this).attr('alt'))).attr('id').replace("tit_for_", "");
			if ($("#cou_for_"+id).text()*1>0)
				poly_list.push(get_coord($(this)));
			}
		});
	for (var poly in poly_list)
		{
		con.save();
		con.beginPath();
		con.moveTo(poly_list[poly][0][0], poly_list[poly][0][1]);
		for (var point in poly_list[poly])
			{
			if (point!=0)
				con.lineTo(poly_list[poly][point][0], poly_list[poly][point][1]);
			}
		con.lineTo(poly_list[poly][0][0], poly_list[poly][0][1]);
		con.closePath();
		con.clip();
		con.drawImage(image, 0, 0);
		con.restore();
		}
	$('canvas').css("display", "");
	}
function find_cats(cats, str)
	{
	var out=0;
	$(cats).each(function()
		{
		if(str.replace($(this).text(), '').length<=2)
			{
			out = this;
			}
		});
	return out;
	}
function cat_info(area, cats)
	{
	var str = $(area).attr("alt");
	var cat = find_cats(cats, str);
	var value = cat?$(cat).parent().next().text():"нет билетов";
	var pod = "(нажмите для заказа)";
	$(area).mousemove(function(e)
		{
		div.style.top = e.pageY +"px";
		div.style.left = e.pageX + "px";
		});
	var div = document.createElement("div");
	div.setAttribute("class", "a_list");
	div.style.position="absolute";
	div.style.color="#fff";
	div.style.margin="0px 0px 0px 30px";
	div.style.background="#000";
	div.style.border="0px";
	div.style.display="none";
	div.style.padding="5px";
	document.body.appendChild(div);
	$('div.a_list').fadeIn(200);
	pod = value=="нет"?"": pod;
	$('div.a_list').html(str+' - '+value+'<p style="color: #686868;padding: 0px; font-size: 80%;">'+pod+'</p>');
	}
function unset_canv()
	{
	var cheme = $('div.cheme');
	if (cheme.length)
		{
		$('<img src='+$('div.cheme img').eq(0).attr("src")+' width="500" class="left" id="sheme" usemap="'+$('div.cheme img').eq(1).attr("usemap").replace("#","")+'">').insertBefore(cheme);
		str = $('div.cheme img').eq(1).attr("usemap")+' area';
		$("<map name='"+$('div.cheme img').eq(1).attr("usemap").replace("#","")+"' id='"+$('div.cheme img').eq(1).attr("usemap").replace("#","")+"'>").insertBefore(cheme);
		$('map[name="'+$('div.cheme img').eq(1).attr("usemap").replace("#","")+'"]').append($(str))
		$('div.cheme').remove();
		}
	}
function set_canv(img, ne_url, cats)
	{
	var cats_counter = [];
	$('#'+img.attr("usemap")+' area').unbind();
	$('#'+img.attr("usemap")+' area').each(function()
		{
		if(find_cats(cats, $(this).attr('alt')))
			{
			areas = find_cats(cats, $(this).attr('alt'))
			if($.inArray(areas, cats_counter)==-1)
				cats_counter.push(areas);
			}
		});
	if (cats_counter.length && cats_counter.length <= $(cats).length)
		img.wrap('<div class="cheme" />');
	var old_map = $('div.cheme img').eq(0).attr("usemap");
	if ($('#'+old_map+' area').length)
		{
		var cav = create_cav();
		if (cav)
			{
			$('div.cheme').css("display", "inline-block");
			$('div.cheme').css("position", "relative");
			$('div.cheme').css("float", "left");
			$('div.cheme img').eq(0).attr("usemap", "");
			$('div.cheme img').eq(0).css("opacity", ".2");
			$(this).attr("usemap", "");
			var clear = document.createElement("img");
			clear.style.position="absolute";
			clear.style.top=0+"px";
			clear.style.left=0+"px";
			clear.style.bottom=0+"px";
			clear.style.right=0+"px";
			clear.style.width=100+"%";
			clear.style.height=100+"%";
			clear.style.display="block";
			clear.setAttribute("usemap", "#"+old_map);
			clear.setAttribute("class", "top_img");
			clear.src=ne_url;
			$('div.cheme').append(clear);
			$('div.cheme').append($('#'+old_map));
			$('div.cheme img').eq(0).attr('src', $('div.cheme img').eq(0).attr('src')+"?2")
			$('div.cheme img').eq(0).load(function()
				{
				$("canvas").attr("width", $(this).width());
				$("canvas").attr("height", $(this).height());
				$('#'+old_map+' area').unbind();
				$('#'+old_map+' area').click(function(e)
					{
					e.preventDefault();
					var str = $(this).attr("alt");
					$("#cat_"+$(find_cats(cats, str)).attr('id').replace("tit_for_", "")).focus();
					});
				$('#'+old_map+' area').hover(function(e)
					{
					var str = $(this).attr("alt");
					if ($(find_cats(cats, str)).attr('id'))
						var id = $(find_cats(cats, str)).attr('id').replace("tit_for_", "");
					if ($("#cou_for_"+id).text()*1>0)
						{
						cat_info($(this), cats);
						show_cav($(this), cav); 
						}
					},function(e)
					{
					$('div.a_list').remove();
					$('div.cheme canvas').css("display", "none");
					fill_canv($('#'+old_map+' area').eq(0).parent(), cav, $('div.cheme img').eq(0)[0], cats);
					});
				fill_canv($('#'+old_map+' area').eq(0).parent(), cav, this, cats);
				});
			}
		}
	}
