function GoogleMap(objName, divID, typeval, func) {
	this.obj = objName;
	this.divObj = divID;
	this.type = isUndefined(typeval)? 1:typeval;
	this.func = isUndefined(func) ? '': func;
	this.mapObj;//google js文件没有被读取
	this.icons = {};
	this.markerarr = [];
	this.htmlarr = [];
	return this;
};
//开始加载google 地图
GoogleMap.prototype.Initialize = function(lat, lng , range)
{
	with(this) {
		var instance = eval(obj);
		this.divObj = $(divObj);
		this.divObj.style.display = '';
		this.mapObj = new GMap2(divObj);
		range = parseInt(range);
		if (type) {
            mapObj.addControl(new GMapTypeControl());
            mapObj.addControl(new GLargeMapControl());
            mapObj.addControl(new GScaleControl());
        } else {
            mapObj.addControl(new GSmallMapControl());
        }
        mapObj.setCenter(new GLatLng(36.94, 106.08), range>0?range:3);
		if ( lat ) {
			var point = new GLatLng(lat, lng);
			mapObj.setCenter(point, range>0?range:10);
            var marker = new GMarker(point);
			mapObj.addOverlay(marker);
			return marker;
		}
	}	
};
GoogleMap.prototype.addXmlMarkers = function(url) {	
	with (this) {        
		if (!url) return;
        var instance = eval(obj);
        var request = GXmlHttp.create();
        request.open("GET", url, true);
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                instance.parseData(request.responseXML.lastChild.firstChild.nodeValue);
            }
        }
        request.send(null);
	}
};

GoogleMap.prototype.parseData = function(str)
{
	with(this) {
		instance = eval(obj);    
		reg = new RegExp("\n", "ig");
        str = str.replace(reg, "");
		var arrs = eval('(' + str + ')');
		for (var i = 0; i < arrs[0][0]; i++)
		{
			var arr = arrs[1][i];
			posn = arr.posn;
			arr.marker = arr.marker?arr.marker:'visit';
			var icon = instance.getIcon(arr.marker);
			html = func(arr);
            var marker = new GMarker(new GLatLng(posn[0], posn[1]), {
                icon: icon
            });      
			marker.bindInfoWindowHtml(html);
			htmlarr.push(html);
            markerarr.push(marker);
			mapObj.addOverlay(marker);
		}
		if ( typeof (GMP_callback) == "function" ) GMP_callback(instance);
	}
};
GoogleMap.prototype.addLogMarkers = function(markers,htmls)
{
	with(this) {
		for (var i in markers) {        
			var value = markers[i].getPoint();
			var marker = new GMarker(new GLatLng(value.lat(), value.lng() ));
			marker.bindInfoWindowHtml(htmls[i]);
			this.mapObj.addOverlay(marker);
		}
	}
};
GoogleMap.prototype.getIcon = function(image) {
	with(this) {
		var icon = null;
		if (image) {
			if (icons[image]) {
				icon = icons[image];
			}else {
				icon = new GIcon();
				icon.image = "image/map/" + image + ".png";
				icon.shadow = "image/map/" + image + "_shadow.png";
				icon.iconSize = new GSize(20, 20);
				icon.iconAnchor = new GPoint(10, 20);
				icon.shadowSize = new GSize(22, 20);
				icon.infoWindowAnchor = new GPoint(5, 1);
				icons[image] = icon;
			}
		}
		return icon;
	}
};
GoogleMap.prototype.randomShow = function(time) {
    with(this) {
        instance = eval(obj);
        var i = parseInt(markerarr.length * Math.random());
        if (markerarr.length) {
            mapObj.addOverlay(markerarr[i]);
            markerarr[i].openInfoWindowHtml(htmlarr[i]);
            setTimeout(obj + ".remove(" + i + ")", time);
        }
        setTimeout(obj + ".randomShow(" + time + ")", time);
    }
};
GoogleMap.prototype.remove = function(i) {
    with(this) mapObj.removeOverlay(markerarr[i]);
};
GoogleMap.prototype.getPoint = function(address) {
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(
          address,
          function(point) {
            if (point) {
				var zoom = mapObj.getZoom();
				if ( zoom < 9) mapObj.setCenter(point,9);
              	var marker = new GMarker(point);
				marker.bindInfoWindowHtml(address);
				mapObj.addOverlay(marker);
            }
          }
        );
}

function fetchData() {
    var bounds = map.getBounds();
    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();
    var min_x = sw.lng(),
    max_x = ne.lng(),
    min_y = sw.lat(),
    max_y = ne.lat();
    var s = document.createElement('script');
    s.type = 'text/javascript';
    s.charset = 'utf-8';
    s.src = "http://www.panoramio.com/map/get_panoramas.php?order=popularity&set=public&from=0&to=20&minx=" + min_x + "&miny=" + min_y + "&maxx=" + max_x + "&maxy=" + max_y + "&size=small&callback=storePhotos";
    document.body.appendChild(s);
}
function onMoveEndMap() {
    var items = document.body.getElementsByTagName("script");
    var last = items.length - 1;
    document.body.removeChild(items[last]);
    fetchData();
}
function storePhotos(panoramio) {
    var photos = panoramio.photos;
    plotPanoramio(photos);
}
function plotPanoramio(photos) {
    var masterIcon = new GIcon();
    masterIcon.iconSize = new GSize(32, 32);
    masterIcon.iconAnchor = new GPoint(16, 16);
    masterIcon.infoWindowAnchor = new GPoint(16, 0);
    if (marker) {
        map.clearOverlays()
    }
    for (var i = 0; i < 20; i++) {
        var p = photos[i]
        p.photo_title = p.photo_title.substring(0, 33) + "…"
        var mark_url = getImageUrl("mini_square", p.photo_id);
        var file_url = getImageUrl("small", p.photo_id);
        var width = p.width;
        var height = p.height;
        var size = 240;
        if (width > height) {
            height = size * height / width;
            width = size
        } else {
            width = size * width / height;
            height = size
        }
        masterIcon.image = mark_url;
        var overlay = new GMarker(new GLatLng(p.latitude, p.longitude), {
            icon: masterIcon
        });
        map.addOverlay(overlay);
        overlay.bindInfoWindowHtml('<img onclick="zoom(this,\''+getImageUrl("medium", p.photo_id)+'\')" id="'+ p.photo_id +'" src="' + file_url + '" style="width:' + width + 'px; height:' + height + 'px;border:0; cursor:pointer;" /><br><div style="width:170xp">' + p.photo_title + '<br><p style="font-size: 80%;">Posted by: ' + p.owner_name + '</div>');
    }
}
function getImageUrl(size, id) {
    return ("http://static" + ((id % 4) + 1) + ".bareka.com/photos/" + size + "/" + id + ".jpg");
}
function getCoordsCenter(map) {
    function dec2sex(dec, lat) {
        var letter = lat ? (dec > 0 ? "N": "S") : (dec > 0 ? "E": "W");
        dec = Math.abs(dec);
        var deg = Math.floor(dec);
        var min = Math.floor((dec - deg) * 60);
        var sec = (dec - deg - min / 60) * 3600;
        return deg + "o" + min + "'" + sec.toFixed(2) + "\"" + letter;
    }
    var center = map.getCenter();
    return dec2sex(center.lat(), true) + " " + dec2sex(center.lng());
}

function gglear(obj){
	var div = $(obj).getElementsByTagName('div');
	for ( var i in div ) {
		if ( div[i].className == 'gmnoprint' && div[i].style.bottom  ) {
			div[i].style.display = 'none';
		}
	}
}

function addLoadEvent(func)
{    
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    } 
    else 
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}
