// ==UserScript==
// @name		teblogEXL_light.js
// @namespace	http://tbp.d2no.info/
// @include		http://tegaki.pipa.jp/*/*.html
// @include		http://tegaki.pipa.jp/VEditBlogS*.jsp*
// @include		http://tegaki.pipa.jp/VEditMessage*.jsp*
// @include     http://tegaki.pipa.jp/VEditBlogByTemplate.jsp*
// @include     http://tegaki.pipa.jp/VEditTemplate.jsp*
// @version		0.1.0.0(beta)
// ==/UserScript==

// 手書きブログ編集機能拡張ツール Light ver0.1

(function(){

// 設定値
	teblogEXL={
		settings:{ /* bool=1か0,int=半角数値 */
			cusTransEx:   1, // 透明度拡張(bool)
			cusWidthEx:   1, // 太さ拡張(bool)
			cusDeflock:   1, // リンクロック(bool)
			cusUraUse:    1, // 裏ペン使用(bool)
			cusTeUse:     1, // てのひらボタン分離(bool)
			cusOutLine:   1, // フチ色デフォルト(0=反転/1=明/2=暗/3=ラ)
			cusWrapper: 700, // パレットが下がる場合変更する(int)
			debug: 0 // デバッグモード
		}
// 設定終わり

//--main--//
		,x:function(a,b){return document.evaluate(a,(b?b:document),null,9,null).singleNodeValue}
		,a:function(a,b){var a=document.evaluate(a,(b?b:document),null,7,null),res=new Array(),i,l;for(i=0,l=a.snapshotLength;i<l;i++){res.push(a.snapshotItem(i))}return res}
		,bind:function(a,b,c,d){return a.addEventListener(b,c,(d?true:false))}
		,click:function(a,b,c){return a.addEventListener('click',b,(c?true:false))}
		,c:function(a,b,c){var e=document.createElement(a);if(b)e.id=b;if(c)e.className=c;return e;}
		,id:function(a){return document.getElementById(a);}
		,append:function(a,b){a.appendChild(b);}
		,textNode:function(a){return document.createTextNode(a);}
		,each:function(a,b){for(var i=0,l=a.length;i<l;++i){if(b(a[i],i))break}return a}
		,unbind:function(a,b,c){return a.removeEventListener(b,c,false)}
		,toggle:function(a){a.style.display=(a.style.display=='none')?'block':'none'}
		,addClass:function(a,b){a.className+=' '+b}
		,delClass:function(a,b){if(!a)return;var c=a.className.split(' ');if(b instanceof Array){for(var i=0,l=b.length;i<l;i++){if(c.indexOf(b[i])>-1)c.splice(c.indexOf(b[i]),1)}}else{if(c.indexOf(b)>-1)c.splice(c.indexOf(b),1)}a.className=c.join(' ')}
		,val:function(a,b){if(typeof a=='string'){a=$.id(a)?$.id(a):$.x('//*[@name="'+a+'"]');}if(b!=undefined){a.value=b;}else{return a.value;}}
		,item:function(a){var i=0,tmp=a;while(tmp=tmp.previousSibling){if(tmp.nodeType==1)i++}return i}
		,idx:function(a,b){var i=0,tmp=a;while(tmp=tmp.previousSibling){if(tmp.nodeType==1&&tmp.childNodes[0].className.indexOf(b)>-1)i++}return i}
		,hex2rgb:function(c){var p=parseInt('0x'+(c.charAt(0)=="#"?c.substring(1):c)),r=(p&0xff0000)>>>0x10,g=(p&0x00ff00)>>>0x08,b=(p&0x0000ff)>>>0x00; return [r,g,b];}
		,rgb2hex:function(r,g,b){return ((r<16)?'0':'')+parseInt(r).toString(16)+((g<16)?'0':'')+parseInt(g).toString(16)+((b<16)?'0':'')+parseInt(b).toString(16);}
		,dec2hex:function(n){var n=parseInt(n).toString(16);while(n.length<6)n='0'+n;return n;}
		,mindec2hex:function(n){return n>-1?$.dec2hex(n):$.dec2hex(16777215+parseInt(n)+1);}
		,hex2dec:function(n){return parseInt('0x'+n).toString(10);}
		,styleColor:function(c){if(c.indexOf('#')<0){c=c.match(/\d+/g);c=$.rgb2hex(c[0],c[1],c[2]);}else if(c.indexOf('#')==0){c=c.substr(1);}return c;}
		,rgb2hsv:function(r,g,b){var h,s,v;h=s=v=0,max=Math.max(r,g,b),min=Math.min(r,g,b),t=max-min;if(t>0){if(g==max){h=(b-r)/t*60+120;}else if(b==max){h=(r-g)/t*60+240;}else{h=(g-b)/t*60;}if(h<0)h+=360;h=parseInt(h);s=parseInt(t/max*0xff);}else{h=0;s=0;}v=max;return[h,s,v];}
		,hsv2rgb:function(h,s,v){var r,g,b,num1=parseInt(v*(0xff-s)/0xff),num2=parseInt(v*(1-s/0xff*(h%60)/60)),num3=parseInt(v*(1-s/0xff*(1-(h%60)/60)));switch(parseInt(h/60)){case 0:r=v;g=num3;b=num1;break;case 1:r=num2;g=v;b=num1;break;case 2:r=num1;g=v;b=num3;break;case 3:r=num1;g=num2;b=v;break;case 4:r=num3;g=num1;b=v;break;default:r=v;g=num1;b=num2;break;}return Array(r,g,b);}
		,hex2y:function(c){var p=parseInt('0x'+(c.charAt(0)=="#"?c.substring(1):c)),r=(p&0xff0000)>>>0x10,g=(p&0x00ff00)>>>0x08,b=(p&0x0000ff)>>>0x00;var y=(0.3*r)+(0.6*g)+(0.1*b);return y<128?'ffffff':'000000';}
		,insertZero:function(n,d,l){n=n.toString(d);while(n.length<l)n='0'+n;return n;}
		,upperRGB:function(c,d){var r=parseInt(c.substr(0,2),16),g=parseInt(c.substr(2,2),16),b=parseInt(c.substr(4,2),16);r+=d;if(r>0xff){r=0xff;}g+=d;if(g>0xff){g=0xff;}b+=d;if(b>0xff){b=0xff;}return $.insertZero(r,16,2)+$.insertZero(g,16,2)+$.insertZero(b,16,2);}
		,lowerRGB:function(c,d){var r=parseInt(c.substr(0,2),16),g=parseInt(c.substr(2,2),16),b=parseInt(c.substr(4,2),16);r-=d;if(r<0)r=0x00;g-=d;if(g<0)g=0x00;b-=d;if(b<0)b=0x00;return $.insertZero(r,16,2)+$.insertZero(g,16,2)+$.insertZero(b,16,2);}
		,getRandomRGB:function(){var r=parseInt(Math.random()*0xFF).toString(16);r.length==1?r='0'+r:r,g=parseInt(Math.random()*0xFF).toString(16);g.length==1?g='0'+g:g,b=parseInt(Math.random()*0xFF).toString(16);b.length==1?b='0'+b:b;return r+g+b;}
		,getPos:function(a){if(a.offsetParent){var p=$.getPos(a.offsetParent);result={x:(p.x+a.offsetLeft),y:(p.y+a.offsetTop)}}else{result={x:a.offsetLeft,y:a.offsetTop}}return result}
		,getPageSize:function(){var xScroll,yScroll;if(window.innerHeight&&window.scrollMaxY){xScroll=window.innerWidth+window.scrollMaxX;yScroll=window.innerHeight+window.scrollMaxY;}else if(document.body.scrollHeight>document.body.offsetHeight){xScroll=document.body.scrollWidth;yScroll=document.body.scrollHeight;}else{xScroll=document.body.offsetWidth;yScroll=document.body.offsetHeight;}var windowWidth,windowHeight;if(self.innerHeight){if(document.documentElement.clientWidth){windowWidth=document.documentElement.clientWidth;}else{windowWidth=self.innerWidth;}windowHeight=self.innerHeight;}else if(document.documentElement&&document.documentElement.clientHeight){windowWidth=document.documentElement.clientWidth;windowHeight=document.documentElement.clientHeight;}else if(document.body){windowWidth=document.body.clientWidth;windowHeight=document.body.clientHeight;}if(yScroll<windowHeight){pageHeight=windowHeight;}else{pageHeight=yScroll;}if(xScroll<windowWidth){pageWidth=xScroll;}else{pageWidth=windowWidth;}arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight);return arrayPageSize;}
		,s:function(a,b){if(!(a instanceof Array)){a=[a]}for(var i=0,l=a.length;i<l;i++){var c=a[i];for(k in b){if(c.style[k]!=null)c.style[k]=b[k]}}}
		,bc:function(a,c){if(a)$.s(a,{backgroundColor:c})}
		,appendCSS:function(css){$.x('//style')||$.append($.x('//head'),$.c('style'));$.append($.x('//style'),$.textNode(css));}

		,initializePaletteStat:function(){
			var s='',a=['nowColor','nowGradUse','nowTrans','nowShape','nowWidth','nowOutLine','uraColor','uraGradUse','uraTrans','uraShape','uraWidth','uraOutLine','clrNow','uraNow','teNow','textNow','eraserNow','paletteNow','paletteOut','markupNow','recodeNow']
			,d=['000000',0,0,0,2,$.settings.cusOutLine,'ffffff',0,0,0,4,1,1,1,0,0,0];
			$.each(a,function(v){s+='<input type="hidden" id="'+v+'" />';});
			$.append($.x('//body'),$.c('div','paletteStat'));
			$.id('paletteStat').innerHTML=s;
			for(var i=0,l=d.length;i<l;i++) $.val(a[i], d[i]);
		}

		,initializeComment:function(){
			var p=[],m=MODE;

			$.appendCSS(''
				+'#PalletPaneH{background:#fff;color:#000;}'
				+'dl,dt,dd,ul,li{list-style:none;margin:0;padding:0;}#colorPalette{margin:0;padding:0;}'
				+'ul.teblogEx li{float:left;border:1px solid #eee;font-size:11px;cursor:pointer;text-align:center;margin:1px}'
				+'ul.teblogEx li:hover{background:#bcf;}#colorSelect{float:left;}.colorCell{width:11px;height:20px;}.colorGrad{width:11px;height:11px;background:#fff;font-size:13px;}'
				+'#colorList{width:310px}#colorList li{margin:1px;padding:1px;text-align:center;}#colorList li div{border:solid 1px #ddd;margin:0;overflow:hidden;}'
				+'#transList{margin-left:200px;}#transList li{float:left;font-size:9px;letter-spacing:-2px;width:20px;margin:1px;padding:1px;}ul#shapeList li{width:18px;margin:1px;padding:1px;font-size:13px;overflow:hidden;}'
				+'.customRecode{width:12px;height:12px;background:#fff;margin:1px;}ul#outlineList li{margin:1px;padding:1px;width:15px;text-align:center;}ul#hsvList li{width:12px;margin:1px;}'
				+'.selectedColor,.selectedTrans,.selectedShape,.selectedWidth,.selectedOutLine{background-color:#39f;}.useGrad{background-color:#f39 !important;}');

			$.initializePaletteStat();
			var userp = $.loadUserPalette(m);

			$.x('//td[@class="PalleteCommand"][2]').innerHTML=$.createShapeSelect(3);
			var z=$.x('//td[@class="PalleteCommand"]');
			z.innerHTML=$.createTransSelect(0);
			z.parentNode.innerHTML+='<td class="PalleteCommand">'+$.createCustomOutline(2)+'</td>';
			$.id('ColorButton').parentNode.replaceChild($.c('div','colorPalette'),$.id('ColorButton'));
			$.id('colorPalette').innerHTML=$.createPaletteCell(userp,m);

			$.each($.a('id("transList")/li'),function(v){$.click(v,function(e){$.flaSelectTrans(e.target,$.item(e.target));e&&e.preventDefault();});});$.addClass($.x('id("transList")/li[1]'),'selectedTrans');
			$.each($.a('id("shapeList")/li'),function(v){$.click(v,function(e){$.flaSelectShape(e.target,$.item(e.target));e&&e.preventDefault();});});$.addClass($.x('id("shapeList")/li[1]'),'selectedShape');
			$.each($.a('id("outlineList")/li'),function(v){$.click(v,function(e){$.controlOutLine(e);e&&e.preventDefault();});});$.addClass($.x('id("outlineList")/li['+($.settings.cusOutLine-0+1)+']'),'selectedOutLine');
			$.bindPaletteList(); $.addClass($.x('id("colorList")/li[2]'),'selectedColor');
		}

		,initializeEditBlog:function(n){
			var p=[],m=MODE
			,w=($.getCookie('teblogEXL'))?$.getCookie('teblogEXL'):$.settings.cusWrapper
            remove=$.a('//head/link[@rel="stylesheet"]').concat($.id('Header'),$.id('Footer'),$.a('//*[contains(@class,"MyMenu")]'),$.id('MenuR'),$.a('//*[contains(@class,"ContensListTitleEnd")]'),$.a('//*[starts-with(@class,"Ad")]'),$.id('FooterA'));
			$.each(remove,function(v){if(v)v.parentNode.removeChild(v);});
 			$.initializePaletteStat();

			$.appendCSS('#ccp{position:fixed;top:0;right:136px;}'
				+'#Wrapper{width:'+w+'px;margin:0 auto;position:relative;}#teblogEXL{float:left;}dl,dt,dd,ul,li{list-style:none;margin:0;padding:0;}#colorPalette,#customPalette{float:left;margin:0;padding:0;line-height:.7;}'
				+'#colorPalette{width:80px;}#customPalette{width:60px;margin-left:4px;}'
				+'#colorPalette dt,#customPalette dt{float:left;margin:2px 0;padding:2px 0;background:#ddd;font-size:8px;font-family:verdana;text-align:center;width:100%;}'
				+'#colorPalette dd,#customPalette dd{float:left;width:100%;}#colorPalette ul,#customPalette ul{margin-left:auto;margin-right:auto;}'
				+'#colorPalette ul{width:80px;}#customPalette ul{width:60px;}#colorPalette ul li,#customPalette ul li{float:left;border:1px solid #ddd;font-size:11px;cursor:pointer;text-align:center;margin:1px 2px;}'
				+'#txtEx span{border:1px solid #ddd;font-size:11px;cursor:pointer;text-align:center;padding:2px 8px;}'
				+'#txtEx{text-align:right;vertical-align:bottom;font-size:11px;}#txtEx input{cursor:pointer;}'
				+'#pcode{color:#49bae8;font-size:11px;height:16px;}#pcode.def{color:#aaa;}'
				+'#colorPalette ul li:hover,#customPalette ul li:hover,#txtEx span:hover{background:#bcf;}'

				+'.colorCell{width:32px;}.colorGrad{width:24px;background:#fff;font-size:13px;}'
				+'#colorList{width:80px;margin-left:8px;}#colorList li,#lockList{margin:1px 2px;padding:2px;text-align:center;}'
				+'#colorList li div{height:12px;border:solid 1px #eee;margin:0;overflow:hidden;}'
				+'ul#transList li{letter-spacing:-2px;width:14px;margin:1px;padding:4px 1px;}'
				+'ul#transList li:first-child{letter-spacing:-2px;width:78px;margin:1px 0;padding:4px 1px;}'
				+'ul#shapeList li{width:16px;margin:1px;padding:4px 0;font-size:11px;overflow:hidden;}'
				+'ul#widthList li{width:70px;height:14px;min-height:12px;max-height:14px;padding:0 2px;}ul#widthList li div{float:left;}ul#widthList li div.w1{width:16px;padding:2px 3px;}ul#widthList li div.w2{background:#000;width:48px;max-height:14px;}'
				+'ul#zoomList li{width:26px;margin:0 0 0 1px;padding:8px 0;}ul#cmdList li{font-size:36px;margin:1px;}ul#cmdList li div{font-size:11px;}'
				+'ul#zoomList li.zoomReset{width:56px;padding:8px 0;}'
				+'#nowList{width:24px;height:14px;margin:0 auto;border:1px solid #eee;background:#000;}'
				+'#uraList{width:56px;text-align:center;font-size:9px;border:2px solid #444;cursor:pointer;}'
				+'#uraList:hover{border-color:#bcf;}#uraList div{padding:24px 2px;}.ura{background:#000;color:#fff;display:block;}.omote{background:#fff;color:#000;display:none;}'
				+'#textList,#eraserList,#teList{font-size:11px;padding:24px 2px;width:56px;text-align:center;cursor:pointer;border:1px solid #aaa;margin-bottom:2px}'
				+'#textList:hover,#eraserList:hover,#teList:hover{border-color:#bcf;}'
				+'ul#outlineList li{margin:1px 2px;padding:2px;width:90%;text-align:center;}'

				+'#Contents,.ContentsList{width:100%;}#EditBlogPane,#EditMsgPane{float:left;margin:0;border:solid 1px #ddd;width:480px;height:640px;}'
				+'#ver{font-size:11px;font-family:verdana;text-align:right;}'
				+'.a{color:#888;text-decoration:underline;cursor:pointer;}.a:hover{color:#444;text-decoration:none;}'
				+'.selectedColor,.selectedTrans,.selectedShape,.selectedWidth,.selectedZoom,.selectedOutLine{background-color:#39f;}.useGrad,.selectedHand{background-color:#f39 !important;}'
				+'.selectedTe{background-color:#3f9;}'
				+'#UploadCmd{clear:both}#UploadCmd td{font-size:11px;}.UploadCmdTitle{width:80px;background:#ddd;}.UploadCmdThemaTitle{width:140px;}.UploadCmdButton{text-align:center;}'
				+'#UploadButton{width:90px;height:30px;}#UploadMsg{margin:1em 0;text-align:center;}.UploadHint{font-size:11px;line-height:1.5;width:90%;}'
				+'#EditTagPane{font-size:13px;line-height:1.5;width:100%;}img#TagImg1{margin-right:12px;float:left;vertical-align:bottom;border:1px solid #eee;}');

			var buf=$.c('div','ver');
			if($.x('//*[@class="ContentsListTitle"]'))
				$.x('//*[@class="ContentsListTitle"]').parentNode.replaceChild(buf,$.x('//*[@class="ContentsListTitle"]'));
			else $.x('//*[@class="Contents"]').insertBefore(buf,$.x('//*[@class="Contents"]').firstChild);
			$.id('ver').innerHTML='<span class="a jump" title="http://tegaki.pipa.jp/">テブロTOPへ</span> / <span class="a jump" title="http://tegaki.pipa.jp/VMyPageS.jsp">マイページTOPへ</span> / <span class="a jump" title="http://tegaki.pipa.jp/VEditPalletS.jsp">パレットカスタマイズ</span> / <span id="toOfficial" class="a jump" title="http://tbp.d2no.info/"><strong>update info</strong></span>';

			if($.id('EditTagPane')){
				$.id('EditTagPane').innerHTML='<tr><td>'
					+'<img src="'+$.id('TagImg1').src+'" id="TagImg1" /><br />'
					+'<span id="tagEdit" class="a">タグを選択</span><br /><span id="tagEditBkm" class="a">お気に入りからタグ選択</span>'
					+'<input type="hidden" name="T1" value="'+$.val($.x('//input[@name="T1"]'))+'" /></td></tr>';
				$.click($.id('tagEdit'),function(){window.open('VTagListS.jsp','TagList','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=760,height=600');});
				$.click($.id('tagEditBkm'),function(){window.open('VTagListS.jsp?OD=2','TagList','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=760,height=600');});
			}
			$.each($.a('//*[contains(@class,"jump")]'),function(v){$.click(v,function(e){window.open(this.title);})});

			var userp=$.loadUserPalette(m);
			$.id('PalletPaneV').parentNode.replaceChild($.c('div','teblogEXL'),$.id('PalletPaneV'));
			$.id('teblogEXL').innerHTML='<dl id="colorPalette">'
				+'<dt>color</dt><dd>'+$.createPaletteCell(userp,m)+'</dd>'
				+'<dt>transparent</dt><dd>'+$.createTransSelect($.settings.cusTransEx)+'</dd>'
				+'<dt>shape</dt><dd>'+$.createShapeSelect(m==2?null:4)+'</dd>'
				+'<dt>width</dt><dd>'+$.createWidthSelect($.settings.cusWidthEx)+'</dd>'
				+'<dd>'+$.createCmdSelect()+'</dd>'
				+'</dl><dl id="customPalette">'
				+'<dt>now</dt><dd>'+$.createCustomNow()+'</dd>'
				+'<dt>extra</dt><dd><div id="textList">text</div></dd><dd><div id="eraserList">ERASER</div></dd>'
				+($.settings.cusUraUse?'<dt>ura</dt><dd>'+$.createCustomUra()+'</dd>':'')
				+($.settings.cusTeUse?'<dt>te</dt><dd>'+$.createCustomTe()+'</dd>':'')
				+'<dt>zoom</dt><dd>'+$.createZoomSelect()+'</dd>'
				+($.settings.cusMarkc?'<dt>markup</dt><dd>'+$.createCustomMarkup($.settings.cusMarkc)+'</dd>':'')
				+($.settings.cusRecsq?'<dt>recode</dt><dd>'+$.createCustomRecode($.settings.cusRecsq)+'</dd>':'')
				+'<dt>outline</dt><dd>'+$.createCustomOutline(1)+'</dd>'
				+'<dt>lock</dt><dd>'+$.createCustomLock()+'</dd>'
				+'</dl>';

			$.each($.a('id("transList")/li'),function(v){$.click(v,function(e){$.flaSelectTrans(e.target,$.item(e.target));e&&e.preventDefault();});});$.addClass($.x('id("transList")/li[1]'),'selectedTrans');
			$.each($.a('id("shapeList")/li'),function(v){$.click(v,function(e){$.flaSelectShape(e.target,$.item(e.target));e&&e.preventDefault();});});$.addClass($.x('id("shapeList")/li[1]'),'selectedShape');
			$.each($.a('id("widthList")/li'),function(v){$.click(v,function(e){var t=(e.target.tagName=='LI')?e.target:e.target.parentNode;$.flaSelectWidth(t,t.childNodes[0].textContent);e&&e.preventDefault();});});$.addClass($.x('id("widthList")/li[2]'),'selectedWidth');
			if($.id('zoomList')){$.each($.a('id("zoomList")/li'),function(v){$.click(v,function(e){$.flaSelectZoom(this,e.target.textContent.substring(1)-0);e&&e.preventDefault();});});$.addClass($.x('id("zoomList")/li[1]'),'selectedZoom');}
			if($.settings.cusUraUse){ $.click($.id('uraList'),function(e){$.controlUra();e&&e.preventDefault();});$.s($.x('//*[@class="ura"]'),{display:'block'}); $.s($.x('//*[@class="omote"]'),{display:'none'});}
 			if($.settings.cusTeUse){ $.click($.id('teList'),function(e){$.controlTe();e&&e.preventDefault();});};
			$.each($.a('id("cmdList")/li'),function(v){$.click(v,function(e){$.flaSelectCmd(e.target.textContent.substring(1));e&&e.preventDefault();})});
			$.click($.id('textList'),function(e){$.controlText();e&&e.preventDefault();})
			$.click($.id('eraserList'),function(e){$.controlEraser();e&&e.preventDefault();});
			$.each($.a('id("outlineList")/li'),function(v){$.click(v,function(e){$.controlOutLine(e);e&&e.preventDefault();});});$.addClass($.x('id("outlineList")/li['+($.settings.cusOutLine-0+1)+']'),'selectedOutLine');
			$.bindPaletteList(); $.addClass($.x('id("colorList")/li[3]'),'selectedColor');
			$.bindLock();
		},

		bindPaletteList:function(){
				$.each($.a('//*[@class="colorCell"]'), function(v){
					$.click(v, function(e){
								var i=$.idx(e.target.parentNode,'colorCell'), c=$.val('paletteNow').split(',')[i];
								$.flaSelectColor(e.target,c);
								$.val('clrNow',i);
							});
					});
				$.each($.a('//*[@class="colorGrad"]'), function(v){
					$.click(v, function(e){
								var i=$.idx(e.target.parentNode.parentNode,'colorGrad'), c=$.val('paletteNow').split(',')[i];
								$.flaSelectGrad(e.target.parentNode,$.createGradColor(c));
								$.val('clrNow',i);
							});
					});
			},

		bindLock:function(){
				var l=$.id('lockBox')
				,f=function(e){e&&e.preventDefault();}
				,lock=function(){
					l.checked?$.each($.a('//a'),function(v){$.click(v,f);$.s(v,{textDecoration:'line-through'});}):$.each($.a('//a'),function(v){$.unbind(v,'click',f);$.s(v,{textDecoration:'none'});});
					};
				l.checked=$.settings.cusDeflock;
				$.click(l,function(){lock();});
				lock();
			},

		loadUserPalette:function(mode){
				var p=[];
				if($.x('id("ColorButton")//input[@type="hidden"]')){
					$.each($.a('id("ColorButton")//input[@type="hidden"]'),function(v){p.push($.dec2hex($.val(v)));});
				} else {
					// safari対策というか妙なhtml対策というか
					var tmp = $.x('//body').innerHTML.match(/<input type="hidden" name="usercolor\d{1,2}" value="\d+">/ig);
					$.each(tmp,function(v){v.match(/value="(\d+)"/i);p.push($.dec2hex(RegExp.$1));});
				}
				return p;
			},

		createFirstPalette:function(p,d,m){
				if(!m)p.unshift('ユーザー'), d.unshift(p);
				var s='';
				$.each(d,function(v){
						s+='<option value="'+v.slice(1)+'" style="color:#'+$.hex2y(v[9])+';background-color:#'+v[9]+';">'+v[0]+'</option>\n';
					});
				return '<select id="colorSelect">'+s+'</select>';
			},

		createPaletteCell:function(p,m){
				var c='',l=0;
				$.val('paletteNow',p); $.val('paletteOut',p);
				l=p.length;
				switch(m){
					case 1: case 2:
						for(var i=0;i<l;i++){
							c+='<li><div class="colorCell" style="background-color:#'+p[i]+'"></div></li>'
								+'<li><div class="colorGrad">'+$.createGradText($.createGradColor(p[i]))+'</div></li>';
						}
						break;
					case 3:
						for(var i=0;i<l;i++) c+='<li><div class="colorCell" style="background-color:#'+p[i]+'"></div></li>';
						for(var i=0;i<l;i++) c+='<li><div class="colorGrad">'+$.createGradText($.createGradColor(p[i]))+'</div></li>';
						break;
				}
				return '<ul id="colorList" class="teblogEx">'+c+'</ul>';
			},

		createGradColor:function(n){
				var c=$.hex2rgb(n),r=c[0],g=c[1],b=c[2],c=[];
				if(r==255&&g==255&&b==255){c=['FA80f9','FFDB16','A5E975','00FFFF','71DBFD'];}
				else{
					var r2=r+Math.floor((255-r)/2),g2=g+Math.floor((255-g)/2),b2=b+Math.floor((255-b)/2);
					c=[$.rgb2hex(r,g,b),$.rgb2hex(r2,g,b),$.rgb2hex(r2,g2,b),$.rgb2hex(r,g2,b2),$.rgb2hex(r,g,b2)];
				}
				c.push(n);
				return c;
			},

		createGradText:function(a){
				var b=''; a.pop();
				$.each(a,function(v){b+='<span style="color:#'+v+';font-weight:bold;">l</span>';});
				return b;
			},

		createTransSelect:function(e){
				var c=(e?9:3),b='100,50,+50,10,+10,30,+30,70,+70'.split(','),r='';
				for(var i=0;i<c;i++) r+='<li>'+b[i]+'</li>';
				r='<ul id="transList" class="teblogEx">'+r+'</ul>';
				return r;
			},

		createShapeSelect:function(n){
				var c='●,■,◎,回'.split(','),n=(n)?n:c.length,z='';
				for(var i=0;i<n;i++)z+='<li>'+c[i]+'</li>';
				return '<ul id="shapeList" class="teblogEx">'+z+'</ul>';
			},

		createWidthSelect:function(e){
				var s=[1,2,4,8,16,24,32,48],b='',c=e?s.length:5;
				for(var i=0;i<c;i++) b+='<li><div class="w1">'+s[i]+'</div><div style="height:'+s[i]+'px" class="w2"></div></li>';
				return '<ul id="widthList" class="teblogEx">'+b+'</ul>';
			},

		createCustomOutline:function(n){
				var s='反転色,明(同系),暗(同系),ランダム'.split(','),c=s.length,z='';
				switch(n){
					case 1: for(var i=0;i<c;i++){z+='<li>'+s[i]+'</li>';}break;
					case 2: for(var i=0;i<c;i++){z+='<li>'+s[i].charAt(0)+'</li>';}break;
				}
				return '<ul id="outlineList" class="teblogEx">'+z+'</ul>';
			},

		createZoomSelect:function(){
				var s=[1,2,4,8],b='';
				b+='<li class="zoomReset">re:zoom</li>';
				for(var i=0,l=s.length;i<l;i++) b+='<li>x'+s[i]+'</li>';
				return '<ul id="zoomList" class="teblogEx">'+b+'</ul>';
			},

		createCmdSelect:function(){return '<ul id="cmdList" class="teblogEx"><li id="undoButton">≪<div>undo</div></li><li id="redoButton">≫<div>redo</div></li></ul>';},
		createCustomNow:function(){return '<div id="nowList"></div>'},
		createCustomUra:function(){return '<div id="uraList"><div class="ura">ウラ</div><div class="omote">オモテ</div></div>';},
		createCustomTe:function(){return '<div id="teList">手</div>';},
		createCustomLock:function(){return '<div id="lockList"><input type="checkbox" id="lockBox" /></div>';},

		controlUra:function(){
				var s={1:0,2:1,4:2,8:3,16:4,24:5,32:6,48:7}
				,o=[$.val('nowColor'),$.val('nowTrans')-0,$.val('nowShape')-0,$.val('nowWidth')-0,$.val('nowGradUse')-0,$.val('nowOutLine')-0,$.val('clrNow')-0]
				,u=[$.val('uraColor'),$.val('uraTrans')-0,$.val('uraShape')-0,$.val('uraWidth')-0,$.val('uraGradUse')-0,$.val('uraOutLine')-0,$.val('uraNow')-0]
				,t=$.a('id("colorList")/li')[u[6]*2+u[4]].firstChild;$.val('nowOutLine',u[5]);
				(u[4])?$.flaSelectGrad(t,$.createGradColor(u[0])):$.flaSelectColor(t,u[0]);
				$.flaSelectTrans($.a('id("transList")/li')[u[1]],u[1]);
				$.flaSelectShape($.a('id("shapeList")/li')[u[2]],u[2]);
				$.flaSelectWidth($.a('id("widthList")/li')[s[u[3]]],u[3]);
				$.delClass($.x('//*[contains(@class,"selectedOutLine")]'),'selectedOutLine');
				$.addClass($.a('id("outlineList")/li')[u[5]],'selectedOutLine');
				$.val('uraColor',o[0]);$.val('uraTrans',o[1]);$.val('uraShape',o[2]);$.val('uraWidth',o[3]);$.val('uraGradUse',o[4]);$.val('uraOutLine',o[5]);
				$.val('clrNow',u[6]);$.val('uraNow',o[6]);
				$.toggle($.x('//*[@class="ura"]'));
				$.toggle($.x('//*[@class="omote"]'));
				$.delClass($.id('teList'),'selectedTe');
				$.val('teNow',0);$.val('textNow',0);$.val('eraserNow',0);
			},

		controlOutLine:function(e){
				var t=e.target,u=$.x('//*[contains(concat(" ",@class," ")," selectedColor ")]//div'),c=$.val('nowColor');
				$.val('nowOutLine',$.item(t));
				$.delClass($.x('//*[contains(@class,"selectedOutLine")]'),'selectedOutLine');
				$.addClass(t,'selectedOutLine');
				$.val('nowGradUse')-0?$.flaSelectGrad(u,$.createGradColor(c)):$.flaSelectColor(u,c);
			},

		controlTe:function(){
				if($.val('teNow')-0){
					$.flaObj().SetPenShape($.val('nowShape')-0);
					$.delClass($.id('teList'),'selectedTe');
					$.val('teNow',0);
				} else {
					$.flaObj().SetPenShape(4);
					$.addClass($.id('teList'),'selectedTe');
					$.val('teNow',1);
				}
			},

		controlText:function(){
				if($.val('textNow')-0){
					$.flaObj().SetPenShape($.val('nowShape')-0);
					$.delClass($.id('textList'),'selectedColor');
					$.val('textNow',0);
				} else {
					$.flaObj().ResetZoom();
					$.addClass($.id('textList'),'selectedColor');
					$.val('textNow',1);
				}
				$.flaObj().TextMode();
			},

		controlEraser:function(){
				if($.val('eraserNow')-0){
					$.flaObj().SetPenShape($.val('nowShape')-0);
					$.delClass($.x('//*[contains(concat(" ",@class," ")," selectedColor ")]'), ['selectedColor','useGrad']);
					$.addClass($.a('id("colorList")/li')[(($.val('clrNow')-0)*2+($.val('nowGradUse')-0))],'selectedColor');

					var c=$.val('nowColor');
					var g=$.createGradColor(c);
					if($.val('nowGradUse')-0){
						$.addClass($.a('id("colorList")/li')[(($.val('clrNow')-0)*2+1)],'useGrad');
						$.flaObj().SetGradientColor('0x'+g[0],'0x'+g[1],'0x'+g[2],'0x'+g[3],'0x'+g[4],'0x'+$.getOutLineColor(g[5]));
					} else {
						$.flaObj().SetColor('0x'+$.val('nowColor'),'0x'+$.getOutLineColor(c));
					}
					$.val('eraserNow',0);
				} else {
					$.flaObj().Erase();
					$.delClass($.x('//*[contains(concat(" ",@class," ")," selectedColor ")]'), ['selectedColor','useGrad']);
					$.addClass($.id('eraserList'),'selectedColor');
					$.val('eraserNow',1);
				}
			},

		getOutLineColor:function(c){
				var t=$.x('//*[contains(@class,"selectedOutLine")]').textContent,f;
				switch(t){
					case '反': case '反転色':   f=$.dec2hex(~parseInt(c,16)&0x00ffffff); break;
					case '明': case '明(同系)': f=$.upperRGB(c,0x40); break;
					case '暗': case '暗(同系)': f=$.lowerRGB(c,0x40); break;
					case 'ラ': case 'ランダム': f=$.getRandomRGB(); break;
				}
				return f;
			},

		getCookie:function(key){
				var c=document.cookie;
				var tmp=c.split(';'),buf=[];
				for(var i=0,l=tmp.length;i<l;i++){
					var x=tmp[i].split('=');
					buf[x[0]]=x[1];
				}
				return buf[key];
			},

		setCookie:function(key,val){
				var d=new Date(),s;
				d.setDate(d.getDate()+30);
				s=key+'='+encodeURIComponent(val)+';expires='+d.toGMTString()+';';
				document.cookie=s;
			},

		delCookie:function(key){
				var d=new Date(),s;
				s=key+'=;expires='+d.toGMTString();
				document.cookie=s;
			},

		flaObj:function(){
				var w=window.wrappedJSObject || window;
				return w.FlashObj(OBJ);
			},

		flaSelectColor:function(target,c){
				($.id('outlineList'))?$.flaObj().SetColor('0x'+c,'0x'+$.getOutLineColor(c)):$.flaObj().SetColor('0x'+c);
				$.val('nowColor',c); $.val('nowGradUse',0);
				if($.id('nowList')) $.bc($.id('nowList'),'#'+c);
				$.delClass($.x('//*[contains(concat(" ",@class," ")," selectedColor ")]'), ['selectedColor','useGrad']);
				$.addClass(target.parentNode, 'selectedColor');
			},

		flaSelectGrad:function(target,c){
				$.flaObj().SetGradientColor('0x'+c[0],'0x'+c[1],'0x'+c[2],'0x'+c[3],'0x'+c[4],'0x'+$.getOutLineColor(c[5]));
				$.val('nowColor',c[5]); $.val('nowGradUse',1);
				if($.id('nowList')) $.bc($.id('nowList'),'#'+c[5]);
				$.delClass($.x('//*[contains(concat(" ",@class," ")," selectedColor ")]'),['selectedColor','useGrad']);
				$.addClass(target.parentNode,'selectedColor');
				$.addClass(target.parentNode,'useGrad');
			},

		flaSelectTrans:function(target,n){
				switch(n){
					case 0: $.flaObj().SetTransparent(100); break;
					case 1: $.flaObj().SetTransparent(100); break;
					case 2: $.flaObj().SetTransparent(50);  break;
					case 3: $.flaObj().SetTransparent(100); break;
					case 4: $.flaObj().SetTransparent(10);  break;
					case 5: $.flaObj().SetTransparent(100); break;
					case 6: $.flaObj().SetTransparent(30);  break;
					case 7: $.flaObj().SetTransparent(100); break;
					case 8: $.flaObj().SetTransparent(70);  break;
				}
				if($.id('outlineList')){
					switch(n){
						case 0: $.flaObj().SetHalftone(100); break;
						case 1: $.flaObj().SetHalftone(50);  break;
						case 2: $.flaObj().SetHalftone(100); break;
						case 3: $.flaObj().SetHalftone(10);  break;
						case 4: $.flaObj().SetHalftone(100); break;
						case 5: $.flaObj().SetHalftone(30);  break;
						case 6: $.flaObj().SetHalftone(100); break;
						case 7: $.flaObj().SetHalftone(70);  break;
						case 8: $.flaObj().SetHalftone(100); break;
					}
				}
				$.val('nowTrans',n);
				$.delClass($.x('//*[contains(@class,"selectedTrans")]'),'selectedTrans');
				$.addClass(target,'selectedTrans');
			},

		flaSelectShape:function(target,n){
				$.flaObj().SetPenShape(n);
				$.val('nowShape',n);
				$.delClass($.x('//*[contains(@class,"selectedShape")]'),'selectedShape');
				$.addClass(target,'selectedShape');
			},

		flaSelectWidth:function(target,n){
				($.id('outlineList'))?$.flaObj().SetPenWidth(n):$.flaObj().SetPen(n);
				$.val('nowWidth',n);
				$.delClass($.x('//*[contains(@class,"selectedWidth")]'),'selectedWidth');
				$.addClass(target,'selectedWidth');
			},

		flaSelectZoom:function(target,n){
				n ? $.flaObj().SetZoom(n) : $.flaObj().ResetZoom();
				$.delClass($.x('//*[contains(@class,"selectedZoom")]'),'selectedZoom');
				$.addClass(target,'selectedZoom');
			},

		flaSelectCmd:function(n){
				switch(n){
					case 'undo': $.flaObj().Undo(); break;
					case 'redo': $.flaObj().Redo(); break;
				}
			},

		flaSelectEraser:function(){
				$.flaObj().Erase();
				$.val('nowColor','erase'); $.val('nowGradUse',0);
				if($.id('nowList')) $.bc($.id('nowList'),'#fff');
				$.delClass($.x('//*[contains(concat(" ",@class," ")," selectedColor ")]'), ['selectedColor','useGrad']);
				$.addClass(target.parentNode, 'selectedColor');
			}
	};

	var $=teblogEXL;

	if(	!$.x('//*[starts-with(@id,"Edit")]/object')
		&&!$.x('//*[starts-with(@id,"Edit")]/*/object')
		&&!$.x('//div[@class="CmtImg"]/object')) return false;

	MODE=
		$.x('id("EditBlogPane")')?1 // ブログ
		:$.x('id("EditMsgPane")')?2 // メッセージ
		:($.x('//div[@class="CommentDetail"]')&&!$.x('//form[starts-with(@action,"VUpdate")]'))?3 // コメント
		:null;

	switch(MODE){
		case 1: case 2:
			OBJ=$.x('//div[starts-with(@id,"Edit")]/object').id;
			$.initializeEditBlog(MODE);
			break;
		case 3:
			if($.x('//*[@class="CommentDetail"][last()]').textContent.match('コメントは許可されていません。'))return false;
			OBJ=$.x('//div[@class="CmtImg"]/object').id;
			$.initializeComment();
			break;
	}

	if($.settings.debug){
		var p=$.id('paletteStat').style;
		p.position='fixed';p.top='0px';p.left='0px';p.width='100px';p.fontSize='11px';
		$.each($.a('//*[@type="hidden"]'),function(v){
				v.setAttribute('type','text');
				v.parentNode.insertBefore($.textNode(v.id), v);
			});
	}
})();

