/**
 * @author fa
 */
//document.write('<script type="text/javascript" src="../js/fmap.js?'
//	+Math.ceil(Math.random()*1000)+'"></script>');
//loadScript('../js/fmap.js?'+Math.ceil(Math.random()*1000),null);	
//Window.onresize=resize(1);
//Window.onload=load();
// onresize="resize(1);" onload="load();"
var EMaxZoom = 12;
/** user datas and proporties will be load in loginCheck() function */
var gUser= {
	name:'',
	id:null
}; //this data is imported from php Session
gUser.refresh_limit = function(){
	if(this.fmap_refresh_limit)return Number(this.fmap_refresh_limit);
	return (120)
}
gUser.setRefresh = function(value){
	var newValue = value;
	if (newValue < EMinRefreshTime) 
		newValue = EMinRefreshTime;
	gUser.refresh_map = newValue;
	if (gUser.refresh_map >= EMinRefreshTime) {
		clearTimeout(gTimer);
		gTimer = setTimeout("refreshA()", gUser.refresh_map*1000);
	}	
}
gUser.setUser = function (user,id,cooky){
	gUser.name=user;
	if(id!=null)gUser.id=id;
	if(cooky!=null)gUser.cooky=cooky;
	var menu_user=(user?user:"");
	if(gUser.id<0 && !gUser.refresh_map)gUser.setRefresh(EPublicRefreshTime);
	if(this.id<0) menu_user=lPubl+'('+menu_user+')';
	_e('menu_user').innerHTML=menu_user;
}
var gType; //value of get type argument
var EMinRefreshTime = 5;
var EPublicRefreshTime = 60;
var EPHP_LastUserPoint = EGetData + "user_tracks_lastpoints";
var EPHP_Logout = EGetData + "logout";
//var ETrackPointsPHP = "php/track_points.xml";
var EUser_Track = EGetData + "user_tracks";
var gTimer;
var gMap;
var gXMLRequestUI = new XMLHttp();
var gXMLRequest = new XMLHttp();
var gData=new Array();
var gPins; //markers class
var gBlink;
var gIfReset=true; //force load all data after loading page in the same session
//var gDialog; //to avoid double dialog
var gSysDialog; //to avoid double dialog

/** url class using in location url parse */
var gUrlLink = window.location.href.parseUrl2Obj();
gUrlLink.isNew=true; 
gUrlLink.getCommaList=function(aField){
	if(!this[aField])return '';
	var ret='';
	for(var i in this[aField]){
		if(i == "indexOf")continue;
		ret+=i+',';
	}
	if(ret)ret=ret.slice(0,ret.length-1);
	return ret;
}
/** last used autoZoom for all users,locs,tracks */
// gUrlLink.autoZoom='';
gUrlLink.setAutoZoom = function (aAutoZoom){
	this.autoZoom=aAutoZoom;
}
gUrlLink.Public=true;
gUrlLink.mapAPI='yahoo';
gUrlLink.mapAPIKey='';
/** get current Link string */
gUrlLink.getLink = function(){
	var s=this.url+'?';
	if(gUrlLink.Public)s+="public";
	if(gUrlLink.mapAPI!='yahoo')s+="&mapAPI="+gUrlLink.mapAPI;
	if(gUser.name) s+="&user="+gUser.name;
	for(var i in gData) {
		if(i=='indexOf')continue;
		var l=gData[i].getSelList();
		if(l!==null)s+="&"+i+"[]="+l;
	}
	if(gData['users']) {
		var l=gData['users'].getAutoPanningList();
		if(l)s+="&autoPanning="+l;
	}
	if(this["autoZoom"])s+="&autoZoom[]="+this.autoZoom;
	//		location.search=s;
	return s;
}
/** get current IFrame Link string */
gUrlLink.getIFrame = function(){
	var s='<iframe src="'+this.getLink()+'&iframe&mapAPIKey='+gUrlLink.mapAPIKey
	+'" height="425px" width="425px"></iframe>';		
	return s;
}

// onload

function load(){
	_console("Start load() gData:", gData);
	//	window.onresize=resize; //onresize attribut not allowed in body tab
	if (gUser.id || _readCookie(gHideCookie)||gUrlLink.sizeof>0) start(); //user is sign in so skip introduction
	else {
		resize();
		reloadAds();
	//		var ee=_e('ads_top_object');
	//		_e('ads_top_object').data="./en/ads_top.html";
	//		var eee=_e('ads_top_object');
	}
}
function startHide(){
	_createCookie(gHideCookie,'yes',365)
	start();
}
function start(){
	_status('Map loading..');
	_show('map'); 
	if(!gIsIframe){
		_show('menu_left');
		_show('map_form_left');
		_show('ads_right');
		reloadAds();
	}
	_hide('menu_introduce');
	_hide('introduce');
	//	_e('ads_top_object').data="./en/ads_top.html";
	gData['tracks'] = new Datas('tracks', Track, gData);
	gData['locs'] = new Datas('locs', Loc, gData);
	gData['users'] = new Datas('users', User, gData);
	resize();
	gBlink = new Blink();
	gPins = new Pins(
	{
		"param":{
			"color":"param",
			"pinl":{
				"iconShadowUrl":"img/pins/dot_empty.png",
				"iconSize":[14,18]
				,
				"iconAnchor":[1,17],
				"iconShadowSize":[5,5]
				}, //12,17
			"flag":{
				"iconShadowUrl":"img/pins/shadow_flag.png",
				"iconSize":[14,18]
				,
				"iconAnchor":[1,17],
				"iconShadowSize":[28,18]
				},
			"approx":{
				"iconShadowUrl":"img/pins/shadow_marker.png",
				"iconSize":[16,22]
				,
				"iconAnchor":[1,21],
				"iconShadowSize":[24,22]
				},
			"dot"   :{
				"iconShadowUrl":"img/pins/dot_empty.png",
				"iconSize":[5,5]
				,
				"iconAnchor":[2,2],
				"iconShadowSize":[5,5]
				},
			"down"  :{
				"iconShadowUrl":"img/pins/shadow_marker.png",
				"iconSize":[16,22]
				,
				"iconAnchor":[1,21],
				"iconShadowSize":[24,22]
				},
			"pause" :{
				"iconShadowUrl":"img/pins/shadow_marker.png",
				"iconSize":[16,22]
				,
				"iconAnchor":[1,21],
				"iconShadowSize":[24,22]
				},
			"speedup":{
				"iconShadowUrl":"img/pins/shadow_marker.png",
				"iconSize":[16,22]
				,
				"iconAnchor":[1,21],
				"iconShadowSize":[24,22]
				},
			"start" :{
				"iconShadowUrl":"img/pins/shadow_marker.png",
				"iconSize":[16,22]
				,
				"iconAnchor":[1,21],
				"iconShadowSize":[24,22]
				},
			"stop"  :{
				"iconShadowUrl":"img/pins/shadow_marker.png",
				"iconSize":[16,22]
				,
				"iconAnchor":[1,21],
				"iconShadowSize":[24,22]
				},
			"ball"   :{
				"iconShadowUrl":"",
				"iconSize":[10,10]
				,
				"iconAnchor":[5,5],
				"iconShadowSize":[10,10]
				},
			"text"  :{
				"iconShadowUrl":"img/pins/shadow_marker.png",
				"iconSize":[16,22]
				,
				"iconAnchor":[1,21],
				"iconShadowSize":[24,22]
				},
			"empty"  :{
				"iconShadowUrl":"img/pins/shadow_marker.png",
				"iconSize":[16,22]
				,
				"iconAnchor":[1,21],
				"iconShadowSize":[24,22]
				},
			"up"    :{
				"iconShadowUrl":"img/pins/shadow_marker.png",
				"iconSize":[16,22]
				,
				"iconAnchor":[1,21],
				"iconShadowSize":[24,22]
				}
		},
		0:{	
			"color":"#6633CC", 
			"pinl"   :{
				"iconUrl":"img/pins/00_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/00_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/00_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/00_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/00_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/00_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/00_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/00_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/00_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/00_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/00_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/00_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/00_up.png"
			}
		},
		1:{	
			"color":"#D81B1B", 
			"pinl"   :{
				"iconUrl":"img/pins/01_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/01_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/01_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/01_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/01_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/01_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/01_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/01_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/01_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/01_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/01_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/01_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/01_up.png"
			}
		},
		2:{	
			"color":"#0E7D5C", 
			"pinl"   :{
				"iconUrl":"img/pins/02_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/02_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/02_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/02_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/02_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/02_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/02_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/02_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/02_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/02_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/02_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/02_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/02_up.png"
			}
		},
		3:{	
			"color":"#991B81", 
			"pinl"   :{
				"iconUrl":"img/pins/03_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/03_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/03_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/03_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/03_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/03_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/03_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/03_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/03_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/03_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/03_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/03_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/03_up.png"
			}
		},
		4:{	
			"color":"#1B9A25", 
			"pinl"   :{
				"iconUrl":"img/pins/04_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/04_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/04_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/04_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/04_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/04_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/04_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/04_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/04_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/04_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/04_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/04_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/04_up.png"
			}
		},
		5:{	
			"color":"#1DB6B6", 
			"pinl"   :{
				"iconUrl":"img/pins/05_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/05_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/05_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/05_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/05_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/05_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/05_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/05_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/05_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/05_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/05_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/05_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/05_up.png"
			}
		},
		6:{	
			"color":"#2DCA16", 
			"pinl"   :{
				"iconUrl":"img/pins/06_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/06_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/06_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/06_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/06_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/06_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/06_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/06_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/06_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/06_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/06_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/06_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/06_up.png"
			}
		},
		7:{	
			"color":"#EA040F", 
			"pinl"   :{
				"iconUrl":"img/pins/07_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/07_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/07_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/07_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/07_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/07_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/07_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/07_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/07_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/07_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/07_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/07_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/07_up.png"
			}
		},
		8:{	
			"color":"#A80978", 
			"pinl"   :{
				"iconUrl":"img/pins/08_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/08_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/08_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/08_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/08_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/08_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/08_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/08_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/08_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/08_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/08_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/08_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/08_up.png"
			}
		},
		9:{	
			"color":"#F1B958", 
			"pinl"   :{
				"iconUrl":"img/pins/09_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/09_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/09_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/09_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/09_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/09_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/09_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/09_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/09_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/09_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/09_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/09_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/09_up.png"
			}
		},
		10:{	
			"color":"#3BFEE0", 
			"pinl"   :{
				"iconUrl":"img/pins/10_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/10_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/10_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/10_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/10_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/10_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/10_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/10_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/10_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/10_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/10_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/10_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/10_up.png"
			}
		},
		11:{	
			"color":"#D5F713", 
			"pinl"   :{
				"iconUrl":"img/pins/11_pinl.png"
			},
			"flag"   :{
				"iconUrl":"img/pins/11_flag.png"
			},
			"approx" :{
				"iconUrl":"img/pins/11_approx.png"
			},
			"dot"    :{
				"iconUrl":"img/pins/11_dot.png"
			},
			"down"   :{
				"iconUrl":"img/pins/11_down.png"
			},
			"pause"  :{
				"iconUrl":"img/pins/11_pause.png"
			},
			"speedup":{
				"iconUrl":"img/pins/11_speedup.png"
			},
			"start"  :{
				"iconUrl":"img/pins/11_start.png"
			},
			"stop"   :{
				"iconUrl":"img/pins/11_stop.png"
			},
			"ball"   :{
				"iconUrl":"img/pins/11_ball.png"
			},
			"text"   :{
				"iconUrl":"img/pins/11_text.png"
			},
			"empty"  :{
				"iconUrl":"img/pins/11_empty.png"
			},
			"up"     :{
				"iconUrl":"img/pins/11_up.png"
			}
		}
	}
	);
	if (navigator.product != 'Gecko'&&!(gUser.id || _readCookie(gHideCookie))) {
		//	var options={points:gUser.points_count};
		gSysDialog = new Dialog('navigator_dialog', null, function(ret, target){
			gSysDialog = null;
			mapInit();
			return true;
		})
	} else {
		mapInit();
	}
}
function mapInit() {
	gMap = new Mapstraction('mapAPI', EMapAPI,gDebug); //'microsoft'); //'google');
	if(!gMap.maps[EMapAPI]) {
		//		window.location.reload(false); //reload page using cache
		return;
	}
	/*   gMap.addControls({
        pan: true,
        zoom: 'large',
        map_type: true
	 *     pan:      true,
 *     zoom:     'large' || 'small',
 *     overview: true,
 *     scale:    true,
 *     map_type: true,
    }); */
	//    gMap.addLargeControls();
	//    gMap.addMapTypeControls();
	if(is_touch_device()){
		_console("Block dragging for touch devices");
		gMap.addControls({
        pan: true,
        zoom: 'small',
        map_type: true
    }); 
//		gMap.dragging(false);
	} else {
		_console("Dragging is enable for notouch browsers");
	}
	gMap.setUIToDefault(); //WW

	if(gUrlLink['public']){
		if(gUrlLink['user'])gUser.setUser(gUrlLink['user'],-1,''); //public with user
		else gUser.setUser('',-1,''); //public
	} else if (gUrlLink['user']&&gUser.id<0){ 
		gUser.setUser(gUrlLink['user'],0); //not public
	} else if (gUrlLink['tracks']&&!gUrlLink['user']){
		gUser.setUser('',-1,''); //public for track without user
	}
	if (gUser.id<0) {//public
		_status('Data loading...');
		loginCheck(gUser.name,gUser.cooky);
	} else if (gUser.id>0&&(!gUrlLink['user']||gUrlLink['user']==gUser.name)) { //the same user
		gUser.setUser(gUser.name, gUser.id,'_');
		_status('Data loading...');
		loginCheck(gUser.name,gUser.cooky);
	}
	else {
		_status('Login ...');
		if(gUrlLink['user'])dialog_login(gUrlLink['user']);
		else dialog_login();
	}
}
function is_touch_device() {  
	try {  
		if(document.documentElement.ontouchmove||window.Touch)
			return true;  
	} catch (e) {  
	}  
	return false;
}
function getBoundingBox(aKeys) {
	var b;
	var array=gData;
	if(aKeys)array=aKeys;
	for (var key in array) {
		if (key == "indexOf"||typeof gData[key]=="undefined") 
			continue;
		b=gData[key].getBoundingBox(b);
	}
	if(!b) b= new BoundingBox(50,18,51,19)
	return b;
}

function showHTML(doc,options){
	var d=new DialogHTML(0,0,options,gLang+"/"+doc,function(ret){
		return true;
	})
}

function dialog_help(){
	var d=new DialogHTML(0,0,{
		bar:lHelp
	},"show/help.html",function(ret){
		return true;
	})
};
/** link menu Item */
function dialog_link(){
	if(gSysDialog)return;
	gUrlLink.Public=(gUser.id<0);
	var options=new Array();
	options['mapAPI']=gUrlLink.mapAPI;
	options['mapAPIKey']=gUrlLink.mapAPIKey;
	options['link']=gUrlLink.getLink();
	options['iframe']=gUrlLink.getIFrame();
	if(gUrlLink.Public)options['public']="yes";
	gSysDialog = new Dialog('link_dialog',options,function(ret,target){
		if(!ret) { //Cancel
			gSysDialog=null;
			return true;
		}
		switch(target){
			case 'add2bookmark':
				var title="FMap";
				var url=ret.link;
				if (window.sidebar) { 
					window.sidebar.addPanel(title, url,"");
					break; 
				} else if( document.all ) {
					window.external.AddFavorite( url, title);
					break;
				} else if( window.opera && window.print ) {
				
				}
				alert(lSorryBrowser); 
				break;
		}
		gSysDialog=null;
		return true;
	})
}
/* onchange link dialog functions */
dialog_link.publicView = function(me){
	if(!gSysDialog)return;
	gUrlLink.Public=me.checked;
	gSysDialog.setValue('link',gUrlLink.getLink());
	gSysDialog.setValue('iframe',gUrlLink.getIFrame());
};
dialog_link.mapAPI = function(me){
	if(!gSysDialog)return;
	gUrlLink.mapAPI=me.value;
	gSysDialog.setValue('link',gUrlLink.getLink());
	gSysDialog.setValue('iframe',gUrlLink.getIFrame());
};
dialog_link.mapAPIKey = function(me){
	if(!gSysDialog)return;
	gUrlLink.mapAPIKey=me.value;
	gSysDialog.setValue('link',gUrlLink.getLink());
	gSysDialog.setValue('iframe',gUrlLink.getIFrame());
};
/** profile menu Item */
function dialog_profile(){
	if(gSysDialog)return;
	//	var options={points:gUser.points_count};
	gSysDialog = new Dialog('profile_dialog',gUser,function(ret,target){
		if(!ret) { //Cancel,Public
			gSysDialog=null;
			return true;
		}
		//trim all field
		for (var i in ret){
			if(typeof ret[i] =='string') ret[i]=ret[i].trim();
		}
		//		ret['public']=(ret['public']?"yes":"no");
		
		var data='';
		if(ret.name!=gUser.name)data+='name='+ret.name+'&';
		if(ret.pass)data+='password='+_lll.ccc(ret.pass)+'&';
		if(ret.email!=gUser.email)data+='email='+ret.email+'&';
		if(ret.refresh_map!=gUser.refresh_map)data+='refresh_map='+ret.refresh_map+'&';
		if(ret.id_s_user!=gUser.id_s_user)data+='id_s_user='+ret.id_s_user+'&';
		if(ret.id_privacy!=gUser.id_privacy)data+='id_privacy='+ret.id_privacy+'&';

		var id=null;
		var mes='';
		if(!data){ //nothing change
			//			gSysDialog=null;
			//			return true;
			mes=lNothingChange;
			id='id_privacy';
		}
		else if (!ret.name.validateUser()) {
			mes=lPleaseCorrentUser;
			id='name';
		} else if (ret.pass.length<5&&ret.pass.length>0) {
			mes=lYourPassShort;
			id='pass';
		} else if (ret.pass!=ret.pass2) {
			mes=lPassNotMatch;
			id='pass2';
		}else if (!ret.email.validateEmail()) {
			mes=lEmailNotCorrect;
			id='email';
		}else if (ret.email!=ret.email2&&ret.email!=gUser.email) {
			mes=lEmailNotMatch;
			id='email2';
		}else if (ret.refresh_map<gUser.refresh_limit()&&ret.refresh_map!=gUser.refresh_map){
			mes=lRefreshTooShort+" (< "+gUser.refresh_limit()+")";
			id='refresh_map';			
		}else if (ret.control!=eval(_getElementById(gSysDialog.elem,'lcontrol').innerHTML)){
			mes=lBotOrHuman;
			id='control';			
		}
		if (id) {
			gSysDialog.innerHTML('info', mes, "color:red;visibility:visible");
			gSysDialog.showError(id);
			return false;
		}
		data =_hexBackEncode(data+"&"+Math.ceil(Math.random()*1000));
		data='p1='+data+'&p2='+_lll.ccc(data);
		gSysDialog.innerHTML("info",lPleaseWait,"color:black;visibility:visible");
		gXMLRequestUI.loadXMLCorr(EProfile+data
			,true,null,false,null,function(status,response){
				if(status!=200){
					gSysDialog.innerHTML("info",lConnError+' ('+status+')'
						,"color:red;visibility:visible");
					return;
				}
				switch(response.key_value('ret')) {
					case 'OK':
						if (ret.email != gUser.email) {
							//							window.confirm(lActivEmailSend);
							gUser.email = ret.email;
						}
						if(ret.email!=gUser.email)gUser.email=ret.email;
						if(ret.refresh_map!=gUser.refresh_map) gUser.setRefresh(ret.refresh_map);
						if(ret.name!=gUser.name)gUser.setUser(ret.name);
						if(ret.id_s_user!=gUser.id_s_user)gUser.id_s_user=ret.id_s_user;
						if(ret.id_privacy!=gUser.id_privacy)gUser.id_privacy=ret.id_privacy;
						gSysDialog.hide();
						gSysDialog=null;
						return;
					case 'NOKu':
						gSysDialog.innerHTML("info",lUserOccupied
							,"color:red;visibility:visible");
						return;
					case 'NOKe':
						gSysDialog.innerHTML("info",lTooMuchAccount
							,"color:red;visibility:visible");
						return;
					default:
						gSysDialog.innerHTML("info",lUserPassNotValid,"color:red;visibility:visible");
				}
			})
		return false;
	})
	gSysDialog.innerHTML('lcontrol',Math.ceil(Math.random()*10)+'+'+
		Math.ceil(Math.random()*10));
}

function dialog_signUp(){
	if(gSysDialog)return;
	//	var options={v1:gUser.name};
	gSysDialog = new Dialog('signup_dialog',null,function(ret,target){
		if(!ret) { //Cancel,Public
			gSysDialog=null;
			return true;
		}
		//trim all field
		for (var i in ret){
			if(typeof ret[i] =='string')ret[i]=ret[i].trim();	
		}
		var id=null;
		var mes='';
		if (!ret.user.validateUser()) {
			mes=lPleaseCorrentUser;
			id='user';
		} else if (ret.pass.length<5) {
			mes=lYourPassShort;
			id='pass';
		} else if (ret.pass!=ret.pass2) {
			mes=lPassNotMatch;
			id='pass2';
		}else if (!ret.email.validateEmail()) {
			mes=lEmailNotCorrect;
			id='email';
		}else if (ret.email!=ret.email2) {
			mes=lEmailNotMatch;
			id='email2';
		}else if (!ret.accept){
			mes=lAgree4Terms;
			id='accept';			
		}else if (ret.control!=eval(_getElementById(gSysDialog.elem,'lcontrol').innerHTML)){
			mes=lBotOrHuman;
			id='control';			
		}
		if (id) {
			gSysDialog.innerHTML('info', mes, "color:red;visibility:visible");
			gSysDialog.showError(id);
			return false;
		}
		var data='p1=';
		var data =_hexBackEncode(ret.user+";"+ret.email+";"+_lll.ccc(ret.pass)+";"
			+Math.ceil(Math.random()*1000));
		data='p1='+data+'&p2='+_lll.ccc(data);
		gSysDialog.innerHTML("info",lPleaseWait,"color:black;visibility:visible");
		gXMLRequestUI.loadXMLCorr(ESignUp+data
			,true,null,false,null,function(status,response){
				if(status!=200){
					gSysDialog.innerHTML("info",lConnError+' ('+status+')'
						,"color:red;visibility:visible");
					return;
				}
				switch(response.key_value('ret')) {
					case 'OK':
						window.confirm(lActivEmailSend);							
						gSysDialog.hide();
						gSysDialog=null;
						return;
					case 'NOKu':
						gSysDialog.innerHTML("info",lUserOccupied
							,"color:red;visibility:visible");
						return;
					case 'NOKe':
						gSysDialog.innerHTML("info",lTooMuchAccount
							,"color:red;visibility:visible");
						return;
					default:
						gSysDialog.innerHTML("info",lUserPassNotValid,"color:red;visibility:visible");
				}
			})
		return false;
	})
	gSysDialog.innerHTML('lcontrol',Math.ceil(Math.random()*10)+'+'+
		Math.ceil(Math.random()*10));
//	gSysDialog.focus("v1");
}

function forgetPass(username){
	var options = {
		bar:lBarPassRecovery,
		l1:lUserName,
		v1:username+":",
		l2:lEmail+":"
	}
	var dialog = new Dialog('dialog',options,function(ret){
		if(!ret)return true; //Cancel
		if (!ret.v1.validateUser()) {
			dialog.innerHTML("info",lPleaseCorrentUser
				,"color:red;visibility:visible");
			dialog.showError('v1');
			return false;
		}
		if (!ret.v2.validateEmail()) {
			dialog.innerHTML("info",lEmailNotCorrect
				,"color:red;visibility:visible");
			dialog.showError('v2');
			return false;
		}
		var data =_hexBackEncode(ret.v1+";"+ret.v2+";"+Math.ceil(Math.random()*1000));
		data='p1='+data+'&p2='+_lll.ccc(data);
		dialog.innerHTML("info",lPleaseWait,"color:black;visibility:visible");
		gXMLRequestUI.loadXMLCorr(EForgetPass+data
			,true,null,false,null,function(status,response){
				if(status!=200){
					dialog.innerHTML("info",lConnError+' ('+status+')'
						,"color:red;visibility:visible");
					return false;
				}
				switch(response.key_value('ret')) {
					case 'OK':
						dialog.innerHTML("info",lPassRecSend
							,"color:green;visibility:visible");
						break;
					case 'NOKu':
						dialog.innerHTML("info",lBadUserName
							,"color:red;visibility:visible");
						break;
					case 'NOKe':
						dialog.innerHTML("info",lBadEmail
							,"color:red;visibility:visible");
						break;
					default:
						dialog.innerHTML("info",lUserNotValid,"color:red;visibility:visible");
				}
				return true;
			})
		return false;
	})
	return false;
}

function dialog_login(aUserName){
	if(gDialog)return;
	var options=null;
	if(aUserName)options={
		v1:aUserName
	};
	gDialog = new Dialog('login',options,function(ret,target){
		switch(target){
			case 'signup':
				dialog_signUp();
				return false;
			case 'forget':
				return forgetPass(ret.v1);
		}
		//OK button
		var user;
		var pass;
		var cooky;
		if(!ret||target=='public') { //Cancel,Public
			pass='';
			user=''; //'Public';
			cooky='';
		//			gDialog=null;
		//			return true;
		} else {
			var m=ret.v2;
			/*		    if (ret.v1 == '') {
				gDialog.innerHTML("info",lPleaseCorrentUser
					,"color:red;visibility:visible;");
				gDialog.showError('v1');
		        return false;
		    }
		    if (ret.v2 == '') {
				gDialog.innerHTML("info",lPleasePassword
					,"color:red;visibility:visible");
				gDialog.showError('v2');
		        return false;
		    }*/
			user=ret.v1;
			pass=ret.v2;
			if(pass=='') cooky=''; //public for certain user
			else cooky = _lll.cook(pass);
		}
		gDialog.innerHTML("info",lPleaseWait
			,"color:black;visibility:visible");
		loginCheck(user,cooky); //use and change gUser and gDialog
		return false;
	})
	_getElementById(gDialog.elem,"v1").focus();
	if(gType){ //message from outer php script ex. activation
		gDialog.innerHTML("info",lActivCompleted
			,"color:green;visibility:visible");
		$gMessage=null;//show only ones
	}
}
/** check user password and load gUser properties */
function loginCheck(user, cooky){
	if(typeof cooky=="undefined")cooky='_'; //old user
	gXMLRequestUI.loadXMLCorr(ELogin + user + '&cook=' + cooky, true, null,false, null, function(status, response){
		if (response.key_value('ret') == 'OK') {
			if(gDialog)
				gDialog.innerHTML("info",lPleaseWait, "color:green;visibility:visible");
			reset = true;
			if (cooky == '') { //Public account
				gUser.setUser(user,-1,cooky);		
			} else {
				gUser = response.toObject(gUser); //setup gUser properties (id_user,email..)
				gUser.setUser(gUser.name, gUser.id_user, cooky);
			}
			refreshA();
			if (gUser.id>0) { //defined if not Public user
				_e('menu_login').style.display = "none";
				_e('menu_signup').style.display = "none";
				_e('menu_logout').style.display = "";
				_e('menu_profile').style.display = "";
			} else {
				_e('menu_login').style.display = "";
				_e('menu_signup').style.display = "";
				_e('menu_logout').style.display = "none";
				_e('menu_profile').style.display = "none";
			}
			if(gDialog) gDialog.hide();
			gDialog = null;
		}
		else {
			if (gDialog) {
				if (response.key_value('mess') == 'not_allowed') {
					gDialog.innerHTML("info", lFMapNotAllowed
						, "color:green;visibility:visible");
				}else if (response.key_value('mess') == 'blocked') {
					gDialog.innerHTML("info", lSorryAccountBlock, "color:red;visibility:visible");
				}else {
					gDialog.innerHTML("info", lUserPassNotValid, "color:red;visibility:visible");
				}
			} else {
				_console('Login error! user:'+user+ ' response:'+response);
				dialog_logout();
			}
		}
	})
}
function refreshStop(){
	clearTimeout(gTimer);
}
function dialog_logout(){
	gXMLRequestUI.loadXMLCorr(EPHP_Logout,true,null,false,null, function(status,response) {
		_console("Logout status:", status, " reply:", response);
		if (response.toUpperCase().indexOf('OK') == 0) {
			refreshStop();
			gUser.setUser(null,0);
			gData['tracks'].reset();
			gData['locs'].reset();
			gData['users'].reset();
			_status(lLogout+"...");
			gIfReset=true;
			_e('menu_login').style.display="";
			_e('menu_logout').style.display="none";
			_e('menu_signup').style.display = "";
			_e('menu_profile').style.display = "none";
			dialog_login();
		}
	});
}
/**
 * setup parameters load from url or cooky
 */
function setupParameters(){
	if(!gUrlLink.isNew)return;
	if(!gData)return;
	// show selected elements
	for(var i in gData) {
		if(i=='indexOf')continue;
		gData[i].hideShow(gUrlLink[i]);
	}
	// autoPanning
	if(gData['users']){
		for(var i in gUrlLink["autoPanning"]) {
			if(i == "indexOf")continue;
			gData['users'].getElemByIdOrName(i).setAutoPanning(true);
		}
	}
	// autoZoom
	var autoZoomKeys=gUrlLink['autoZoom'];
	if(!autoZoomKeys&&gUrlLink['tracks'])autoZoomKeys={
		'tracks':''
	}; //default zoom for tracks
	gUrlLink.isNew=false;
	return autoZoomKeys;
}
/** This function refresh ads if len>ERefreshAdsLong after T1 or after T2 */
var ERefreshAdsT1= 60000; //max time to refresh for long XML answer in ms
var ERefreshAdsT2= 600000; //max time to refresh for short XML answer in ms
var ERefreshAdsLong = 200; //long is more than this bytes
var gRefreshAdsTS = (new Date()).getTime();
function refreshAds(aLen){
	var time=(new Date()).getTime();
	if(aLen>ERefreshAdsLong){
		if(time-gRefreshAdsTS<ERefreshAdsT1) return;
	} else {
		if(time-gRefreshAdsTS<ERefreshAdsT2) return;		
	}
	gRefreshAdsTS=time;
	reloadAds();
}
function reloadAds()
{
	_e('ads_top_object').data="./"+gLang+"/ads_top.html";
	//	_e('ads_top_object').src="./en/ads_top.html";
	var ads_top=_e("ads_top");
	if(ads_top){
		var inner=ads_top.innerHTML;
		ads_top.innerHTML="";
		ads_top.innerHTML=inner;
	}	
}
function refreshA(){
	//	if(!gUser.name)return;
	var trackIds=''
	_console("RefreshA Array trackIds:"+trackIds);
	if(gUrlLink && gUrlLink.isNew) trackIds=gUrlLink.getCommaList('tracks');
	else trackIds=gData['tracks'].selListJoin();
	if(trackIds)trackIds='&i='+trackIds;
	//_console("refreshA",'time');
	gXMLRequest.loadXMLArray(EPHP_LastUserPoint+trackIds+(gIfReset?'&reset':''),false, function(aArray,status) {
		_console("RefreshA Array copy",'time');
		_status(lStatus+':'+status);
		if(status==401) { //bad user
			dialog_logout();
			return;
		} else if(status !=200){
			return;
		}
		/** copy new datas to users,locs,tracks arrays */
		for(var key in aArray) {
			if (key == "indexOf"||typeof aArray[key]=="undefined") 
				continue;			
			if (key in gData) {
				if (aArray[key].length) 
					gData[key].add(aArray[key]);
			}else {
				if(key!="points")_console("load undefined key:", key, 'log');
			}
		}
		/** load tracks points into tracks arrays */ 
		if(aArray['points']){
			var index=0;
			while(index<aArray['points'].length){
				var id_track=aArray['points'][index]['id_track'];
				if(gData['tracks'].array[id_track])
					index=gData['tracks'].array[id_track].addPoints(aArray['points'],index);
				else index++;
			}
		}
		if(gUrlLink && gUrlLink.isNew)var autoZoomKeys=setupParameters(); //setup parameters load from url or cooky 
		//		if ('users' in aArray && aArray['users'].length > 0) {
		if(gIfReset) gMap.setBounds(getBoundingBox(autoZoomKeys));
		gIfReset = false; //init datas were loaded
		//		}
		for(var key in aArray) { //first we load all datas than we show them
			if (key == "indexOf"||typeof aArray[key]=="undefined") 
				continue;			
			if (key in gData) {
				gData[key].refreshIfOrd();
			//				if (aArray[key].length||gIfReset||key=='users') 
			//					gData[key].show(); //table.refresh();
			}
		}
		_status(lOk);
		_console("RefreshA Array copy",'timeEnd');
	});
	if (gUser.refresh_map >= EMinRefreshTime) {
		clearTimeout(gTimer);
		gTimer = setTimeout("refreshA()", gUser.refresh_map*1000);
	}
//_console("refreshA",'timeEnd');
}

function autoZoomCenter(){
	gMap.autoCenterAndZoom();
}

function getWindowHeight(){
	if (window.self && self.innerHeight) {
		return self.innerHeight;
	}
	if (document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	}
	return 0;
}

function getWindowWidth(){
	if (window.self && self.innerHeight) {
		return self.innerWidth;
	}
	if (document.documentElement && document.documentElement.clientWidth) {
		return document.documentElement.clientWidth;
	}
	return 0;
}

function resize() { //aIfRefresh){
	//	aIfRefresh=false; //on Android, iPhone browser set event parameter so it is true and make unnessecerary refresh 
	_console("Start resize()"); // aIfRefresh:",aIfRefresh,"<end bool:",!!aIfRefresh);
	document.body.style.overflow="hidden";
	document.body.style.height="";
	document.body.style.width="";
	var windowHeight=getWindowHeight();
	var windowWidth=getWindowWidth();
	var dHeight=0;
	var dWidth=0;

	if(windowHeight<500){
		windowHeight=550;
		document.body.style.overflow="auto";
		document.body.style.height="550px";
		dWidth=20;
	}
	if(windowWidth<600){
		windowWidth=650;
		document.body.style.overflow="auto";
		document.body.style.width="650px";
		dHeight=20;
	}
	windowHeight-=dHeight;
	windowWidth-=dWidth;
	//	_e("footer").style.position="absolute";
	var mapD = _e("map");
	var offsetTop = _e("header").offsetTop+_e("header").offsetHeight; //mapD.offsetTop;
	_console("offsetTop:"+offsetTop+" header.Top:"+_e("header").offsetTop+" header.Height:"+_e("header").offsetHeight);	
	var height = windowHeight - offsetTop - _e("footer").offsetHeight + 0;
	_console("height:"+height+" WindowHeight"+windowHeight+" footer.Height"+_e("footer").offsetHeight);
	
	if (height >= 0) {
		mapD.style.height = height + "px";
		_e("introduce").style.height = (height-10-10) + "px"; //minus padding Top and Bottom
		_e("map_form_left").style.height = (height-2) + "px";
		_e("ads_right").style.height = (height) + "px";
	}
	var adsD = _e("ads_right");
	var adsW = 0;
	if (adsD) 
		adsW = adsD.offsetWidth;
	var w = windowWidth - _e("map_form_left").offsetWidth - adsW - 0;
	if (w > 0) 
		mapD.style.width = w + "px";
	//tracks table height    
	var tracks=_e("s_tracks");
	var h=windowHeight-tracks.offsetTop - _e("footer").offsetHeight - 10;
	if(h>80)tracks.style.height=h + "px";
//    if (aIfRefresh) 
//        refreshA();
}


