﻿
// Google Map API

/* PGMarker */
function PGMarker(latlng, imgUrl, imgWidth, imgHeight) {
	this._latlng = latlng;
	this._imgUrl = imgUrl;
	this._imgWidth = imgWidth;
	this._imgHeight = imgHeight;
}
PGMarker.prototype = new GOverlay();

PGMarker.prototype.initialize = function(map) {
	this._map = map;
	var self = this;
	
	var pane = map.getPane(G_MAP_MARKER_PANE);
	this._div = document.createElement("div");
	var s = this._div.style;
	s.border = "1px solid #FFFFFF";
	s.width = (this._imgWidth) + "px";
	s.height = (this._imgHeight) + "px";
	s.padding = "1px";
	s.position = "absolute";
	s.cursor = "pointer";
	s.zIndex = GOverlay.getZIndex(this._latlng.lat());
	var img = document.createElement("img");
	img.src = this._imgUrl;
	img.width = this._imgWidth;
	img.height = this._imgHeight;
	this._div.appendChild(img);
	pane.appendChild(this._div);
	GEvent.addDomListener(this._div, "click", function(){
		GEvent.trigger(self, "click", 0);
	});
	GEvent.addDomListener(this._div, "mouseover", function(){
		s.border = "2px solid #FF0000";
	});
	GEvent.addDomListener(this._div, "mouseout", function(){
		s.border = "0px solid #FFFFFF";
	});
};

PGMarker.prototype.remove = function(map) {
	this._div.parentNode.removeChild(this._div);
	GEvent.clearInstanceListeners(this._div);
};

PGMarker.prototype.copy = function() {
	return new PGMarker(this._latlng);
};

PGMarker.prototype.redraw = function(force) {
	if (!force)
		return;

	var c = this._map.fromLatLngToDivPixel(this._latlng);
	var s = this._div.style;
	s.left = (c.x - this._imgWidth / 2 - 1) + "px";
	s.top = (c.y - this._imgHeight / 2 - 1) + "px";
};
PGMarker.prototype.showAtTop= function() {
	this._div.style.zIndex = 999999;
}

PGMarker.prototype.openInfoWindowHtml = function(content) {
	this._map.openInfoWindowHtml(this._latlng, content);
}
/* end PGMarker */

function PSwitchControl() {}   
PSwitchControl.prototype = new GControl();   
// 起始   
PSwitchControl.prototype.initialize = function(map) {   
    var container = document.createElement("div");
    container.style.width = "50px";
    container.appendChild(this.createButton("用户", 0));
    container.appendChild(this.createButton("照片", 1));
    map.getContainer().appendChild(container);   
    return container;   
}   
// 預設位置   
PSwitchControl.prototype.getDefaultPosition = function() {   
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(5, 30));   
}   
// 建立按鈕   
PSwitchControl.prototype.createButton = function(name, style) {   
    var div = document.createElement("div");
    this.items[name] = div;
    this.setButtonStyle(div);    
    div.appendChild(document.createTextNode(name));
    GEvent.addDomListener(div, "click", function() {   
        location.href = "?style=" + style;
    });
    return div;
}
// 設定按鈕樣式   
PSwitchControl.prototype.setButtonStyle = function(button) {   
    button.style.textDecoration = "underline";    
    button.style.font = "small Arial";   
    button.style.padding = "2px";    
    button.style.width = "50px";
    button.style.border = "1px solid #000000";
    button.style.color = "#000000";
    button.style.backgroundColor = "#FFFFFF";
    button.style.textAlign="center";
    //button.style.styleFloat = "left";
    //button.style.marginRight = "10px";
    button.style.marginTop = "3px";
    button.style.cursor = "pointer";    
}
PSwitchControl.prototype.items = { };

PSwitchControl.prototype.setCurrentItem = function(name){
    if(this.items[name])
        this.items[name].style.fontWeight = "bold";
}

function P960Map()
{
	var selfObj = this;
	
	this.canvas = this.cmd = this.userId = this.dragging =  false;
	this.loadMapType = this.loadLargeMap = true;
	this.mapType = G_NORMAL_MAP;
	this.gmap = null;
	this.geocoder = null;
	this.canvas = arguments[0];
	this.zoomLevel = 12;
	this.uids = new ListBuilder(",");
	this.pids = new ListBuilder(",");
	this.loginId = 0;
	
	if(typeof(arguments[1]) == "string")this.cmd = arguments[1];
	if(typeof(arguments[2]) == "number")this.userId = arguments[2];
	this.mapDragend = function(){
	    if(selfObj.cmd == "photo" || selfObj.cmd == "user")
            selfObj.loadPhotos(selfObj.gmap.getBounds());
	};
	
	this.loadPhotos = function(bounds){
		var sw = bounds.getSouthWest(), ne = bounds.getNorthEast();
		var min_x = sw.lng(), max_x = ne.lng(), min_y = sw.lat(), max_y = ne.lat();
	    var masterIcon = new GIcon();
		masterIcon.iconSize = new GSize(16, 16);
		masterIcon.iconAnchor = new GPoint(16, 16);
		masterIcon.infoWindowAnchor = new GPoint(16, 0);
		var url = sitePath + "tools/mapserver.aspx?cmd=" + selfObj.cmd + "&userid=" + selfObj.userId + (selfObj.cmd=="user" && selfObj.userId>0 ? "&type=friend":"");
		url += "&size=100&minlat="+min_y+"&minlon="+min_x+"&maxlat="+max_y+"&maxlon="+max_x+"&ids=";
//		if(selfObj.cmd == "user")
//			url += escape(selfObj.uids.value);
//		else
//			url += escape(selfObj.pids.value);
		url += "&"+Math.random();
		var dom = createXmlDocument(url);
		var imgroot = dom.documentElement;
		var items = imgroot.selectNodes("item");
		var marker = null;
		var ety = null,ids = null;
		for(var m = 0; m < items.length; m++)
		{
		    if(selfObj.cmd == "photo")
			{
			    ety = new PhotoInfo(items[m]);
				ids = selfObj.pids;
			}
			else if(selfObj.cmd == "user")
			{
			    ety = new UserInfo(items[m]);
				ids = selfObj.uids;
			}
			if(!ids.exists(ety.id))
			{
				ids.add(ety.id);
				marker = selfObj.createMarker( ety);
				selfObj.addOverlay(marker);
				if(selfObj.loginId == ety.id && selfObj.cmd == "user")
				{
					marker.openInfoWindowHtml(ety.infoHtml);
					marker.showAtTop();
					marker._div.style.border = "2px solid #FF0000";
				}
			}
		}		
	}
	
	this.createMarker = function(baseInfo) {
		var marker = new PGMarker(new GLatLng(parseFloat(baseInfo.lat), parseFloat(baseInfo.lon)), baseInfo.img, 32, 32);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(baseInfo.infoHtml);
		});
		return marker;
	}
	
	this.createIcon = function(img){
		var masterIcon = new GIcon();
		masterIcon.iconSize = new GSize(32, 32);
		masterIcon.iconAnchor = new GPoint(16, 16);
		masterIcon.infoWindowAnchor = new GPoint(16, 0);
		masterIcon.image = img;
		return masterIcon;
	}
	
	this.addOverlay = function(){
		for(var i = 0; i < arguments.length; i++)
		{
			selfObj.gmap.addOverlay(arguments[i]);			
		}
	};
	
	this.create = function(){
		if(GBrowserIsCompatible()){
			this.gmap = new GMap2($(this.canvas));
			//this.gmap.setMapType(this.mapType);
			this.geocoder = new GClientGeocoder();
			if(this.loadLargeMap)
			    this.addControl(new GLargeMapControl());
			if(this.loadMapType)
			    this.addControl(new GMapTypeControl());
			if(typeof(arguments[0]) == "string")
			{
			    if(arguments[0] != "")
				    this.setAddress(arguments[0], false);
				else
				    this.gmap.setCenter(new GLatLng(35.870415551,107.3359375), 4);
			}
			else if(typeof(arguments[0]) == "number")
				this.gmap.setCenter(new GLatLng(arguments[0], arguments[1]), this.zoomLevel);
			else if(typeof(arguments[0]) == "object")
				this.gmap.setCenter(arguments[0], this.zoomLevel);
			else
				this.gmap.setCenter(new GLatLng(35.870415551,107.3359375), 4);						
			if(this.dragging)
				GEvent.addListener(this.gmap, "dragend", this.mapDragend);			
		}
	};
	
	this.addControl = function(control){
	    selfObj.gmap.addControl(control);
	}
	this.setAddress = function(address, ists){
		selfObj.geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
				    if(ists)
				    {
						alert("不能解析: " + address);						
					}
					selfObj.gmap.setCenter(new GLatLng(35.870415551,107.3359375), 4);
				} else {
					selfObj.gmap.setCenter(point, selfObj.zoomLevel);
					selfObj.loadPhotos(selfObj.gmap.getBounds());
				}
			}
		);
	}
}

function UserInfo(node)
{
	this.id = getDOMAtt(node, "id");
	this.online = node.selectSingleNode("online").text;
	this.username = node.selectSingleNode("username").text;
	this.nickname = node.selectSingleNode("nickname").text;
	this.lat = node.selectSingleNode("lat").text;
	this.lon = node.selectSingleNode("lon").text;
	this.img = node.selectSingleNode("icon").text;
	this.score = node.selectSingleNode("score").text;
	this.rankName = node.selectSingleNode("rankName").text;
	this.renown = node.selectSingleNode("renown").text;
	this.pcount = node.selectSingleNode("pcount").text;
	this.infoHtml = "<div class='green'><a href='"+sitePath+"user/photo/list-"+this.id+".aspx' target='_blank'><img src='" + this.img + "' alt='" + this.nickname + "' width='100' align='left' style='margin:2px;'></a>";
	this.infoHtml += "昵  称：<a href='"+sitePath+"user/photo/list-"+this.id+".aspx' target='_blank' class='blue1'>"+this.nickname+"</a><br />";
	this.infoHtml += "照片数："+this.pcount+" 张<br />";
	this.infoHtml += "头  街："+this.rankName+"<br />";
	this.infoHtml += "声  望："+this.renown+" 点<br />";
	this.infoHtml += "积  分："+this.score+" 分<br />";
	this.infoHtml += "</div>";
}

function PhotoInfo(node)
{
	this.id = getDOMAtt(node, "id");
	this.userId = node.selectSingleNode("userId").text;
	this.author = node.selectSingleNode("author").text;
	this.name = node.selectSingleNode("name").text;
	this.lat = node.selectSingleNode("lat").text;
	this.lon = node.selectSingleNode("lon").text;
	this.img = node.selectSingleNode("img").text;
	this.content = node.selectSingleNode("content").text;
	this.hits = node.selectSingleNode("hits").text;
	this.plcount = node.selectSingleNode("plcount").text;
	this.shootDate = node.selectSingleNode("shootDate").text;
	this.cameraInfo = node.selectSingleNode("cameraInfo").text;
	this.infoHtml = "<div class='green'><a href='"+sitePath+"user/photo/"+this.id+".aspx' target='_blank'><img src='" + this.img + "' alt='" + this.name + "' width='100' align='left' style='margin:2px;'></a>";
	this.infoHtml += "名  称：<a href='"+sitePath+"user/photo/"+this.id+".aspx' target='_blank' class='blue1'>"+this.name+"</a><br />";
	this.infoHtml += "作  者：<a href='"+sitePath+"user/index-"+this.userId+".aspx' target='_blank' class='blue1'>"+this.author+"</a><br />";
	this.infoHtml += "评论数："+this.plcount+"<br />";
	if(this.cameraInfo != "")
	{
	    this.infoHtml += "拍摄时间："+this.shootDate+"<br />";
	    this.infoHtml += "拍摄设备："+this.cameraInfo+"<br />";
	}
	this.infoHtml += this.content;
	this.infoHtml += "</div>";
}

function ListBuilder(separator)
{
	this.separator = separator;
    this.value = "";
    this.add = function(str){
        if(!this.exists(str))
		{
			if(this.value.length > 0)
				this.value += this.separator;
			this.value += str;
		}
    };
	this.exists = function(str){
		if((this.separator + this.value + this.separator).indexOf((this.separator + str + this.separator)) == -1)
			return false;
		else
			return true;
	};
}