var Cities = new Array();
var Comments = new Array();

var map_first_time = true;
var map_showed = false;
var get_h = false;

var comment_div_padding = 23;

var show_comment_timer;
var current_city = 0;
var h;

var resize_timer;
var need_recalc = true;

function MapShow ()
{
	dic_mapcoverbox = $("#MapCoverBox");
	if (!get_h)
	{
		$("#pageinside").css('z-index', '1');
		h = $("#pageinside").height();
		get_h = true;
	}

	dic_mapcoverbox.css('top', document.getElementById("pageinside").offsetTop+document.getElementById("menuLMap").offsetTop+$("#menuLMap").height());
	
	if (map_showed)
	{
		cityHide();
		vis = 'hidden';

		dic_mapcoverbox.height('1px');
		$("#pageinside").height(h + 'px');

		$("#menuLMapButton").html(map_open_btn).css('background', "url('" + map_open_btn_pic + "') right top no-repeat");
	}
	else
	{
		vis = 'visible';
		if (h<715)
		{
			$("#pageinside").height("715px");
		}
		$("#menuLMapButton").html(map_close_btn).css('background', "url('" + map_close_btn_pic + "') right top no-repeat");

		mapShow_show();
	}
	dic_mapcoverbox.css('visibility', vis);
	map_showed = !map_showed;
	map_first_time = false;
}

function cityOver(v)
{
	cityHide();
	clearTimeout(show_comment_timer);

	div_comment = $("div[name='comm_" + v + "']");

	comment_div_width = div_comment.width() + 2 * Number(comment_div_padding);

	if(Cities[v][0] > div_map_cover.width()/2)
	{
		x = Number(Cities[v][0]) + Number(cityDim[Cities[v][2]][0])/2 - comment_div_width;
	}
	else
	{
		x = Number(Cities[v][0]) + Number(cityDim[Cities[v][2]][0])/2;
	}

	x = Math.round(x);

	comment_div_height = div_comment.height() + 2 * Number(comment_div_padding);
	if(!(Comments[v]))
	{
		div_comment.height(comment_div_height).css('padding', comment_div_padding + 'px');
	}

	if(Cities[v][1] > div_map_cover.height()/2)
	{
		y = Number(Cities[v][1]) + Number(cityDim[Cities[v][2]][1])/2 - comment_div_height;
	}
	else
	{
		y = Number(Cities[v][1]) + Number(cityDim[Cities[v][2]][1])/2;
	}

	y = Math.round(y);

	x = Number(x) + Number(varLeft);

	div_comment.css('top', y + 'px');
	div_comment.css('left', x + "px");

	$("div[name='city_pic_" + v + "']").css('background', "url('" + Cities[v]['pic_act'] + "') left top no-repeat");
	$("div[name='city_name_" + v + "']").css("color", '#' + cityNameColorActivated);
	
	div_comment.css("visibility", "visible");
	current_city = v;
	Comments[v] = 1;
}

function cityOut()
{
	show_comment_timer = setTimeout("cityHide()", 2000);
}

function cityHide()
{
	if(current_city > 0)
	{
		$("div[name='city_pic_" + current_city + "']").css('background', "url('" + Cities[current_city]['pic'] + "') left top no-repeat");
		$("div[name='comm_" + current_city + "']").css("visibility", "hidden");
		$("div[name='city_name_" + current_city + "']").css("color", '#' + cityNameColor);
	}
}

function mapShow_show()
{
	if(need_recalc)
	{
		div_map_cover = $("#MapCover");
		div_map_cover_box = $("#MapCoverBox");

		div_map_cover_box.width($("#menuLMapBG").width() + 320).height('579px');
		$("#MapCoverBG").height(div_map_cover_box.height()).width(div_map_cover_box.width());
		$("div[name='net_map']").height('515px').width('762px');

		varLeft = Math.round(div_map_cover_box.width() / 2 - 762/2);

		div_map_cover.css('left', varLeft + 'px').height('515px').width('762px');
		
		for (v in Cities)
		{
			div_city_pic = $("div[name='city_pic_" + v + "']");
			div_city_name = $("div[name='city_name_" + v + "']");

			if(map_first_time)
			{

				if(Cities[v][2] == 1)
				{
					if(Cities[v][3] == 1)
					{
						pic = cityRegionalActive;
						pic_act = cityRegionalActivated;
					}
					else
					{
						pic = cityRegionalUnactive;
						pic_act = "";
					}
				}
				else
				{
					if(Cities[v][3] == 1)
					{
						pic = cityActive;
						pic_act = cityActivated;
					}
					else
					{
						pic = cityUnactive;
						pic_act = "";
					}
				}
				Cities[v]['pic'] = pic;
				Cities[v]['pic_act'] = pic_act;
				div_city_pic.css('top', Cities[v][1] + 'px').css('background', "url('" + pic + "') left top no-repeat");
				div_city_name.css('top', Cities[v][6] + 'px').css("color", '#' + cityNameColor);
			}

			left_const = varLeft;
			Cities[v]['0x'] = Number(Cities[v][0]) + Number(left_const);
			Cities[v]['5x'] = Number(Cities[v][5]) + Number(left_const);

			div_city_pic.css('left', Cities[v]['0x'] + "px");
			
			div_city_name.css('left', Cities[v]['5x'] + "px");
		}
	}
	need_recalc = false;
}

window.onresize = function(){
	need_recalc = true;
	if(map_showed)
	{
		clearTimeout(resize_timer);
		resize_timer = setTimeout("mapShow_show()", 1000);
	}
}

$(document).ready(function(){
if($("#MapCoverBox").length > 0) MapShow();
});