var map="";
var markers=new Array();

var lat=37.30;
var lng=138.95;
var zoom=11;


 jQuery(document).ready(function(){
	jQuery.ajax({
		cache:false,
		url:"map/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.children("id").text()==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(640,520)});
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());
	var center = new GLatLng(lat,lng);
	map.setCenter(center, zoom);
	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 parseXml(xml)
{
	jQuery(xml).find("location").each(function(){
		markers.push(new icons(jQuery(this)));
	});
}


function icons(xml){

	this.xml=xml;
	this.id=xml.children("id").text();
	this.htmlbody;
	this.markerIcon;
	this.opt;
	this.marker;

	this.htmlbody='<div class="gmap-baloon">';
	this.htmlbody+='<div class="gmap-baloon-head">'+xml.children("name").text()+'</strong></div>';
	this.htmlbody+='<div class="gmap-baloon-address">'+xml.children("address").text()+'</div>' ;
	if(xml.children("img").text()!=""){
		this.htmlbody+='<div class="photo-container"><div class="photo"><img src="pop/img/'+xml.children("img").text()+'"alt="" width="180" height="135" class="gmap-baloon-img" /></div></div>';
	}
	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()=="central"){
		this.markerIcon.image="map/central.png";
	}else if(xml.children("type").text()=="office"){
		this.markerIcon.image="map/office.png";
	}else if(xml.children("type").text()=="car"){
		this.markerIcon.image="map/car.png";
	}else if(xml.children("type").text()=="plant"){
		this.markerIcon.image="map/plant.png";
	}else if(xml.children("type").text()=="gas"){
		this.markerIcon.image="map/gas.png";
	}else if(xml.children("type").text()=="shop"){
		this.markerIcon.image="map/shop.png";
	}else{
		this.markerIcon.image="map/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.children("lng").text(),xml.children("lat").text()),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(""); 
	});
}

function switchStoreTable(id){
	jQuery("#storeTable1").css("display","none");
	jQuery("#storeTable2").css("display","none");
	jQuery("#storeTable3").css("display","none");
	jQuery(id).css("display","block");
}


