
var map_is_loaded=false;
var map_c_x=0;
var map_c_y=0;
var map_c_zoom=0;

var map_view_changed_extra=null;

function map_viewchanged(x,y,zoom)
{
	map_c_x=x;
	map_c_y=y;
	map_c_zoom=zoom;
	if (map_view_changed_extra) map_view_changed_extra(x,y,zoom);
}

function map_embed(element_id,width,height,background,altcontent)
{

	var map_so = new SWFObject('engine.swf', 'mapa', width, height, '7', background);
	//map_so.useExpressInstall('lib/expressinstall.swf');
	map_so.addParam("allowFullScreen", "true");
	map_so.write(element_id);
	
	if (!map_obj())
	{
		document.getElementById(element_id).innerHTML=altcontent;
	}
		
}

function map_ico_click(id)
{

	if (map_ico_click_extra)
	{
		setTimeout(function () {map_ico_click_extra(id);},10);
	}
}

function map_obj()
{
	return document.getElementById('mapa');
}


function map_dummy_pos(x,y,z)
{
}

function map_dummy()
{
}

function map_loaded()
{
	map_is_loaded=true;
	if (map_loaded_extra)
	{
		setTimeout(map_loaded_extra,10);
	}
}


function map_icons_loaded()
{
	setTimeout(function () {map_vector_command('load');},100);
	
	if (map_icons_loaded_extra)
	{
		setTimeout(map_icons_loaded_extra,10);
	}
}

function map_set_var(variable,value)
{
	//if (!map_is_loaded) return;
	map_obj().SetVariable(variable,value);
}

function map_command(cmd)
{
 //  if (!map_is_loaded) return;
	map_set_var('map_command',cmd);
}

function map_vector_command(cmd)
{
 //  if (!map_is_loaded) return;
	map_set_var('vector_command',cmd);
	dbg('vector: '+cmd);
}

var map_old_il=null;
var map_vec_layer='';


function map_show_icons_done()
{
	//alert('l: '+map_vec_layer);
	map_set_var('vector_layer',map_vec_layer);
	setTimeout(function () {map_vector_command('load');}, 1000);
	map_icons_loaded_extra=map_old_il;
}


function map_show_icons(icon_set)
{
	map_vec_layer=icon_set;
	map_old_il=map_icons_loaded_extra;
	map_icons_loaded_extra=map_show_icons_done;
	map_set_var('icon_set',icon_set+'-'+Math.floor(Math.random()*10000));
	setTimeout(function () {map_command('load_icons');},100);
	//setTimeout(function () {alert('aaa');map_show_icons_done(); },2000);
}

function map_zoom(dir)
{
	setTimeout(function() {map_command('zoom_'+dir);},10);
}


function map_click(id)
{
	
	if (my_map_click) my_map_click(id);
	click(id);
}




function map_set_location(x,y,zoom)
{
	setTimeout(function () {map_set_var('map_loc_x',x);},50);
	setTimeout(function () {map_set_var('map_loc_y',y);},100);
	setTimeout(function () {map_set_var('map_loc_zoom',zoom);},150);
	setTimeout(function () {map_command('set_location');},200);
	map_c_x=x;
	map_c_y=y;
	map_c_zoom=zoom;
	//dbg("set location");
}

function map_transit_location(x,y,zoom)
{
	setTimeout(function () {map_set_var('map_loc_x',x);},5);
	setTimeout(function () {map_set_var('map_loc_y',y);},10);
	setTimeout(function () {map_set_var('map_loc_zoom',zoom);},15);
	setTimeout(function () {map_command('transit_location');},20);
	map_c_x=x;
	map_c_y=y;
	map_c_zoom=zoom;
	//dbg("set location");
}

function map_vector_line(x,y)
{
	setTimeout(function () {map_set_var('vector_x',x);},5);
	setTimeout(function () {map_set_var('vector_y',y);},10);
	setTimeout(function () {map_vector_command('line');},20);
	
}

function map_vector_move(x,y)
{
	setTimeout(function () {map_set_var('vector_x',x);},5);
	setTimeout(function () {map_set_var('vector_y',y);},10);
	setTimeout(function () {map_vector_command('move');},20);
	
}
/*
function loc(x,y,zoom)
{
	if (loc_real) loc_real(x,y,zoom);
}
*/

var vector_lines=new Array();
var vector_polys=new Array();


function vector_new_line(color,thickness,x,y)
{
	var i=vector_lines.length;
	vector_lines[i]=new Array();
	vector_lines[i][0]=color;
	vector_lines[i][1]=thickness;
	vector_lines[i][2]=new Array();
	vector_lines[i][3]=new Array();
	vector_lines[i][4]=x;
	vector_lines[i][5]=y;
	return i;
}

function vector_add_line_point(i,x,y)
{
	var k=vector_lines[i][2].length;
	vector_lines[i][2][k]=x;
	vector_lines[i][3][k]=y;
}

function vector_draw_lines()
{
	var t=30;
	for (i=0;i<vector_lines.length;i++)
	{
		var l=vector_lines[i];
		map_vector_move(l[4],l[5]);
		for (k=0;k<l[2].length;k++)
		{
			
			setTimeout(function () {dbg('l '+l[2][k]+' '+l[3][k]);map_vector_line(l[2][k],l[3][k]);} ,t);
			t+=100;
		}
	}
}



function map_vector_load()
{
	map_icons_loaded_extra=map_vector_load_();
	map_show_icons('vector');
}

function map_vector_load_()
{
	map_vector_command('load');
}




//function 
var loc=null;
var loc_real=null;
var map_set_location_done=map_dummy;
//var map_viewchanged=map_dummy_pos;
var map_loaded_extra=null;
var map_icons_loaded_extra=null;
var map_ico_click_extra=null;
var map_current_location=null;
var my_map_click=null;