// Blog Accessories For Textcube.com
/* 기초 XHTML 규격을 크게 벗어나지 않는 다른 TC 툴에도 수정하여 이용이 가능합니다 */
// Author : By KUNA - kuna.wo.tc
// Not allowed to redistribute or business purpose without Author's permission.
// for MIT Style Editing/Using.
// 2009-03-01 Released. ver 1.00
// using prototype.js

// 밑의 init 옵션에서, 필요한 기능들을 켜고 끄실 수 있습니다.
// 과도한 자바스크립트 사용은 블로그에 과부하를 줄 수 있으니 주의하세요.

document.observe("dom:loaded", accessories_init);

function accessories_init()
{
	// 초기화 함수입니다
	// 사용하고자 하는 기능에 따라 disable이 가능합니다
	// ＊여기에 나오는 모든 라이브러리들은 블로그의 정상적인 작동과는 무관하며 dom:loaded 이후에 실행됩니다
	// ＊
	
	ae_boldstyler();	// 굵은 글씨체에 반짝임 효과를 줍니다. [Boldstyler prototype.ver 1.00]
	repbg_init();		// 리플창의 배경을 랜덤으로 설정해 줍니다. 또한 마우스/포커스 변경시 테두리 효과를 줍니다. 해당 함수에서 세부설정이 가능합니다. [ver 1.1]
	rep_rank_init();	// reply rank marker 리플 목록에 등수놀이를 매겨줍니다. 해당 함수에서 세부설정이 가능합니다. [ver 1.1]
	rep_sty_init();		// reply styler 리플에 스타일을 매겨줍니다. 자세한 정보는 해당 함수에서 볼 수 있습니다 [ver 1.1]
	access2_init();		// article styler - Article의 태그/자막으로 이미지에 특수효과를 줍니다. [ver 1.5]
	mixsh_attach();		// mixsh attacher - textcube.com에서 XHTML 조작 없이 자바스크립트 DOM만으로 믹시를 설치합니다 [ver 1.0]
}

// 설정이 가능한 전역 변수입니다

/* boldstyler 관련 변수 */
var fntOrg;     // 폰트 변경 전에 있을 원본 스타일 [임시 변수]
var fnt_style = "font-weight:bold; color:#333333; border-bottom: #cccccc 1px dotted;";	// 굵게 된 곳에 Mouseover하면 될 color

/* repbg_init 관련 변수 */
var bef_bordersty;
var obj_onfocus = null;
var over_bordersty = '1px #cc9999 solid';
var focus_bordersty = '1px #ff9999 solid';



/* **************************
// 세부 함수 - !important
************************** */

/* boldstyler 메인 함수 */
function ae_boldstyler()
{
	// document의 모든 태그를 확인하여 span 태그를 얻는다
	var obj_spanTag = document.getElementsByTagName('span');
	
	// 굵게 칠해진 태그에 대해서 attachEvent를 시행함
	for (i=0; i<obj_spanTag.length; i++)
	{
		if (obj_spanTag[i].style.fontWeight == 'bold')
		{
			// 이벤트 붙이기
			Event.observe(obj_spanTag[i], "mouseover", ae_boldstyler_mouseover);
			Event.observe(obj_spanTag[i], "mouseout", ae_boldstyler_mouseout);
		}
	}
}

function ae_boldstyler_mouseover(event)
{
	var elem = event.element();
	fntOrg = elem.style.cssText;
	elem.style.cssText = fnt_style;
}

function ae_boldstyler_mouseout(event)
{
	var elem = event.element();
	elem.style.cssText = fntOrg;
}



/* repbg 초기화 함수 */
function repbg_init()
{
	/* 기본 설정 - 사용자 입맛에 맞게 수정하세요 */
	/* 주의 사항 - 배경 URL 갯수와 텍스트 갯수가 같아야 합니다 */
	/* 넣을 텍스트나 배경이 없으면 빈칸("")으로 놓으세요 */

	// 배경의 URL을 입력하세요
	var _bg_url = new Array();
	_bg_url.push("url(http://fs.textcube.com/blog/0/723/attach/Xe3ZMH0FsW.gif) top right no-repeat #ffffff");
	_bg_url.push("url(http://fs.textcube.com/blog/0/723/attach/XCw5aaV6f6.gif) top right no-repeat #ffffff");

	/* 세부 설정 - 보통의 경우 수정할 필요가 없지만 XHTML이 표준 규격을 벗어나는 경우 수정하세요 */
	/* 함부로 수정하면 오작동을 유발할 우려가 있으므로 주의하세요 */

	// index를 선언합니다
	var rep_index = Math.floor(Math.random()*_bg_url.length);
	var obj_btn = new Array();	var obj_textarea = new Array();	var obj_input = new Array();

	// textarea element를 얻습니다
	var _ele_ta = document.getElementsByTagName("textarea");
	// id에 comment를 포함하는 textarea 선별
	for (i=0; i<_ele_ta.length; i++)
	{
		if (_ele_ta[i].id.indexOf("comment")!=-1)
			obj_textarea.push(_ele_ta[i]);
	}
	
	// btn element를 얻습니다
	var _ele_btn = document.getElementsByTagName("button");
	for (i=0; i<_ele_btn.length; i++)
	{
		if (_ele_btn[i].className.indexOf("Comment")!=-1)
			obj_btn.push(_ele_btn[i]);
	}

	// input element를 얻습니다
	var _ele_input = document.getElementsByTagName("input");
	for (i=0; i<_ele_input.length; i++)
	{
		if (_ele_input[i].name.indexOf("search")==-1)
			obj_input.push(_ele_input[i]);
	}
	
	// 각각의 textarea / button / input에 focus, mouseover, mouseout 이벤트를 적용합니다
	for (i=0; i<obj_textarea.length; i++)
	{
		Event.observe(obj_textarea[i], "focus", ae_focus);
		Event.observe(obj_textarea[i], "blur", ae_focusout);
		Event.observe(obj_textarea[i], "mouseover", ae_mouseover);
		Event.observe(obj_textarea[i], "mouseout", ae_mouseout);
		// 해당 textarea element에 속성적용 [마무리]
		obj_textarea[i].style.background = _bg_url[rep_index];
	}
	for (i=0; i<obj_btn.length; i++)
	{
		Event.observe(obj_btn[i], "focus", ae_focus);
		Event.observe(obj_btn[i], "blur", ae_focusout);
		Event.observe(obj_btn[i], "mouseover", ae_mouseover);
		Event.observe(obj_btn[i], "mouseout", ae_mouseout);
	}
	for (i=0; i<obj_input.length; i++)
	{
		Event.observe(obj_input[i], "focus", ae_focus);
		Event.observe(obj_input[i], "blur", ae_focusout);
		Event.observe(obj_input[i], "mouseover", ae_mouseover);
		Event.observe(obj_input[i], "mouseout", ae_mouseout);
	}
	
	// [textarea 기준으로] 기존 border 값을 저장합니다
	bef_bordersty = '1px #cccccc solid';//obj_textarea[0].style.border;
}

/* repbg 세부 함수 */
function ae_focus(ele)
{
	var elem = ele.element();
	obj_onfocus = elem;
	elem.style.border = focus_bordersty;
}

function ae_focusout(ele)
{
	var elem = ele.element();
	obj_onfocus = null;
	elem.style.border = bef_bordersty;
}

function ae_mouseover(ele)
{
	var elem = ele.element();
	if (elem != obj_onfocus)
	{
		elem.style.border = over_bordersty;
	}
}

function ae_mouseout(ele)
{
	var elem = ele.element();
	if (elem != obj_onfocus)
	{
		elem.style.border = bef_bordersty;
	}
}
// Reflector for TC Tag

/**
 * reflection.js v2.0
 * http://cow.neondragon.net/stuff/reflection/
 * Freely distributable under MIT-style license.
 * TC용 개조 by kuna
 */

var Reflection = {
	defaultHeight : 0.2,
	defaultOpacity: 0.5,
	
	add: function(image, options) {
		Reflection.remove(image);
		
		doptions = { "height" : Reflection.defaultHeight, "opacity" : Reflection.defaultOpacity }
		if (options) {
			for (var i in doptions) {
				if (!options[i]) {
					options[i] = doptions[i];
				}
			}
		} else {
			options = doptions;
		}
	
		try {
			var d = document.createElement('div');
			var p = image;
			
			var classes = p.className.split(' ');
			var newClasses = '';

			for (j=0;j<classes.length;j++) {
				if (classes[j] != "reflect") {	// reflect를 imageblock로 변경
					if (newClasses) {
						newClasses += ' '
					}
					
					newClasses += classes[j];
				}
			}

			var reflectionHeight = Math.floor(p.height*options['height']);
			var divHeight = Math.floor(p.height*(1+options['height']));
			
			var reflectionWidth = p.style.width.substring(0, p.style.width.length - 2); // p.width 대신 p.style.width를 사용합니다.
			
			if (document.all && !window.opera) {
				/* Fix hyperlinks */
                if(p.parentElement.tagName == 'A') {
	                var d = document.createElement('a');
	                d.href = p.parentElement.href;
                }  
                    
				/* Copy original image's classes & styles to div */
				d.className = newClasses;
				p.className = 'reflected';
				
				d.style.cssText = p.style.cssText;
				// width 값을 추가로 적용합니다
				p.style.cssText = 'vertical-align: bottom; ' + p.style.cssText; // width:' + reflectionWidth + 'px;';
			
				var reflection = document.createElement('img');
				reflection.src = p.src;
				reflection.style.width = reflectionWidth+'px';
				reflection.style.display = 'block';
				reflection.style.height = p.height+"px";
				
				reflection.style.marginBottom = "-"+(p.height-reflectionHeight)+'px';
				reflection.style.filter = 'flipv progid:DXImageTransform.Microsoft.Alpha(opacity='+(options['opacity']*100)+', style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy='+(options['height']*100)+')';
				
				d.style.width = reflectionWidth+'px';
				d.style.height = divHeight+'px';
				p.parentNode.replaceChild(d, p);
				
				d.appendChild(p);
				d.appendChild(reflection);
			} else {
				var canvas = document.createElement('canvas');
				if (canvas.getContext) {
					/* Copy original image's classes & styles to div */
					d.className = newClasses;
					p.className = 'reflected';
					
					d.style.cssText = p.style.cssText;
				// width 값을 추가로 적용합니다
				p.style.cssText = 'vertical-align: bottom; ' + p.style.cssText;
			
					var context = canvas.getContext("2d");
				
					canvas.style.height = reflectionHeight+'px';
					canvas.style.width = reflectionWidth+'px';
					canvas.height = reflectionHeight;
					canvas.width = reflectionWidth;
					
					d.style.width = reflectionWidth+'px';
					d.style.height = divHeight+'px';
					p.parentNode.replaceChild(d, p);
					
					d.appendChild(p);
					d.appendChild(canvas);
					
					context.save();
					
					context.translate(0,image.height-1);
					context.scale(1,-1);
					
					context.drawImage(image, 0, 0, reflectionWidth, image.height);
	
					context.restore();
					
					context.globalCompositeOperation = "destination-out";
					var gradient = context.createLinearGradient(0, 0, 0, reflectionHeight);
					
					gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
					gradient.addColorStop(0, "rgba(255, 255, 255, "+(1-options['opacity'])+")");
		
					context.fillStyle = gradient;
					context.rect(0, 0, reflectionWidth, reflectionHeight*2);
					context.fill();
				}
			}
		} catch (e) {
	    }
	},
	
	remove : function(image) {
		if (image.className == "reflected") {
			image.className = image.parentNode.className;
			image.parentNode.parentNode.replaceChild(image, image.parentNode);
		}
	}
}

function addReflections(obj) {
	// reflect >> imageblock
	var tmp_obj = obj.getElementsByTagName('div');
	var rdivs = new Array();
	
	for (c=0; c<tmp_obj.length; c++)
	{
		// imageblock 클래스와 done가 없는 클래스만 대상으로 함
		if ((tmp_obj[c].className.indexOf('imageblock') != -1) && (tmp_obj[c].className.indexOf('done') == -1)) rdivs.push(tmp_obj[c]);
	}
	
	for (c=0;c<rdivs.length;c++) {
		if (rdivs[c].getElementsByTagName('img').length != 0)
		{
			// 이미지를 가운데로 정렬
			rdivs[c].innerHTML = "<center>" + rdivs[c].innerHTML + "</center>";
			Reflection.add(rdivs[c].getElementsByTagName('img').item(0), { height: null, opacity : null});
		}
	}
}

// ImageBorderor For TC Tag Edition
// 이미지/원본 코어 구현 by lain님 (http://lain1207.tistory.com/)
// Detail 부분 editor - by kuna

/**********************************************************************************
Author:ｌａｉｎ
E-mail me: lain1207@gmail.com
Blog: lain1207.tistory.com
About the script: http://lain1207.tistory.com/entry/hobbyimageborder2nd 
***********************************************************************************/

function setImageborder(obj, type){
	var tmp_obj = obj.getElementsByTagName('div');
	var rdivs = new Array();
	
	for (c=0; c<tmp_obj.length; c++)
	{
		if ((tmp_obj[c].className.indexOf('imageblock') != -1) && (tmp_obj[c].className.indexOf('done') == -1)) rdivs.push(tmp_obj[c]);
	}
	
	// All의 경우에는 value_reflect에서 2또는 3일때 맨 앞/뒤에 처리부분
	for (c=0; c<rdivs.length; c++)
	{
		setimgborder(rdivs[c], type);
	}
}

function setimgborder(obj, type) {
var image_edit1 = "<center><TABLE cellSpacing=0 cellPadding=0 border=0><TBODY><TR height=13><TD width=15 height=13><IMG height=13 alt='' src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_lu.gif' width=15 border=0></TD><TD background=http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_u.gif height=13><IMG height=13 alt='' src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_spacer.gif' width=1 border=0></TD><TD width=15 height=13><IMG height=13 alt='' src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_ru.gif' width=15 border=0></TD></TR><TR><TD width=15 background=http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_l.gif><IMG height=1 alt='' src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_spacer.gif' width=15 border=0></TD><TD style='text-align:left'>";

var image_edit2 = "</TD><TD width=15 background=http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_r.gif><IMG height=1 alt='' src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_spacer.gif' width=15 border=0></TD></TR><TR height=14><TD width=15 height=14><IMG height=14 alt='' src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_ld.gif' width=15 border=0></TD><TD background=http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_d.gif height=14><IMG height=14 alt='' src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_spacer.gif' width=1 border=0></TD><TD width=15 height=14><IMG height=14 alt='' src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img_rd.gif' width=15 border=0></TD></TR></TBODY></TABLE></center>";

var image_edit3 = "<center><table border=0 cellspacing=0 cellpadding=0><tr height=5><td height=5 colspan=3 background='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_u.gif'><img src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_spacer.gif' width=5 height=5 border=0 /></td></tr><tr><td width=5 background='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_l.gif'><img src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_spacer.gif' width=5 height=5 border=0 /></td><td TD style='text-align:left'><p style='position: relative; margin:0px; padding:0px'>";

var image_edit4 = "<img src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_ld.png' width=12 height=12 border=0 style='position:absolute; bottom:-5px; left:-5px;' class='png24'/><img src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_lu.png' width='12' height='12' border='0' style='position:absolute; top:-5px; left:-5px;'class='png24'/><img src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_rd.png' width=12 height=12 border=0 style='position:absolute; bottom:-5px; right:-5px; class='png24'/><img src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_ru.png' width=12 height=12 border=0 style='position:absolute; top:-5px; right:-5px;'class='png24'/></p></td><td width=5 background='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_r.gif'><img src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_spacer.gif' width=5 height=5 border=0 /></td></tr><tr><td height=5 colspan=3 background='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_d.gif'><img src='http://cfs.tistory.com/custom/blog/9/94967/skin/images/img2_spacer.gif' width=5 height=5 border=0 /></td></tr></table></center>"
	
	
	if (type == 'out')
	{
		obj.innerHTML = image_edit1 + obj.innerHTML + image_edit2
	} else if (type == 'inside') {
		// 태그가 있다면 P 태그를 이전과 이후를 분리하여 앞으로 꺼냄
		if (obj.innerHTML.toUpperCase().indexOf('<P') != -1)
		{
			var str_pbef = obj.innerHTML.substring(0, obj.innerHTML.toUpperCase().indexOf('<P'));	// 이미지
			var str_pafter = obj.innerHTML.substring(obj.innerHTML.toUpperCase().indexOf('<P'), obj.innerHTML.length);	// 태그
			obj.innerHTML = image_edit3 + str_pbef + image_edit4 + str_pafter.replace('[iborder]', '');
		} else {
			obj.innerHTML = image_edit3 + obj.innerHTML + image_edit4;
		}
	}
}


function progressTag(obj)
{
	var tmp_obj = obj.getElementsByTagName('div');
	var rdivs = new Array();
	
	// imageblock 클래스를 포함하는 div를 선별
	for (c=0; c<tmp_obj.length; c++)
	{
		if (tmp_obj[c].className.indexOf('imageblock') != -1) rdivs.push(tmp_obj[c]);
	}
	
	// 각각의 div에 대해서 작업 처리
	for (c=0; c<rdivs.length; c++)
	{
		// [예외처리] 태그가 있다는 전제하에 진행
		if (rdivs[c].innerHTML.toUpperCase().indexOf('</P>') != -1)
		{
			// 태그를 구한 뒤 [reflect], [oborder], [iborder]에 따라서 확인. 첫번째 태그를 기준으로 함
			var str_tag = rdivs[c].getElementsByTagName('p').item(0).innerHTML;
			
			// 태그에 선행 명령어가 있는가?
			if (str_tag.indexOf('[none]') != -1) {
				rdivs[c].getElementsByTagName('p').item(0).innerHTML = str_tag.replace('[none]', '');
				rdivs[c].className += ' done';
			} else if (str_tag.indexOf('[reflect]') != -1) {
				rdivs[c].innerHTML = "<center>" + rdivs[c].innerHTML + "</center>";
				Reflection.add(rdivs[c].getElementsByTagName('img').item(0));
				rdivs[c].getElementsByTagName('p').item(0).innerHTML = str_tag.replace('[reflect]', '');
				rdivs[c].className += ' done';
			} else if (str_tag.indexOf('[iborder]') != -1) {
				setimgborder(rdivs[c], 'inside');
				rdivs[c].className += ' done';
			} else if (str_tag.indexOf('[oborder]') != -1) {
				setimgborder(rdivs[c], 'out');
				rdivs[c].getElementsByTagName('p').item(0).innerHTML = str_tag.replace('[oborder]', '');
				rdivs[c].className += ' done';
			}
		}
	}
}


/* mixsh addon attacher */
function mixsh_attach()
{
	var obj_titlewrap = document.myGetElementsByClassName('titleWrap');
	var obj_article = document.myGetElementsByClassName('article');
	
	for (i=0; i<obj_titlewrap.length; i++)
	{
		var str_date = obj_titlewrap[i].getElementsByTagName('abbr').item(0).innerHTML;
		str_date = str_date.substring(0, 4) + str_date.substring(5, 7) + str_date.substring(8, 10);
		var str_url = obj_titlewrap[i].getElementsByTagName('a').item(0).href;		// document.location.href
		var mixsh_source = "<div style='padding:20px; text-align:center;'><embed src='http://www.mixsh.com/widget/mixup/loader.html?muid=47164&guid=" + str_url + "&rdate=" + str_date + "&rawhtml=&skin=1&showhitcnt=1&platform=4' width='349' height='89' wmode='window' allowScriptAccess='always'></embed></div>";
		obj_article[i].innerHTML += mixsh_source;
	}
}



// 기초 Base function
/* From prototype.js */
if (!document.myGetElementsByClassName) {
	document.myGetElementsByClassName = function(className) {
		var children = document.getElementsByTagName('*') || document.all;
		var elements = new Array();
	  
		for (var i = 0; i < children.length; i++) {
			var child = children[i];
			var classNames = child.className.split(' ');
			for (var j = 0; j < classNames.length; j++) {
				if (classNames[j] == className) {
					elements.push(child);
					break;
				}
			}
		}
		return elements;
	}
}

