var map = new Object();
var selector_drawn = false;
var selector = new Array();


function load() {
	if (GBrowserIsCompatible()) {
		DrawMap(0);
		Event.observe('ski_selector', 'change', MovePoint);
	}
}

function DrawMap(area){
	map = new GMap2(document.getElementById("gmaparea"));
	map.addControl(new GLargeMapControl());
	map.setCenter(centers[area].point, centers[area].zoom);
	map.addControl(new GOverviewMapControl());
	
	RPointDraw(0);
	SPointDraw();
	
	if(!selector_drawn) SelectorDraw();
//	if(area > 0)
	CatDraw(0);
	
	selector_drawn = true;
	
	GEvent.addListener(map, 'moveend', function(){
		CatDraw(0)
	} );
	GEvent.addListener(map, 'zoom', function(oldZoomLevel, newZoomLevel){
		CatDraw(0);
	} );
	
}

function SPointDraw(){
	for(var i=0; i<pt.length ; i++){
		if(pt[i].marker){
			if(pt[i].name){
				pt[i].key = i;
			}
			map.addOverlay(pt[i].marker );
			if(!selector_drawn){
				selector.push(pt[i]);
			}
		}else if(pt[i].marker ){
			map.removeOverlay(pt[i].marker);
		}
	}
}

function RPointDraw(catid){
	for(var i=0; i<rp.length ; i++){
		if( rp[i].marker && (catid < 1 || catid == rp[i].cat) ){
			map.addOverlay(rp[i].marker);
//			if(typeof(entryid) != 'undefined' && rp[i].id == entryid ){
//					rp[i].marker.openInfoWindowHtml(WindowMessage(rp[i]));
//			}
		}else if(rp[i].marker ){
			map.removeOverlay(rp[i].marker);
		}
	}
}

function MapPoint(id, cat, lat, lng, zoom, name, url, img, body, school, schtel, ski_flag){
	this.id = parseInt(id);
	this.cat = parseInt(cat);
	if(lat) this.lat = parseFloat(lat);
	if(lng) this.lng = parseFloat(lng);
	this.zoom = parseInt(zoom);
	this.name = name;
	this.url = url;
	this.img = img;
	if(body) this.body = body + "...";
	else this.body = "<br /><br />";
	if(school) this.school = school;
	if(schtel) this.schtel = "tel: " + schtel;
	else  this.schtel = "";
	if(this.lat && this.lng ) this.point = new GLatLng(this.lat, this.lng);
	if(this.point && ski_flag){
		this.marker = new GMarker(this.point,  {icon: MainIcon});
		PointWindow(this);
	}else if( this.cat && this.point ){
		this.marker =  new GMarker(this.point, {icon: CatIcon[this.cat] });
		PointWindow(this);
//		if(typeof(entryid) != 'undefined' && rp[i].id == entryid ){
//				this.marker.openInfoWindowHtml(WindowMessage(rp[i]));
//		}
	}
}

function SelectorDraw(){
	$('ski_selector').options[0] = new Option("▼スキー場", "0");
	selector.each( function(val, index){
		$('ski_selector').options[index + 1] =  new Option(val.name, val.id);
	});
}
function MovePoint(){
	var k = $('ski_selector').options[$('ski_selector').selectedIndex].value;
	OpenWindowbyId(k);
}

function CatDraw(k){
	var maprange = map.getBounds();
//	GLog.write(maprange.getSouthWest());
	var str = "<ul id='cat_list'>";
	pt.each( function(val, key){
		if(val.point){
			if(maprange.containsLatLng(val.point) ){
				str += "<li><img src='" + MainIcon.image + "' /><a href='javascript:OpenWindowbyId("+ val.id +")'>"+ val.name +"</a></li>";
			}
		}
	} );
	rp.each( function(val, key){
		if(val.point && ((k > 1 && k==val.cat) || k==0) ) {
			if(maprange.containsLatLng(val.point) ){
				str += "<li><img src='" + CatIcon[val.cat].image + "' /><a href='javascript:OpenWindowbyId_rp("+ val.id +")'>"+ val.name +"</a></li>";
			}
		}
	} );
	str += "</ul>";
	$('cat_navi').innerHTML = str;
}

function PointWindow(obj){
	GEvent.addListener(obj.marker, "click", function(){
			obj.marker.openInfoWindowHtml(WindowMessage(obj));
		}
	);
}

function OpenWindowbyId(id){
	var target = pt.find( function(value){
		return value.id == id;
	});
	map.panTo(target.point);
	var zoom = map.getZoom();
	if(zoom < 13 ){
		map.setZoom(13);
	}
	target.marker.openInfoWindowHtml(WindowMessage(target));
}
function OpenWindowbyId_rp(id){
	var target = rp.find( function(value){
		return value.id == id;
	});
	map.panTo(target.point);
	target.marker.openInfoWindowHtml(WindowMessage(target));
}

function PointWindowbyKey(k, rpf){
	if(rpf){
		map.setCenter(rp[k].point);
		rp[k].marker.openInfoWindowHtml(WindowMessage(rp[k]));
	}else{
		map.setCenter(pt[k].point);
		pt[k].marker.openInfoWindowHtml(WindowMessage(pt[k]));
	}
}

function AreaChange(k){
	map.closeInfoWindow();
	for(var i=0; i<centers.length ; i++){
		if(centers[i].cat==k ){
			var center = centers[i];
		}
	}
//	map.panTo(center.point);
//	map.setZoom(center.zoom);
	map.setCenter(center.point, center.zoom);
//	var f = $$("ul.area_list li#area05 a");
//	f.style.backgroundPosition = "0px -28px";
	CatDraw(0);
}

function GenreSelect(k){
//	map.clearOverlays();
	RPointDraw(k);
	CatDraw(k);
//	SPointDraw();
}


function WindowMessage(obj){
	if(obj.cat==6){
		var msg = "<div  style='width:440px;height:370px'>";
		msg+= "<div class='mapinfotitle'><a href='" + obj.url +"'>" + obj.name + "</a></div>"
		msg+= obj.body;
		msg += "</div>";
	}else{
		var msg = "<div class='mapinfowindow'>";
		msg += "<div class='mapinfobody'>";
		msg+= "<div class='mapinfotitle'><a href='" + obj.url +"'>" + obj.name + "</a></div>"
		msg+= obj.body;
		if(obj.school ){
			msg += "<div class='mapschooltitle'>提携スキー学校</div>"+ obj.school  + "<br>" + obj.schtel + "</div>";
		}else{
			msg += "<div class='mapschooltitle'>&nbsp;</div></div>";
		}
			if(obj.img) {
				msg += "<div class='mapthumimg'>" + obj.img +"</div>";
			}else{
				msg += "<div class='mapthumimg'><img src='"+ iconurl +"noimage.gif' /></div>";
			}
		if(obj.zoom){
			var postfix = "";
		}else{
			var postfix = "RP";
		}
			
		msg += "<div class='zoomlink'><a href='javascript:ZoomMap"+ postfix +"("+obj.key + ")'><img src='"+ iconurl + "zoomin.png' /></a></div>"
		 		+ "<div class='zoomlink'><a href='javascript:ZoomOMap"+ postfix +"("+obj.key+")'><img src='" + iconurl + "zoomout.png' /></a></div>"
		msg += "</div>";
	}
	return msg;
}

function ZoomMap(k){
	map.zoomIn();
	if(k)
		PointWindowbyKey(k);
}
function ZoomMapRP(k){
	map.zoomIn();
	PointWindowbyKey(k, "rp");
}
function ZoomOMap(k){
	map.zoomOut();
	if(k)
		PointWindowbyKey(k);
}
function ZoomOMapRP(k){
	map.zoomOut();
	PointWindowbyKey(k, "rp");
}

