/// <reference path="http://www-app.gdepb.gov.cn/ext-2.2/vswd-ext_2.1.js" />

Ext.namespace('EQPublish');

EQPublish.RaqiViewport = Ext.extend(Ext.Viewport, {
	initComponent: function() {
		Ext.apply(this, {
			layout: 'border',
			border: false,
			defaults: { border: true },
			items: [
				{ id: 'north', region: 'north', contentEl: 'hd', height: '160', border: false },
				{
					id: 'leftPanel', title: '粤港珠三角空气质量', region: 'west', width: 290, split: true,
					collapsible: true, collapseMode: 'mini', layout: 'border',
					items: [
						{
							id: 'raqipanel', layout: 'fit', xtype: 'RaqiPanel', region: 'center', bbar: ['－：表示仪器维护中',  { id: 'tbLevelDescription', text: '<image src="Images/RaqiLegend.gif"/>', tooltip: '点击查看区域空气质量指数RAQI详细说明', handler: this.ShowLevelDescription}]
						},
						{
							id: 'legend', title: '发布单位&nbsp;&nbsp;&nbsp;<a href="http://www-app.gdepb.gov.cn/raqi3/RAQI_cht.htm" target="_blank" >[繁体版]</a>&nbsp;&nbsp;&nbsp;<a href="http://www-app.gdepb.gov.cn/raqi3/RAQI_en.htm" target="_blank" >[英文版]</a>', height: 100, html: '<center><a href="http://www.epd.gov.hk/epd/" target="_blank" ><img src="Images/logo_hkepa1.jpg"/></a><img src="Images/logo_jcz.jpg"/></center><hr/><span style="font-size:12px;">&nbsp;&nbsp;下载:<a href="http://www.gdep.gov.cn/hjjce/hjxt/201010/t20101021_114994.html" target="_blabk">相关历史报告</a></span>',
							region: 'south', collapsible: false, titleCollapse: false, autoScroll: false
						}
					]
				},
				{ xtype: 'vepanel', id: 'vemap', region: 'center' },
				{ id: 'south', region: 'south', contentEl: 'ft', collapsible: false, border: false }
			]
		});
		EQPublish.RaqiViewport.superclass.initComponent.call(this);
	},
	initEvents: function() {
		/// 这里注册事件绑定
		Ext.getCmp('raqipanel').on('timeChange', this.onCondiChange, this);
		Ext.getCmp('raqipanel').on('rowclick', this.onRowSelect, this);
	},
	afterRender: function() {
		EQPublish.RaqiViewport.superclass.afterRender.call(this);
		this.initEvents();
		/// 这里加载页面默认的数据
	},
	onRowSelect: function(thisGird, rowIndex, evObj) {
		var Longitude = thisGird.store.getAt(rowIndex).get('Longitude');
		var Latitude = thisGird.store.getAt(rowIndex).get('Latitude');
		var title = thisGird.store.getAt(rowIndex).get('PointNameCHS');
		Ext.getCmp('vemap').setMapPosition(Latitude, Longitude);
		var item = {
			id: 'marker',
			lat: Latitude + 0.002,
			lon: Longitude + 0.002,
			icon: '/EQPublish/Images/MapIcon/marker.gif',
			title: title
		}
		Ext.getCmp('vemap').deleteIcon('marker');
		Ext.getCmp('vemap').addIcon(item, true);
	},
	onCondiChange: function(thisGrid, condiObj) {
		//条件改变，走此函数，条件状态在condiObj中。在此加载地图
		if (condiObj.isShowPoint) {
			function onSuccess(request, options) {
				var result = Ext.decode(request.responseText);
				if (result.Success) {
					if (result.Data.length > 0 && condiObj.isShowPoint) {
						Ext.getCmp('vemap').addIcon.defer(400, Ext.getCmp('vemap'), [result.Data]);
					}
					else {
						Ext.getCmp('vemap').cleanIcon();
					}

				}
				else {
					//Ext.MessageBox.alert('', result.ErrorMessage);
					return false;
				}
			}
			function onFailure(response, options) { }

			var param = { d: condiObj.Date };
			Ext.Ajax.request({
				url: this.url,
				params: { op: 'QueryRaqiIcon', param: Ext.encode(param) },
				success: onSuccess,
				failure: onFailure
			});
		}
		else {
			Ext.getCmp('vemap').cleanIcon();
		}
   	        if(Ext.getCmp('vemap').map.GetTileLayerByID("Raqi"))  Ext.getCmp('vemap').map.DeleteTileLayer("Raqi"); 
		if (condiObj.isShowIsoline) {
			Ext.getCmp('vemap').addLayer("Raqi",'http://'+ window.location.hostname + "/EQPublish/Handlers/RaqiMapHandler.ashx?layername=IssueWWW_PureMercator.png&tilename=%4&time="+ condiObj.Date, 0.6, 2);
		}
	},
	ShowLevelDescription: function() {
		var win;
		if (!win) {
			win = new Ext.Window({
				contentEl: 'divRAQIDescription',
				layout: 'fit',
				width: 600,
				height: 450,
				closeAction: 'hide',
				plain: true,
				title: '区域空气质量指数RAQI说明',
				html: '<iframe src="images/html/RAQIDescription_zh.htm" style="width:100%;height:100%" frameborder="no"></iframe>'
			});
		}
		win.show();
	}
});
