var map="";

var markers=new Array();



$(document).ready(function(){



	$("#smenu-shop").hover(function(){

		$("#smenu-shop-sub").css({display:"block"});

		$("#smenu-shop-link").css({backgroundPosition:"0 -39px"});

	},

	function(){

		$("#smenu-shop-sub").css({display:"none"});

		$("#smenu-shop-link").css({backgroundPosition:"0 0"});

	});

	$("#smenu-agri").hover(function(){

		$("#smenu-agri-sub").css({display:"block"});

		$("#smenu-agri-link").css({backgroundPosition:"0 -39px"});

	},

	function(){

		$("#smenu-agri-sub").css({display:"none"});

		$("#smenu-agri-link").css({backgroundPosition:"0 0"});

	});





	$.ajax({

		url:"map/location.xml",

		type:"GET",

		error: function(){

			alert("xmlファイルの読み込みに失敗しました");

		},

		dataType:"xml",

		success:function(xml){

			parseXml(xml);

			$(".maplink").each(function(){

				$(this).bind("click", function(){

					for(i=0;i<markers.length;i++){

						if(markers[i].xml.children("id").text()==$(this).attr("rel")){

							markers[i].marker.openInfoWindowHtml(markers[i].marker.infohtml);

							map.setZoom(12);

						}

					}

				});

			});

			$("a.popup").each(function(){

				$(this).bind("click", function(){

					popup($(this).attr("href"));

					return false;

				});

			});

		}

	});



	map = new GMap2(document.getElementById("gmap"),{size:new GSize(553,550)});

	map.addControl(new GLargeMapControl());

	map.addControl(new GMapTypeControl());

	map.addControl(new GOverviewMapControl());

	var center = new GLatLng(lat,lng);

	map.setCenter(center, zoom);

	map.enableScrollWheelZoom();



	$("#gmap").mousemove(function(e){

		$("#altwnd").css("top",e.pageY-$("#gmap").offset().top+20+'px');

		$("#altwnd").css("left",e.pageX-$("#gmap").offset().left+20+'px');

	});

});

function popup(href){

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

}





function parseXml(xml)

{

	$(xml).find("location").each(function(){

		markers.push(new icons($(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+='<p class="gmap-baloon-p">'+xml.children("address").text()+'</p>' ;

	if(xml.children("img").text()!=""){

		this.htmlbody+='<p><img src="img/'+xml.children("img").text()+'"alt="" class="gmap-baloon-img" /></p>';

	}

	this.depart=xml.children("department");

/*

	for(i=0;i<this.depart.length;i++){

		this.htmlbody+='<table class="depart-table">';

		if(this.depart.children("name").eq(i).text()!=""){

			this.htmlbody+='<thead><tr><th>部署名</th><td>'+this.depart.children("name").eq(i).text()+'</td></tr></thead>';

		}

		this.htmlbody+='<tbody>';

		for(j=0;j<this.depart.children("open").eq(i).length;j++){

			this.htmlbody+='<tr><th>営業日</th><td>'+this.depart.children("open").eq(i).children("day").eq(j).text()+'</td></tr>';

			this.htmlbody+='<tr><th>営業時間</th><td>'+this.depart.children("open").eq(i).children("time").eq(j).text()+'</td></tr>';

		}

		if(this.depart.children("tel").eq(i).text()!=""){

			this.htmlbody+='<tr><th>電話番号</th><td>'+this.depart.children("tel").eq(i).text()+'</td></tr>';

		}

		if(this.depart.children("fax").eq(i).text()!=""){

			this.htmlbody+='<tr><th>FAX</th><td>'+this.depart.children("fax").eq(i).text()+'</td></tr>';

		}

		this.htmlbody+='</tbody>';

		this.htmlbody+='</table>';

	}

*/

		this.htmlbody+='<p><a href="'+xml.children("id").text()+'.html" onclick="popup(\''+xml.children("id").text()+'.html\');return false;"><img src="img/baloon_btn_detail.gif" style="margin-left:auto;display:block" alt="詳細はこちら" /></a></p>' ;

/*

	this.htmlbody+='<table>';

		if(this.depart.children("atm").text()!=""){

			this.htmlbody+='<tr><th>ATM</th><td>ATM設置あり</td></tr>';

		}

	this.htmlbody+='</table>';

	if(this.depart.children("services").text()!=""){

		this.htmlbody+='<p>'+this.depart.children("services")+'</p>';

	}

*/



	this.htmlbody+='</div>';



	this.markerIcon=new GIcon();

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

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

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

		this.markerIcon.iconAnchor=new GPoint(24, 42);

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

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

		this.markerIcon.image="map/gs.png";

		this.markerIcon.iconSize=new GSize(30,38);

		this.markerIcon.iconAnchor=new GPoint(15, 38);

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

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

		this.markerIcon.image="map/farmstand.png";

		this.markerIcon.iconSize=new GSize(32,30);

		this.markerIcon.iconAnchor=new GPoint(16, 30);

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

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

		this.markerIcon.image="map/funeral.png";

		this.markerIcon.iconSize=new GSize(38,32);

		this.markerIcon.iconAnchor=new GPoint(19, 32);

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

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

		this.markerIcon.image="map/develop.png";

		this.markerIcon.iconSize=new GSize(32,38);

		this.markerIcon.iconAnchor=new GPoint(16, 38);

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

	}else{

		this.markerIcon.image="map/store.png";

		this.markerIcon.iconSize=new GSize(32,32);

		this.markerIcon.iconAnchor=new GPoint(16, 32);

		this.markerIcon.infoWindowAnchor=new GPoint(16, 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);

		$("#altwnd").css("display","none");

		$("#altwnd").html(""); 

	});

	GEvent.addListener(this.marker, 'mouseover', function(){

		$("#altwnd").css("display","block");

		$("#altwnd").html(this.name); 

	});

	GEvent.addListener(this.marker, "mouseout", function() {

		$("#altwnd").css("display","none");

		$("#altwnd").html(""); 

	});

}










