/*************************************************************/
/*                                                           */
/*	file	: CommentIcon.js                                 */
/*	auther	: Cyril                                          */
/*                                                           */
/*	http://feefifofum.cocolog-nifty.com/                     */
/*	                                                         */
/*************************************************************/
/*
*
*	2005.10.15
*		add open iconlist in drawIconLauncher()
*
*/

var xBase = 110;
var yBase = 100;
var space = 0;
var sizeIcon = 16;
var sizeLauncher = 18;
var out;
var oTextArea;
var oLabel;
var iconFlg = '0';
var iconFlgArray = new Array();
var offsetY = 0;
var offsetX = 0;
var tileOffsetY = 15;
var tileOffsetX = 30;
var flipflag = 0;


/************************************************/
/*                                              */
/* initializer                                  */
/*                                              */
/************************************************/
function initCommentIcon() {

	out = document.getElementsByTagName("body");
	oTextArea = document.getElementsByTagName("textarea");
	oLabel = document.getElementById("comment-text-field");

	if( out == null || oTextArea == null || oLabel == null ) {
		return;
	}
	
	if( !getLauncherDrawPos( oLabel ) ) {
		return;
	}

	offsetX = getIconLauncherAdjX();
	offsetY = getIconLauncherAdjY();

	if( 0 == drawIconLauncher() ) {
		return;
	}
	
	drawIconPalettRect();
	drawIconTile();
}

/************************************************/
/*                                              */
/* get object position info                     */
/*                                              */
/************************************************/
function getLauncherDrawPos( obj )
{
	if( obj == null ) {
		return -1;
	}
	
	xBase = findPosX( obj );
	yBase = findPosY( obj );
	
	return 1;
}

/************************************************/
/*                                              */
/* draw icon launcher                           */
/*                                              */
/************************************************/
function drawIconLauncher() {

	var oTextLabels = null;
	var oLaunch = null;
	
	if( null == (oTextLabels = oLabel.getElementsByTagName('label') ) ) {
		return 0;
	}
	
	if( oTextLabels.length == 0 ) {
		return 0;
	}

	for( var i = 0; i < oTextLabels.length; i++ ) {

		if( -1 != oTextLabels[i].innerHTML.match( "内容：" ) ) {
			//
			//	insert link
			//
			oTextLabels[i].innerHTML += "<a onclick=\"window.open('http://feefifofum.cocolog-nifty.com/blog/dumpiconlist.html', 'iconlist', 'width=570, height=800, scrollbars=yes, left=600, top=100 ');\" title='iconlist' alt='iconlist'>iconlist</a>";
			//
			oTextLabels[i].innerHTML += "<span id='iconLauncher' vertical-align='bottom'></span>";
			break;
		}
	}
	
	if( null == (oLaunch = document.getElementById("iconLauncher") ) ) {
		return;
	}

	oLaunch.innerHTML += "<a href='JavaScript:;' onMouseOver='flipTile(1);' onMouseOut='flipTile(0);' onClick='flipTile(3);'><img src=\"" +
							iconLauncherImg + "\" border='0'></a>";

	return 1;
}

/************************************************/
/*                                              */
/*                                              */
/*                                              */
/************************************************/
function drawIconPalettRect() {

	var strPaletteParent = "";
	var strPaletteChildren = "";
	var azimuthX = 0;
	var curWidth = 0;
	var oLaunch = document.getElementById("iconLauncher");
	var lineWidth = 7;

	getLauncherDrawPos( oLabel );

	strPaletteParent = "<DIV style='" +
		"top:"  + (drawStartY() -2 ) + "px; " +
		"left:" + (drawStartX() -2)  + "px; " +
		"position : absolute; " +
		"z-index : 4; " +
		"margin-top: 20px; " +
		"visibility : hidden; " +
		"border-top: solid 1px #000000;" + 
		"border-bottom: solid 1px #000000;" + 
		"border-left: solid 1px #000000;" + 
		"border-right: solid 1px #000000;" + 
		"background : #FF0000; '" +
		"id='IconPalette' " + 
		" onMouseOver='flipTile(1)';" +
		" onMouseOut='flipTile(0)';" +
		"></DIV>";
	
	for( var i=0; i < iconList.length; i++ ) {

		var strWidth = "";
		var strHeight = "";
		var nWidth = 0;
		
		if( iconList[i][PALETTE_COLS] != 0 )
		{
			nWidth = ( iconList[i][ICON_SIZE] * (iconList[i][PALETTE_COLS]) +
			( (iconList[i][PALETTE_COLS]) * lineWidth  ) - 5 );
			
			strWidth = "width:" +
			( iconList[i][ICON_SIZE] * (iconList[i][PALETTE_COLS]) +
			( (iconList[i][PALETTE_COLS]) * lineWidth  ) - 5 ) + "px; ";
			
		}
		
		if( iconList[i][PALETTE_LOWS] != 0 )
		{
			strHeight = "height:" +
			( iconList[i][ICON_SIZE] * (iconList[i][PALETTE_LOWS]) +
			( (iconList[i][PALETTE_LOWS]) * lineWidth  ) ) + "px; ";
		}
		
		if( iconList[i][PALETTE_WIDTH] != 0 )
		{
			nWidth = iconList[i][PALETTE_WIDTH];
			strWidth = "width:" + iconList[i][PALETTE_WIDTH] + "px; ";
		}

		if( iconList[i][PALETTE_HEIGHT] != 0 )
		{
			strHeight = "height:" + iconList[i][PALETTE_HEIGHT] + "px; ";
		}
		
		strPaletteChildren += "<span " + "style='" +
		strWidth +
		strHeight +
		"left:"  + curWidth + "px; " +
		"position : absolute; " +
		"z-index : 4; " +
		"visibility : hidden; " +
		"text-align : left; " +
		"border-top: solid 1px #000000;" + 
		"border-bottom: solid 1px #000000;" + 
		"border-left: solid 1px #000000;" + 
		"border-right: solid 1px #000000;" + 
		"background : #D9E0E6; '" +
		"id='IconPaletteChild_" + i +"' " + 
		"></span>";

		curWidth += nWidth;
	}
	
	var oIns = document.createElement("div");
	var oLaunch = document.getElementById("iconLauncher");
	
	//out[0].innerHTML += strPaletteParent;
	//oLaunch.innerHTML += strPaletteParent;
	//oIns.innerHTML = strPaletteParent;
	//var oNew = oLaunch.appendChild( oIns );
	//oNew.innerHTML = strPaletteParent;
	//document.insertBefore( oTextArea[0], oIns );
	//oTextArea[0].insertAdjacentHTML( 'AfterEnd' , strPaletteParent );
	
	document.body.insertAdjacentHTML( 'BeforeEnd' , strPaletteParent );

	var oPal = document.getElementById("IconPalette");

	oPal.innerHTML += strPaletteChildren;

}

/************************************************/
/*                                              */
/*                                              */
/*                                              */
/************************************************/
function drawIconTile() {

	var yCur = drawStartY();
	var xCur = drawStartX();
	var strOut = "";
	var strTmp = "";
	var idx = 0;
	var oTest = document.getElementById("Icon_0");
	var lineWidth = 7;
	
	if( oTest != null ) {
		return;
	}
	for( var listCnt = 0; listCnt < iconList.length; listCnt++ )
	{
		for( var iconCnt = 0;  iconCnt < (iconList[listCnt][0]).length; iconCnt++ )
		{
			if( iconList[listCnt][0][iconCnt][ICON_NAME].length == 0 &&
				iconList[listCnt][0][iconCnt][ICON_FILE].length == 0 )
			{
				strOut += "<br>";
				continue;
			}
			
			strTmp = 
				("<span id='Icon_" + idx + "' " +
				"style='" +
				" visibility: hidden;" +
				" border='0';" +
				"'>" +
				"<a href='JavaScript:;'" + 
				" onclick='"     + "insertIcon(" + listCnt + "," + iconCnt +  ")"     + "'; " +
				">" + 
				"<img src=\"" + (iconList[listCnt][0])[iconCnt][1] + "\" " + 
				" alt='" + (iconList[listCnt][0])[iconCnt][ICON_NAME] + "' " +
				" title='" + (iconList[listCnt][0])[iconCnt][ICON_NAME] + "' " +
				" style='border: 1px solid #D9E0E6'"  + 
				" onMouseOut= 'this.style.border=\"1px solid #D9E0E6\"';" +
				" onMouseOver='this.style.border=\"1px solid #0000FF\"';" +
				" id='isolateIcon_" + idx + "'>" +
				"</a>" +
				"</span>");

			idx++;
			
			strOut += strTmp;

		}

		document.getElementById( "IconPaletteChild_" + listCnt ).innerHTML += strOut;
		strOut = "";
	}

}

/************************************************/
/*                                              */
/* show & hide all icons                        */
/*                                              */
/************************************************/
function flipTile( flg ) {

	var idx = 0;
	var strId = "";
	var oIcon;
	var setFlg = flg == '1' ? 'visible' : 'hidden';
	
	if( flg == '3' ) {
	
		if( document.getElementById("IconPalette").style.visibility == "visible" ) {
			flg = 0;
			setFlg = flg == '1' ? 'visible' : 'hidden';
		}
		else if( document.getElementById("IconPalette").style.visibility == "hidden" ) {
			flg = 1;
			setFlg = flg == '1' ? 'visible' : 'hidden';
		}
	}

	for( var listCnt = 0; listCnt < iconList.length; listCnt++ )
	{
		for( var iconCnt = 0;  iconCnt < (iconList[listCnt][0]).length; iconCnt++ )
		{
			strId = "Icon_" + idx;
			
			oIcon = document.getElementById( strId );
			if( oIcon != null ) {
				oIcon.style.visibility = setFlg;
			}
			else {
				;
			}
			idx++;
		}
	}	
	
	//document.getElementById('IconPalette').style.visibility = setFlg;

	for(var i=0; i < iconList.length; i++ ) {
		document.getElementById( "IconPaletteChild_" + i ).style.visibility = setFlg;
	}

}

/************************************************/
/*                                              */
/*  get object position                         */
/*                                              */
/************************************************/
function insertIcon( listCnt, iconCnt )
{
	flipTile(0);
	
	oTextArea[0].value += "[" + (iconList[listCnt][0])[iconCnt][ICON_NAME] + "]";

	oTextArea[0].focus();
}

/************************************************/
/*                                              */
/*  get object position                         */
/*                                              */
/************************************************/
function getPosition(obj, dir)
{
	//identify supported computed style object
	var computed = typeof document.defaultView != 'undefined' && typeof document.defaultView.getComputedStyle != 'undefined' ?
		'computed' : typeof document.getElementsByTagName('body')[0].currentStyle != 'undefined' ? 'current' : 'none';

	//get x or y position
	var pos = dir == 'x' ? obj.offsetLeft : obj.offsetTop;
	//store offset parent
	var tmp = obj.offsetParent;

	//get computed style position of offset parent
	var position = computed == 'computed' ?
		document.defaultView.getComputedStyle(tmp, '').getPropertyValue('position') : computed == 'current' ? tmp.currentStyle.position : '';

	//while offset parent exists and doesn't have relative or absolute positioning
//	while( tmp != null && !/^(relative|absolute)/.test(position)) {
	while( tmp != null ) {
		//add the offset from its parent
		pos += dir == 'x' ? tmp.offsetLeft : tmp.offsetTop;

		//set reference upwards
		tmp = tmp.offsetParent;
	}

	//return the position
	return pos;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent) 
	{
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	
	}
	else if (obj.x)
	{
		curleft += obj.x;
	}

	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	
	if (obj.offsetParent) {
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
	{
		curtop += obj.y;
	}
	
	return curtop;
}

function drawStartX()
{
	return xBase + sizeLauncher + tileOffsetX + offsetX;
}

function drawStartY()
{
	return yBase + sizeLauncher + offsetY;
}
