var map="";

var markers=new Array();



var lat=37.838;

var lng=139.267;

var zoom=12;



 jQuery(document).ready(function(){

	jQuery.ajax({

		cache:false,

		url:"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(645,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 popup(href){

	window.open(href, 'popup', 'width=640, height=660, menubar=yes, toolbar=yes, scrollbars=yes');

}







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="img/'+xml.children("img").text()+'"alt="" width="155" height="114" 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();

	this.markerIcon.iconSize=new GSize(42,50);

	this.markerIcon.iconAnchor=new GPoint(21, 46);

	this.markerIcon.infoWindowAnchor=new GPoint(21, 2)



	if(xml.children("type").text()=="central"){

		this.markerIcon.image="icon/central.png";

	}else if(xml.children("type").text()=="branch"){

		this.markerIcon.image="icon/branch.png";

		this.markerIcon.iconAnchor=new GPoint(21, 31);

	}else if(xml.children("type").text()=="facilities"){

		this.markerIcon.image="icon/facilities.png";

	}else if(xml.children("type").text()=="gas"){

		this.markerIcon.image="icon/gas.png";

		this.markerIcon.iconAnchor=new GPoint(21, 61);

	}else{

		this.markerIcon.image="icon/branch.png";

	}



	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");

}




