/*
 *  きせかえシステム"siori" Ver.2.40
 *  Copyright (C)2005.3.20 by みなみせい (http://ig.sakura.ne.jp/~sei/)
 */
var PREF_CEL = "cel-";

var cels = [];
var group = [];
var mvcels;

var orgX;
var orgY;
var restorePixel;
var snapPixel = 16;

var posMode;
var posNo;


/*
 *  セルのオブジェクト
 */
function Cel(name, click)
{
	this.name	= name;
	this.click	= (!isNaN(parseInt(click))) ? parseInt(click) : 0;
	this.el;
	this.snapX;
	this.snapY;
}


/*
 *  グループ化セルのオブジェクト
 */
function Group()
{
	this.item	= [];
}

Group.prototype.append = function(obj)
{
	this.item[this.item.length] = obj;
	return this;
}

Group.prototype.snap = function(range)
{
	var		i, x, y;
	var		wk = this.item[0];

	if (!isNaN(parseInt(wk.snapX)) && !isNaN(parseInt(wk.snapY))) {
		x = wk.snapX - parseInt(wk.el.style.left);
		y = wk.snapY - parseInt(wk.el.style.top);

		if ((range > Math.abs(x)) && (range > Math.abs(y))) {
			for (i = 0; i < this.item.length; i++) {
				this.item[i].el.style.left = parseInt(this.item[i].el.style.left) + x + "px";
				this.item[i].el.style.top  = parseInt(this.item[i].el.style.top)  + y + "px";
			}
		}
	}
}


/*
 *  移動セルのオブジェクト
 */
function MoveCels(no)
{
	var		i;

	this.item	= (cels[no].name) ? group[cels[no].name].item : [cels[no]];
	this.orgX	= [];
	this.orgY	= [];

	with (this) {
		for (i = 0; i < item.length; i++) {
			orgX[i] = parseInt(item[i].el.style.left);
			orgY[i] = parseInt(item[i].el.style.top);
		}
	}
}

MoveCels.prototype = new Group();


/*
 *  パラメータの解析
 */
function parseParam(arg)
{
	var		i, wk, args = [];

	wk = arg.replace(/\s/g, "").split(";");

	for (i = 0; i < wk.length; i++) {
		args[i] = wk[i].replace(/:/, ",").split(",");
	}

	return args;
}


/*
 *  イベント処理
 */
function mouseDown(e)
{
	if (document.all) {
		orgX = event.clientX + document.body.scrollLeft;
		orgY = event.clientY + document.body.scrollTop;
	}
	else {
		orgX = e.pageX;
		orgY = e.pageY;
	}

	mvcels = new MoveCels(this.id.substr(PREF_CEL.length));

	with (mvcels.item[0]) {
		click--;
		restorePixel = (15 <= click) ? 0 : ((0 <= click) ? 2 : null);
	}

	return false;
}


function mouseMove(e)
{
	var		i, x, y;

	if (!mvcels) {
		return true;
	}

	if (document.all) {
		x = event.clientX + document.body.scrollLeft - orgX;
		y = event.clientY + document.body.scrollTop  - orgY;
	}
	else {
		x = e.pageX - orgX;
		y = e.pageY - orgY;
	}

	if (!isNaN(parseInt(restorePixel))) {
		if ((restorePixel < Math.abs(x)) || (restorePixel < Math.abs(y))) {
			x = y = 0;
		}
	}

	for (i = 0; i < mvcels.item.length; i++) {
		mvcels.item[i].el.style.left = mvcels.orgX[i] + x + "px";
		mvcels.item[i].el.style.top  = mvcels.orgY[i] + y + "px";
	}

	return false;
}


function mouseUp(e)
{
	var		i;

	if (!mvcels) {
		return;
	}

	if (!isNaN(parseInt(restorePixel))) {
		for (i = 0; i < mvcels.item.length; i++) {
			mvcels.item[i].el.style.left = mvcels.orgX[i] + "px";
			mvcels.item[i].el.style.top  = mvcels.orgY[i] + "px";
		}
	}
	else {
		mvcels.snap(snapPixel);
	}

	mvcels = null;
}


function sioriInit()
{
	var		i;

	for (i = 0; i < cels.length; i++) {
		cels[i].el = (document.all) ? document.all[PREF_CEL + i] : document.getElementById(PREF_CEL + i);
		cels[i].el.onmousedown = mouseDown;
	}

	document.onmousemove = mouseMove;
	document.onmouseup   = mouseUp;
}


/*
 *  セット切り替え
 */
function startpos(mode)
{
	var		i;

	if ("cel" == mode) {
		for (i = 0; i < cels.length; i++) {
			cels[i].el.style.visibility = "hidden";
		}
	}
	else if ("snap" == mode) {
		for (i = 0; i < cels.length; i++) {
			cels[i].snapX = null;
			cels[i].snapY = null;
		}
	}

	posMode = mode;
	posNo   = 0;
}


function setpos(arg)
{
	var		i, x, y, args;

	args = parseParam(arg);

	for (i = 0; i < args.length; i++) {
		if (cels.length <= posNo) {
			break;
		}

		x = parseInt(args[i][0]);
		y = parseInt(args[i][1]);

		if (!isNaN(x) && !isNaN(y)) {
			if ("cel" == posMode) {
				cels[posNo].el.style.left = x + "px";
				cels[posNo].el.style.top  = y + "px";

				if ("*" != args[i][2]) {
					cels[posNo].el.style.visibility = "visible";
				}
			}
			else if ("snap" == posMode) {
				if ("*" != args[i][2]) {
					cels[posNo].snapX = x;
					cels[posNo].snapY = y;
				}
			}
		}

		posNo++;
	}
}


/*
 *  セルの読み込み
 */
function cel()
{
	var		i, j, wk, name, arg = [];
	var		no = cels.length;

	if (!(document.all || document.getElementById)) {
		return;
	}

	wk = parseParam(arguments[0]);
	for (i = 0; i < wk.length; i++) {
		for (j = 1; j < wk[i].length; j++) {
			arg[wk[i][0] + j] = wk[i][j];
		}
	}

	cels[no] = new Cel(name = arg.name1, arg.click1);

	if (name) {
		if (!group[name]) {
			group[name] = new Group();
		}
		group[name].append(cels[no]);
	}

	document.write(
		'<span id="' + PREF_CEL + no +
		'" style="position:absolute; visibility:hidden; left:0; top:0"><img src="' + arg.src1 +
		'" alt="CelNo.' + no +
		'" title="' + ((arg.title1) ? arg.title1 : "") +
		'"></span>\n');
}


