/*
 *  maid-system  Ver.2.10
 *  Copyright (C)2011.5.5 by みなみせい (http://sei.sakuraweb.com/)
 */
var PREF_IMG = "img";

var maid = {
	color:["#FFFFFF", "#3060FF", "#444444", "#E0DED8"],
	op:[100,90,80,70,60,50,40,30,20,10]
};

var ua = (document.getElementById && [].push) ? {
/*@cc_on
	LE_IE8: (9 > @_jscript_version),
@*/
	fixup: function() {
		ua.STD = ("undefined" != typeof document.getElementsByTagName("body")[0].style.opacity);
	}
} : null;

var pnls = [];
var imgs = [];
var imgno = 0;
var effuse = 1;

/*
 *  その他の関数
 */
function $(nm)
{
	return document.getElementById(nm);
}

function parseParam(arg)
{
	var i, j, wk, wklist = [], arglist = [];

	wk = arg.replace(/\s/g, "").split(";");
	for (i = 0; i < wk.length; i++) {
		wklist[i] = wk[i].replace(/:/, ",").split(",");
	}

	for (i = 0; i < wklist.length; i++) {
		for (j = 1; j < wklist[i].length; j++) {
			arglist[wklist[i][0] + j] = wklist[i][j];
		}
	}

	return arglist;
}

/*
 *  Imgオブジェクト
 */
function Img(src, no)
{
	var op = (ua.LE_IE8) ? 'filter:alpha(opacity=100, enabled=0) blendTrans(duration=0.4)' : 'opacity:1.0';

	this.ld = (ua.LE_IE8 && /\.(jpg|png)$/i.test(src));
	this.no = no;

	if (this.ld) {
		document.write('<span style="position:absolute; left:0; top:0; width:1px; height:1px; ' + op + '"><span id="' + PREF_IMG + no +
			'" style="display:inline-block; position:absolute; left:0; top:0; visibility:hidden; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\')">&nbsp;</span></span>\n');
	}
	else {
		document.write('<img src="' + src + '" id="' + PREF_IMG + no + '" alt="" style="position:absolute; left:0; top:0; visibility:hidden; ' + op + '">\n');
	}
}

Img.prototype = {
	fixup: function() {
		with (this) {
			this.eff = this.img = $(PREF_IMG + no);

			if (ld) {
				eff = eff.parentNode;
				eff.style.width  = img.offsetWidth  + "px";
				eff.style.height = img.offsetHeight + "px";
			}
			eff.style.zIndex = imgno - no -1;
		}
	},

	change: function(src) {
		if (this.ld) {
			try {
				this.img.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src;
			}
			catch (ex) {}
		}
		else {
			this.img.src = src;
		}
	},

	setVisible: function(mode) {
		if (ua.LE_IE8) {
			var wk = this.eff.filters.alpha;
			wk.enabled = (mode && (100 > wk.opacity));
		}
		this.img.style.visibility = (mode) ? "visible" : "hidden";
	},

	setOpacity: function(n) {
		if (ua.LE_IE8) {
			var wk = this.eff.filters.alpha;
			wk.enabled = (100 > n);
			wk.opacity = n;
		}
		else if (ua.STD) {
			this.eff.style.opacity = n / 100;
		}
	}
};

/*
 *  Pnlオブジェクト
 */
function Pnl(arg, item)
{
	var i, n, wk, tb, s1, s2;

	this.opIndex;
	this.img = [];
	this.src = [];
	this.set = [];
	this.item = item || [];

	panel.param.call(this, arg);

	tb = [0,1,17,15,13,11,9,7,5,3,2,4,6,8,10,12,14,16];
	s1 = arg.set1 || "";
	s2 = arg.set2 || "";
	wk = [];

	for (i = 0; i < this.item.length; i++) {
		this.item[i] = parseParam(this.item[i]);
		if (this.item[i].chr1) wk[this.item[i].chr1.charAt(0)] = i;
	}

	for (i = 0; i < s1.length; i++) this.set.push(tb[parseInt(s1.charAt(i), 18) | 0] | (wk[s2.charAt(i)] << 5));

	n = this.chgIndex = this.getset(0).n;
	wk = imgs[arg.group1] || [];

	for (i = 0; i < wk.length; i++) {
		this.src.push(wk[i][0]);
		this.img.push(new Img(this.getsrc(i, n), wk[i][1]));
	}
}

Pnl.prototype = {
	getset: function(n) {
		n = this.set[n];
		return { v:!!(n & 1), o:(n >> 1) & 15, n:(n >>> 5) };
	},

	getsrc: function(i, n) {
		return (this.src[i] || "") + ((this.item[n] && this.item[n].src1) || "");
	},

	eff: function(func) {
		for (var i = 0; i < this.img.length; i++) {
			if (effuse && ua.LE_IE8 && $("effect-on").checked) {
				this.img[i].eff.filters.blendTrans.apply();
				func.call(this, i);
				this.img[i].eff.filters.blendTrans.play();
			}
			else {
				func.call(this, i);
			}
		}
	},

	visible: function(mode) {
		this.eff(function(i){
			this.img[i].setVisible(mode);
		});
	},

	opacity: function(index) {
		this.eff(function(i){
			this.img[i].setOpacity(maid.op[index]);
		});
		this.opIndex = index;
	},

	change: function(index) {
		if (this.chgIndex == index) return;

		this.eff(function(i){
			this.img[i].change(this.getsrc(i, index));
		});
		this.chgIndex = index;
	}
};

/*
 *  パラメータ取り込み
 */
function img(arg, item)
{
	if (!ua) return;
	arg = parseParam(arg);

	var g = arg.group1 || (arg.group1 = "_" + imgno);

	if (!imgs[g]) imgs[g] = [];
	imgs[g].push([arg.src1, imgno++]);

	if ("_" == g.charAt(0)) pnls.push(new Pnl(arg, item));
}

function pnl(arg, item)
{
	if (!ua) return;
	pnls.push(new Pnl(parseParam(arg), item));
}

/*
 *  パネル制御で使用する関数
 */
function syncImg(no, index, func)
{
	var i, wk, ct = 300, tmp = [], load = function(obj, n){
		var i, wk;
		if (obj.chgIndex == n) return;

		for (i = 0; i < obj.src.length; i++) {
			wk = tmp[tmp.push(new Image()) -1];
			wk.onload = wk.onerror = wk.onabort = function(){ this.$ok = true; };
			wk.src = obj.getsrc(i, n);
		}
	};

	if (-1 == no) {
		for (i = 0; i < pnls.length; i++) load(pnls[i], pnls[i].getset(index).n);
	}
	else {
		load(pnls[no], index);
	}

	(wk = function(){
		for (i = 0; (i < tmp.length) && (0 < ct); i++) {
			if (!(tmp[i].complete || tmp[i].$ok)) {
				ct--;
				setTimeout(wk, (-1 == no) ? 200 : 100);
				return;
			}
		}
		setTimeout(func, 1);
	})();
}

function applySet(index)
{
	syncImg(-1, index, function(){
		var i, wk, rv;

		effuse = 0;
		for (i = 0; i < pnls.length; i++) {
			wk = pnls[i];
			rv = wk.getset(index);

			wk.visible(rv.v);
			wk.opacity(rv.o);
			wk.change(rv.n);
		}
		effuse = 1;
	});
}

/*
 *  スタートアップ
 */
function init()
{
	var i, j, flag;

	if (!pnls.length) return;
	ua.fixup();

	for (i = 0; i < pnls.length; i++) {
		for (j = 0; j < pnls[i].img.length; j++) {
			pnls[i].img[j].fixup();
		}
	}

	panel.init();
}

/*
 *  パネル制御処理
 *  setTimeout(,1) はIEの表示乱れ防止のため使用
 */
var panel = new function() {
	var PREF_CHK  = "chk";
	var PREF_CHG  = "chg";
	var PREF_NAME = "name";
	var curNo = -1, op;

	function focus(mode) {
		var wk = $(PREF_NAME + curNo).style;
		wk.color = maid.color[(mode) ? 0 : 2];
		wk.backgroundColor = maid.color[(mode) ? 1 : 3];
	}

	this.param = function(arg) {
		this.ctrl = arg.ctrl1 || 3;
		this.name = arg.name1;
	};

	this.init = function() {
		var i, j, wk, bf, dsp;

		bf = '<table>\n';
		for (i = 0; i < pnls.length; i++) {
			dsp = (pnls[i].ctrl & 2) ? '' : ' style="display:none"';

			bf += '<tr' + dsp + '><td><input type="checkbox" id="' + PREF_CHK + i +
				'" onclick="panel.checkItem(' + i +
				',this.checked)"' + ((pnls[i].ctrl & 1) ? '' : ' disabled') +
				'></td><td class="name" id="' + PREF_NAME + i +
				'" onmousedown="panel.selectItem(' + i +
				')">' + (pnls[i].name || i) + '</td></tr>\n';

			if ((wk = pnls[i].item).length) {
				bf += '<tr class="chg"' + dsp + '><td>&nbsp;</td><td>\n';

				for (j = 0; j < wk.length; j++) {
					bf += '  <input type="radio" name="' + PREF_CHG + i +
						'" id="' + (PREF_CHG + i + '-' + j) +
						'" onclick="panel.changeItem(' + i + ',' + j +
						')"><label for="' + (PREF_CHG + i + '-' + j) +
						'">' + (wk[j].name1 || (i + '-' + j)) + '</label><br>\n';
				}

				bf += '</td></tr>\n';
			}
		}

		bf += '</table>\n';
		$("itemlist").innerHTML = bf;

		for (i = 0; (wk = (i < pnls.length)) && !(pnls[i].ctrl & 2); i++);
		curNo = (wk) ? i : 0;
		focus(1);

		op = $("oplist");
		this.changeSet(0);
	};

	this.checkItem = function(no, mode) {
		pnls[no].visible(mode);
	};

	this.selectItem = function(no) {
		var ctrl = !!(pnls[no].ctrl & 1);

		focus(0);
		curNo = no;
		focus(1);

		if (ctrl) {
			$(PREF_CHK + no).checked = true;
			pnls[no].visible(true);
		}
		op.disabled = !ctrl;
		op.selectedIndex = pnls[no].opIndex;
	};

	this.changeItem = function(no, index) {
		syncImg(no, index, function(){
			if (no != curNo) panel.selectItem(no);
			pnls[no].change(index);
		});
	};

	this.changeOp = function(index) {
		if (-1 == curNo) return;
		setTimeout(function(){ pnls[curNo].opacity(index); }, 1);
	};

	this.changeSet = function(index) {
		var i, wk, rv;

		if (-1 == curNo) return;
		op.disabled = !(pnls[curNo].ctrl & 1);
		op.selectedIndex = pnls[curNo].getset(index).o;

		for (i = 0; i < pnls.length; i++) {
			wk = pnls[i];
			rv = wk.getset(index);

			if (wk.item.length) $(PREF_CHG + i + '-' + rv.n).checked = true;
			$(PREF_CHK + i).checked = rv.v;
		}

		applySet(index);
	};
};

var changeOp  = panel.changeOp;
var changeSet = panel.changeSet;

