/*######ICON MAGIC##############
//icon
var micons = new Array(10);

for(p in micons){
	micons[p] = new Gicon();
	micon("hoho.jpg",micons[p]);
}

function micon(img,Micon){
###############################*/

function micon(img){
	var Micon = new GIcon();
	Micon.image = '/images/icons/'+img;
	Micon.shadow = '/images/icons/shadow.png';
	Micon.iconSize = new GSize(29,33);
	Micon.shadowSize = new GSize(50,50);
	Micon.iconAnchor =new GPoint(23,33);
	Micon.infoWindowAnchor = new GPoint(10,10);
	Micon.transparent = '/images/icons/'+img;
	return Micon;
}
/*
function parserIcon(originalRequest){
	this.xmlDoc = originalRequest.responseXML;
	this.icon = this.xmlDoc.documentElement.getElementsByTagName('icon');
	this.i = 0;
	this.id = returnIconId;
	this.name = returnIconName;
}

function returnIconId(){
	var id = parseInt(this.icon[this.i].getAttribute("id"));
	return id;
}

function returnIconName(){
	var name = this.icon[this.i].firstChild.nodeValue;
	return name;
}
*/
//--icon

//infowindow check
function checkWindow(){
	GEvent.addListener(map, "infowindowopen", function(){
		win = false;
	});
	GEvent.addListener(map, "infowindowclose", function(){
		win = true;
		checlick = true;
	});
}

//--infowindow check

//Marker
function addMarker(point,icon,window){
	var IconG = gainIcon(icon);
	var marker = new GMarker(point,IconG);
	GEvent.addListener(marker,"click",function(){
		marker.openInfoWindowHtml(window);
	});
	map.addOverlay(marker);
}

function returnMarker(point,icon,window){
	var IconG = gainIcon(icon);
	var marker = new GMarker(point,IconG);
	GEvent.addListener(marker,"click",function(){
		marker.openInfoWindowHtml(window);
		checlick = false;
	});
	GEvent.addListener(marker,"mouseover",function(){
		if(checlick){
			marker.openInfoWindowHtml(window);
		}
	});
	//GEvent.addListener(marker,"mouseout",function(){
	//	 map.closeInfoWindow();
	//});
	return marker;
}

function pushMarker(id,point,pic,name,address,tel,icon,valueP){
	if(pic){
		InfoWindow = '<div class="info"><table><tr><th rowspan="3"><a href="/spot/'+id+'"><img src="/?action_img=1&mode=map&pic='+pic+'" style="width:100px;height:100px" border="0" /></a></th>';
	}else{
		InfoWindow = '<div class="info"><table><tr><th rowspan="3"><a href="/spot/'+id+'"><img src="/images/noimage.jpg" style="width:100px;height:100px" border="0" /></a></th>';
	}
	InfoWindow += '<td><a href="/spot/'+id+'">'+name+'</a></td></tr>';
	InfoWindow += '<tr><td>'+address+'</td></tr>';
	InfoWindow += '<tr><td>評価'+valueP[0]+'点<img src="/images/'+valueP[1]+'" style="width:78;height:14px;"/></td></tr></table></div>';
	addMarker(point,icon,InfoWindow);
	//map.addOverlay(returnMarker(point,icon,InfoWindow));
}

function returnWindow(id,pic,name,address,tel,valueP){
	if(pic){
		InfoWindow = '<div class="info"><table><tr><th rowspan="3"><a href="/spot/'+id+'"><img src="/?action_img=1&mode=map&pic='+pic+'" style="width:100px;height:100px" border="0" /></a></th>';
	}else{
		InfoWindow = '<div class="info"><table><tr><th rowspan="3"><a href="/spot/'+id+'"><img src="/images/noimage.jpg" style="width:100px;height:100px" border="0" /></a></th>';
	}
	InfoWindow += '<td><a href="/spot/'+id+'">'+name+'</a></td></tr>';
	InfoWindow += '<tr><td>'+address+'</td></tr>';
	InfoWindow += '<tr><td>評価'+valueP[0]+'点<img src="/images/'+valueP[1]+'" style="width:78px;height:14px;"/></td></tr></table></div>';
	return InfoWindow;
	//addMarker(point,icon,InfoWindow);
}

function onMarker(){
	var hash = $H(MK);
	var key = hash.keys();
	for(var i = 0;i < key.length;i++){
		if(MK[key[i]] && on[key[i]] == 1){
			map.addOverlay(MK[key[i]]);
			on[key[i]] = 2;
		}else if(MK[key[i]] && on[key[i]] == 0){
			map.removeOverlay(MK[key[i]]);
		}
	}
}

//--Marker
//move
function moveT(){
	GEvent.addListener(map,"moveend",function(){
		//if(win){
		//	map.clearOverlays();
		//}
//		searchDisp();
		if(checkmove == 0){
			$('searchstate').innerHTML = '只今全てのスポットを表示しています　<a href="javascript:reqRanking();">口コミ数の多い30件を表示する</a>';
			checkmove = 1
		}
		if(searchstate){
			getTXML();
		}
	});
}

function moveE(){
	GEvent.addListener(map,"moveend",function(){
		//if(win){
		//	map.clearOverlays();
		//}
		getEXML();
	});
}
//--move
//form
function clickMarker(){
	var lay;
	GEvent.addListener(map,'click',function(overlay,point){
		if(overlay){
			map.removeOverlay(lay);
		}else if(point){
			if(lay){
				map.removeOverlay(lay);
			}
			lay = new GMarker(point);
			map.addOverlay(lay);
			$('polex').value = point.y;
			$('poley').value = point.x;
		}
	});
}

//--form

//XMLParser for Marker
function parser(originalRequest){
	this.xmlDoc = originalRequest.responseXML;
	this.spot = this.xmlDoc.documentElement.getElementsByTagName('spot');
	this.area = returnArea;
	this.bcateId = returnBid;
	this.scateId = returnSid;
	this.fav = returnFav;
	this.i = 0;
	this.id = returnId;
	this.point = returnPoint;
	this.name = returnName;
	this.img = returnImg;
	this.address = returnAddress;
	this.tel = returnTel;
	this.icon = returnIcon;
	this.value = returnValue;
}

function returnArea(){
	var exist = parseInt(this.xmlDoc.getElementsByTagName('area')[0].getAttribute("num"));
	return exist;
}

function returnBid(){
	var bid = parseInt(this.spot[this.i].getAttribute("bid"));
	return bid;
}

function returnSid(){
	var sid = parseInt(this.spot[this.i].getAttribute("sid"));
	return sid;
}
function returnFav(){
	var fav = parseInt(this.spot[this.i].getAttribute("fav"));
	return fav;
}

function returnPoint(){
	var lat = parseFloat(this.spot[this.i].getAttribute("lat"));
	var lon = parseFloat(this.spot[this.i].getAttribute("lon"));
	var point = new GLatLng(lat,lon);
	return point;
}

function returnId(){
	var id = this.spot[this.i].getElementsByTagName('id')[0].firstChild.nodeValue;
	return id;
}

function returnName(){
	var name = this.spot[this.i].getElementsByTagName('name')[0].firstChild.nodeValue;
	return name;
}
function returnImg(){
	var check = parseInt(this.spot[this.i].getElementsByTagName('image')[0].getAttribute("img"));
	if(check){
		var img = this.spot[this.i].getElementsByTagName('image')[0].firstChild.nodeValue;
	}
	return img;
}
function returnValue(){
	var valueP = new Array(2);
	valueP[0] = parseInt(this.spot[this.i].getElementsByTagName('point')[0].getAttribute("value"));
	valueP[1] = this.spot[this.i].getElementsByTagName('point')[0].firstChild.nodeValue;
	return valueP;
}

function returnAddress(){
	var address = this.spot[this.i].getElementsByTagName('address')[0].firstChild.nodeValue;
	return address;
}
function returnTel(){
	var check = parseInt(this.spot[this.i].getElementsByTagName('tel')[0].getAttribute("tel"));
	if(check){
		var tel = this.spot[this.i].getElementsByTagName('tel')[0].firstChild.nodeValue;
	}
	return tel;
}
function returnIcon(){
	var icon = parseInt(this.spot[this.i].getElementsByTagName('marker')[0].getAttribute("icon"));
	return icon;
}

//--XMLParser for Marker

//index tab

function displayTab(image,tab,imgurl){
	$('life-tab').style.backgroundImage = 'url("/images/tab01.jpg")';
	$('eat-tab').style.backgroundImage = 'url("/images/tab02.jpg")';
	$('play-tab').style.backgroundImage = 'url("/images/tab03.jpg")';
	$('other-tab').style.backgroundImage = 'url("/images/tab04.jpg")';
	$(image).style.backgroundImage = 'url("'+imgurl+'")';
	$('tabShelter').style.display = 'none';
	$('tabFood').style.display = 'none';
	$('tabPlay').style.display = 'none';
	$('tabOther').style.display = 'none';
	$(tab).style.display = 'block';
}


function changeSscate(id){
	if($(id).style.display == 'none'){
		$(id).style.display = 'block';
	}else{
		$(id).style.display = 'none';
	}
}
//--index tab

//set size zoom
function setSize(){
	var northEastLatLng = new GLatLng(34.81619919423895,135.79195976257324);
	var southWestLatLng = new GLatLng(34.79421088478986,135.75642585754395);
	//var northEastLatLng = new GLatLng(35.04461384251413,136.05674743652344);
	//var southWestLatLng = new GLatLng(34.76587114702708,135.28839111328125);
	latLngBounds = new GLatLngBounds(southWestLatLng,northEastLatLng);
	var mapCenter = new GLatLng((northEastLatLng.lat()+southWestLatLng.lat())/2.0,(northEastLatLng.lng()+southWestLatLng.lng())/2.0);
	map = new GMap2(document.getElementById("xmap"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	var zoomLevel = map.getBoundsZoomLevel(latLngBounds);
	map.setCenter(mapCenter,zoomLevel);
	//alert(mapCenter);
	map.enableDoubleClickZoom();
}

//--set size zoom
//set resize
/*
function resizeMap(){
	if(!iecheck()){
		map.checkResize();
		zoomLevel = map.getBoundsZoomLevel(latLngBounds);
		map.setCenter(mapCenter,zoomLevel,G_NORMAL_MAP);
	}
}
*/
//--set resize

function getEXML(judge){
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var url = '/index.php';
	if(judge && judge != 'entry'){
		var paras = 'action_ajax_viewspot=1&judge='+judge;
		var requestXML = new Ajax.Request(url,{method: 'post',parameters: paras,onComplete: returnFXML});
	}else{
		var paras = 'action_ajax_viewspot=1&blat='+northEast.lat()+'&slat='+southWest.lat()+'&blng='+northEast.lng()+'&slng='+southWest.lng();
		var requestXML = new Ajax.Request(url,{method: 'post',parameters: paras,onComplete: returnXML});
	}
}

function getSpotXML(seed,id){
	var url = '/index.php';
	var paras = 'action_ajax_spotinfo=1&seed=tag&id='+id;
	var requestSeedXML = new Ajax.Request(url,{method: 'post',parameters: paras,onComplete: returnInfoXML});
}

function returnFXML(originalRequest){
	var spot = new parser(originalRequest);
	if(spot.area()){
		for(spot.i = 0;spot.i < spot.spot.length;spot.i++){
			if(on[spot.id()] != 1 && spot.fav() == 1){
				pushMarker(spot.id(),spot.point(),spot.img(),spot.name(),spot.address(),spot.tel(),spot.icon(),spot.value());
				on[spot.id()] = 1;
			}
		}
	}
}

function returnXML(originalRequest){
	var spot = new parser(originalRequest);
	if(spot.area()){
		for(spot.i = 0;spot.i < spot.spot.length;spot.i++){
			if(on[spot.id()] != 1){
				pushMarker(spot.id(),spot.point(),spot.img(),spot.name(),spot.address(),spot.tel(),spot.icon(),spot.value());
				on[spot.id()] = 1;
			}
		}
	}
}

function returnInfoXML(originalRequest){
	var windowInfo;
	var spot = new parser(originalRequest);
	if(spot.area()){
		spot.i = 0;
		map.setCenter(spot.point(),12);
		for(spot.i = 0;spot.i < spot.spot.length;spot.i++){
			windowInfo = returnWindow(spot.id(),spot.img(),spot.name(),spot.address(),spot.tel(),spot.value());
			MK[spot.id()] = returnMarker(spot.point(),spot.icon(),windowInfo);
			on[spot.id()] = 1;
		}
		onMarker();
	}
}

function getTXML(){
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var url = '/index.php';
	var paras = 'action_ajax_viewspot=1&blat='+northEast.lat()+'&slat='+southWest.lat()+'&blng='+northEast.lng()+'&slng='+southWest.lng();
	var requestXML = new Ajax.Request(url,{method: 'post',parameters: paras,onComplete: returnTXML});
}

function returnTXML(originalRequest){
	xml = originalRequest;
	var spot = new parser(xml);
	if(!spot.area()){
		return;
	}
//	while(okreq == true){;}
	var windowInfo;
	for(spot.i = 0;spot.i < spot.spot.length;spot.i++){
		windowInfo = returnWindow(spot.id(),spot.img(),spot.name(),spot.address(),spot.tel(),spot.value());
		MK[spot.id()] = returnMarker(spot.point(),spot.icon(),windowInfo);
		if(on[spot.id()] != 2){
			on[spot.id()] = 0;
			if($('favoriteOn').checked){
				if(spot.fav() == 1 && $(spot.scateId()+'cate').checked){
					on[spot.id()] = 1;
				}
			}else if($(spot.scateId()+'cate').checked){
				on[spot.id()] = 1;
			}
		}
	}
	onMarker();
}

function returnVXML(originalRequest){
	xml = originalRequest;
	checkX = 1;
	var spot = new parser(xml);
	if(!spot.area()){
		return;
	}
//	while(okreq == true){;}
	var windowInfo;
	for(spot.i = 0;spot.i < spot.spot.length;spot.i++){
		windowInfo = returnWindow(spot.id(),spot.img(),spot.name(),spot.address(),spot.tel(),spot.value());
		MK[spot.id()] = returnMarker(spot.point(),spot.icon(),windowInfo);
		if(on[spot.id()] != 2){
			on[spot.id()] = 0;
			if($('favoriteOn').checked){
				if(spot.fav() == 1 && $(spot.scateId()+'cate').checked){
					on[spot.id()] = 1;
				}
			}else if($(spot.scateId()+'cate').checked){
				on[spot.id()] = 1;
			}
		}
	}
	onMarker();
}


//checked
function checkMark(bid){
	//map.clearOverlays();
	//MK = new Array();
	//on = new Array();
	//reload();
}

function checkBcate(id){
	var n = document.scate[id+'scate'].length;
	for(var i=0;i < n;i++){
		document.scate[id+'scate'][i].checked = true;
	}
	//map.clearOverlays();
	//MK = new Array();
	//on = new Array();
	//reload();
}
function uncheckBcate(id){
	var n = document.scate[id+'scate'].length;
	for(var i=0;i < n;i++){
		document.scate[id+'scate'][i].checked = false;
	}
	//map.clearOverlays();
	//MK = new Array();
	//on = new Array();
	//reload();
}

//center
function centerspot(lon,lat,id){
	var point = new GLatLng(lat,lon);
	map.panTo(point);
	$('mainmap').focus();
	GEvent.trigger(MK[id],"click");
	//map.setCenter(point,17,G_NORMAL_MAP);
}

//--center

//allcheck
/*
function allCheck(bool){
	for(var i=1;i <= 4;i++){
		$('bcate'+i).checked = bool;
		var n = document.scate[i+'scate'].length;
		for(var j=0;j < n;j++){
			document.scate[i+'scate'][j].checked = bool;
		}
	}
	map.clearOverlays();
	MK = new Array();
	on = new Array();
	reload();
}
*/
//--allcheck

//reload
function reload(){
//	searchDisp();
	returnTXML(xml);
}

//IE check
function iecheck(){
	if(navigator.userAgent.indexOf("MSIE")!=-1){
		return 1;
	}
}

//Jump
function Jump(url){
	location.href = url;
}

function Fly(url){
	setTimeout("Jump('"+url+"')",3000);
}
//--Jump
