/* 
 * Copyright (c) 2009 National Research Institute for Earth Science and
 * Disaster Prevention (NIED).
 * This code is licensed under the GPL 3.0 license, availible at the root
 * application directory.
 */

function ecom_map(ecom_map_url, ecom_map_auth, return_url)
{
	this.ecom_map_url = ecom_map_url;
	this.ecom_map_auth = ecom_map_auth;
	this.return_url = return_url;
}
ecom_map.prototype = 
{
	ecom_map_url : null,
	ecom_map_auth : null,
	return_url: null,
	
	win : null,
	
	//eコミマップ関連
	/** eコミマップエディタ表示 */
	openWindow : function(cid, mid)
	{
		var win = window.open(this.ecom_map_url+"map/?cid="+cid+"&mid="+mid+(this.ecom_map_auth?("&auth="+this.ecom_map_auth):""),
				"ecom_map", "location=no,personalbar=no,status=no,resizable=yes");
		win.focus();
	},
	/** eコミマップ作成ウィザード表示 */
	showWizard : function(cid)
	{
		var win = window.open(this.ecom_map_url+"wizard/wizard.jsp?cid="+cid+(this.ecom_map_auth?("&auth="+this.ecom_map_auth):""),
				"wizard", "location=no,personalbar=no,scrollbars=yes,status=no,resizable=yes,width=620,height=560");
		win.focus();
		var thread = setInterval(
			function() {if (win != null && win.closed) { clearTimeout(thread); win = null; location.reload(); } }, 1000
		);
	},
	
	/** 地図管理画面表示 */
	mapAdmin : function(cid)
	{
		var url = this.ecom_map_url+"admin/map.jsp?cid="+cid+(this.ecom_map_auth?("&auth="+this.ecom_map_auth):"");
		if (this.return_url) url += "&ret="+this.return_url;
		location.href = url;
	},
	/** 地図設定画面表示 */
	mapEdit : function(cid, mid)
	{
		var url = this.ecom_map_url+"admin/map-edit.jsp?cid="+cid+"&mid="+mid+(this.ecom_map_auth?("&auth="+this.ecom_map_auth):"");
		if (this.return_url) url += "&ret="+this.return_url;
		location.href = url;
	},
	createUser : function(eid, cid)
	{
		var win = window.open(this.ecom_map_url+"parts/ecom/createuser.jsp?eid="+eid+"&cid="+cid+(this.ecom_map_auth?("&auth="+this.ecom_map_auth):""),
				"createuser", "location=no,personalbar=no,status=no,resizable=yes,width=400,height=240");
		win.focus();
		var self = this;
		var thread = setInterval(
			function() {if (win != null && win.closed) { clearTimeout(thread); win = null; location.reload(); } }, 1000
		);
	},
	
	//パーツブロック表示用 
	/** Ajaxでブロックをロード */
	loadBlock : function(divid, params, authId)
	{
		jQuery.ajax({
			url: "modules/ecom_map/block_load.php?params="+params+"&authid="+authId,
			cache: false,
			success: function(data) {
				document.getElementById(divid).innerHTML = data;
			}
		});
	},
	/** メインブロック表示 */
	showMain : function(eid, mid, noedit, layer, fid, full)
	{
		var url = "/index.php?module=ecom_map&eid="+eid+"&mid="+mid;
		if (layer && fid) url += "&layer="+layer+"&fid="+fid;
		if (full) url += "&full=1";
		if (noedit) url += "&noedit=1";
		location.href = url;
	},
	
	/** ブロックをリサイズ
	 * @param full trueなら全画面 */
	resize : function(eid, full)
	{
		try {
			var d = document;
			var w = d.getElementById('wrapper');
			var c = d.getElementById('container');
			var sp1 = d.getElementById('space_1');
			var box = d.getElementById('box_'+eid); 
			var f = d.getElementById('ecom_map_iframe');
			if (full) {
				var p = f;
				while (p) {
					if (p.style) {
						p.style.padding="0px";
						p.style.margin="0px";
						p.style.borderWidth = "0px";
					}
					p = p.parentNode;
				}
				var ft = d.getElementById('footer');
				if (ft) ft.style.display = "none";
				var sp2 = d.getElementById('space_2');
				if (sp2) sp2.style.display = "none";
				var sp3 = d.getElementById('space_3');
				if (sp3) sp3.style.display = "none";
				d.getElementById('menubar').style.display = "none";
				c.style.position = "absolute";
				c.style.width = "100%";
				c.style.height = "100%";
				c.style.top = "0px";
				c.style.left = "0px";
				c.style.zIndex = "1000";
				sp1.style.position = "absolute";
				sp1.style.top = "0px";
				sp1.style.left = "0px";
				sp1.style.width = "100%";
				sp1.style.height = "100%";
				sp1.style.backgroundColor = "white";
				if (box) {
					box.style.width = "100%";
					box.style.heihgt = "100%";
				}
				f.style.width = "100%";//(this.getBrowserWidth())+"px";
				f.style.height = (this.getBrowserHeight()-(box.offsetHeight-f.offsetHeight))+"px";
			} else {
				d.getElementById('ecom_map_iframe').style.height = (this.getBrowserHeight()-w.offsetTop-c.offsetTop-sp1.offsetTop-(box.offsetHeight-f.offsetHeight))+"px";
			}
		} catch (e) {console.warn(e);}
	},
	
	getBrowserWidth : function()
	{
		var d = document;
		if (window.innerWidth) return window.innerWidth;
		else if ( d.documentElement && d.documentElement.clientWidth != 0 ) return d.documentElement.clientWidth;
		else if ( d.body ) return d.body.clientWidth;
		return 0;
	},
	getBrowserHeight : function()
	{
		var d = document;
		if (window.innerHeight) return window.innerHeight;
		else if ( d.documentElement && d.documentElement.clientHeight != 0 ) return d.documentElement.clientHeight;
		else if ( d.body ) return d.body.clientHeight;
		return 0;
	}

};
