/** light用 情報ページjsファイル*/

var _m = null;
var _ms = null;
var _mm = null;
var _ct = null;
var _mt = null;
var _skdk = null;

var mapElement=null;

/** 距離画像デフォルト位置 */
var MIR=5;
var MIB=20;


// デフォルト緯度経度
var iniNl = "";
var iniEl="";

var _layerCollect=new Array();

function init() {
	// 情報ページのログを残す
	mapionTracker("/map/Info");
	if((nl != "" && !nl != null) && (el != "" && !el != null) ){
		iniNl = nl;
		iniEl = el;
	}
	var flg = false;
	// history.back()してきた場合の処理
	if( document.getElementById("tmpnl").value != "" && document.getElementById("tmpel").value != "" ) {
		iniNl = document.getElementById("tmpnl").value;
		iniEl = document.getElementById("tmpel").value;
		iniScl = document.getElementById("tmpscl").value;
		flg = true;
	}
	mapElement = document.getElementById("map");
	// 地図操作クラス生成
	_m = new ProAtlasMapCtrl(mapElement,iniMapXML);
	// 初期表示
	_m.moveAndLayerTo( new LLPoint(iniNl, iniEl),_m.getLayerId("5000"));
	

	if( !flg ) _m.asyncGetLayerScaleList("map", new LLPoint(iniNl,iniEl), function(list){create_map(list);});
	else create_map(new Array());
};


/** 地図作成 */
function create_map(list) {

	var small = "";
	for( i=0; i<list.length; i++ ) {
		if( small == "" ) {
			small = list[i];
		} else if( small > list[i] ) {
			small = list[i];
		}
	}
	var _iniCenter = new LLPoint(iniNl, iniEl);
	var _iniScale = small?small:iniScl;
	var _contentsLimit = 250000;
	// 緯度経度を変換
	var pos=Converter.convert10to60(nl)+","+Converter.convert10to60(el);

	_m.setVisibleCenterMark(VCM);
	// 初期表示
	_m.moveAndLayerTo(_iniCenter,_m.getLayerId(_iniScale));
	
	// スクロール禁止、クリック移動禁止、キー変更禁止、ホイール操作禁止
	_m.setEnableKeyboardOperation(KeyFlg);
	_m.setEnableDragScroll(DrgFlg);
	_m.setEnableDbClickCentering(DbCFlg);
	_m.setEnableWheelOperation(WHLFlg);
	_m.setEnableAutoLayerChange(true);
	
	// マップコントロール追加
	if( MCType ) {
		_ct = new MapionMapControl( _m, 10, 10 );
		mapElement.appendChild(_ct.e);
		_ct.e.onmousedown=function(e){CancelBubble(e);};
		_ct.e.onclick=function(e){CancelBubble(e);};
		_ct.e.ondblclick=function(e){CancelBubble(e);};
	}
    // 距離画像追加
    if( Meter ) {
    	_mt = new MapionMapMeter( _m, MIR, MIB );
    	mapElement.appendChild(_mt.e);
    	_mt.e.onmousedown=function(e){CancelBubble(e);};
    	_mt.e.onclick=function(e){CancelBubble(e);};
    	_mt.e.ondblclick=function(e){CancelBubble(e);};
    	_mt.dispMeter();
    }
    // 8方向移動パネル追加
    if( EP ) {
    	_ep = new MLargeMapControl(_m, mapElement);
    	_ep.show();
    	_ep.div.onmousedown=function(e){CancelBubble(e);};
    	_ep.div.onclick=function(e){CancelBubble(e);};
    	_ep.div.ondblclick=function(e){CancelBubble(e);};
    }
    
    // そこどこ表示
    if( document.getElementById("sokodoko") ) {
		_skdk = new sokodoko(_grp);
		_skdk.parse(el, nl);
	}
	
	// POI操作クラス生成
	_ms = new MapsvrCtrl(_m,iniPoiXML);
	// イベント追加
	AlpsEvent.addEvent(_m,"layerListChanged", LLChanged);
	AlpsEvent.addEvent(_m,"layerChanged",layerChanged);
	AlpsEvent.addEvent(_m,"layerChanging",smooth);
	AlpsEvent.addEvent(_m,"mapMoved",mapMoved);
	AlpsEvent.addEvent(_m,"mapMoving",mapMoving);
	
	// アイコン指定
	if( icon != null && icon != '') {
		_ms.categoryMap[categories].iconImage[0] = icon;
		var im = new Image();
		im.src = icon;
		_ms.categoryMap[categories].iconSize[0] = im.width;
		_ms.categoryMap[categories].iconSize[1] = im.height;
	}
	
	// expandIconをオーバーライド
	_ms.expandIcon = function (index) {
		var ms = this;
		return function () {
			if (!ms.contents.length)return;
			if( ms.contents[index]["id"] != 'bl' ) {
				ms.icon[index].style.zIndex = 1;
			}
			if (ms.expand) {
			    window.clearTimeout(ms.itimer[index]);
			    ms.itimer[index] = window.setTimeout(ms.gidle(index, true), 20);
			    if (!ms.fixPopup)ms.closePopup();
			}
			else {
			    ms.showPopup(index);
			}
			AlpsEvent.triggerEvent(ms, "mouseOver", ms.contents[index].id);
		};
	};
	// layerChangedをオーバーライド
	_ms.layerChanged = function() {
		_ms.hidePopup();
		set_layers();
		_ms.contents.clear();
		_ms.contents.push(new MapsvrPOI("bl","blinker",new LLPoint(pos), "blink", null, null));
		_ms.contents.push(new MapsvrPOI(id,title, new LLPoint(pos), categories, title, null));
		_ms.showContents();
		// ズームバー変更
		if( MCType ) {
			_ct.setnoneScale(); _ct.resetCnt();
		}
		// 距離画像変更
		if( Meter) {
			_mt.changeMeter();
		}
	};
	// リサイズするとコンテンツclearされるので上書き
	_ms.findContents = function(){};
	
	// POI配置
	_ms.contents.push(new MapsvrPOI("bl","blinker",new LLPoint(pos), "blink", null, null));
	_ms.contents.push(new MapsvrPOI(id,title, new LLPoint(pos), categories, title, null));
	
	create_layers();
	//_ms.showContents = function() {
	//};
	// POI表示
	_ms.showContents();
};

// 移動で再検索
function mapMoved() {
	// スクロールログを残す
	mapionTracker("/map/map/scroll");
	var p = _m.getCenterLatLon();
	var ctr = p.toString().split(",");
	_ms.showContents();
	if( !_skdk )_skdk.parse(ctr[1], ctr[0]);

};

// 移動中はPOPUPを消す
function mapMoving() {
	_ms.hidePopup();
};

function LLChanged() {
	set_layers();
};

// 縮尺変更時はPOPUPを消す
function smooth() {
	_ms.hidePopup();
};

function layerChanged() {};

/** 縮尺リスト生成*/
function create_layers(){
	if(!_m) return;
	if( document.getElementById("layers") != null ) {
		for(var i=0;i<_layerCollect.length;i++){
			var menu=_layerCollect[i];
			document.getElementById("layers").removeChild(menu);
		}
		_layerCollect.length=0;
		var tbl=document.createElement("table");
		tbl.style.border="0";
		tbl.style.cellSpacing="0";
		tbl.cellPadding="0";
		var tbdy=document.createElement("tbody");
		tbdy.style.vAlign="top";
		var tr=document.createElement("tr");
		tbl.style.border=0;
		tbl.style.width=350+"px";
		tbl.style.cellSpacing=2;
		tbl.style.cellPadding=0;
		for(var i=0;i<_m.getLayerCount();i++){
			var scale=_m.getLayerScale(i);
			if(!_layers['s'+scale])continue;
			var td=document.createElement('td');
			var menu=document.createElement("img");
			
			menu.setAttribute("layerid",i);
			menu.onclick=layerto(i);
			menu.onmouseover=Layer_img_FB();
			menu.onmouseout=Layer_img_FB();
			menu.src=_layers['s'+scale]+".gif";
			menu.style.visibility = "hidden";
			_layerCollect.push(menu);
			td.appendChild(menu);
			tr.appendChild(td);
		}
		tbdy.appendChild(tr);
		tbl.appendChild(tbdy);
		document.getElementById("layers").appendChild(tbl);
		set_layers();
	} else {
		set_layers();
	}
};

/** 縮尺変更処理をする@param [String]	index */
function layerto(index){
	if(!_m)return;
	return function(e){
		_m.smoothLayerTo(index);
	};
};

/** 縮尺リストの見た目変更 */
function Layer_img_FB(){
	return function(e){
		if(typeof(e)=='undefined'){
			if(window.event.type=="mouseover") {
				this.src=_layers['s'+_m.getLayerScale(this.getAttribute("layerid"))]+"_a.gif";
			} else if(window.event.type=="mouseout") {
				if(_m.getCurrentLayerId() != this.getAttribute("layerid")) {
					this.src=_layers['s'+_m.getLayerScale(this.getAttribute("layerid"))]+".gif";
				}
			}
		} else if(e.type=="mouseover") {
			this.src=_layers['s'+_m.getLayerScale(this.getAttribute("layerid"))]+"_a.gif";
		} else if(e.type=="mouseout") {
			if(_m.getCurrentLayerId() != this.getAttribute("layerid")){
				this.src=_layers['s'+_m.getLayerScale(this.getAttribute("layerid"))]+".gif";
			}
		}
	};
};

/** レイヤーセット2 */
function set_layers2(s) {
	if(!_m) return;
	//var s=_m.getLayerScaleList();
	for(var i=0;i<_layerCollect.length;i++) {
		var menu=_layerCollect[i];
		var layerid=menu.getAttribute("layerid");
		var scale=_m.getLayerScale(layerid);
		if(layerid==_m.getCurrentLayerId()) {
			menu.src=_layers['s'+scale]+"_a.gif";
		} else {
			menu.src=_layers['s'+scale]+".gif";
		}
		if(!s) continue;
		if(s.find(scale)<0) {
			menu.style.visibility="hidden";
		} else {
			menu.style.visibility="visible";
		}
	}
	// ズームバー変更
	if( MCType ) {
		_ct.setnoneScale(); 
		_ct.resetCnt();
	}
	// 距離画像変更
	if( Meter) {
		_mt.changeMeter();
	}
};

/** レイヤーセット */
function set_layers() {
	if(!_m) return;
	var s=_m.getLayerScaleList();
	for(var i=0;i<_layerCollect.length;i++) {
		var menu=_layerCollect[i];
		var layerid=menu.getAttribute("layerid");
		var scale=_m.getLayerScale(layerid);
		if(layerid==_m.getCurrentLayerId()) {
			menu.src=_layers['s'+scale]+"_a.gif";
		} else {
			menu.src=_layers['s'+scale]+".gif";
		}
		if(!s) continue;
		if(s.find(scale)<0) {
			menu.style.visibility="hidden";
		} else {
			menu.style.visibility="visible";
		}
	}
	// ズームバー変更
	if( MCType ) {
		_ct.setnoneScale(); _ct.resetCnt();
	}
	// 距離画像変更
	if( Meter) {
		_mt.changeMeter();
	}
};


/** そこどこクラス */
function sokodoko(_g) {
	this.uc = "/map/uc/Where";
	this.g = _g;
};

/** そこどこ表示 */
sokodoko.prototype.parse=function(el,nl){
	var sd=this;
    var xmlhttp=AlpsUtil.getXMLHttp();
    var e = document.getElementById("sokodoko");
	if(!e) return;
    if(!xmlhttp)return;
	var URL = sd.uc+"?grp="+sd.g+"&el="+el+"&nl="+nl+"&xsl=sokodoko";
    xmlhttp.open("GET",URL,true);
	if(_bi.type!=1) xmlhttp.overrideMimeType("application/xml");
 	xmlhttp.setRequestHeader("Referer", location.href);
	if(_bi.type==1) xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=UTF-8;");
	else xmlhttp.setRequestHeader("Content-Type", "application/application/x-www-form-urlencoded; charset=utf-8");
	xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4){
		var ad=(xmlhttp.responseText.toLowerCase().indexOf("span")>0)? xmlhttp.responseText : "";
		e.innerHTML=ad;
		}
	};
	xmlhttp.send(null);
};

// Iconの幅・高を取得する
function getIconWH( poiID ) {
	var mm = this;
	//var iwh = '';
	var iwh = new Object();
	var doc = AlpsUtil.getXMLDOM(iniPoiXML);
	var n = doc.getElementsByTagName("poi")[0];
	var nCate = n.getElementsByTagName("category");
	for( var i=0; i<nCate.length; i++ ) {
		if( nCate[i].getAttribute("id") != poiID ) continue;
		var nChild = nCate[i].childNodes;
		for( var j=0; j<nChild.length; j++ ) {
			var n1 = nChild[j];
			if (n1.nodeType != 1)continue;
			if( n1.nodeName == 'icon' ) {
				iwh.x = parseInt(n1.getAttribute("iw"));
				iwh.y = parseInt(n1.getAttribute("ih"));
				//iwh = icw+","+ich;
			} else if( n1.nodeName == 'expand' ) {
				iwh.x = parseInt(n1.getAttribute("iw"));
				iwh.y = parseInt(n1.getAttribute("ih"));
				//iwh = icw+","+ich;
			}
		};
	}
	return iwh;
};


// showContents上書き
MapsvrCtrl.prototype.showContents=function(){
	for(var i=0;i<this.icon.length;i++){
		window.clearTimeout(this.itimer[i]);
	}
	this.itimer.clear();
	this.icon.clear();
	if(!this.contents.length) return this.removeCanvas();
	this.clearCanvas();
	var shapeCount=0;
	for(var i=0;i<this.contents.length;i++){
		var cnt=this.contents[i];
		if(cnt.type!=Mapsvr.POI){
			shapeCount++;
			continue;
		}
		if(!this.categoryMap[cnt.cat]) continue;
		var icon=document.createElement("img");
		icon.src=this.categoryMap[cnt.cat].iconImage[cnt.status];
		icon.onmouseover=this.expandIcon(i);
		icon.onmouseout=this.deflateIcon(i);
		icon.onclick=this.onClick(i);
		//var size=this.categoryMap[cnt.cat].iconSize[cnt.status];
		var size = getIconWH(cnt.cat);
		icon.width=size.x;
		icon.height=size.y;
		icon.style.position="absolute";
		icon.style.display='none';
		icon.style.zIndex=0;
		var order=this.resultProperty["order"];
		if(order=="reverse"){
			this.canvas.insertBefore(icon,this.canvas.firstChild);
		}
		else if(order=="random"){
			var nl=this.canvas.childNodes;
			var index=Math.floor(Math.random()*nl.length);
			this.canvas.insertBefore(icon,nl[index]);
		}
		else{
			if(document.getElementById("canvas")) document.getElementById("canvas").appendChild(icon);
			else this.canvas.appendChild(icon);
		}
		this.icon[i]=icon;
		this.dispIcon(i);
	}
	if(shapeCount){
		this.dispShape();
	}
	else if(this.background){
		this.background.style.filter="";
		this.background.style.backgroundImage="";
	}
};

MapsvrCtrl.prototype.dispIcon=function(index){
	if(!this.map)return;
	var c=this.contents[index];
	if(!c) return;

	if(c.type!=Mapsvr.POI) return;
	var p=this.map.latLon2Log(c.pos);
	var icon=this.icon[index];
	if(_bi.type==1){
		icon.style.filter='alpha(opacity='+this.opacity+')';
	}
	else{
		icon.style.opacity=this.opacity/100.0;
		icon.style.MozOpacity=this.opacity/100.0;
	}
	icon.style.visibility="hidden";
	if(p.x>0&&p.x<this.width&&p.y>0&&p.y<this.height){
		icon.style.display='block';
	}
	else{
		icon.style.display='none';
	}
	var d=this.categoryMap[c.cat].iconDiff[c.status]||new Point(0,0);
	var cp=this.canvasPos?this.map.latLon2Log(this.canvasPos):new Point(0,0);
	if(this.expand&&this.categoryMap[c.cat].expandSize[c.status]){
		icon.style.left=Math.floor(p.x-cp.x-icon.width/2+d.x)+"px";
		icon.style.top=Math.floor(p.y-cp.y-icon.height/2+d.y)+"px";
	}
	else{
		var hs=(this.categoryMap[c.cat].iconSize[c.status]||16)/2;
		icon.style.left=Math.floor(p.x-cp.x-icon.width/2+d.x)+"px";
		icon.style.top=Math.floor(p.y-cp.y-icon.height/2+d.y)+"px";
	}
	icon.style.visibility="visible";
};

/*
 * 印刷ページへのリンク
 */
function print(){
	var _url = "/map/uc/PoiInfo?grp="+_grp;
	_url += "&poi_code="+id;
	_url += "&xsl=print";
	_url += "&br="+br;
	_url += "&brand="+brand;
	
	// 現在の緯度・経度、縮尺を取得
	var latlong = _m.getCenterLatLon().toString();
	var scale = _m.getCurrentScale();
	var latlong2 = latlong.split(",");
	_url += "&nl="+latlong2[0];
	_url += "&el="+latlong2[1];
	_url += "&scl="+scale;
	
	document.getElementById("tmpnl").value = latlong2[0];
	document.getElementById("tmpel").value = latlong2[1];
	document.getElementById("tmpscl").value = scale;
	
	//if( !navigator.userAgent.indexOf('Safari') ) window.location.href = _url;
	//else window.open( _url, "info");
	window.open( _url, "_self");
};

/*
 * helpページ表示
 */
function helpPageOpen(){

	window.open( "/sevenandifs/help.html", "help", "scrollbar=no,width=350px,height=560px;");
	return false;
};

/*
 * 注意書き表示
 */
function Comment( flg ){
	if( flg != 4 ) return;
	var comm = document.createElement("div");
	comm.style.position = "absolute";
	comm.style.bottom = "3px";
	comm.style.left = "3px";
	var img = document.createElement("img");
	img.src = "/sevenandifs/images/comment.png";
	comm.appendChild(img);
	m = document.getElementById("map");
	m.appendChild(comm);
};


