/****************************
(c) Wataru Kanzaki, Wings-Winds, 2007-2009
http://www.wi-wi.jp/
****************************/

function EI(id){return (typeof id=='string')?document.getElementById(id):id;}
function EN(id){return ENT(document.getElementsByName(id));}
function ET(id){return document.getElementsByTagName(id);}
function ENT(elms){return (emls)?elms.length==1?elms[0]:elms:null;}
function CE(id){return document.createElement(id);}
function CT(id){return document.createTextNode(id);}

function show(id, style){
	var elm=(typeof id=='string')?EI(id):id;

//	if(!elm || elm.nodeType!=Node.ELEMENT_NODE)
	if(!elm || elm.nodeType!=1)
		return false;
	
	if(!style)
		style=defaultStyles(elm.nodeName);
	chgStyle(elm, 'display', style);
	return style;
}
function hide(){
	if(arguments.length==1){
		return chgStyle(arguments[0],'display','none');
	}else{
		return chgStyle(arguments,'display','none');
	}
}

function visible(){
	if(arguments.length==1){
		return chgStyle(arguments[0],'visibility','visible');
	}else{
		return chgStyle(arguments,'visibility','visible');
	}
}
function invisible(){
	if(arguments.length==1){
		return chgStyle(arguments[0],'visibility','hidden');
	}else{
		return chgStyle(arguments,'visibility','hidden');
	}
}

function chgStyle(elms, prop, value){
	if(elms && (typeof(elms)=='string' || !elms.length)){
		_chgStyle(elms, prop, value);
		var cnt=1;
	}else{
		for(var i=0,cnt=0,el;i<elms.length;i++){
			cnt+=_chgStyle(elms[i], prop, value);
		}
	}
	return cnt;
	
	function _chgStyle(e,p,v){
		var _e=EI(e);
//		if(!_e || _e.nodeType!=Node.ELEMENT_NODE)
		if(!_e || _e.nodeType!=1)
			return 0;
		_e.style[prop]=value;
		return 1;
	}
}

function toggle_show(id, style){
	var elm=(typeof id=='string')?EI(id):id;
//	if(!elm || elm.nodeType!=Node.ELEMENT_NODE)
	if(!elm || elm.nodeType!=1)
		return false;
	var st=(elm.currentStyle)?elm.currentStyle('display'):document.getComputedStyle(elm,null).getPropertyValue('display');

	if(st=='none')
		return show(elm,style);
	else
		return hide(elm);
}
function toggle_visible(id){
	var elm=(typeof id=='string')?EI(id):id;
	var st=(elm.currentStyle)?elm.currentStyle('visibility'):document.getComputedStyle(elm,null).getPropertyValue('visibility');
	
	elm.style.visibility=(st=='visible')?'hidden':'visible';
}

/**************/
function pos(el,opt){
var _pos={left:0,top:0,right:0,bottom:0,width:0,height:0};
var isMac=(navigator.platform.toLowerCase().indexOf("mac")!=-1);
if(!el)return _pos;
//position:absolute or static only
// todo: for position:relative
	if(el.getBoundingClientRect){
		var doc,t=0,l=0;
		if(!!document.body){
			doc=document.body;
		}else if(!!document.documentElement){
			doc=document.documentElement;
		}else{
			doc={scrollTop:0,scrollLeft:0};
		}
		if(!!doc.scrollTop)t=parseInt(doc.scrollTop);
		if(!!doc.scrollLeft)l=parseInt(doc.scrollLeft);
		var rect=el.getBoundingClientRect();
		_pos.top=rect.top+t;
		_pos.left=rect.left+l;
		_pos.right=rect.right+l;
		_pos.bottom=rect.bottom+t;
		_pos.width=rect.right-rect.left;
		_pos.height=rect.bottom-rect.top;
	}else if(document.getBoxObjectFor){
		var rect=document.getBoxObjectFor(el);
		_pos.top=rect.y;
		_pos.left=rect.x;
		_pos.width=rect.width;
		_pos.height=rect.height;
		_pos.right=rect.x+rect.width;
		_pos.bottom=rect.y+rect.height;
	}else{
		_pos.width=el.clientWidth;
		_pos.height=el.clientHeight;
		while(el){
			_pos.left+=parseInt(el.offsetLeft);
			_pos.top+=parseInt(el.offsetTop);
			el=el.offsetParent;
		}
		if (!isMac){
		_pos.left-=document.body.offsetLeft; // html margin-left
		_pos.top-=document.body.offsetTop; // html margin-top
		}
		_pos.right=_pos.left+_pos.width;
		_pos.bottom=_pos.top+_pos.height;
	}
	if(!!opt){
		if(!!opt.width)_pos.width=opt.width;
		if(!!opt.height)_pos.height=opt.height;
		if(!!opt.top)_pos.top=opt.top;
		if(!!opt.left)_pos.left=opt.left;
		if(!!opt.right)_pos.right=opt.right;
		if(!!opt.bottom)_pos.bottom=opt.bottom;
	}
	return _pos;
}

/**************/
function move(el,x,y){
	if(x!==null)
		el.style.left=x||0+'px';
	if(x!==null)
		el.style.top=y||0+'px';
	return true;
}

/**************/
var defaultStyles;

(function(){
	var ds={};
	var _defaultStyles={
		'block':['html','address','blockquote','body','dd','div','dl','dt','fieldset','form','frame','frameset','h1','h2','h3','h4','h5','h6','noframes','ol','p','ul','center','dir','hr','menu','pre'],
		'list-item':['li'],
		'none':['head'],
		'table':['table'],
		'table-row':['tr'],
		'table-header-group':['thead'],
		'table-row-group':['tbody'],
		'table-footer-group':['tfoot'],
		'table-column':['col'],
		'table-column-group':['colgroup'],
		'table-cell':['th','td'],
		'table-caption':['caption'],
		'inline-block':['img','iframe','object','embed','input','select','button','textarea'],
	};
	for(var i in _defaultStyles){
		var es=_defaultStyles[i];
		for(var j=0;j<es.length;j++)
			ds[es[j]]=i;
	}
	defaultStyles=function(nodeName){
		return defaultStyles.ds[nodeName.toLowerCase()]||'inline';
	};
	defaultStyles.ds=ds;
})();

