/* 
 * 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.
 */
j$ = jQuery;

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;
	},
	
	/*---------------- ページレイアウト ----------------*/
	/** ３カラムなら右を非表示 */
	setLayoutMain : function(eid)
	{
		var s3 = j$('#space_3');
		if (s3) {
			var s1 = j$('#space_1');
			var sw = j$('#space_wrapper');
			s3 .hide();
			var s2 = j$('#space_2');
			s2.css('width', s2.width()+'px');//%表記の場合サイズが変わらないようにする
			var w = s3.width();
			s3.hide();
			sw.width(sw.width()+w);
			s1.width(sw.width()-s2.width()-8);
		}
	},
	/** ブロックをリサイズ
	 * @param full trueなら全画面切り換え */
	resize : function(eid, full)
	{
		//try {
			var d = document;
			var w = d.getElementById('wrapper');
			var sw = d.getElementById('space_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) {
				if (sp1.style.position != "absolute") {
					j$('#footer').hide();
					j$('#space_2').hide();
					j$('#menubar').hide();
					this.styleBak = {};
					this.styleBak.w = {};
					this.styleBak.w.width = w.style.width;
					w.style.width = "100%";
					if (sw) {
						this.styleBak.sw = {};
						this.styleBak.sw.width = sw.style.width;
						sw.style.width = "100%";
					}
					this.styleBak.c = {};
					this.styleBak.c.width = c.style.width;
					this.styleBak.c.padding = c.style.padding;
					this.styleBak.c.margin = c.style.margin;
					c.style.position = "absolute";
					c.style.width = "100%";
					c.style.height = "100%";
					c.style.padding = "0px";
					c.style.margin = "0px";
					c.style.top = "0px";
					c.style.left = "0px";
					c.style.zIndex = "1000";
					this.styleBak.sp1 = {};
					this.styleBak.sp1.width = sp1.style.width;
					this.styleBak.sp1.padding = sp1.style.padding;
					this.styleBak.sp1.backgroundColor = sp1.style.backgroundColor;
					sp1.style.position = "absolute";
					sp1.style.padding = "0px";
					sp1.style.top = "0px";
					sp1.style.left = "0px";
					sp1.style.width = "100%";
					sp1.style.height = "100%";
					sp1.style.backgroundColor = "white";
					f.style.height = (this.getBrowserHeight()-(box.offsetHeight-f.offsetHeight))+"px";
				} else {
					j$('#footer').show();
					j$('#space_2').show();
					j$('#menubar').show();
					w.style.width = this.styleBak.w.width;
					if (sw) sw.style.width = this.styleBak.sw.width;
					c.style.position = "";
					c.style.width = this.styleBak.c.width;
					c.style.height = "";
					c.style.padding = this.styleBak.c.padding;
					c.style.margin = this.styleBak.c.margin;
					c.style.top = "";
					c.style.left = "";
					c.style.zIndex = "";
					sp1.style.position = "";
					sp1.style.padding = this.styleBak.sp1.padding;
					sp1.style.top = "";
					sp1.style.left = "";
					sp1.style.width = this.styleBak.sp1.width;
					sp1.style.height = "100%";
					sp1.style.backgroundColor = this.styleBak.sp1.backgroundColor;
					f.style.height = (this.getBrowserHeight()-c.offsetTop-sp1.offsetTop-box.offsetHeight+f.offsetHeight)+"px";
				}
			} else {
				f.style.height = (this.getBrowserHeight()/*-w.offsetTop*/-c.offsetTop-sp1.offsetTop-box.offsetHeight+f.offsetHeight)+"px";
			}
		//} catch (e) {console.warn(e);}
	},
	styleBak : null,
	
	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;
	}

};
