//---------- класс обслуживания массива маркеров карты --------
//картинки маркеров в папке /images/mapicon		названы в зависимости от индексов массивов
// id должен быть уникальным

dragg = {	"i":'',
	"j":'',
	"k":''}

ArrObj = function(mapp){
	this.map = mapp;
	this.a = Array(7);
	this.a[0] = Array(4);
	this.a[1] = Array(1);
	this.a[2] = Array(3);
	this.a[3] = Array(3);
	this.a[4] = Array(3);
	this.a[5] = Array(3);
	this.a[6] = Array(4);
	var j=0; var i=0;
	for(i=0; i<this.a.length; i++)
		for(j=0; j<this.a[i].length; j++){ this.a[i][j] = Array(); }
	this.ids = Array(7);
	this.ids[0] = Array(4);
	this.ids[1] = Array(1);
	this.ids[2] = Array(3);
	this.ids[3] = Array(3);
	this.ids[4] = Array(3);
	this.ids[5] = Array(3);
	this.ids[6] = Array(4);
	for(i=0; i<this.ids.length; i++)
		for(j=0; j<this.ids[i].length; j++){ this.ids[i][j] = Array(); }
	this.keys = Array(7);
	this.keys[0] = Array(4);
	this.keys[1] = Array(1);
	this.keys[2] = Array(3);
	this.keys[3] = Array(3);
	this.keys[4] = Array(3);
	this.keys[5] = Array(3);
	this.keys[6] = Array(4);
	for(i=0; i<this.keys.length; i++)
		for(j=0; j<this.keys[i].length; j++){ this.keys[i][j] = Array(); }
}

ArrObj.prototype.add = function(key1, key2, x, y, id, infa){	//alert('id='+ id +'  x='+ x +'  y='+ y);
	var icon = new GIcon();
	icon.image = "images/mapicon/ic_"+ key1 +"_"+ key2 +".gif";
	icon.shadow = "images/mapicon/ic_"+ key1 +"_"+ key2 +".gif";
	icon.iconSize = new GSize(40, 26);
	icon.shadowSize = new GSize(40, 26);
	icon.iconAnchor = new GPoint(20, 13);
	icon.infoWindowAnchor = new GPoint(20, 13);
	var opts = {
		"icon": icon,
		"draggable": false
    }
    if(isAdmin) opts.draggable = true;
	var m = new GMarker(new GLatLng(y, x), opts);
	m.bindInfoWindowHtml(infa);
	this.map.addOverlay(m);
	if(flShowUserId){		m.openInfoWindowHtml(infa);
		flShowUserId = false;
	}
	this.a[key1][key2][ this.a[key1][key2].length ] = m;
	this.ids[key1][key2][ this.ids[key1][key2].length ] = id;
	this.keys[key1][key2][ this.keys[key1][key2].length ] = -1;
}

ArrObj.prototype.del = function(key1, key2, key3){
	this.map.removeOverlay( this.a[key1][key2][key3] );
	this.a[key1][key2][key3] = this.a[key1][key2][ this.a[key1][key2].length - 1 ]
	this.ids[key1][key2][key3] = this.ids[key1][key2][ this.ids[key1][key2].length - 1 ]
	var ret = {
		"skey" : this.keys[key1][key2][key3],
		"key1" : key1
	}
	this.keys[key1][key2][key3] = this.keys[key1][key2][ this.keys[key1][key2].length - 1 ]
	this.a[key1][key2].pop();
	this.ids[key1][key2].pop();
	this.keys[key1][key2].pop();
	return ret;
}

ArrObj.prototype.delOfId = function(id){
	for(var i=0; i<this.ids.length; i++)
		for(var j=0; j<this.ids[i].length; j++)
			for(var k=0; k<this.ids[i][j].length; k++){
				if(this.ids[i][j][k] == id){ return this.del(i, j, k); }
			}//for
}

ArrObj.prototype.moveOfId = function(id, x, y){
	for(var i=0; i<this.ids.length; i++)
		for(var j=0; j<this.ids[i].length; j++)
			for(var k=0; k<this.ids[i][j].length; k++){
				if(this.ids[i][j][k] == id){
					this.a[i][j][k].setLatLng( new GLatLng(y, x) );
					break;
				}
			}//for
}

ArrObj.prototype.setInfaOfId = function(id, infa){	for(var i=0; i<this.ids.length; i++)
		for(var j=0; j<this.ids[i].length; j++)
			for(var k=0; k<this.ids[i][j].length; k++){
				if(this.ids[i][j][k] == id){
					var ret = {
						"skey" : this.keys[i][j][k],
						"key1" : i,
						"key2" : j
					}
					this.a[i][j][k].bindInfoWindowHtml(infa);
					return ret;
				}
			}//for
}

ArrObj.prototype.hide = function(key1, key2){
	for(var i=0; i<this.a[key1][key2].length; i++){ this.a[key1][key2][i].hide(); }
}
ArrObj.prototype.show = function(key1, key2){
	for(var i=0; i<this.a[key1][key2].length; i++){	this.a[key1][key2][i].show(); }
}

ArrObj.prototype.showInfoWin = function(id){
	for(var i=0; i<this.ids.length; i++)
		for(var j=0; j<this.ids[i].length; j++)
			for(var k=0; k<this.ids[i][j].length; k++){
				if(this.ids[i][j][k] == id){
					this.a[i][j][k].openInfoWindowHtml('Этот маркер выбран');
					break;
				}
			}//for
}

ArrObj.prototype.setKeyStrOfId = function(id, key){
	for(var i=0; i<this.ids.length; i++)
		for(var j=0; j<this.ids[i].length; j++)
			for(var k=0; k<this.ids[i][j].length; k++){
				if(this.ids[i][j][k] == id){
					this.keys[i][j][k] = key;
					if(isAdmin){						this.a[i][j][k].enableDragging();
						GEvent.addListener(this.a[i][j][k], "dragend", function(){
							var l = a.a[ dragg.i ][ dragg.j ][ dragg.k ].getLatLng();
							sendXMLh(onMyLocDrag, "act=move&id="+ a.ids[ dragg.i ][ dragg.j ][ dragg.k ] +"&x="+ l.lng() +"&y="+ l.lat() );
						});
						var str = 'GEvent.addListener(this.a[i][j][k], "dragstart", function(){ dragg.i ='+ i +'; dragg.j='+ j +'; dragg.k='+ k +'; });';
						eval(str);
					}
					break;
				}
			}//for
}

ArrObj.prototype.compareIds = function(arr){
	var i=0; var j=0; var k=0;
	var buf = new Array();
	for(i=0; i<this.ids.length; i++)
		for(j=0; j<this.ids[i].length; j++)
			for(k=0; k<this.ids[i][j].length; k++) buf[buf.length] = this.ids[i][j][k];
	for(i=0; i<buf.length; i++)
		for(j=0; j<arr.length; j++)
			if(buf[i] == arr[j]){
				buf[i] = 0;
				arr[j] = 0;
				break;
			}
	var ret = {
		"news": new Array(),
		"dels": new Array()
	}
	for(i=0; i<buf.length; i++) if(buf[i] > 0) ret.dels[ret.dels.length] = buf[i];
	for(i=0; i<arr.length; i++) if(arr[i] > 0) ret.news[ret.news.length] = arr[i];
	return ret;
}

//объект для администрирования маркеров карты
CAdmStrs = function(div_el, aa_user, aa_shop, aa_road, en_filter, word_filter){
	this.idiv = div_el;
	this.current = 'shops';

	this.en_filt = en_filter;
	this.word_filt = word_filter;

	this.a_user = aa_user;
	this.a_shop = aa_shop;
	this.a_road = aa_road;

	this.shops = new Array();
	this.app_shops = new Array();
	this.upd_shops = new Array();
	this.infa_shops = new Array();

	this.roads = new Array();
	this.app_roads = new Array();
	this.upd_roads = new Array();
	this.infa_roads = new Array();

	this.users = new Array();
	this.app_users = new Array();
	this.upd_users = new Array();
	this.infa_users = new Array();

	this.fls = false;
	this.flr = false;
	this.flu = false;
}
CAdmStrs.prototype.addShop = function(id, vid, infa, en, edit){
	var i = this.shops.length;
	this.infa_shops[i] = infa;
	//var rg = new RegExp(divider, 'gi');
	//infa = infa.replace(rg, '; ');
	this.shops[i] = '<a href="javascript:a.showInfoWin('+ id +');">'+ vid +divider+ infa +'</a>&nbsp;</td><td width="31%">';
	if(edit){
		this.upd_shops[i] = '  <input type="button" value="Удалить" onclick="delMarker('+ id +')">  <input type="button" value="Редактировать" onclick="editMarker('+ id +','+ i +',\'shop\')">';
	}else this.upd_shops[i] = '';
	if(isAdmin==1 && en == 0) this.app_shops[i] = '  <input type="button" id="app_id_'+ id +'" value="Разрешить" onclick="approveMarker('+ id +','+ i +',\'shop\')">';
	else this.app_shops[i] = '';
 	a.setKeyStrOfId(id, i);
 	this.fls = true;
}
CAdmStrs.prototype.addRoad = function(id, vid, infa, en, edit){
	var i = this.roads.length;
	this.infa_roads[i] = infa;
	//var rg = new RegExp(divider, 'gi');
	//infa = infa.replace(rg, '; ');
	this.roads[i] = '<a href="javascript:a.showInfoWin('+ id +');">'+ vid +divider+ infa +'</a></td><td width="31%">';
	if(edit){
		this.upd_roads[i] = '  <input type="button" value="Удалить" onclick="delMarker('+ id +')">  <input type="button" value="Редактировать" onclick="editMarker('+ id +','+ i +',\'road\')">';
	}else this.upd_roads[i] = '';
	if(isAdmin==1 && en == 0) this.app_roads[i] = '  <input type="button" id="app_id_'+ id +'" value="Разрешить" onclick="approveMarker('+ id +','+ i +'\'road\')">';
	else this.app_roads[i] = '';
 	a.setKeyStrOfId(id, i);
 	this.flr = true;
}
CAdmStrs.prototype.addUser = function(id, vid, infa, en, edit){	var i = this.users.length;
	this.infa_users[i] = infa;
	//var rg = new RegExp(divider, 'gi');
	//infa = infa.replace(rg, '; ');
	this.users[i] = '<a href="javascript:a.showInfoWin('+ id +');">'+ vid +divider+ infa +'</a>&nbsp;</td><td width="31%">';
	if(edit){		this.upd_users[i] = '  <input type="button" value="Удалить" onclick="delMarker('+ id +')">  <input type="button" value="Редактировать" onclick="editMarker('+ id +','+ i +',\'user\')">';
	}else this.upd_users[i] = '';
	if(isAdmin==1 && en == 0) this.app_users[i] = '  <input type="button" id="app_id_'+ id +'" value="Разрешить" onclick="approveMarker('+ id +','+ i +'\'user\')">';
	else this.app_users[i] = '';
 	a.setKeyStrOfId(id, i);
 	this.flu = true;
}
CAdmStrs.prototype.updShop = function(id, vid, infa, key){
	this.infa_shops[key] = infa;
	//var rg = new RegExp(divider, 'gi');
	//infa = infa.replace(rg, '; ');
	this.shops[key] = '<a href="javascript:a.showInfoWin('+ id +');">'+ vid +divider+ infa +'</a>&nbsp;</td><td width="31%">';
 	this.fls = true;
}
CAdmStrs.prototype.updRoad = function(id, vid, infa, key){
	this.infa_roads[key] = infa;
	//var rg = new RegExp(divider, 'gi');
	//infa = infa.replace(rg, '; ');
	this.roads[key] = '<a href="javascript:a.showInfoWin('+ id +');">'+ vid +divider+ infa +'</a></td><td width="31%">';
 	this.flr = true;
}
CAdmStrs.prototype.updUser = function(id, vid, infa, key){
	this.infa_users[key] = infa;
	//var rg = new RegExp(divider, 'gi');
	//infa = infa.replace(rg, '; ');
	this.users[key] = '<a href="javascript:a.showInfoWin('+ id +');">'+ vid +divider+ infa +'</a>&nbsp;</td><td width="31%">';
 	this.flu = true;
}
CAdmStrs.prototype.delShop = function(key){
	if( key < 0 || key >= this.shops.length ) return;
	this.shops[key] = '';
	this.app_shops[key] = '';
	this.upd_shops[key] = '';
	this.infa_shops[key] = '';
 	this.fls = true;
}
CAdmStrs.prototype.delRoad = function(key){
	if( key < 0 || key >= this.roads.length ) return;
	this.roads[key] = '';
	this.app_roads[key] = '';
	this.upd_roads[key] = '';
	this.infa_roads[key] = '';
 	this.flr = true;
}
CAdmStrs.prototype.delUser = function(key){
	if( key < 0 || key >= this.users.length ) return;
	this.users[key] = '';
	this.app_users[key] = '';
	this.upd_users[key] = '';
	this.infa_users[key] = '';
 	this.flu = true;
}
CAdmStrs.prototype.approve = function(i, vid){
	if(vid == 'shop'){
		this.app_shops[i] = '';
	 	this.fls = true;
	}else if(vid == 'road'){
		this.app_roads[i] = '';
	 	this.flr = true;
	}else if(vid=='user'){
		this.app_users[i] = '';
	 	this.flu = true;
	}
}

CAdmStrs.prototype.show = function(vid){	if(vid == 'shops'){		this.current = vid;
		this.fls = true;
	}else if(vid == 'roads'){		this.current = vid;
		this.flr = true;
	}else if(vid == 'users'){		this.current = vid;
		this.flu = true;
	}
	this.draw();}

CAdmStrs.prototype.draw = function(){	var flag = true;
	var i=0; var s='';
	var buf = new Array();
	if(this.fls && this.current == 'shops'){		document.getElementById(this.a_user).style.background = colorHide;
		document.getElementById(this.a_shop).style.background = colorShow;
		document.getElementById(this.a_road).style.background = colorHide;
		if(myUserId <= 0) return;
		if(this.en_filt.checked == true){			var w = this.word_filt.value;
			w = '.*'+ w.replace(/ +/gi, '.*') +'.*';
			var rg = new RegExp(w, 'gi');
			for(i=0; i<this.shops.length; i++){
				w = this.shops[i] + this.app_shops[i] + this.upd_shops[i];
				if( w!='' && w.search(rg)>-1) buf[i] = '<tr><td>'+ w +'</td></tr>';
				else buf[i] = '';
			}
		}else{
			for(i=0; i<this.shops.length; i++){
				buf[i] = this.shops[i] + this.app_shops[i] + this.upd_shops[i];
				if(buf[i] != '') buf[i] = '<tr><td>'+ buf[i] +'</td></tr>';
			}
		}
		s = buf.join('');
		if(s.length >0) s = '<table cellspacing="0">'+ s  +'</table>'; else s = 'Нет добавленных точек';
		document.getElementById(this.idiv).innerHTML = s;
		this.fls = false;
	}else if(this.flr && this.current == 'roads'){
		document.getElementById(this.a_user).style.background = colorHide;
		document.getElementById(this.a_shop).style.background = colorHide;
		document.getElementById(this.a_road).style.background = colorShow;
		if(myUserId <= 0) return;
		if(this.en_filt.checked == true){
			var w = this.word_filt.value;
			w = '.*'+ w.replace(/ +/gi, '.*') +'.*';
			var rg = new RegExp(w, 'gi');
			for(i=0; i<this.roads.length; i++){
				w = this.roads[i] + this.app_roads[i] + this.upd_roads[i];
				if( w!='' && w.search(rg)>-1) buf[i] = '<tr><td>'+ w +'</td></tr>';
				else buf[i] = '';
			}
		}else{
			for(i=0; i<this.roads.length; i++){
				buf[i] = this.roads[i] + this.app_roads[i] + this.upd_roads[i];
				if(buf[i] != '') buf[i] = '<tr><td>'+ buf[i] +'</td></tr>';
			}
		}
		s = buf.join('');
		if(s.length >0) s = '<table cellspacing="0">'+ s  +'</table>'; else s = 'Нет добавленных точек';
		document.getElementById(this.idiv).innerHTML = s;
		this.flr = false;
	}else if(this.flu && this.current == 'users'){
		document.getElementById(this.a_user).style.background = colorShow;
		document.getElementById(this.a_shop).style.background = colorHide;
		document.getElementById(this.a_road).style.background = colorHide;
		if(myUserId <= 0) return;
		if(this.en_filt.checked == true){
			var w = this.word_filt.value;
			w = '.*'+ w.replace(/ +/gi, '.*') +'.*';
			var rg = new RegExp(w, 'gi');
			for(i=0; i<this.users.length; i++){
				w = this.users[i] + this.app_users[i] + this.upd_users[i];
				if( w!='' && w.search(rg)>-1) buf[i] = '<tr><td>'+ w +'</td></tr>';
				else buf[i] = '';
			}
		}else{
			for(i=0; i<this.users.length; i++){
				buf[i] = this.users[i] + this.app_users[i] + this.upd_users[i];
				if(buf[i] != '') buf[i] = '<tr><td>'+ buf[i] +'</td></tr>';
			}
		}
		s = buf.join('');
		if(s.length >0) s = '<table cellspacing="0">'+ s  +'</table>'; else s = 'Нет добавленных точек';
		document.getElementById(this.idiv).innerHTML = s;
		this.flu = false;
	}
}
CAdmStrs.prototype.isShop = function( key ){
	if( key>0 && key<6 ) return true;
	else return false;
}
CAdmStrs.prototype.isRoad = function( key ){
	if( key==6 ) return true;
	else return false;
}
CAdmStrs.prototype.isUser = function( key ){
	if( key==0 ) return true;
	else return false;
}

//семафор (глобальный буфер с блокировкой и разблокировкой)
CSemafor = function(id_msg){
	this.free = true;
	this.key1 = '';
	this.key2 = '';
	this.x = '';
	this.y = '';
	this.infa = '';
	this.id_msg = id_msg;
}
CSemafor.prototype.start = function(id, key1, key2, x, y, infa, msg){
	if(!this.free) return false;
	this.free = false;
	this.id = id;
	this.key1 = key1;
	this.key2 = key2;
	this.x = x;
	this.y = y;
	this.infa = infa;
	document.getElementById(this.id_msg).style.display = 'block';
	document.getElementById(this.id_msg).innerHTML = msg;
	return true;
}
CSemafor.prototype.end = function(){
	this.free = true;
	document.getElementById(this.id_msg).style.display = 'none';
}
