/* 
 * @class FakePopup
 * 
 * 擬似ポップアップ
 * 
 * Author : bambi_sage
 */

FakePopup = Class.create();

Object.extend(FakePopup.prototype,
{
	/**
	 * コンストラクタ
	 * 
	 * @return なし
	 */
	initialize : function (name)
	{
		this._name = name;
	},

	/**
	 * ポップアップの表示
	 * 
	 * @param file	ファイル名
	 * @param width	[幅]
	 * @param height	[高さ]
	 * @param tmargin	[上部マージン](デフォルト：100px)
	 */
	show : function (file, width, height, tmargin/*=100px*/)
	{
		if ((tmargin == undefined) || (tmargin < 0))	tmargin = 100;
		
		if (this._div == null)
		{
			this._div = document.createElement("div");
			this._div.name = "_popup_" + this._name;
			this._div.style.position = 'absolute';
			this._div.style.display = 'none';
			this._div.style.top = '0px';
			this._div.style.left = '0px';
			this._div.style.width = '100%';
			this._div.style.height = '100%';
			this._div.style.zIndex = 2;
			this._div.style.textAlign = 'center';
			this._div.style.verticalAlign = 'middle';
			document.body.appendChild(this._div);
			this._div.onclick = this.hide.bindAsEventListener(this);
		}
		
		// create elements
		// ┌──────────────┐
		// ｜┌────────────┐｜
		// ｜｜　　　close div 　　　　｜｜
		// ｜└────────────┘｜
		// ｜┌────────────┐｜
		// ｜｜　　　body div　　　　　｜｜
		// ｜└────────────┘｜
		// ｜┌────────────┐｜
		// ｜｜　　　close div 　　　　｜｜
		// ｜└────────────┘｜
		// └──────────────┘
		
		// close div(top)
		if (tmargin > 0)
		{
			var div = document.createElement("div");
			div.style.width = "100%";
			div.style.height = tmargin;
			div.innerHTML = "&nbsp;";
			div.onclick = this.hide.bindAsEventListener(this);
			this._div.appendChild(div);
		}
		// body div
		{
			var div = document.createElement("div");
			div.style.width = "100%";
			div.style.textAlign = "center";
			div.onclick = this.hide.bindAsEventListener(this);
			
			if (file.match(/(.jpg|.jpeg|.gif|.png|.bmp)$/))
			{
				// 画像ファイル
				var img = document.createElement("img");
				var imgObj = new Image();
				imgObj.src = file;
				if (imgObj.width == 0)
				{
					// 読み込みエラー
					return;
				}
				else
				{
					img.src = file;
					if ((width != undefined) && (width > 0))
					{
						img.style.width = width + "px";
					}
					if ((height != undefined) && (height > 0))
					{
						img.style.height = height + "px";
					}
					img.onclick = this.hide.bindAsEventListener(this);
					div.appendChild(img);
				}
			}
			else
			{
				// その他のファイル
				var frame = document.createElement("iframe");
				frame.src = file;
				frame.name = "_pop_up_";
				frame.id = "_pop_up_";
				frame.frameborder = '0';
				frame.style.border = 'none';
				frame.style.top = '0px';
				frame.style.left = '0px';
				frame.style.width = width + "px";
				frame.style.height = height + "px";
				div.appendChild(frame);
			}
			
			this._div.appendChild(div);
		}
		// close div(bottom)
		{
			var div = document.createElement("div");
			div.style.width = "100%";
			div.innerHTML = "&nbsp;";
			div.onclick = this.hide.bindAsEventListener(this);
			this._div.appendChild(div);
		}
		
		this._div.style.display = 'block';
	}, 
	
	/**
	 * ポップアップの非表示
	 */
	hide : function ()
	{
		this._div.style.display = 'none';
	}
});

