﻿/*
地標廣告
UAD
opts = {label:'', html:'', width:0, height:0, latlng:new ULatLng(), icon:new UIcon()};
*/
var UAD = function(_UMAP) {
	var self = this;
	var queryUrl = '';
	var overlayManager;
	var ads = [
		{label: '鵬程旅行社',
		description: '地址：台北市林森北路575號2樓之1(201-2室)<br />電話：(02)25972558',
		html: '<div style="width:280px;font-size:13px !important;font-size:12px;margin:0 auto;"><p style="margin:0;"><a href="http://www.pcgogo.com.tw" target="_blank"><img border="0" src="adimg/pcgogo02.jpg" width="280" height="165"></a></p><p style="margin:13px 0;text-align:center;">專業創新‧品質第一‧價格最優‧服務用心</p><p style="text-align:center;"><font color="#0000FF">鵬程旅行社</font>&nbsp;<a href="http://www.pcgogo.com.tw" target="_blank">www.pcgogo.com.tw</a></font></p></div>', 
		width: 300,
		height: 230,
		latlng: 'BBCJhaFFCgFaHgjcg',
		image: 'adimg/pcgogo01icon.png', 
		iconWidth: 43, 
		iconHeight: 19}
	];
	this.setZoomlevel = function() {
		
	};
	this.add = function(opts) {
		if (!overlayManager) return;
		var opts = opts || {label:'', html:'', width:0, height:0, latlng:null, icon:null};
		/*var showInfoWindow = function() {
			if (opts.width && opts.height)
				this.openInfoWindow(opts.html, opts.width, opts.height);
			else
				this.openInfoWindow(opts.html);
		};*/
		var showInfoWindow = function() {
			var infowindowOptions = new UInfoWindowOptions();
			if (opts.width && opts.height) {
				infowindowOptions.width = opts.width;
				infowindowOptions.height = opts.height;
			}
			infowindowOptions.content = opts.html;
			infowindowOptions.themeStyle = 'blue';
			this.openInfoWindow(infowindowOptions);
		};
		if (opts.latlng) {
			var marker = new UMarker(opts.latlng, opts.icon);
			if (opts.label) marker.setMouseonInfo(opts.label, opts.description);
			//if (opts.icon) marker.setIcon(opts.icon);
			marker.addListener('click', showInfoWindow);
			overlayManager.add(marker);
			overlayManager.refresh();
		}
	};
	this.remove = function(index) {
		if (!overlayManager) return;
		if (index)
			overlayManager.remove(index);
		else
			overlayManager.remove();
	};
	this.importData = function() {
		//$.post(queryUrl, {}, self.importCallback);
		var len = ads.length;
		for (var i=0; i<len; i++) {
			var latlng = null;
			if (ads[i].latlng)
				latlng = new ULatLng(ads[i].latlng, 'DES');
			var icon = null;
			if (ads[i].image && ads[i].iconWidth && ads[i].iconHeight) {
				var iconWidth = parseInt(ads[i].iconWidth);
				var iconHeight = parseInt(ads[i].iconHeight);
				icon = new UIcon({
							image: ads[i].image,
							iconWidth: iconWidth,
							iconHeight: iconHeight,
							iconAnchor: new UPoint(iconWidth/2, iconHeight),
							infoWindowAnchor: new UPoint(iconWidth/2, 0)
						});
			}
			var opts = {
				label: decodeURI(ads[i].label),
				description: decodeURI(ads[i].description),
				html: ads[i].html,
				width: parseInt(ads[i].width),
				height: parseInt(ads[i].height),
				latlng: latlng,
				icon: icon
			};
			self.add(opts);
		}
	};
	this.importCallback = function(json) {
		if (typeof json == 'string') 
			eval('json = ' + json);
		var len = json.length;
		for (var i=0; i<len; i++) {
			var latlng = null;
			if (json[i].latlng)
				latlng = new ULatLng(json[i].latlng, 'DES');
			var icon = null;
			if (json[i].image && json[i].iconWidth && json[i].iconHeight) {
				var iconWidth = parseInt(json[i].iconWidth);
				var iconHeight = parseInt(json[i].iconHeight);
				icon = new UIcon({
							image: json[i].image,
							iconWidth: iconWidth,
							iconHeight: iconHeight,
							iconAnchor: new UPoint(iconWidth/2, iconHeight),
							infoWindowAnchor: new UPoint(iconWidth/2, 0)
						});
			}
			var opts = {
				label: decodeURI(json[i].label),
				description: decodeURI(json[i].description),
				html: json[i].html,
				width: parseInt(json[i].width),
				height: parseInt(json[i].height),
				latlng: latlng,
				icon: icon
			};
			self.add(opts);
		}
	};
	(function(){
		if (!_UMAP) return;
		overlayManager = new UOverlayManager(_UMAP, {
			minLevel: 9,
			maxLevel: 10,
			autoMergeMarker: true
		});
	})();
};