// Neon Sign Program - Version 1.00 copyright by mi-ya 2010/04/14

pattern ="fo eooo foo eooo fo ac bd gh ko ij lo moo fo noo fooo"; //点灯パターン
nsel = "div.neon"; //セレクター名

ctable = new Array("black","red","yellow","lime","aqua","blue","fuchsia","white");
colornum = 1;
seqnum = 0;
chrnum = 0;
nlen = 0;
nstr = "";

function grdcol(col,bright) //グラデーションを生成
	{
	switch(col)
		{
		case 1: return "rgb(" + bright + "%,0%,0%)";
		case 2: return "rgb(" + bright + "%," + bright + "%,0%)";
		case 3: return "rgb(0%," + bright + "%,0%)";
		case 4: return "rgb(0%," + bright + "%," + bright +"%)";
		case 5: return "rgb(0%,0%," + bright +"%)";
		case 6: return "rgb(" + bright + "%,0%," + bright +"%)";
		case 7: return "rgb(" + bright + "%," + bright + "%," + bright +"%)";
		}
	}

function rndcol() //ランダムな色番号を生成
	{
	return Math.floor(Math.random() * 7.0) + 1;
	}

function setcol(pos, col) //ポジションの文字の色をセット
	{
	$("#neon" + pos).css("color",ctable[col]);
	}

function waitwait() //一定時間待機
	{
	chrnum++;

	if (chrnum >= 5)
		{
		chrnum = 0;
		seqnum++;
		}
	}

function setall(col) //全文字色をセット
	{
	for (i = 0; i < nlen; i++)
		{
		setcol(i,col);
		}

	chrnum = 0;
	seqnum++;
	}

function setgrad(col,bright) //全文字を明度指定でセット
	{
	for (i = 0; i < nlen; i++)
		{
		$("#neon" + i).css("color",grdcol(col,bright));
		}

	chrnum++;

	if (chrnum > 20)
		{
		chrnum = 0;
		seqnum++;
		}
	}

function setstep_r(col) //右へ一文字ずつセット
	{
	setcol(chrnum,col);

	chrnum++;

	if (chrnum >= nlen)
		{
		chrnum = 0;
		seqnum++;
		}
	}

function setstep_l(col) //左へ一文字ずつセット
	{
	setcol(nlen - chrnum - 1,col);

	chrnum++;

	if (chrnum >= nlen)
		{
		chrnum = 0;
		seqnum++;
		}
	}

function setflow_r(col1,col2) //右へ流れるようにセット
	{
	for (i = 0; i < nlen; i++)
		{
		if ((i >= chrnum)&&(i <= (chrnum + 2)))
			{
			setcol(i,col2);
			}
		else
			{
			setcol(i,col1);
			}
		}

	chrnum++;

	if (chrnum >= nlen)
		{
		setall(col1);
		chrnum = 0;
		seqnum++;
		}
	}

function setflow_l(col1,col2) //左へ流れるようにセット
	{
	for (i = nlen - 1; i >= 0; i--)
		{
		if ((i <= (nlen - chrnum - 1))&&(i >= (nlen - chrnum - 3)))
			{
			setcol(i,col2);
			}
		else
			{
			setcol(i,col1);
			}
		}

	chrnum++;

	if (chrnum >= nlen)
		{
		setall(col1);
		chrnum = 0;
		seqnum++;
		}
	}

function update()
	{
	switch(pattern.charAt(seqnum))
		{
		case 'a': setstep_r(colornum); break; //右へ一文字ずつ点灯
		case 'b': setstep_l(colornum); break; //左へ一文字ずつ点灯
		case 'c': setstep_r(0); break; //右へ一文字ずつ消灯
		case 'd': setstep_l(0); break; //左へ一文字ずつ消灯
		case 'e': setall(colornum); break; //全文字を点灯
		case 'f': setall(0); break; //全文字を消灯
		case 'g': setflow_r(0,colornum); break; //右へ流れるように点灯
		case 'h': setflow_l(0,colornum); break; //左へ流れるように点灯
		case 'i': setflow_r(colornum,0); break; //右へ流れるように消灯
		case 'j': setflow_l(colornum,0); break; //左へ流れるように消灯
		case 'k': setgrad(colornum,chrnum * 5); break; //全文字をだんだんに点灯
		case 'l': setgrad(colornum,100 - chrnum * 5); break; //全文字をだんだんに消灯
		case 'm': setstep_r(rndcol()); break; //右へ一文字ずつランダムな色で点灯
		case 'n': setstep_l(rndcol()); break; //左へ一文字ずつランダムな色で点灯
		case 'o': waitwait(); break; //一定時間待機
		default: seqnum++;
		}

	if (seqnum >= pattern.length)
		{
		colornum++;

		if (colornum >= ctable.length)
			{
			colornum = 1;
			}

		seqnum = 0;
		}
	}

$(document).ready(function()
	{
	nstr = $(nsel).text();
	nlen = nstr.length;

	$(nsel).empty();

	for (i = 0; i < nlen; i++)
		{
		$(nsel).append('<span id="neon' + i + '">' + nstr.charAt(i) + '</span>');
		}

	setInterval("update()","50");
	});
