var map="";
var markers=new Array();

 jQuery(document).ready(function(){
	jQuery.ajax({
		url:"js/location.xml",
		type:"GET",
		error: function(){
			alert("xmlファイルの読み込みに失敗しました");
		},
		dataType:"xml",
		success:function(xml){
			parseXml(xml);
			jQuery("a.maplink").each(function(){
				jQuery(this).bind("click", function(){
					for(i=0;i<markers.length;i++){
						if(markers[i].xml.attr("id")==jQuery(this).attr("rel")){
							markers[i].marker.openInfoWindowHtml(markers[i].marker.infohtml);
							map.setZoom(12);
						}
					}
				});
			});
			jQuery("a.popup").each(function(){
				jQuery(this).bind("click", function(){
					popup(jQuery(this).attr("href"));
					return false;
				});
			});
		}
	});

	map = new GMap2(document.getElementById("gmap"),{size:new GSize(610,610)});
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());
	var center = new GLatLng(35.65876, 139.608734);
	map.setCenter(center, 11);
	map.enableScrollWheelZoom();

	jQuery("#gmap").mousemove(function(e){
		jQuery("#altwnd").css("top",e.pageY-jQuery("#gmap").offset().top+20+'px');
		jQuery("#altwnd").css("left",e.pageX-jQuery("#gmap").offset().left+20+'px');
	});
});
function popup(href){
	window.open(href, 'popup', 'width=600, height=660, menubar=yes, toolbar=yes, scrollbars=yes');
}


function parseXml(xml)
{
	jQuery(xml).find("marker").each(function(){
		markers.push(new icons(jQuery(this)));
	});
}


function icons(xml){
	
	
	this.xml=xml;
	this.id=xml.attr("id");
	this.htmlbody;
	this.markerIcon;
	this.opt;
	this.marker;

	this.htmlbody;
	this.htmlbody='<div class="baloon">';
	this.htmlbody+='<div class="baloon-header">'+xml.children("name").text()+'</div>';
	if(xml.children("altname").text()!="")
	{
		this.htmlbody+='<div class="baloon-altname">';
		if(xml.children("altname:eq(0)").text()!="")
		{
			this.htmlbody+=xml.children("altname:eq(0)").text();
		}
		if(xml.children("altname:eq(1)").text()!="")
		{
			this.htmlbody+=' / ';
			this.htmlbody+=xml.children("altname:eq(1)").text();
		}
		this.htmlbody+='</div>';
	}
	this.htmlbody+='<div class="baloon-address">'+xml.children("address").text()+'</div>' ;

	if(xml.children("img").text()!=""){
		this.htmlbody+='<p><img src="img/'+xml.children("img").text()+'"alt="" class="baloon-img" width="184" height="139" /></p>';
	}
	if(xml.children("html").text()!=""){
		this.htmlbody+='<p class="link"><a href="'+xml.children("html").text()+'" onclick="popup(\''+xml.children("html").text()+'\');return false;">詳細はこちら</a></p>' ;
	}
	this.htmlbody+='</div>';

	this.markerIcon=new GIcon();
	/*
	if(xml.children("type").text()=="office"){
		this.markerIcon.image="map/office.png";
	}else if(xml.children("type").text()=="rapport"){
		this.markerIcon.image="map/rapport.png";
	}else if(xml.children("type").text()=="pitland"){
		this.markerIcon.image="map/pitland.png";
	}else if(xml.children("type").text()=="echo"){
		this.markerIcon.image="map/echo.png";
	}else if(xml.children("type").text()=="other"){
		this.markerIcon.image="map/other.png";
	}
	*/
	
	this.markerIcon.image='js/office.png';
	
	this.markerIcon.iconSize=new GSize(34,36);
	this.markerIcon.iconAnchor=new GPoint(17, 36);
	this.markerIcon.infoWindowAnchor=new GPoint(17, 2)

	this.opt={icon:this.markerIcon};

	this.marker=new GMarker(new GPoint(xml.attr("lng"),xml.attr("lat")),this.opt);
	this.marker.infohtml=this.htmlbody;
	this.marker.name=this.xml.children("name").text();

	if(this.id!=""){
		map.addOverlay(this.marker);
	}

	GEvent.addListener(this.marker, 'click', function(){
		this.openInfoWindowHtml(this.infohtml);
		jQuery("#altwnd").css("display","none");
		jQuery("#altwnd").html(""); 
	});
	GEvent.addListener(this.marker, 'mouseover', function(){
		jQuery("#altwnd").css("display","block");
		document.getElementById("altwnd").innerHTML=this.name; 
	});
	GEvent.addListener(this.marker, "mouseout", function() {
		jQuery("#altwnd").css("display","none");
		jQuery("#altwnd").html(""); 
	});
}



