var VEHandler = {
	map: null, 
	flg: null, 
	layers:[null,null,null], 

	// Initialize
	init: function(){
		VEHandler.isCompatible();
		VEHandler.map = new VEMap('myMap');
		VEHandler.map.SetDashboardSize(VEDashboardSize.Tiny);
		VEHandler.map.LoadMap(VEConfig.preset.point, VEConfig.preset.zoom, VEConfig.preset.style);
		VEHandler.map.AttachEvent("onclick", VEHandler.clickListener);
		// Pushpin
		VEHandler.layers[0]=new VEShapeLayer();
		for(var i=0,l=VEConfig.pins.length;i<l;i++){
			var p = new VEShape(VEShapeType.Pushpin,VEConfig.pins[i].point);
			p.SetCustomIcon('img/flg'+(i+1).toString()+'.gif');
			VEHandler.layers[0].AddShape(p);
		}
		VEHandler.map.AddShapeLayer(VEHandler.layers[0]);
		// Polyline
		VEHandler.layers[1]=new VEShapeLayer();
		VEHandler.map.AddShapeLayer(VEHandler.layers[1]);
		// CurrentPin
		VEHandler.layers[2]=new VEShapeLayer();
		VEHandler.map.AddShapeLayer(VEHandler.layers[2]);
	},
	clickListener: function(e){
		if(!e.leftMouseButton) return;
		VEHandler.hideInfoWindow();
		if(e.elementID!=null){
			var id = e.elementID;
			if(id.substr(10,1)=='1') {
			var ids = id.slice(-2);
			if(ids.substr(0,1)=='0') var ids=ids.substr(1,1);
			//alert(ids);
			VEHandler.map.SetZoomLevel(3);
			VEHandler.showInfoWindow(ids);
			}
		}
	},
	clickListenerMove: function(idNum,flgNum){
			VEHandler.hideInfoWindow();
			var id = idNum;
			var ids =flgNum;
			VEHandler.map.SetZoomLevel(3);
			VEHandler.showInfoWindow(ids);
	},
	// CurrentPin
	showCurrentPin: function(){
		//var p = new VEShape(VEShapeType.Pushpin, VEHandler.map.GetCenter());
		//p.SetCustomIcon('images/pin'+(parseInt(VEHandler.flg)+1).toString()+'_.gif');
		//VEHandler.layers[2].AddShape(p);
		setTimeout('VEHandler.showInfoWindowContent();', 0);
	},
	hideCurrentPin: function(){
		VEHandler.layers[2].DeleteAllShapes();
	},

	// InfoWindow
	showInfoWindow: function(flg){
		VEHandler.hideInfoWindow();
		VEHandler.flg = flg;
		//alert(VEHandler.flg);
		VEHandler.showCurrentPin();
		//VEHandler.map.AttachEvent('onchangeview', VEHandler.showCurrentPin);
		VEHandler.map.SetCenter(VEConfig.pins[flg].point);
	},
	showInfoWindowContent: function(){
		var id='iw'+VEHandler.flg;
		$show(id);
		VEHandler.showSwf();
		//VEHandler.hideCurrentPin();
		VEHandler.map.DetachEvent('onchangeview', VEHandler.showCurrentPin);
	},
	hideInfoWindow: function(){
		if(VEHandler.flg==null) return;
		var id='iw'+VEHandler.flg;
		VEHandler.flg=null;
		if($(id).style.display=='block'){
			VEHandler.hideSwf();
			$hide(id);
		}
	},

	// SwfContent
	showSwf: function(){
		var so = new SWFObject('wd.swf', 'wd', '621px', '282px', '8', '#ffffff');
		so.addParam('menu', 'false');
		so.addParam('wmode', 'transparent');
		so.addVariable("iw", VEConfig.pins[VEHandler.flg].id);
		so.write('detail_flash'+VEHandler.flg);
	},
	hideSwf: function(){
//		$('wd.swf').removeChild();
	},

	// CurrentLine
	showCurrentLine: function(flg){
		var c = VEConfig.lines[flg];
		VEHandler.hideInfoWindow();
		VEHandler.hideCurrentLine();
		VEHandler.map.SetMapStyle(c.preset.style);
		VEHandler.map.SetCenterAndZoom(c.preset.point,c.preset.zoom);
		for(var i=0,l=c.point.length;i<l;i++){

			var p = new VEShape(VEShapeType.Pushpin,c.point[i]);
			p.SetCustomIcon('images/pin_'+c.preset.color+(i+1).toString()+'.gif');
			VEHandler.layers[1].AddShape(p);
		}
		var l = new VEShape(VEShapeType.Polyline,c.line);
		l.HideIcon();
		l.SetLineColor(new VEColor(255,0,0,1));
		l.SetLineWidth(3);
		VEHandler.layers[1].AddShape(l);
	},
	hideCurrentLine: function(){
		VEHandler.layers[1].DeleteAllShapes();
		VEHandler.map.SetCenterAndZoom(VEConfig.preset.point,VEConfig.preset.zoom);
		VEHandler.hideInfoWindow();
	},

	// Browser Detection (for IE6+, FF1.5+)
	isCompatible: function(){
		var u = navigator.userAgent;
		if(navigator.appName.toUpperCase().indexOf("MICROSOFT")!=-1){
			var s = u.indexOf("MSIE ",0) + 5;
			var e = u.indexOf(";",s);
			if(eval(u.substring(s,e))>=6) return;
		}else if(u.toUpperCase().indexOf("FIREFOX")!=-1){
			s = u.toUpperCase().indexOf("FIREFOX/",0);
			if(parseFloat(u.substring(s+8,s+8+3))>=1.5) return;
		}
		//window.location.href='attention.htm';
	}
};
