// ウインドウ定数
var WS_TITLEBAR		= 0x01000000;
var WS_STATUSBAR	= 0x02000000;

var WS_CLOSEBUTTON	= 0x000000001;
var WS_SIZEBUTTON	= 0x000000100;

var MAX_HEIGHT		= 800;


// カラーパレット定数
var COLOR_HUE = new Array();
COLOR_HUE[0] = 0xFF0000;
COLOR_HUE[1] = 0xFF8800;
COLOR_HUE[2] = 0xFFFF00;
COLOR_HUE[3] = 0x88FF00;
COLOR_HUE[4] = 0x00FF00;
COLOR_HUE[5] = 0x00FF88;
COLOR_HUE[6] = 0x00FFFF;
COLOR_HUE[7] = 0x0088FF;
COLOR_HUE[8] = 0x0000FF;
COLOR_HUE[9] = 0x8800FF;
COLOR_HUE[10] = 0xFF00FF;
COLOR_HUE[11] = 0xFF0088;

var COLOR_GRAY = new Array();
COLOR_GRAY[0] = 0x000000;
COLOR_GRAY[1] = 0x181818;
COLOR_GRAY[2] = 0x2F2F2F;
COLOR_GRAY[3] = 0x464646;
COLOR_GRAY[4] = 0x5E5E5E;
COLOR_GRAY[5] = 0x737373;
COLOR_GRAY[6] = 0x8B8B8B;
COLOR_GRAY[7] = 0xA2A2A2;
COLOR_GRAY[8] = 0xB9B9B9;
COLOR_GRAY[9] = 0xD0D0D0;
COLOR_GRAY[10] = 0xE7E7E7;
COLOR_GRAY[11] = 0xFFFFFF;

var COLOR_WEB = new Array();
COLOR_WEB[0] = 'white'; 
COLOR_WEB[1] = 'whitesmoke'; 
COLOR_WEB[2] = 'gainsboro'; 
COLOR_WEB[3] = 'lightgrey'; 
COLOR_WEB[4] = 'silver'; 
COLOR_WEB[5] = 'darkgray'; 
COLOR_WEB[6] = 'gray'; 
COLOR_WEB[7] = 'dimgray'; 
COLOR_WEB[8] = 'black'; 

COLOR_WEB[9] = 'snow';
COLOR_WEB[10] = 'lightcoral';
COLOR_WEB[11] = 'rosybrown';
COLOR_WEB[12] = 'indianred';
COLOR_WEB[13] = 'red';
COLOR_WEB[14] = 'firebrick';
COLOR_WEB[15] = 'brown';
COLOR_WEB[16] = 'darkred';
COLOR_WEB[17] = 'maroon';

COLOR_WEB[18] = 'mistyrose';
COLOR_WEB[19] = 'salmon';
COLOR_WEB[20] = 'tomato';
COLOR_WEB[21] = 'darksalmon';
COLOR_WEB[22] = 'lightsalmon';
COLOR_WEB[23] = 'coral';
COLOR_WEB[24] = 'orangered';
COLOR_WEB[25] = 'sienna';
COLOR_WEB[26] = 'seashell';
COLOR_WEB[27] = 'chocolate';
COLOR_WEB[28] = 'saddlebrown';
COLOR_WEB[29] = 'sandybrown';
COLOR_WEB[30] = 'peachpuff';
COLOR_WEB[31] = 'linen';
COLOR_WEB[32] = 'peru';
COLOR_WEB[33] = 'bisque';
COLOR_WEB[34] = 'darkorange';
COLOR_WEB[35] = 'antiquewhite';
COLOR_WEB[36] = 'burlywood';
COLOR_WEB[37] = 'tan';
COLOR_WEB[38] = 'blanchedalmond';
COLOR_WEB[39] = 'navajowhite';
COLOR_WEB[40] = 'papayawhip';
COLOR_WEB[41] = 'moccasin';
COLOR_WEB[42] = 'oldlace';
COLOR_WEB[43] = 'wheat';
COLOR_WEB[44] = 'orange';
COLOR_WEB[45] = 'floralwhite';
COLOR_WEB[46] = 'darkgoldenrod';
COLOR_WEB[47] = 'goldenrod';
COLOR_WEB[48] = 'cornsilk';
COLOR_WEB[49] = 'gold';
COLOR_WEB[50] = 'lemonchiffon';
COLOR_WEB[51] = 'palegoldenrod';
COLOR_WEB[52] = 'khaki';
COLOR_WEB[53] = 'darkkhaki';

COLOR_WEB[54] = 'ivory';
COLOR_WEB[55] = 'lightyellow';
COLOR_WEB[56] = 'beige';
COLOR_WEB[57] = 'lightgoldenrodyellow';
COLOR_WEB[58] = 'yellow';
COLOR_WEB[59] = 'olive';

COLOR_WEB[60] = 'yellowgreen';
COLOR_WEB[61] = 'olivedrab';
COLOR_WEB[62] = 'darkolivegreen';
COLOR_WEB[63] = 'greenyellow';
COLOR_WEB[64] = 'chartreuse';
COLOR_WEB[65] = 'lawngreen';

COLOR_WEB[66] = 'honeydew';
COLOR_WEB[67] = 'palegreen';
COLOR_WEB[68] = 'lightgreen';
COLOR_WEB[69] = 'darkseagreen';
COLOR_WEB[70] = 'lime';
COLOR_WEB[71] = 'limegreen';
COLOR_WEB[72] = 'forestgreen';
COLOR_WEB[73] = 'green';
COLOR_WEB[74] = 'darkgreen';

COLOR_WEB[75] = 'mediumseagreen';
COLOR_WEB[76] = 'seagreen';
COLOR_WEB[77] = 'mintcream';
COLOR_WEB[78] = 'springgreen';
COLOR_WEB[79] = 'mediumaquamarine';
COLOR_WEB[80] = 'turquoise';
COLOR_WEB[81] = 'lightseagreen';
COLOR_WEB[82] = 'mediumturquoise';
COLOR_WEB[83] = 'mediumspringgreen';
COLOR_WEB[84] = 'aquamarine';

COLOR_WEB[85] = 'azure';
COLOR_WEB[86] = 'lightcyan';
COLOR_WEB[87] = 'paleturquoise';
COLOR_WEB[88] = 'cyan';
COLOR_WEB[89] = 'aqua';
COLOR_WEB[90] = 'darkcyan';
COLOR_WEB[91] = 'teal';
COLOR_WEB[92] = 'darkslategray';

COLOR_WEB[93] = 'cadetblue';
COLOR_WEB[94] = 'darkturquoise';
COLOR_WEB[95] = 'powderblue';
COLOR_WEB[96] = 'lightblue';
COLOR_WEB[97] = 'deepskyblue';
COLOR_WEB[98] = 'skyblue';
COLOR_WEB[99] = 'lightskyblue';
COLOR_WEB[100] = 'steelblue';
COLOR_WEB[101] = 'aliceblue';
COLOR_WEB[102] = 'dodgerblue';
COLOR_WEB[103] = 'lightslategray';
COLOR_WEB[104] = 'slategray';
COLOR_WEB[105] = 'lightsteelblue';
COLOR_WEB[106] = 'cornflowerblue';
COLOR_WEB[107] = 'royalblue';

COLOR_WEB[108] = 'ghostwhite';
COLOR_WEB[109] = 'lavender';
COLOR_WEB[110] = 'blue';
COLOR_WEB[111] = 'mediumblue';
COLOR_WEB[112] = 'darkblue';
COLOR_WEB[113] = 'midnightblue';
COLOR_WEB[114] = 'navy';

COLOR_WEB[115] = 'mediumslateblue';
COLOR_WEB[116] = 'slateblue';
COLOR_WEB[117] = 'darkslateblue';
COLOR_WEB[118] = 'mediumpurple';
COLOR_WEB[119] = 'blueviolet';
COLOR_WEB[120] = 'indigo';
COLOR_WEB[121] = 'darkorchid';
COLOR_WEB[122] = 'darkviolet';
COLOR_WEB[123] = 'mediumorchid';

COLOR_WEB[124] = 'thistle';
COLOR_WEB[125] = 'plum';
COLOR_WEB[126] = 'violet';
COLOR_WEB[127] = 'magenta';
COLOR_WEB[128] = 'fuchsia';
COLOR_WEB[129] = 'darkmagenta';
COLOR_WEB[130] = 'purple';

COLOR_WEB[131] = 'orchid';
COLOR_WEB[132] = 'mediumvioletred';
COLOR_WEB[133] = 'deeppink';
COLOR_WEB[134] = 'hotpink';
COLOR_WEB[135] = 'lavenderblush'; 
COLOR_WEB[136] = 'palevioletred';
COLOR_WEB[137] = 'crimson';
COLOR_WEB[138] = 'pink';
COLOR_WEB[139] = 'lightpink';


// 数値のチェック
////////////////////////////////////////////////////
function winIsNumeric(num) {
	if (num == undefined)
		return false;

    if (num.match(/[^0-9]/g)) {
        return false;
    }
    return true;
}


// DOMオブジェクトを取得
////////////////////////////////////////////////////
function winGetObject(name) {
	if (window.document[name])  {
	  return window.document[name];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1) {
		if (document.embeds && document.embeds[name])
		  return document.embeds[name]; 
	}
	
	if (document.getElementById) {
		return document.getElementById(name);
	}
}

// Documentを取得
////////////////////////////////////////////////////
function winGetDocument()
{
	if (window.document != undefined) {
		return window.document;
	}
	return document;
}

function winObject(id, style, title) {

	this.Init = function(id, style, title) {
		var me = this;

		// 対象ウインドウがあるか確認
		var obj = winGetObject(id);
		if (obj == undefined)
			return false;

		// システムコントロールの生成	
		var systemHtml = ""; 
		if (style & WS_CLOSEBUTTON) {
			systemHtml = "<span id='" + id + "_close' class='window_close'>×</span>";
		}

		// タイトルバーの生成
		var titleHtml = "";
		if (style & WS_TITLEBAR) {
			titleHtml = systemHtml + "<div id='" + id + "_title' class='window_title'>" + title + "</div>";
		}

		// アンダーシステムコントロールの生成	
		var underHtml = ""; 
		if (style & WS_SIZEBUTTON) {
			underHtml = "<span id='" + id + "_size' class='window_size'>&equiv;</span>";
		}

		// タイトルバーの生成
		var statusHtml = "";
		if (style & WS_STATUSBAR) {
			statusHtml = "<div id='" + id + "_status' class='window_status'>&nbsp;" + underHtml + "</div>";
		}

		// インターフェイスを更新
		html = obj.innerHTML;
		obj.innerHTML = titleHtml + html + statusHtml;

		// 初期化
		this.WindowName		= id;
		this.WindowObject	= null;
		this.OldX			= 0;
		this.OldY			= 0;
		try {
			if (document.all) {
				this.Height = parseInt(obj.offsetHeight);
			} else {
				this.Height = parseInt(obj.style.height);
			}
		} catch (e) {
		}

		// タイトルイベント	
		var obj_title = winGetObject(id+'_title');
		if (obj_title != undefined) {
			obj_title.onmousedown	= function(e){me.winOnMouseDown(e);}; //this.winOnMouseDown;
		}

		// システムイベント
		var obj_close = winGetObject(id+'_close');
		if (obj_close != undefined) {
			obj_close.onmousedown	= function(e){me.winOnClose(e);}; //this.winOnClose;
		}

		// アンダーシステムイベント
		var obj_size = winGetObject(id+'_size');
		if (obj_size != undefined) {
			obj_size.onmousedown	= function(e){me.winOnResize(e);}; //this.winOnClose;
		}
	}
	
	this.TheNode = function() { return winGetObject(this.WindowName); }

	this.Show = function(bShow)
	{
		var obj = this.TheNode();
		if (obj == undefined)
			return false;

		obj.style.display = bShow;
		if (bShow !='none')
			this.ResetPosition();
		return false;
	}

	this.AppendChild = function(child)
	{
		var obj = this.TheNode();
		if (obj == undefined)
			return false;

		obj.appendChild(child);
	}
	this.RemoveChild = function(child)
	{
		if (!child)
			return false;

		var obj = this.TheNode();
		if (obj == undefined)
			return false;

		obj.removeChild(child);
	}

	// ウインドウ制御
	this.GetWindowWidth = function(){
		if (document.documentElement) {
			return document.documentElement.clientWidth;
		} else if (document.all) {
			return document.body.clientWidth;
		} else {
			return window.innerWidth;
		}
		return 0;
	}
	this.GetWindowHeight = function(){
		if (document.documentElement) {
			return document.documentElement.clientHeight;
		} else if (document.all) {
			return document.body.clientHeight;
		} else {
			return window.innerHeight;
		}
		return 0;
	}
	
	this.GetPageOffsetX = function() {
		var x = 0;

		if (document.body) {
			x = document.body.scrollLeft;
		}
		if (x==0 && document.documentElement) {
			x = document.documentElement.scrollLeft;
		}
		if (x!=0)
			return x;

		/*
		if (window.pageXOffset) {
			return window.pageXOffset;

		} else {
			return window.scrollX;
		}
		*/
		return 0;
	}

	this.GetPageOffsetY = function() {
		var y = 0;
		if (document.body) {
			y = document.body.scrollTop;

		}
		if (y==0 && document.documentElement) {
			y = document.documentElement.scrollTop;

		}
		if (y!=0)
			return y;

		/*
		if (window.pageYOffset) {
			return window.pageYOffset;

		} else {
			return window.scrollY;

		}
		*/
		return 0;
	}

	this.ResetPosition = function()
	{
		var obj = this.TheNode();
		if (!obj)
			return ;

		if (document.all) {
			obj.style.pixelLeft	= this.GetWindowWidth()/2-obj.clientWidth/2 + this.GetPageOffsetX();
			obj.style.pixelTop	= this.GetWindowHeight()/2-obj.clientHeight/2 + this.GetPageOffsetY();
			//obj.style.pixelLeft	= obj.offsetLeft;
			//obj.style.pixelTop	= obj.offsetTop;
		} else {
			obj.style.left	= (this.GetWindowWidth()/2-obj.clientWidth/2 + this.GetPageOffsetX()) +'px';
			obj.style.top	= (this.GetWindowHeight()/2-obj.clientHeight/2 + this.GetPageOffsetY()) +'px';
			//obj.style.left	= obj.offsetLeft+'px';
			//obj.style.top	= obj.offsetTop +'px';
		}
	}

	this.SetX = function(x)
	{
		var obj = this.TheNode();
		if (!obj)
			return ;

		if (document.all) {
			obj.style.pixelLeft	= this.GetPageOffsetX()+x;
		} else {
			obj.style.left	= x +'px';
		}
	}

	this.SetY = function(y)
	{
		var obj = this.TheNode();
		if (!obj)
			return ;

		if (document.all) {
			obj.style.pixelTop	= this.GetPageOffsetY()+y;
		} else {
			obj.style.top	= y +'px';
		}
	}

	// イベント処理
	/////////////////////////////////////////////

	this.winOnClose = function(e) 
	{
		this.Show('none');
	
		return false;
	}
	
	this.winOnResize = function(e) 
	{
		this.WindowObject = this.TheNode();
		if (this.WindowObject == undefined)
			return;
		
		var h;
		if (document.all) {
			h = parseInt(this.WindowObject.style.pixelHeight);
		} else {
			h = parseInt(this.WindowObject.style.height);
		}

		if (!h || this.Height == h) {
			h = MAX_HEIGHT + 'px';
		} else {
			if (!this.Height)
				h = "";
			else
				h = this.Height +'px';
		}

		if (document.all) {
			this.WindowObject.style.pixelHeight = parseInt(h);
		} else {
			this.WindowObject.style.height = h;
		}

		return false;
	}

	this.winOnMouseDown = function(e) 
	{
		//gWinObject = this;
		this.WindowObject = this.TheNode();
	
		if (!this.WindowObject) {
			return true;
		}
		
		// マウスダウンしたらドラッグモードに以降
		var me = this;
		var doc = winGetDocument();
		if (doc == undefined)
			return false;
		doc.onmousemove			= function(e){me.winOnMouseMove(e);}; //this.winOnMouseMove;
		doc.onmouseup			= function(e){me.winOnMouseUp(e);}; //this.winOnMouseUp;
	
		if (document.all) {
		  this.OldX = event.clientX;// + document.body.scrollLeft;
		  this.OldY = event.clientY;// + document.body.scrollTop;
		} else if (this.WindowObject.getElementsByTagName) {
		  this.OldX = e.pageX;
		  this.OldY = e.pageY;
		}
	
		//if (gWinObject.getElementsByTagName) {
			this.WindowObject.style.left = this.WindowObject.offsetLeft + 'px';
			this.WindowObject.style.top = this.WindowObject.offsetTop + 'px';
		//}
		return false;
	}
	
	this.winOnMouseMove = function(e) 
	{
		if (!this.WindowObject) {
			return true;
		}
		
		if (document.all) {
		  nowX = event.clientX;// - document.body.scrollLeft;
		  nowY = event.clientY;// - document.body.scrollTop;
		} else if (this.WindowObject.getElementsByTagName) {
		  nowX = e.pageX;
		  nowY = e.pageY;
		}
	
		vx = nowX-this.OldX;
		vy = nowY-this.OldY;
		posx = parseInt(this.WindowObject.style.left) + vx;
		posy = parseInt(this.WindowObject.style.top) + vy;
	
		/*if (document.documentElement) {
			this.WindowObject.style.left = posx;// + document.documentElement.scrollLeft;
			this.WindowObject.style.top = posy;// + document.documentElement.scrollTop;
		} else */
		if (document.all) {
			this.WindowObject.style.left = posx;// + document.documentElement.scrollLeft;
			this.WindowObject.style.top = posy;// + document.documentElement.scrollTop;
			//this.WindowObject.style.left = vx + document.body.scrollLeft;
			//this.WindowObject.style.top = vy + document.body.scrollTop;
		} else {
			this.WindowObject.style.left = posx + 'px';
			this.WindowObject.style.top = posy + 'px';
	
		}
	
		this.OldX = nowX;
		this.OldY = nowY;
	
		return false;
	}
	
	this.winOnMouseUp = function(e) 
	{
	   this.WindowObject = null;
	}
	
	this.winOnMouseOut = function(e) 
	{
	   this.WindowObject = null;
	}

	this.Init(id, style, title);

}

function clsColorPalette(id, style, title) 
{
	this.Init = function(id, style, title)
	{
		this.winObject = new winObject(id, style, title);
	}
	
	this.MakePalette = function(funcColor) {
		if (!this.winObject)
			return false;

		// 一度、このパレットを削除
		this.winObject.RemoveChild(this.palette);
		this.palette = null;

		var doc = winGetDocument();
		if (doc == undefined)
			return false;

		var div = doc.createElement("div");
		if (div == undefined)
			return false;

		var i;
		for (i=0; i<COLOR_WEB.length; i+=8) {
			var colors = new Array();
			for (c=0; c<8; c++) {
				if ((i+c)<COLOR_WEB.length)
					colors[c] = COLOR_WEB[i+c];
			}
			this.MakeColor12(div, colors, funcColor);
		}
		//this.MakeColor12(div, COLOR_HUE, funcColor);
		//this.MakeColor12(div, COLOR_GRAY, funcColor);

		this.palette = div;
		this.winObject.AppendChild(this.palette);
	}

	this.MakeColor12 = function(div, colors, funcColor) {
		var i;
		var doc = winGetDocument();
		if (doc == undefined)
			return false;

		var ul = doc.createElement("ul");
		if (ul == undefined)
			return false;
		ul.style.listStyleType = "none";
		ul.style.margin		= '0px';
		ul.style.padding	= '2px';
		ul.style.clear		="both";

		for (i=0; i<colors.length; i++) {
			var li = doc.createElement("li");
			if (li == undefined)
				break;
			var text = doc.createElement("text");
			if (text == undefined)
				break;

			if (document.all) 
				li.style.styleFloat = "left";
			else
				li.style.cssFloat = "left";
			li.margin = "1px";
			li.padding = "1px";

			if (!winIsNumeric(colors[i])) {
				html = "<a href='javascript:void(0);' onclick=\"" + funcColor(colors[i]) + " return false;\">";
				html+= "<font color='" + colors[i] + "'>■</font>";
			} else {
				html = "<a href='javascript:void(0);' onclick=\"" + funcColor("#" + (0x1000000 + colors[i]).toString(16).substr(1,6)) + " return false;\">";
				html+= "<font color='#" + (0x1000000 + colors[i]).toString(16).substr(1,6) + "'>■</font>";
			}
			html+= "</a>";
			text.innerHTML = html;
				
			li.appendChild(text);
			ul.appendChild(li);
		}
		div.appendChild(ul);
	}
	
	this.TheWindow = function() { return this.winObject; }
	this.TheNode = function() { return this.palette; }
	this.TheParentNode = function() {
		if (!this.winObject)
			return null;
		return this.winObject.TheNode();
	}

	this.Init(id, style, title);
}

