function SVMap(cfg){
	var _this=this;
	this.default_position = '';	// '39.908173,116.397947';
	this.enable_drag = false;
	this.search_showto = '';
	this.map = null;
	var latlng='';
	var latlng_bf='';
	var drag_id=0;
	var markers=[];
	var htmlDiv = "";
	var searchDrag = null;
	var drugname = '';
	this.load_accessed = 0;		//是否加载授权视频点
	this.load_accessed_flags = '';	//如果加载授权视频点，授权标记，如'play,schedule'或'play'
	this.drags_loadcomplete = function(){};
	this.drag_click = function(id, title){};
	this.renderto = '';
	//是否公开
	_this.is_pub=false;
	var iconImage = "images/map/device1.gif";
	var iconCurImage = "images/map/device3.gif";
	
	//地图位置
	if (Cfg(cfg,'default_position','')!='') {
		_this.default_position = Cfg(cfg,'default_position','39.908173,116.397947');
	}
	else{
		if (typeof(g_map_default_position)=='string' && g_map_default_position.length>0) {
			_this.default_position = g_map_default_position;
		}
		else{
			_this.default_position = '39.908173,116.397947';
		}
	}
	if (_this.default_position.length>0) {
		_this.default_position = _this.default_position.split(',');
	}
	
		
	//初始化地图
	_this.initialize=function() {
		if (GBrowserIsCompatible()) {
			GDraggableObject.setDraggableCursor('crosshair');
			_this.map = new GMap2(document.getElementById(_this.renderto));
			//_this.map.setCenter(new GLatLng(39.908173,116.397947),MAP_ZOOM);　 北京
			//_this.map.setCenter(new GLatLng(30.65741,104.06471),MAP_ZOOM);	成都
			_this.map.setCenter(new GLatLng(_this.default_position[0],_this.default_position[1]),MAP_ZOOM);
			
			var customUI = _this.map.getDefaultUI();
			customUI.maptypes.hybrid = false;
			_this.map.setUI(customUI);

			geocoder = new GClientGeocoder();
			gLocalSearch = new google.search.LocalSearch();
			gLocalSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
			if(_this.is_pub)
				pubOpr();
			else
				ajaxOpr();
		}
	}
	var initidrag=function(drags){
		for(var i=0; i<drags.length;i++){
			str = drags[i]['latlng'].split(',');

			lat =parseFloat(str[0].substring(1));
			lng =parseFloat(str[1].substring(0,str[1].length-1));

			point = new GLatLng(lat,lng);
			var obj = document.createElement("input");
			obj.type='hidden';
			obj.value=drags[i]['drag_name'];
			obj.id = drags[i]['camera_id'];
			var j = drags[i]['camera_id'];
			//document.getElementById('drag1').appendChild(obj);
			addDrags(point,obj,j);
		}
		_this.drags_loadcomplete();
	}
	var clearDragId = function(){
		///alert(markers[drag_id]);
		if(drag_id != 0){
			markers[drag_id].disableDragging();
			markers[drag_id].setImage(iconImage);
			drag_id = 0;
		}
	}
	var addDrags = function(point,obj,i){
		var cusIcon = new GIcon(G_DEFAULT_ICON);
		cusIcon.image = iconImage;
		cusIcon.iconSize = new GSize(38.4,28);
		markers[i]= new GMarker(point,{draggable:true,title:obj.value,icon:cusIcon});
		markers[i].id=obj.id;
		markers[i].disableDragging();
		GEvent.addListener(markers[i], "click", function() {
			//html = "<div><img src = '"+iconImage+"' width = '39px' height = '31px' /><div style='margin-top:10px;margin-left:40px;'><font style='font-size:14px;color:#097286'><b>"+obj.value+"</b></font></div></div>";
			markers[i].openInfoWindowHtml(obj.value);
			_this.drag_click(i, obj.value);
		});
		GEvent.addListener(markers[i], "dragstart", function() {
			latlng_bf = markers[i].getLatLng();
			markers[i].closeInfoWindow();
		});

		GEvent.addListener(markers[i], "dragend", function(){
			latlng = markers[i].getLatLng();
			modifyDrag(latlng);
		});
		_this.map.addOverlay(markers[i]);
	}
	//查找详细地址
	var setCenterPoint = function (lat,lng){
		point = new GLatLng(lat,lng);
		if(searchDrag)
		_this.map.removeOverlay(searchDrag);
		searchDrag =new GMarker(point);
		_this.map.addOverlay(searchDrag);
		_this.map.setCenter(point, MAP_ZOOM);
	}
	this.showAddress = function(address){
		if (_this.search_showto!='') {
			gLocalSearch.execute(address);
			gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);
		}
		else{
			Ext.QuickMsg.msg('map','没有指定搜索结果显示的容器');
		}
	}
	var OnLocalSearch = function() {
		if (!gLocalSearch.results) return;

		// Clear the map and the old search well
		document.getElementById(_this.search_showto).innerHTML = "";
		gCurrentResults = [];
		for (var i = 0; i < gLocalSearch.results.length; i++) {
			gCurrentResults.push(new LocalResult(gLocalSearch.results[i]));
		}

		var attribution = gLocalSearch.getAttribution();
		if (attribution) {
			document.getElementById(_this.search_showto).appendChild(attribution);
		}
	}
	var LocalResult =  function(result) {
		resultNode = unselectedHtml(result);
		document.getElementById(_this.search_showto).appendChild(resultNode);
	}
	// Returns the HTML we display for a result before it has been "saved"
	var unselectedHtml = function(result) {
		var container = document.createElement("div");//创建容器
		var obj = document.createElement("a");
		var conAddress = document.createElement("div");//具体地址容器
		obj.setAttribute("href","javascript:void(0)");//显示结果
		if(result.titleNoFormatting==result.streetAddress){
			var txttitle =  document.createTextNode((result.titleNoFormatting).replace("中国",""));
			var txtAddress = document.createTextNode(result.streetAddress);//具体地址
		}
		else{
			var txttitle =  document.createTextNode(result.titleNoFormatting);
			var txtAddress = document.createTextNode(result.region+result.city+result.streetAddress);//具体地址
		}
		obj.style.cssText ="font-size:16px;color:#097286";
		obj.appendChild(txttitle);
		container.appendChild(obj);
		conAddress.appendChild(txtAddress);
		container.appendChild(conAddress);

		Ext.get(obj).on("click", function(){
			setCenterPoint(parseFloat(result.lat),parseFloat(result.lng));
		});
		return container;
	}

	var addNew = function(drags){
		//alert(id);
		str = drags['latlng'].split(',');

		lat =parseFloat(str[0].substring(1));
		lng =parseFloat(str[1].substring(0,str[1].length-1));

		point = new GLatLng(lat,lng);
		//drage = drags[i]['latlng_now'];
		var obj = document.createElement("input");
		obj.type='hidden';
		obj.value=drags['drag_name'];
		obj.id = drags['Id'];
		i = drags['Id'];
		//document.getElementById('drag1').appendChild(obj);
		addDrags(point,obj,i);
		//alert(drags['drag_name']);
		markers[i].enableDragging();
		markers[i].setImage(iconCurImage);
		drag_id = drags['Id'];
	}

	//添加标记点
	this.dragAdd = function(camera_id, camera_title){
		if (checkDragExists(camera_id))
		Ext.Msg.show({title:'您好', msg:'您已经标定了此设备', buttons: Ext.Msg.OK, minWidth:300, icon:Ext.MessageBox.INFO});
		else{
			showEdit(null,camera_title);
			GEvent.addListener(_this.map, "click", function(overlay, latlng){
				if (latlng != null) {
					ajaxOprAdd(camera_id, drugname, latlng);
					GEvent.clearListeners(_this.map, "click");
				}
			});
		}
		//searchByCameraId();
	}
	//删除标记点
	this.dragDel = function(camera_id){
		if (!checkDragExists(camera_id))
		Ext.QuickMsg.msg('', '请选择需要删除标定的视频点');
		else
		delDrag(camera_id);
		//searchByCameraId();
	}

	_this.setCurDragId = function(camera_id, camera_title){
		if(drag_id != 0) {
			if(camera_id != drag_id){
				markers[drag_id].disableDragging();
				markers[drag_id].setImage(iconImage);
			}
		}
		
		if (!checkDragExists(camera_id)) {
			drag_id = 0;
			return false;
		}

		drag_id = camera_id;

		if(_this.is_pub || !this.enable_drag)
		markers[drag_id].disableDragging();
		else
		markers[drag_id].enableDragging();

		_this.map.setCenter(markers[drag_id].getLatLng(), MAP_ZOOM);
		markers[drag_id].setImage(iconCurImage);
		title = markers[drag_id].getTitle();
		markers[drag_id].openInfoWindowHtml(title);
		if(typeof(resetCen)!='undefined'&&resetCen) resetCenter();
	}
	
	var resetCenter = function(){
		var newlat = _this.map.getCenter().lat();
		var newlng = _this.map.getCenter().lng()-1;
		_this.map.panTo(new GLatLng(newlat,newlng));
	}
	var checkDragExists = function(id) {
		var exists = false;
		try{
			//markers[id].getIcon();
			if(!markers[id].isHidden()) exists = true;
		}
		catch(e){}
		return exists;
	}
	var addOption = function(optname){
		var div_list = document.getElementById('google_daohang');
		var list = document.createElement('option');
		var item = document.createElement("li");
		item.appendChild(document.createTextNode(optname));
		list.appendChild(item);
		div_list.appendChild(list);
	}
	var deleteDrag = function(){
		//_this.map.removeOverlay(markers[drag_id]);
		markers[drag_id].hide();
		markers[drag_id].closeInfoWindow();
		drag_id=0;
	}
	this.getDrag = function(id){
		if(checkDragExists(id))
		return markers[id];
		else
		return false;
	}
	//初始化地图标记
	var ajaxOpr = function(){
		Ext.Ajax.request({
			url: g_root+'com/google_map/map_server.php',
			params: 'opr=display&load_accessed='+_this.load_accessed+'&load_accessed_flags='+_this.load_accessed_flags,
			success: function(response, options){
				try{
					//alert(qpDHanzi(response.responseText));
					var rjdata = Ext.util.JSON.decode(response.responseText);
					//alert('ceshiceshi');
					if (rjdata.success=='true') {
						//alert((rjdata.data)[0]['Id']);
						initidrag((rjdata.data));
					}
					else{
						//Ext.Msg.show({title:'错误', msg:qpDHanzi(rjdata.data), buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING});
					}
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
			},
			failure: function(){
				//alert('意外错误，请与管理员联系');
			}
		});
		// return drag;
	}
	var ajaxOprAdd = function(camera_id, camera_title, latlng){
		showWaitWindow();
		Ext.Ajax.request({
			url: g_root+'com/google_map/map_server.php',
			params: 'opr=add'+'&latlng='+latlng+'&camera_id='+camera_id+'&dra_name='+camera_title,
			success: function(response, options){
				hideWaitWindow();
				try{
					var rjdata = Ext.util.JSON.decode(response.responseText);
					//alert(rjdata.success);
					if (rjdata.success=='true') {
						//alert('guaiguai');
						Ext.QuickMsg.msg('成功','视频点位置已标定');
						//alert('sucssce');
						addNew(rjdata.data);
					}
					else{
						Ext.Msg.show({title:'错误', msg:rjdata.data, buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING, minWidth:300});
					}
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
			},
			failure: function(response, options){
				hideWaitWindow();
				Ext.Msg.show({title:'意外错误', msxtg:qpDHanzi(response.respresponseTexton), buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING, minWidth:300});
			}
		});
	}
	var ajaxOprSearch = function(id){
		Ext.Ajax.request({
			url: g_root+'com/google_map/map_server.php',
			params: 'camera_id='+ id+'&opr=searchid',
			success: function(response, options){
				try{
					var rjdata = Ext.util.JSON.decode(response.responseText);
					//alert(rjdata.success);
					if (rjdata.success=='true') {
						//alert('guaiguai');
						Ext.QuickMsg.msg('成功','添加成功' );
						//alert(rjdata.data);
						//addNew(rjdata.data);
						showDrag();
					}
					else{
						Ext.Msg.show({title:'错误', msg:qpDHanzi(rjdata.data), buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING});
					}
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
			},
			failure: function(){
				//alert('意外错误，请与管理员联系');
			}
		});
	}
	var searchByCameraId = function(){
		Ext.Ajax.request({
			url: g_root+'com/google_map/map_server.php',
			params: 'camera_id='+ curNode.id+'&opr=searchByCameraId',
			success: function(response, options){
				try{
					//alert('ceshi');
					var rjdata = Ext.util.JSON.decode(response.responseText);
					//	alert('ceshiceshi');
					if (rjdata.success=='true') {
						//Ext.Msg.show({title:'您好',msg:'您已经标定了改设备',buttons: Ext.Msg.OK});
						setCurDragId(rjdata.data);
					}
					else
					{
						clearDragId();
					}
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
			},
			failure: function(){
				//alert('意外错误，请与管理员联系');
			}
		});
		// return drag;
	}
	var modifyDrag = function(latlng){
		showWaitWindow();
		Ext.Ajax.abort();
		Ext.Ajax.request({
			url: g_root+'com/google_map/map_server.php',
			params: 'drag_id='+ drag_id + '&opr=modify' + '&latlng=' +latlng,
			success: function(response, options){
				hideWaitWindow();
				try{
					var rjdata = Ext.util.JSON.decode(response.responseText);
					if (rjdata.success=='true') {
						Ext.QuickMsg.msg('成功','视频点位置已重新标定');
					}
					else{
						Ext.Msg.show({title:'错误', msg:rjdata.data, buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING, minWidth:300});
					}
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
			},
			failure: function(response, options){
				hideWaitWindow();
				Ext.Msg.show({title:'意外错误', msxtg:qpDHanzi(response.respresponseTexton), buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING, minWidth:300});
			}
		});
		// return drag;
	}
	var delDrag = function(){
		showWaitWindow();
		Ext.Ajax.request({
			url: g_root+'com/google_map/map_server.php',
			params: 'drag_id='+ drag_id + '&opr=del',
			success: function(response, options){
				hideWaitWindow();
				try{
					var rjdata = Ext.util.JSON.decode(response.responseText);
					if (rjdata.success=='true') {
						deleteDrag();
						Ext.QuickMsg.msg('成功','视频点已取消标定');
					}
					else{
						Ext.Msg.show({title:'错误', msg:rjdata.data, buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING, minWidth:300});
					}
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
			},
			failure: function(response, options){
				hideWaitWindow();
				Ext.Msg.show({title:'意外错误', msxtg:qpDHanzi(response.responseText), buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING, minWidth:300});
			}
		});
	}
	var pubOpr = function(){
		Ext.Ajax.request({
			url: g_root+'com/google_map/map_server.php',
			params: 'opr=pub',
			success: function(response, options){
				try{
					var rjdata = Ext.util.JSON.decode(response.responseText);
					if (rjdata.success=='true') {
						initidrag((rjdata.data));
					}
					else{
						//Ext.Msg.show({title:'错误', msxtg:'暂无公开设备展示', buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING});
						//Ext.QuickMsg.msg('提示','暂无公开设备展示');
					}
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
			},
			failure: function(){
				//alert('意外错误，请与管理员联系');
			}
		});
		// return drag;
	}

	_this.ipToPlace = function(addre){
		//if (geocoder) {
		//	geocoder.getLatLng(addre,function(point){
		//		if (point){
		//			_this.map.setCenter(point, MAP_ZOOM);
		//		}
		//	});
		//}
	}
	var showEdit = function(animobj,da){

		if (_this.editform) {_this.editform.destroy(); _this.editform=null;}
		if (!_this.editform) {
			_this.editform = new Ext.form.FormPanel({
				id: 'edit_form', labelWidth:50,
				autoHeight:true, frame:true,  bodyBorder:false, border:false,
				bodyStyle: 'padding:0px',
				items: [{xtype:'textarea',name:'Name',value:'',hideLabel:true,width:300,height:100}
				]
			});
		}

		if (_this.editWindow) {_this.editWindow.close(); _this.editWindow.destroy(); _this.editWindow=null;}
		if (!_this.editWindow) {
			_this.editWindow = new Ext.Window({
				closeAction: 'close', layout: "fit", modal: true, plain: true, resizable: false, header: true,
				title:'添加标记名称',
				closable: true, shadow: true,
				autoHeight: true, width: 325,
				bodyStyle: 'padding:0px',
				border: false,
				items: _this.editform,
				buttons:[
					{text:'确定', handler:function(){doedit();}}
					,{text:'取消', handler:function(){_this.editWindow.close();}}
				],
				keys:[{
					key:Ext.EventObject.ENTER,
					fn:doedit,
					scope:this
				}]
			});
		}
		_this.editWindow.show(animobj);

		//清除值
		//huiyuanItems.reset();

		//设置初始值
		if (da) {
			_this.editform.getForm().findField('Name').setValue(da);
		}
	};

	var doedit = function(){
		//if (!huiyuanItems.isValid()) return false;
		if (!_this.editform.getForm().isDirty()) {
			_this.editform.destroy(); _this.editform=null;
			_this.editWindow.close(); _this.editWindow.destroy(); _this.editWindow=null;
			return false;
		}
		drugname = _this.editform.getForm().getValues(true);
		drugname = drugname.substring(5);
		_this.editWindow.close();
	}
	
	//显示等待窗口
	var waitWindow = null;
	var showWaitWindow = function(msgstr){
		if (waitWindow) {waitWindow.hide(); waitWindow = null;}
		if (!waitWindow) {
			if (typeof(msgstr)=='undefined') msgstr = '请稍候...';
			waitWindow = new Ext.LoadMask(Ext.getBody(), {msg:msgstr, removeMask:true});
		}
		waitWindow.show();
	};
	//关闭等待窗口
	var hideWaitWindow = function(){
		if (waitWindow) waitWindow.hide();
	};
}