﻿function Theme()
{
this.border="";
this.color="";
this.backgroundColor="";
this.fontSize="";
this.fontFamily="";
}
function Control(e)
{
var _self=this;
_self.attach=function(target)
{
if(target&&target.nodeType==1)
{
// properties
target.moveable=false;
target.resizeable=true;

target.move=function(x,y)
{
if(target.moveable===true)
{
var pos=$p(target)
exec(target.onmovebegin,target);
target.style.position='absolute';
if(x>0){target.style.left=x+'px';}
if(y>0){target.style.top=y+'px';}
exec(target.onmoveend,target,pos);
}
}
target.moveBy=function(x,y)
{
if(target.moveable===true)
{
var pos=$p(target);
exec(target.onmovebegin,target);
target.style.position='absolute';
target.style.left=target.offsetLeft+x+'px';
target.style.top=target.offsetTop+y+'px';
exec(target.onmoveend,target,pos);
}
}

// methods
target.$=function()
{
switch(arguments.length)
{
case 0:
return target.getAttribute('id');
case 1:
if(typeof(arguments[0])!='string'){throw 'invalid argument type.';}
return target.setAttribute('id',arguments[0]);
default:
if(typeof(arguments[0])!='string'||typeof(arguments[1])!='boolean')
{
throw 'invalid argument type.';
}
var children=(arguments[1]===true?target.all:target.childNodes);
var result=[];
for(var i=0;i<children.length;i++)
{
if(children[i].nodeType==1&&children[i].getAttribute('id')==arguments[0])
{
result.push(children[i]);
}
}
return result;
}
}
target.$$=function()
{
switch(arguments.length)
{
case 0:
throw 'require arguments';
case 1:
if(arguments[0])
{
var child=null;
if(typeof(arguments[0])=='string')
{
if(/^#(\w+)/i.exec(arguments[0]))
{
//child=target.appendChild($(RegExp.$1));
child=target.appendChild(document.getElementById(RegExp.$1));
}
else
{
child=target.appendChild($$(arguments[0]));
}
}
else
{
if(arguments[0].nodeType==1)
{
child=target.appendChild(arguments[0]);
}
else
{
throw 'invalid argument type.'
}
}
return child;
}
else
{
throw 'invalid argument.';
}
break;
}
}
target.$$t=function(str){return target.appendChild($$t(str));}
target.attr=function()
{
switch(arguments.length)
{
case 0:
return target.attributes;
case 1:
if(typeof(arguments[0])=='string')
{
return target.getAttribute(arguments[0]);
}
return null;
case 2:
if(typeof(arguments[0])=='string'&&(typeof(arguments[1])=='string'||typeof(arguments[1])=='number'||typeof(arguments[1])=='boolean'))
{
return target.setAttribute(arguments[0],arguments[1]);
}
return null;
case 3:
if(typeof(arguments[0])=='string'&&typeof(arguments[2])=='boolean')
{
var children=[];
var result=[];
if(arguments[1]===true)
{
children=target.all;
}
else
{
children=target.childNodes;
}
for(var i=0;i<children.length;i++)
{
if(children[i].nodeType==1&&children[i].getAttribute(arguments[0])==arguments[1])
{
result.push(children[i]);
}
}
return result;
}
}
}
target.cssclass=function()
{
switch(arguments.length)
{
case 0:
return target.className;
case 1:
if(tyepof(arguments[0])=='string')
{
return target.className=arguments[0];
}
return null;
case 2:
if(typeof(arguments[0])=='string'&&typeof(arguments[1])=='boolean')
{
var children=[];
var result=[];
if(arguments[1]===true)
{
children=target.all;
}
else
{
children=target.childNodes;
}
for(var i=0;i<children.length;i++)
{
if(children[i].nodeType==1&&children[i].className==arguments[0])
{
result.push(children[i]);
}
}
return result;
}
return null;
}
}
target.cssstyle=function()
{

switch(arguments.length)
{
case 0:
return target.style;
case 1:
if(typeof(arguments[0])=='string')
{
if(arguments[0]=='opacity')
{
return _getopacity(target);
}
else if(target.style[arguments[0]])
{
return target.style[arguments[0]];
}
return null;
}
return null;
case 2:
if(typeof(arguments[0]=='string'))//&&typeof(arguments[1])=='string')
{
if(arguments[0]=='opacity'&&!isNaN(parseInt(arguments[1])))
{
return _setopacity(target,parseInt(arguments[1]));
}
return target.style[arguments[0]]=arguments[1];
}
return null;
case 3:
if(typeof(arguments[0]=='string')&&typeof(arguments[1])=='string'&&typeof(arguments[2])=='boolean')
{
var children=[];
var result=[];
if(arguments[2]===true)
{
children=target.all;
}
else
{
children=target.childNodes;
}
for(var i=0;i<children.length;i++)
{
var value=null;
if(arguments[0]=='opacity')
{
value=_getopacity(children[i]);
}
else
{
value=children[i].style[arguments[0]];
}

if(children[i].nodeType==1&&value==arguments[1])
{
result.push(children[i]);
}
}
}
}
}
target.event=function(name,fn)
{
var reg=/^on\w+/i;
if(!reg.exec(name))
{
name='on'+name;
}
target.attachEvent(name,fn);
}
target.resize=function(x,y)
{
if(target.resizeable===true)
{
var pos=$p(target);
exec(target.onmovebegin,target);
target.style.width=x+'px';
target.style.height=y+'px';
exec(target.onmoveend,target,pos);
}
}
target.resizeBy=function(x,y)
{
if(target.resizeable===true)
{
var pos=$p(target);
exec(target.onresizebegin,target);
target.style.width=target.offsetWidth+x+'px';
target.style.height=target.offsetHeight+y+'px';
exec(target.onresizeend,target,pos);
}
}
target.show=function(x,y)
{
if(!target.parentElement){document.body.appendChild(target);}
if(typeof(x)=='number'&&typeof(y)=='number'){target.move(x,y);}
exec(target.onshowbegin,target);
target.style.display='';
target.style.visibility='visible';
exec(target.onshowend,target);
}
target.hide=function()
{
exec(target.onhidebegin,target);
target.style.display='none';
target.style.visibility='hidden';
exec(target.onhideend,target);
}
target.max=function()
{
var maxWidth=$b.clientWidth
var maxHeight=$b.clientWidth;
target.move(0,0);
target.resize(maxWidth,maxHeight);
}
target.applyTheme=function(theme)
{throw 'can not call a abstract method directly.'}

// opacity is an integer between 0 and 100.
// 0 means transparent,100 means solid
_setopacity=function(e,opacity)
{
if(!(e&&e.nodeType==1&&typeof(opacity)=='number')){return null;}
if(window.gecko===true)
{
e.style.opacity=opacity/100;
}
else
{
var alpha=/alpha\(.*opacity=(\d+)\)/i;
if(alpha.exec(e.style.filter))
{
e.style.filter=e.style.filter.replace(alpha,changeAlpha);
function changeAlpha()
{
return arguments[0].replace(/opacity=\d+/i,'opacity='+opacity);
}
}
else
{
e.style.filter+=' alpha(opacity='+opacity+')';
}
}
return opacity;
}
_getopacity=function(e)
{
if(window.gecko===true)
{
return e.style.opacity*100;
}
var reg=/alpha\(.*opacity=(\d+)\)/i;
if(reg.exec(e.style.filter))
{
return parseInt(RegExp.$1);
}
return 100;
}

// events
target.onmovebegin={}; //callback:function(target){...}
target.onmoveend={}; //callback:function(target,pos before moved){...}
target.onresizebegin={}; //callback:function(target){...}
target.onresizeend={}; //callback:function(target,pos before resized){...}
target.onshowbegin={}; //callback:function(target){...}
target.onshowend={}; //callback:function(target){...}
target.onhidebegin={}; //callback:function(target){...}
target.onhideend={}; //callback:function(target){...}

return target;
}
return null;
}
if(e)
{
if(typeof(e)=='string')
{
if(/^#(\w+)/i.exec(e))
{
//e=$(RegExp.$1);
e=document.getElementById(RegExp.$1);
}
else
{
e=$$(e);
}
}
if(e&&e.nodeType==1)
{
return _self.attach(e);
}
}
}