//!!!!



//props:
//type: 0 - fixed element (app or window), 1 - popup element (menu), 2 - global listener (desktop)
//owner: free format value to be used as backlink to owner of focus (app or end object etc)
//thispar: this for all handlers
//param: user param for all handlers
//parent: parent _uFocus object which will get all events

//       Event handlers are propagated from the highest parent focus down to current focus
//       True return value from handler stops propagation (so parent focus can intercept event)
//       Global focus listeners get all events and can cancel them for all other focus objects.
//onkeydown: thispar.func(event,param,target focusobj)     true return value means that event must not be propagated to child focus objects (or all other is current is global)
//onkeyup: thispar.func(event,param,target focusobj)
//onkeypress: thispar.func(event,param,target focusobj)
//       Activates are propagated in the same way as key events but propagation cannot be cancelled, except for new activate
//onactivate: only for type 0 and 1; thispar.func(param,oldfocus,target focusobj) return value of -1 shows that activate() was called and current focus change processing must be stopped immediately
//		 Check whether focus can be activated; propagated as key events; false - forbid activation
//canactivate: only for type 0 and 1; thispar.func(param,oldfocus,target focusobj) false - cannot, true (but not -1) - can, return value of -1 shows that activate() was called and current focus change processing must be stopped immediately
//       Deactivates are propagated in reverse order, from current to highest parent 
//ondeactivate: only for type 0 and 1; thispar.func(param,oldfocus (THIS is part of it),target focusobj) return value of -1 shows that activate() was called and current focus change processing must be stopped immediately

function _uFocus(props) {
	if(!props) props={};
	this.constructor=_uFocus;
	this._tp=props.type || 0;
	this._thispar=props.thispar || null;//this for all handlers
	this._param=props.param || null;//user param for all handlers
	this._parent=props.parent || null;//user param for all handlers
	delete props.type;
	delete props.thispar;
	delete props.param;
	delete props.parent;

	this.owner=null;

	jQuery.extend(this,props || {});
	this.destroyed=0;

	if(!_uFocus.globalset) {
		_uFocus.globalset=true;
	    jQuery(document).bind("keydown keyup keypress",_uFocus._onkey);
	}
	if(this._tp==2) {
		_uFocus.glisteners.push(this);
	}
}

_uFocus.current=null; //current listener of type 0 or 1 if any
_uFocus.last_fixed=null; //if current has type=1, then previos current of type 0 stored here
_uFocus.glisteners=[]; //list of global listeners
_uFocus.globalset=false;
_uFocus.inprocess=0;
_uFocus.delayedactivate=null; //if somefocus.activate() was called from canactivate or onactivate handler, somefocus is stored here and repeated after

_uFocus._onkey=function(e) {
	var fn='on'+e.type,t;
	//correct opera bugs in processing arrow keys
	if(e.type=='keypress') {
		if(
			(	(e.keyCode==37 || e.keyCode==38 || e.keyCode==39 || e.keyCode==40 || 
					(e.keyCode==0 && jQuery.browser.opera)
				) &&
				e.target && 
				e.target.tagName.toLowerCase()!='input' && e.target.tagName.toLowerCase()!='textarea'
			) ||
			(e.keyCode==32 && e.ctrlKey)
		   )
//alert(e.target);
//e.stopPropagation();
			e.preventDefault();
	}
	if(e.type=='keydown' && jQuery.browser.opera && e.keyCode==0) { //it must be APP key 93
		e.keyCode=93;
	}
	//send event to global handlers
	for(var i=0;i<_uFocus.glisteners.length;i++) {
		t=_uFocus.glisteners[i];
		if(!t[fn]) continue;
		if(t[fn].call(t._thispar,e,t._param,_uFocus.current)) return; //stop propagation
	}
	t=_uFocus.current;
	if(!t) return; //no active focus
	//else send to active focus beginning from deepest parent
	var pars=[]; //list of our parents and self
	while(t) {
		pars.unshift(t);
		t=t._parent;
	}
	for(var i=0;i<pars.length;i++) {
		if(!pars[i][fn]) continue;
		if(pars[i][fn].call(pars[i]._thispar,e,pars[i]._param,_uFocus.current)) return; //stop propagation
	}
};
_uFocus.prototype={
	//return 0 if no focus was set, true on success, -1 if activate was delayed
	activate: function() { 
		if(this.destroyed || (this._tp!=0 && this._tp!=1)) return 0; //only such types can use explicit activate
		if(_uFocus.inprocess) {
			_uFocus.delayedactivate=this;
			return -1;
		}
		var r;
		_uFocus.inprocess=1;
//		_uFocus.delayedactivate=null;
		r=this._activate(this,_uFocus.current);
		_uFocus.inprocess=0;
		if(_uFocus.delayedactivate) {
			setTimeout("var f=_uFocus.delayedactivate;_uFocus.delayedactivate=null;if(f)f.activate();",0);
		}
		return r;
	},
	deactivate: function() {
		if(this.destroyed || (this._tp!=0 && this._tp!=1) || _uFocus.inprocess) return; //only such types can use explicit deactivate
		var r;
		_uFocus.inprocess=1;
//		_uFocus.delayedactivate=null;
		r=this._deactivate(_uFocus.current,null); //after this call we have current pointing to parent of THIS if r!=-1
		_uFocus.inprocess=0;
		if(_uFocus.delayedactivate) {
			setTimeout("var f=_uFocus.delayedactivate;_uFocus.delayedactivate=null;if(f)f.activate();",0);
			return;
		}
		if(r==1 && _uFocus.last_fixed && (!_uFocus.current || _uFocus.current._tp!=1)) { //here tp can be only ==1
			_uFocus.last_fixed.activate();
		}
	},
	isactive: function(nochild) {
		return this==_uFocus.current || (!nochild && this.isparentof(_uFocus.current));
	},
	isparentof: function(obj) { //checks whether obj is parent of THIS
		if(!obj) return 0;
		var t=obj._parent;
		while(t) {
			if(t==this) return 1;
			t=t._parent;
		}
		return 0;
	},
	_activate: function(tobj,prevobj,nochild) { //target focus object of first activate() for correct propagation. previous focus
		var t,r;
//console.log('Focus in _activate('+nochild+') '+descr(this));
		//check that current focus is not this on any depth
		if(this.isactive()) {
			t=_uFocus.current;
			if(!nochild || t==this) return 1;
			//else deactivate all child focuses
//			while(t && t._parent!=this) {
//				t=t._parent;
//			}
//			if(t) t._deactivate(prevobj,tobj); //it will update _uFocus.current
		}
//console.log('Focus isactive is false for '+descr(this));
		
		//transfer command to parent if have any
		if(this._parent) {
			if(!this._parent._activate(this._parent,prevobj)) return 0;
		}
		//at this point all parents are active or none exists but _uFocus.current can point to old focus

		//deactivate previous focus up to the common parent or null
		t=_uFocus.current;
		if(t && t!=this._parent) {
			while(t._parent && t._parent!=this._parent) {
				t=t._parent;
			}
			if(t._deactivate(prevobj,tobj)==-1) return 0; //it will update _uFocus.current; -1 means another activate was called
		}

		//at this point all parents are active and _uFocus.current points to parent (if any) or null
		//we can try to activate THIS focus
		//send canactivate notification to global handlers
		for(var i=0;i<_uFocus.glisteners.length;i++) {
			t=_uFocus.glisteners[i];
			if(!t.canactivate) continue;
			r=t.canactivate.call(t._thispar,t._param,prevobj,tobj);
			if(!r || (r==-1 && _uFocus.delayedactivate)) return 0; //cancel focus activation
		}
		//send canactivate notification to all parents. any parent can cancel activation of THIS focus
		var pars=[this]; //list of our parents and self
		t=this._parent;
		while(t) {
			pars.unshift(t);
			t=t._parent;
		}
		for(var i=0;i<pars.length;i++) {
			if(!pars[i].canactivate) continue;
			r=pars[i].canactivate.call(pars[i]._thispar,pars[i]._param,prevobj,tobj);
			if(!r || (r==-1 && _uFocus.delayedactivate)) return 0; //cancel focus activation
		}
		//here all parents and THIS allowed activation
		_uFocus.current=this;
		if(this._tp==1 && prevobj && prevobj._tp==0) _uFocus.last_fixed=prevobj; //remember last fixed focus for new popup focus
			else if(this._tp==0) _uFocus.last_fixed=null; //if this type==1 and prev type==1 then last_fixed unchanged

		//notify about successful activation
		for(var i=0;i<_uFocus.glisteners.length;i++) {
			t=_uFocus.glisteners[i];
			if(t.onactivate) 
				if(t.onactivate.call(t._thispar,t._param,prevobj,tobj)==-1 && _uFocus.delayedactivate) return 0;
		}
		for(var i=0;i<pars.length;i++) {
			if(pars[i].onactivate) 
				if(pars[i].onactivate.call(pars[i]._thispar,pars[i]._param,prevobj,tobj)==-1 && _uFocus.delayedactivate) return 0;
		}
		//here all parents and THIS are notified of activation
		return 1;		
	},
	_deactivate: function(tobj,newobj) {
		//check that current focus is really in use
		if(!this.isactive()) return 0;

		//send ondeactivate to all children and THIS
		var ii,tt;
		var t=_uFocus.current;
		while(t) {
			_uFocus.current=t._parent; //ondeactivate handlers must not see its active status
			if(t.ondeactivate) {
				if(t.ondeactivate.call(t._thispar,t._param,tobj,newobj)==-1 && _uFocus.delayedactivate) return -1;
			}
			if(t._tp!=1) _uFocus.last_fixed=null; //only deactivation of whole hierarchy of focuses with tp==1 preserves last_fixed

			//send deactivate notification to global handlers
			for(ii=0;ii<_uFocus.glisteners.length;ii++) {
				tt=_uFocus.glisteners[ii];
				if(tt.ondeactivate) {
					tt.ondeactivate.call(tt._thispar,tt._param,t,newobj);
				}
			}

			if(t==this) break;
			t=t._parent;
		}
		return 1;
	},
	destroy: function() {
		if(this.destroyed) return;
		this.deactivate();
		if(this._tp==2) { //remove global listener
			for(var i=0;i<_uFocus.glisteners.length;i++) {
				if(_uFocus.glisteners[i]==this) {
					_uFocus.glisteners.splice(i,1);
					break;
				}
			}
		}
		this.destroyed=1;
	}
};

function _uGetOffset(obj){
	if(!obj) return { 'left' : 0, 'top' : 0 };
	var left_offset = obj.offsetLeft;
	var top_offset = obj.offsetTop;
	if(!left_offset && !top_offset && obj.offsetParent==null) {
	left_offset=parseInt(obj.style.left);
	top_offset=parseInt(obj.style.top);
	} else
	while ((obj = obj.offsetParent) != null)
	{
		left_offset += obj.offsetLeft;
		top_offset += obj.offsetTop;
	}

	return { 'left' : left_offset, 'top' : top_offset };
}


function _uMenu(id,par,group) {
    this.init(id,par,group);
}
_uMenu.get=function(id) {
    var o=jQuery('#'+id)[0];
    if(o) return o._umenu;
    return null;
}
_uMenu.show=function(id,par,tp,mid,dy,dx) {
    var o=_uMenu.get(id);
    if(o)o.show_menu(par,tp,mid,dy,dx);
};
_uMenu.hide=function(id,c) {
    var o=_uMenu.get(id);
    if(o) if(!c)o.hide_menu();else o.hide_child();
};
_uMenu.hideAll=function(e) {
//    if (e.which!=1 && e.type == 'click')return;
    var gr;
    with(_uMenu.prototype) {
	for (gr in have_active) {
	    if(have_active[gr] && !donothide[gr]) {
		for (var i in all_menus[gr]) _uMenu.hide(i);
    	    have_active[gr]='';
	    }
	donothide[gr]=false;
	}
    }
};
_uMenu.showOver=function (id,par,tp,mid,dy,dx) {
    var o=_uMenu.get(id);
    if(o) {
	var gr=o.group;
        with(_uMenu.prototype) {
	    if(timerid2[gr]) clearTimeout(timerid2[gr]);
	    if (have_active[gr].length>0 && have_active[gr].indexOf(","+id+",")==-1)
		o.show_menu(par,tp,mid,dy,dx);
	}
    }
};
_uMenu.schedule_hidechild=function(id) {
    var o=_uMenu.get(id);
    if(o) {
	var gr=o.group;
        with(_uMenu.prototype) {
    	if(timerid2[gr]) clearTimeout(timerid2[gr]);
	    timerid2[gr]=setTimeout("_uMenu.hide('"+id+"',1);_uMenu.prototype.timerid2['"+gr+"']=0;",800);
	}
    }
}

_uMenu.prototype={
    donothide: [], //group is first key
    all_menus: [], //group is first key
    have_active: [], //group is first key
    timerid: [], //group is first key
    timerid2: [], //group is first key
    global_set: false,
    init: function (id,par,group){
		this.id=id;
        this.obj=jQuery('#'+id)[0];
        if(!this.obj) return;
        this.obj._umenu=this;
        if(!par) par=id;
        this.parentid=par;
        if(!group) group='def';
        this.group=group;
        this.obj.style.display = 'none';
        jQuery(this.obj).bind("click",this,function(e) {e.data.donothide[e.data.group]=true;});
	if(this.all_menus[group]==undefined) this.all_menus[group]=[];
	if(this.have_active[group]==undefined) this.have_active[group]='';
	this.all_menus[group][id]=this;
	if(!this.global_set) {
	    jQuery(document).bind("click",_uMenu.hideAll);
	    jQuery(window).bind("resize",_uMenu.hideAll);
	    _uMenu.prototype.global_set=true;	
	}
    },
    show_menu: function (par,tp,mid,dy,dx) { 
        var ddX=0;
        var ddY=0;
        if(this.timerid2[this.group]) clearTimeout(this.timerid2[this.group]);
        if(this.have_active[this.group].indexOf(","+this.id+",")>=0) {
                this.hide_menu();
                return;
        }
        if(!dy)dy=0;
        if(!dx)dx=0;
        if(mid){
                ddX=jQuery('#'+mid)[0].offsetLeft;
                ddY=jQuery('#'+mid)[0].offsetTop;
        }
        this.allmenus_hidenp();
        if(!par) par=this.obj.parentNode; else par=jQuery('#'+par)[0];
        var pos=_uGetOffset(par);
        pos['left']+=dx-ddX;
        pos['top']+=dy-ddY;
        if(tp=='r') pos['left']+=par.offsetWidth-4;
    	    else pos['top']+=par.offsetHeight+1; //tp=='d'
        var ww=jQuery(window).width();
        with(this.obj.style) {
    	left=pos['left']+'px';
    	top=pos['top']+'px';
    	display='';
    	visibility='visible';
        };
        try { if(pos['left']+this.obj.offsetWidth>ww) {
                pos['left']=ww-this.obj.offsetWidth-5;
                this.obj.style.left=pos['left']+'px';
                }
        } catch(e) {}
        if(!this.have_active[this.group]) this.have_active[this.group]='';
        this.have_active[this.group]+=','+this.id+',';
        if(this.timerid[this.group]) clearTimeout(this.timerid[this.group]);
        this.donothide[this.group]=true;
        this.timerid[this.group]=setTimeout("with(_uMenu.prototype){donothide['"+this.group+"']=false;timerid['"+this.group+"']=null;};",100);
    },
    allmenus_hidenp: function () {
        var parents={};
        var id=this.id;
        while(this.all_menus[this.group][id] && this.all_menus[this.group][id].parentid!=id && id) {parents[this.all_menus[this.group][id].parentid]=1;id=this.all_menus[this.group][id].parentid;}
        for (var i in this.all_menus[this.group]) {
                if(parents[i]==1) continue;
                this.all_menus[this.group][i].hide_menu();
        }
    },
    hide_menu: function () {
      with(this) {
        hide_child();
        var x=have_active[group].indexOf(","+id+",");
        if(x>=0) have_active[group]=have_active[group].substring(0,x);
        obj.style.display = 'none';
        obj.style.visibility = 'hidden';
      }
    },
    hide_child: function () {
      with(this) {
        for (var i in all_menus[group]) if(all_menus[group][i].parentid==id && i!=id) all_menus[group][i].hide_menu();
      }
    }
};


/*
Each menu item can be in form:
1.  'string' - just string with default action onitem (if it's defined) or without action. in last case item has not hover hightlight by default
2.  'sep' - separator
3.  ['label',action,props]
	def props is {hl:0} if action is not defined
	action can be:
	3.1 'a' - tells that item has <a> tag and item action is to follow href in that tag
	3.2 function - action(item_index,menuobj,id,item_opts,event). if null then global onitem is used. default this parameter is thispar from menu props
	3.3 array - submenu
	props:
		{
		action - 'a' or function if second element was array for creating submenu (to add action to item opening submenu)
		id - item string id which will be sent to action or onitem func in addition to index
		hl  - hover highlight. can be used to overwrite menu property
		thispar - this parameter for action
		mark - if menu has marks enabled specifies the state of mark (false - hidden, true - visible)
		marktext - if mark is visible (mark is true) then specifies html content for mark element (e.g. img tag). can be specified as array (object). then mark true values are used as index to this array. menu.props.withmarks is the default value of this field
		onldown - action for left mouse down
		onrdown - action for right mouse down
		pos - override default pos properties for submenu
		opts - override default properties for submenu
		}
*/

function _uMENU(name,pos,opts,elems,noinit) {
	this.constructor=_uMENU;
	this.name=name;
	this.pos=jQuery.extend({
		pos: null,
		alignObj: null,
		align: 'd',
//r(R) - to the right from object + dx, top edge from object + dy ; R will change horiz pos to l-rules if goes out screen
//l(L) - to the left from object + dx, top edge from object + dy ; L will change horiz pos to r-rules if goes out screen
//d(D) - left (right from rtl) edge from object+dx, to the down from object + dy ; D will change vert pos to u-rules if goes out screen
//u(U) - left (right from rtl) edge from object+dx, to the up from object + dy ; U will change vert pos to d-rules or horiz pos to l-rules if goes out screen
		parent: null,
		parentfocus:null, //apps must specify app._focus for popup without windows
		dx:0,
		dy:0,
		childdy:0,
		childdx:0
	},pos || {});
	this.props=jQuery.extend({
		parentnode: null, //inherited by child menus
		design: _uMENU.defdesign || 'std',
		hidden: opts.statical && elems && elems.length>0 ? 0 : 1,
		shadow: 1,
		addclass: '', //add this class to class of top menu html object
		withmarks: null, //if true (some object specified) then items of this menu can have marks to the left (u-menumarks is added to top menu object). value of this field is the default marktext
		highlight: 1, //whether hovered items must be highlighted by adding class u-menuitemhl
		statical: opts.horiz ? 1 : 0, //do not hide menu on mouse clicks
		hidetimer: 0, //time in ms to hide menu if no item was hovered
		horiz: 0, //whether menu is horizontal
		noabs: 0, //do not use position:absolute for menu
		width: 0, //0 - width is calculated by content, auto - width is just not assigned (for noabs=1), X - specified value
		onshow: null, //(menuobj,menuidx) called just before menu is shown (can be used to set marks)
		onhide: null,
		ondestroy: null,
		onitem: null, //inherited by child menus, reaction to item click
		onldown: null, //inherited by child menus, reaction to item left mouse down
		onrdown: null, //inherited by child menus, reaction to item right mouse down
		thispar: null, //'this' param for handlers; inherited by child menus
		onreadycreate: null, //(menu) called when menu is fully inited
		wnd:null, //inherited by child menus, focus will be child of window's focus
		rtl:0 //inherited by child menus
	},opts || {});
	this.state={visible:false,init:false,destroyed:false};
    this.design=this.props.design && _uMENU.designs[this.props.design] ? _uMENU.designs[this.props.design] : _uMENU.designs['std'];
    this.idx=_uMENU.nextidx++;
    _uMENU.all[this.idx]=this;
   	if(!this.pos.parent) this.zpos=_uMENU.defz;
   		else this.zpos=this.pos.parent.zpos+5;
    this.xpos=this.ypos=0;
	this.width=10;
	this.height=10;	
	this.elems=[];
	this.add_elems=elems && elems.length>0 ? [elems] : [];
	this.del_elems=[];
	this.pend_show=null;
	this.appendtimer=null;
	this.decor={w:0,h:0};
	this.frame=null;
	this.childtimer=null;
	this.sh=null;
	this.hidetimer=null;

	this.hlitem=-1; //highlighted item or -1 if none
	this.have_active=null;

	this._focus=new _uFocus({type:1,thispar:this,owner:this,parent:this.pos.parentfocus ? this.pos.parentfocus : (this.pos.parent ? this.pos.parent._focus : (this.props.wnd ? this.props.wnd._focus : null)),onkeydown:this.onkeydown,ondeactivate:this.onfocusdeactivate});

	var t=document.createElement("div");
	t.id="_umenu"+this.idx;
	if(!this.props.parentnode) jQuery(jQuery("body")[0]).prepend(t);
		else this.props.parentnode.appendChild(t);

	this.top=t;
    jQuery(t).addClass("x-unselectable").css({visibility:'hidden',display:'block'});
    var m=document.createElement("div");
    t.appendChild(m);
    this.menu=m;

	if(
//	!this.props.horiz || 
	!this.props.noabs) {
    	jQuery(t).css({position:'absolute',zIndex:this.zpos});
	    if(jQuery.browser.msie && parseFloat(jQuery.browser.version)<7){
		    var html = '<iframe tabindex="-1" '
    		    +'style="display:block; position:absolute; '
			+'filter:Alpha(Opacity=\'0\'); '
			+'width:'+this.width+'px'
			+'height:'+this.height+'px;border:0"/>';

	    	this.frame=document.createElement(html);
			t.appendChild(this.frame);
	    }
	    if(this.props.shadow && !this.props.horiz && (!jQuery.browser.msie || jQuery.browser.version>6)) {
			this.shadow_init();
			this._resizeSh();
	    }
    	jQuery(m).css({position:"absolute",zIndex:2,left:0});
	}
   	jQuery(m).css("width","50px").bind("mousedown",this,_uMENU._onmenumousedown);

    this.parts=this.design.menu_init(m,this.props.horiz ? 10 : 50,this.props,this.pos.parent);
	if(!_uMENU.globalset) {
		_uMENU.globalset=true;
	    jQuery(document).bind("mousedown",_uMENU.hideallmenus);
	    jQuery(window).bind("resize",_uMENU.hideallmenus);
	}
	this.toinit=[]; //submenus which require init1()
	this.tocalcsize=[]; //submenus which require calcsize()
	this.createsubmenus();

	if(!this.props.hidden) this.show();
	if(!noinit)setTimeout("var m=_uMENU.all["+this.idx+"];if(m)m.init1();",10);
}


_uMENU.all=[];
_uMENU.nextidx=0;
_uMENU.lastz=0;
_uMENU.defz=25050;
_uMENU.defdesign='std';
_uMENU.globalset=false;
_uMENU.ignoreclick=null;

_uMENU.getbyname=function(name) {
    var a=this.all;
    for(var i=0;i<a.length;i++) if(a[i] && a[i].name==name) return a[i];
    return null;
};

_uMENU.designs={
    std: {
	sh_sz: [4,2], //design specific shadow config

	shadow_init: function(top) {
	    var sh=[];
	    for(var i=0;i<3;i++) {
		sh[i]=document.createElement("div");
		top.appendChild(sh[i]);
		jQuery(sh[i]).attr("class","x-sh").css("position","absolute").css("z-index",1);
	    }
	    jQuery(sh[0]).addClass("xsl").css({width:(this.sh_sz[0]+this.sh_sz[1])+"px",left:(-this.sh_sz[0])+"px",top:"0px"}).html('<div class="xstl"><div class="xsml"></div></div>');
	    jQuery(sh[1]).addClass("xsr").css({width:(this.sh_sz[0]+this.sh_sz[1])+"px",top:"0px"}).html('<div class="xstr"><div class="xsmr"></div></div>');
	    jQuery(sh[2]).addClass("xsb").css({height:(this.sh_sz[0]+this.sh_sz[1])+"px",left:(-this.sh_sz[0])+"px"}).html('<div class="xsbl"><div class="xsbr"><div class="xsbc"></div></div></div>');
	    return sh;
	},
	shadow_resize: function(sh,w,h) {
	    jQuery(sh[0]).css({height:(h-this.sh_sz[1])+"px"});
	    jQuery(sh[1]).css({height:(h-this.sh_sz[1])+"px",left:(w-this.sh_sz[1])+"px"});
	    jQuery(sh[2]).css({width:(w+this.sh_sz[0]*2)+"px",top:(h-this.sh_sz[1])+"px"});
	},
	shadow_hide: function(sh) {
	    jQuery(sh[0]).add(sh[1]).add(sh[2]).hide();
	},
	shadow_show: function(sh) {
	    jQuery(sh[0]).add(sh[1]).add(sh[2]).show();
	},
	menu_init: function(menu,inith,props,parent) {
	    var p={};
		if(!props.horiz) {
			jQuery(menu).attr("class","u-menu"+(props.addclass ? ' '+String(props.addclass): '')+(props.withmarks ? ' u-menumarks': ''));
			if(jQuery.browser.msie) jQuery(menu).css("overflow","hidden");
		    jQuery(menu).html(
(parent && parent.props.horiz ? '' : '<div class="xw-tl"><div class="xw-tr"><div class="xw-tc xw-tsps"></div></div></div>')
+ '<div class="xw-ml"><div class="xw-mr"><div class="xw-mc"><div class="u-menubody">'
+ '<div class="u-menucont" style="overflow:visible;height:'+inith+'px"></div></div></div></div></div>'
+ '<div class="xw-bl"><div class="xw-br"><div class="xw-bc"><div class="xw-footer"></div></div></div></div>'
		    );
//		    p.upper=jQuery(menu).find(".xw-tl")[0];
//		    p.center=jQuery(wnd).find(".xw-ml")[0];
//	    	jQuery(wnd).find(".xw-mc").bind("mousedown",function(e){e.stopPropagation();});
		} else {
			jQuery(menu).attr("class","u-menuh"+(props.addclass ? ' '+String(props.addclass): '')+(props.withmarks ? ' u-menumarks': '')).css("overflow","hidden");
			jQuery(menu).html('<div class="u-menubody"><div class="u-menucont" style="overflow:hidden;height:'+inith+'px"></div></div>');
		}
	    p.content=jQuery(menu).find(".u-menucont")[0];
		p.elems=[];
		p.marks=[];
		jQuery(menu).find("div,span").andSelf().attr("unselectable","on");
	    return p;
	},
	append_item: function(o,el,hl) {
		var i=o.elems.length,a,txt,act=null,mark,actl,actr;
		act=el[1] ? el[1] : (typeof (el[2].action)!='undefined' ? el[2].action : o.props.onitem);
		actl=el[2].onldown || o.props.onldown;
		actr=el[2].onrdown || o.props.onrdown;
		txt=el[0];
		if(el[2].hl!=undefined) hl=el[2].hl;
		if(o.props.withmarks && txt!='sep' && el[2].mark!=undefined) { //if we have to add u-menumark object
			txt='<div class="u-menumark"></div>'+txt;
			mark=1;
		}
		if(act && (act=='a' || el[2].action=='a')) {
			a=document.createElement('a');
			jQuery(a).css('display','block');
		} else a=document.createElement('div');
		if(!o.props.horiz) {
			if(txt=='sep') {
				a.className='u-menuvsep';
			} else {
				a.className='u-menuvitem';
				if(!act || act.constructor!=_uMENU) {
					jQuery(a).html(txt);
					if(hl) jQuery(a).bind("mouseover",{hl:hl,item:i,obj:o},_uMENU._onitemmouseover).bind("mouseout",{hl:hl,item:-1,obj:o},_uMENU._onitemmouseout);
					if(act) jQuery(a).bind("click",{act:act,obj:o,item:i,itemobj:el},_uMENU._onitemclick);
					if(actl || actr) jQuery(a).bind("mousedown",{actl:actl,actr:actr,obj:o,item:i,itemobj:el},_uMENU._onitemmousedown);
				} else {
//					jQuery(a).addClass('u-menuvitemparent').html('<div class="u-menuarrow" style="float:right"></div>'+txt);
//					jQuery(a).addClass('u-menuvitemparent').html('<table align="center" style="text-align:left;padding:0;margin:0;botder:0" cellspacing="0" cellpadding="0"><tr><td style="white-space:nowrap">'+txt+'</td><td><div class="u-menuarrow"></div></td></tr></table>');
					jQuery(a).addClass('u-menuvitemparent').html('<div class="u-menuarrow"></div>'+txt);
					jQuery(a).bind("mouseover",{hl:hl,item:i,obj:o},_uMENU._onitemmouseover).bind("mouseout",{hl:hl,item:i,obj:o},_uMENU._onitemmouseout);
					jQuery(a).bind("mousedown",{child:act,obj:o,item:i,itemobj:el},_uMENU._onitemclick);
					if(el[2].action) jQuery(a).bind("click",{act:el[2].action,obj:o,item:i,itemobj:el},_uMENU._onitemclick);
					if(actl || actr) jQuery(a).bind("mousedown",{actl:actl,actr:actr,obj:o,item:i,itemobj:el},_uMENU._onitemmousedown);
				}
			}
		} else { //horiz
			jQuery(a).css("float","left"); 
			if(txt=='sep') {
				a.className='u-menuhsep';
			} else {
				a.className='u-menuhitem';
				if(!act || act.constructor!=_uMENU) {
					jQuery(a).html(txt);
					if(hl) jQuery(a).bind("mouseover",{hl:hl,item:i,obj:o},_uMENU._onitemmouseover).bind("mouseout",{hl:hl,item:i,obj:o},_uMENU._onitemmouseout);
					if(act) jQuery(a).bind("click",{act:act,obj:o,item:i,itemobj:el},_uMENU._onitemclick);
					if(actl || actr) jQuery(a).bind("mousedown",{actl:actl,actr:actr,obj:o,item:i,itemobj:el},_uMENU._onitemmousedown);
				} else {
					jQuery(a).addClass('u-menuhitemparent').html(txt);
					jQuery(a).bind("mouseover",{hl:hl,item:i,obj:o},_uMENU._onitemmouseover).bind("mouseout",{hl:hl,item:i,obj:o},_uMENU._onitemmouseout);
					jQuery(a).bind("mousedown",{child:act,obj:o,item:i,itemobj:el},_uMENU._onitemclick);
					if(el[2].action) jQuery(a).bind("click",{act:el[2].action,obj:o,item:i,itemobj:el},_uMENU._onitemclick);
					if(actl || actr) jQuery(a).bind("mousedown",{actl:actl,actr:actr,obj:o,item:i,itemobj:el},_uMENU._onitemmousedown);
				}
			}
		}
		if(hl) jQuery(a).find("*").andSelf().filter('[nodeType=1]').attr("unselectable","on");
		o.elems[i]=el;
		o.parts.elems[i]=a;
		if(mark) o.parts.marks[i]=jQuery(a).find('.u-menumark')[0];
		if(o.props.horiz && (o.props.rtl || window._rtl))
			o.parts.content.insertBefore(a,o.parts.content.firstChild);
			else
			o.parts.content.appendChild(a);
	},
	calc_size: function(o) {
		var w=0,h=0,hm=0,prevm=0;
		if(!o.props.horiz) {
			for(var i=0;i<o.parts.elems.length;i++) {
				w=Math.max(w,Math.max(o.parts.elems[i].offsetWidth,o.parts.elems[i].scrollWidth));
				hm+=Math.max(prevm,parseInt('0'+jQuery(o.parts.elems[i]).css('margin-top')));
				prevm=parseInt('0'+jQuery(o.parts.elems[i]).css('margin-bottom'));
				h+=o.parts.elems[i].offsetHeight;
			}
			h+=hm+prevm;
		} else {  //horiz
			for(var i=0;i<o.parts.elems.length;i++) {
				w+=Math.max(o.parts.elems[i].offsetWidth,o.parts.elems[i].scrollWidth);
				h=Math.max(h,o.parts.elems[i].offsetHeight);
			}
		}
		return {w:w,h:h};
	},
	_onitemhl: function(e,st) {
		if(st) jQuery(this).addClass("u-menuitemhl");
			else jQuery(this).removeClass("u-menuitemhl");
	}
    }	
};
_uMENU._onitemmouseover=function(e) {
		var d=e.data;

		var prev=d.obj.hlitem,it,hl;
		if(prev>=0 && prev!=d.item) { //unhighlight previous item
			it=d.obj.elems[prev];
			if(it && it[0]!='sep') {
				hl=d.obj.props.highlight;
				if(it[2].hl!=undefined) hl=it[2].hl;
				if(hl) {
					d.obj.design._onitemhl.apply(d.obj.parts.elems[prev],[null,0]);
				}
			}
		}


		if(d.hl) d.obj.design._onitemhl.apply(this,[e,1]);
		d.obj.hlitem=d.item;
		if(d.obj.hidetimer) clearTimeout(d.obj.hidetimer); d.obj.hidetimer=null;
		if(d.item>=0)if(!d.obj.props.horiz || d.obj._focus.isactive())d.obj.schedule_childopen(d.item);
};
_uMENU._onitemmouseout=function(e) {
		var d=e.data,it;
		//that that we do not have opened child
		if(d.hl) {
			if(d.obj.props.statical && d.obj._focus.isactive()) return; //do not blur active static menus
			if(d.item==d.obj.hlitem && d.item>=0) {
				it=d.obj.elems[d.item];
				if(it && it[1] && it[1].constructor==_uMENU && it[1].state.visible) return; //do not blur active submenu
				d.obj.design._onitemhl.apply(this,[e,0]);
				d.obj.hlitem=-1;
			}
		}
//		d.obj.reset_childopen();
};
_uMENU._onitemclick=function(e) {
		var d=e.data,act=d.itemobj[1] || d.itemobj[2].action || d.obj.props.onitem;
		if(!d.child && act) {
			if(!d.obj.props.statical)d.obj.hide(true);else d.obj.hidechildren();
			if(act.constructor==Function) {
				act.apply(d.itemobj[2].thispar || d.obj.props.thispar,[d.item,d.obj,d.itemobj[2].id,d.itemobj[2],e]);
			} 
			else if(act=='a') {
				var l=null;
			    if(this==e.target || e.target.tagName.toLowerCase()!='a') {
					if(this!=e.target) { //check that we do not have tag-A parent
						var c1=jQuery(e.target).find('*').andSelf(),c2=jQuery(this).find('*').not(c1);
						if(c2.filter("A").length==0) l=jQuery(this).find("a")[0];
							else l=null;
					} else l=jQuery(this).find("a")[0];
					if(l) {
						this.target=l.target;
						this.href=l.href;
						if(l.onclick) return l.onclick();
					}
				}
				if(!l){
					this.removeAttribute('href');
				}
			}
			return;
		}
		if(!d.child) return;
//		_uMENU.ignoreclick=d.child;
		d.obj.reset_childopen();
		d.obj.hidechildren(d.child);
		if(d.obj.props.horiz && d.child.state.visible) {d.child.hide();d.obj._focus.deactivate();}
			else
		if(!d.child.state.visible)// d.child.hide();
//			else 
			d.obj.childopen(d.item);
};
_uMENU._onitemmousedown=function(e) {
		var d=e.data,actl=d.actl || d.obj.props.onldown,actr=d.actr || d.obj.props.onrdown;
		if(e.which==1 && e.ctrlKey) e.which=3;
		if(e.which==1 && actl) {
			_uWnd.globalmousedown();
			actl.apply(d.itemobj[2].thispar || d.obj.props.thispar,[d.item,d.obj,d.itemobj[2].id,d.itemobj[2],e]);
			e.stopPropagation();
		} else if(e.which==3 && actr) {
//			_uWnd.globalmousedown();
			actr.apply(d.itemobj[2].thispar || d.obj.props.thispar,[d.item,d.obj,d.itemobj[2].id,d.itemobj[2],e]);
			e.preventDefault();
			e.stopPropagation();
		}
};
//hides all non-statical menus which are visible except for 'exc' and its parents
_uMENU.hideallmenus=function() { //optional exception
	var i,m,ig;
loop:
	for(i=0;i<_uMENU.all.length;i++) {
		m=_uMENU.all[i];
		if(!m || !m.state.visible) continue;
		if(m.props.statical) {m._focus.deactivate();continue;}
		ig=_uMENU.ignoreclick;
		while(ig) {
			if(ig==m) continue loop;
			ig=ig.pos.parent;
		}
		m.hide();
	}
	if(_uMENU.ignoreclick)setTimeout("_uMENU.ignoreclick=null;",10);
};
_uMENU._onmenumousedown=function(e) {
//	if(!_uMENU.ignoreclick)_uMENU.ignoreclick=e.data;
	if(e.data.props.wnd) e.data.props.wnd.activate(e,1);
	if(e.which==1 && e.ctrlKey) e.which=3;
	if(e.which==3 && e.data.props.horiz) return;
	e.stopPropagation();
};
_uMENU.prototype={
	onfocusdeactivate: function() {
		if(this.props.statical) { //unhighlight previous item
			var prev=this.hlitem,it,hl;
			if(prev>=0) { 
				it=this.elems[prev];
				if(it && it[0]!='sep') {
					hl=this.props.highlight;
					if(it[2].hl!=undefined) hl=it[2].hl;
					if(hl) {
						this.design._onitemhl.apply(this.parts.elems[prev],[null,0]);
					}
				}
			}
		} else {
			this.hide();
		}
	},
	onkeydown: function(e,un,t) {
		var k=e.keyCode,n,cur,it,hl,prev,i,delta;
		if(k==27 && t==this._focus) { //escape, hide topmost menu
			if(!this.props.statical) {
				this.hide();
				if(this.pos.parent && this.pos.parent.props.statical) //closing child of static menu must disable focus of static menu
					this.pos.parent._focus.deactivate();
			}
			else { //this can happen only when static menu was activated by opening child and then cursor moved to non-dropdown item of that static menu
				this.hidechildren();
				this._focus.deactivate();
			}
			e.preventDefault();
			e.stopPropagation();
			return 1;
		}
		if((k==17 || k==18) && e.ctrlKey && e.altKey) { //ctrl+alt, hide all menus
//			this.hidechildren();
//			if(!this.props.statical) this.hide();
			this._focus.deactivate();
			e.preventDefault();
			e.stopPropagation();
			return 1;
		}

		if((k==13 || (k==40 && this.props.horiz)) && t==this._focus) {//enter
			e.preventDefault();
			e.stopPropagation();
			if(this.hlitem<0) return 1;
			cur=this.hlitem;
			it=this.elems[cur];
			var act=it[1] || it[2].action || this.props.onitem;
			var actl=it[2].onldown || this.props.onldown;
			hl=0; //whether to hide menu
			if(act && act.constructor==_uMENU) { //submenu, open it
				this.reset_childopen();
				this.hidechildren(cur);
				if(!act.state.visible) this.childopen(cur,0);
			}  //continue processing in case action of ldown specified
			else if(it[2].action) act=it[2].action;
			else if(!act || act.constructor!=Function) act=null;
			if(k==40) return 1; //DOWN was pressed in horiz menu when no popup is opened
			if(act && act.constructor==Function) { //run main action
				act.apply(it[2].thispar || this.props.thispar,[cur,this,it[2].id,it[2],e]);
				hl=1;
			} 
			if(actl && actl.constructor==Function) { //run left down action
				actl.apply(it[2].thispar || this.props.thispar,[cur,this,it[2].id,it[2],e]);
				hl=1;
			}
			if(hl) 
				if(!this.props.statical) this.hide(1);else this.hidechildren();
			return 1;
		}
		if(k==93 && t==this._focus) {//APP-key
			e.preventDefault();
			e.stopPropagation();
			if(this.hlitem<0) return 1;
			cur=this.hlitem;
			it=this.elems[cur];
			var actr=it[2].onrdown || this.props.onrdown;
			if(actr && actr.constructor==Function) { //run right down action
				actr.apply(it[2].thispar || this.props.thispar,[cur,this,it[2].id,it[2],e]);
			}
			return 1;
		}
		if((k==37 || k==39) && t==this._focus && !this.props.horiz) { //LEFT and RIGHT on vert menus
			e.preventDefault();
			e.stopPropagation();
			if(k==39 && this.hlitem>=0) { //right, try to open submenu if any
				cur=this.hlitem;
				it=this.elems[cur];
				if(it && it[1] && it[1].constructor==_uMENU) {
					this.childopen(cur,0);
					return 1;
				}
			}
			else if(k==37 && this.pos.parent && !this.pos.parent.props.horiz) {//left, close submenu if our parent not horiz
				this.hide();
				return 1;
			}
			//otherwise just give transfer event to horizontal parent
			it=this.pos.parent;
			while(it && !it.props.horiz && it!=this) it=it.pos.parent;
			if(it && it.props.horiz) it.onkeydown(e,un,it._focus);
		}
		if((k==40 || k==38 || ((k==37 || k==39) && this.props.horiz)) && t==this._focus) { //DOWN and UP in vert and LEFT RIGHT in horiz
			e.preventDefault();
			e.stopPropagation();
			if(this.props.horiz && (k==40 || k==38)) return 1;
			if(k==40 || k==39) delta=1; //down or right
				else delta=-1;
			prev=cur=this.hlitem;
			n=this.elems.length;
			for(i=0;i<n;i++) {
				cur+=delta;
				if(cur>=n || cur<0) cur=delta>0 ? 0 : n-1;
				it=this.elems[cur];
				if(it && it[0]!='sep') {
					hl=this.props.highlight;
					if(it[2].hl!=undefined) hl=it[2].hl;
					if(hl) break;
				}
			} 
			if(i>=n || prev==cur) return 1; //no movement
			this.hlitem=cur;

			if(prev>=0) { //unhighlight previous item
				it=this.elems[prev];
				if(it && it[0]!='sep') {
					hl=this.props.highlight;
					if(it[2].hl!=undefined) hl=it[2].hl;
					if(hl) {
						this.design._onitemhl.apply(this.parts.elems[prev],[e,0]);
						this.reset_childopen();
					}
				}
			}

			//highlight new item
			this.design._onitemhl.apply(this.parts.elems[cur],[e,1]);
			if(this.hidetimer) clearTimeout(this.hidetimer); this.hidetimer=null;
			if(this.props.horiz) this.childopen(cur,0);
			return 1;
		}
	},
	shadow_init:function() {
    	this.sh=this.design.shadow_init(this.top);
	},
	_resizeSh:function() {
	    if(this.sh)this.design.shadow_resize(this.sh,this.width,this.height);
	},
	moveTo:function(x,y) {
		with(this) {
			jQuery(top).css("left",x+'px').css("top",y+'px');
		    xpos=x;
    		ypos=y;
  		}
	},
	resizeTo:function(w,h) {
		with(this) {
			if(props.width==0) jQuery(menu).css("width",w+'px');
				else if(props.width!='auto') jQuery(menu).css("width",props.width);
				else jQuery(menu).css("width",'auto');
			if(props.horiz) {
				jQuery(menu).css("height",h+'px');
	    		jQuery(parts.content).css("width",(w-decor.w)+'px').css("height",(h-decor.h)+'px');
			} else {
//			    jQuery(menu).css("width",w+'px');//.css("height",h+'px');
    			jQuery(parts.content).css("width",(w-decor.w)+'px');//.css("height",(h-decor.h)+'px');
				if(jQuery.browser.opera) jQuery(top).css("width",w+'px'); //correct bug with thin menus
			}
//   		jQuery(parts.upper).css("width",w+'px');
//    		jQuery(parts.upper.firstChild.firstChild).css("width",w+'px');
		    if(frame) jQuery(frame).css("width",w+'px').css("height",h+'px');
		    width=w;
		    height=h;
		    _resizeSh();
		}
	},
	init1: function(nocalcsize) {
		with(this) {
    		decor.w=50-parts.content.offsetWidth;
    		decor.h=menu.offsetHeight-(props.horiz ? 10 : 50);
			state.init=true;
//alert('in init of '+this.idx);
			_initsubmenus();
			if(add_elems)appendItems(null,0,nocalcsize); 
				else {
				if(pend_show)show(pend_show);
				if(props.onreadycreate) props.onreadycreate.call(props.thispar,this);
				props.onreadycreate=null;
//				if(oninit && props.wnd)oninit.apply(props.wnd);
				}
  		}
	},
	show: function(pos,inititem) {
		if(this.state.destroyed) return;
		if(!this.state.init){this.pend_show=pos || {};return;}
		this.pend_show=null;
		this.pos=jQuery.extend(this.pos,pos || {});
		if(this.pos.parent && !this.pos.parent.state.visible) return; //children of hidden parent cannot be shown
		this._focus.deactivate();
		this._focus._parent=this.pos.parentfocus ? this.pos.parentfocus : (this.pos.parent ? this.pos.parent._focus : (this.props.wnd ? this.props.wnd._focus : null));
	    if(!this.pos.parent) {
	    	this.zpos=_uMENU.defz;
	    }
    	else {
    		this.zpos=this.pos.parent.zpos+5;
    	}
		jQuery(this.top).css("z-index",this.zpos);
		if(this.pos.pos || this.pos.alignObj) {
			var a,pos,ow,oh;
//			this.moveTo(this.pos.pos.x,this.pos.pos.y);
			if(this.pos.alignObj) {
				a=this.pos.alignObj;
				pos=jQuery(a).offset();
				ow=a.offsetWidth;
				oh=a.offsetHeight;
			} else {
				pos={left:this.pos.pos.x,top:this.pos.pos.y};
				ow=oh=0;
			}
		        var ppos,x,y,
		        	d=_uWnd.getdims(),ww=d.clientW,wh=d.clientH,wl=d.clientLeft,wt=d.clientTop,
		        	al=this.pos.align || 'd';
				if(this.props.wnd && this.props.wnd.desktop) {
					d=this.props.wnd.desktop;
					ww=d.width;
					wh=d.height;
					wl=0;
					wt=0;
				}
				if(this.props.rtl || window._rtl) x=pos.left+ow+this.pos.dx-this.width;
					else
					x=pos.left+this.pos.dx;
				y=pos.top+this.pos.dy;

				if(al=='r' || al=='R') x=pos.left+ow+this.pos.dx;
					else if(al=='l' || al=='L') x=pos.left-this.width+this.pos.dx;
					else if(al=='d' || al=='D') y=pos.top+oh+this.pos.dy;
					else if(al=='u' || al=='U') y=pos.top-this.height+this.pos.dy;
				if(x-wl+this.width>ww-3-this.design.sh_sz[0] || x-wl<1)
					if(al=='R' || al=='U') x=pos.left-this.width-this.pos.dx;
						else if(al=='L') x=pos.left+ow-this.pos.dx;
				if(x-wl+this.width>ww-3) x=ww-3-this.design.sh_sz[0]-this.width+wl;
				if(x-wl<1) x=1+wl;

				if(y-wt+this.height>wh-3-this.design.sh_sz[1] || y-wt<1)
					if(al=='D') y=pos.top-this.height-this.pos.dy;
						else if(al=='U') y=pos.top+oh-this.pos.dy;
				if(y-wt+this.height>wh-3) y=wh-3-this.design.sh_sz[1]-this.height+wt;
				if(y-wt<1) y=1+wt;

		        if(this.props.parentnode){
		        	ppos=jQuery(this.props.parentnode).offset();
		        	x-=ppos.left;
		        	y-=ppos.top;
		        }
        		this.moveTo(x,y);
			}
    	if(this.props.onshow) this.props.onshow.apply(this.props.thispar,[this,this.idx]);
		//check marks
		if(this.props.withmarks) {
			for(var i=0;i<this.elems.length;i++) {
				var c=this.elems[i];
				if(c && c[2] && this.parts.marks[i]) {
					if(!c[2].mark) this.parts.marks[i].style.display='none';
						else {
							var m=c[2].marktext || this.props.withmarks;
							if(typeof m=='object' && m.constructr!=String) m=m[c[2].mark];
							this.parts.marks[i].innerHTML=String(m);
							this.parts.marks[i].style.display='block';
						}
				}
			}
		}

		if(!this.props.statical && this.hlitem>=0) { //unhighlight previous item
			var	it=this.elems[this.hlitem];
			if(it && it[0]!='sep') {
				var hl=this.props.highlight;
				if(it[2].hl!=undefined) hl=it[2].hl;
				if(hl) {
					this.design._onitemhl.apply(this.parts.elems[this.hlitem],[null,0]);
				}
			}
			this.hlitem=-1;
		}
		jQuery(this.top).show();
		this.state.visible=true;
//		if(this.pos.parent) 
		if(!this.props.statical) {
			this._focus.activate();//this.pos.parent.have_active=this;
		}
		if(typeof inititem!=undefined && Number(inititem)!=NaN) {
			var a=Number(inititem);
			if(a>=0 && a<this.elems.length) {
				var	it=this.elems[a];
				if(it && it[0]!='sep') {
					var hl=this.props.highlight;
					if(it[2].hl!=undefined) hl=it[2].hl;
					if(hl) {
						this.design._onitemhl.apply(this.parts.elems[a],[null,1]);
						this.hlitem=a;
					}
				}
			}
		}
		if(this.props.hidetimer>0) this.hidetimer=setTimeout("var m=_uMENU.all["+this.idx+"];if(m)m.hide();",this.props.hidetimer);
	},
	hidechildren: function(except) {
		if(this.state.destroyed) return;
		var e=this.elems;
		for(var i=0;i<e.length;i++)
			if(e[i] && e[i].constructor==Array && e[i][1] && e[i][1].constructor==_uMENU) if(e[i][1]!=except) e[i][1].hide();
	},
	hide: function(withpar) {
		if(this.state.destroyed) return;
		if(!this.state.visible) return;
		jQuery(this.top).hide();
		this.state.visible=false;
		if(this.hidetimer) clearTimeout(this.hidetimer); this.hidetimer=null;

		this.pend_show=null;
		this.hidechildren();
//		if(this.pos.parent && this.pos.parent.have_active==this) this.pos.parent.have_active=null;
    	if(this.props.onhide) this.props.onhide.apply(this.props.thispar,[this,this.idx]);
		this._focus.deactivate();
		
		if(withpar) { //hide all non-statical parents
			var p=this.pos.parent;
			while(p && !p.props.statical && p!=this) {p.hide(false);p=p.pos.parent;}
			if(p && p!=this && p.props.statical) p._focus.deactivate();
		} 
	},
	childopen: function(i,inititem) {
		if(this.state.destroyed) return;
		if(this.childtimer) clearTimeout(this.childtimer);
		this.childtimer=null;
		var h=this.parts.elems[i];
		var c=this.elems[i];
		if(!c) return;
		c=c[1];
		if(!c || c.constructor!=_uMENU) {
//			if(this.props.horiz || 1) {this.hidechildren(c);this.have_active=this;}
			this.hidechildren(c);
			return;
		}
		this.hidechildren(c);
		c.show({dx:(this.props.horiz ? 0 : -3 * (this.props.rtl || window._rtl ? -1 : 1)),alignObj:h,align:(this.props.horiz ? 'D' : (this.props.rtl || window._rtl ? 'L' : 'R')),parent:this},inititem);
	},
	schedule_childopen: function(i) {
		if(this.state.destroyed) return;
		if(this.childtimer) clearTimeout(this.childtimer);
		this.childtimer=setTimeout("var m=_uMENU.all["+this.idx+"];if(m)m.childopen("+i+");",100);
	},
	reset_childopen: function() {
		if(this.childtimer) clearTimeout(this.childtimer);
	},
	_initsubmenus: function() {
	  with(this) {
		for(var i=0;i<toinit.length;i++) {toinit[i].init1(true);tocalcsize[tocalcsize.length]=toinit[i];}
		toinit.length=0;
	  }
	},
	_calcsizesubmenus: function() {
	  with(this) {
		for(var i=0;i<tocalcsize.length;i++) tocalcsize[i]._setsize();
		tocalcsize.length=0;
	  }
	},
	createsubmenus: function() {
		var i,was=false;
		for(i=0;i<this.add_elems.length;i++) {
			var el=this.add_elems[i];
			var n=el.length;
			for(var j=0;j<n;j++) {
				if(el[j] && el[j].constructor==Array && el[j][1] && el[j][1].constructor==Array) { //create submenu
					var o={},pos,opts,s;
					if(el[j][2] && (typeof el[j][2])=='object') o=el[j][2];
						else if((typeof el[j][2])=='number') o={hl:el[j][2]};
						else if((typeof el[j][2])=='string') o={id:el[j][2]};
					el[j][2]=o;
					pos=jQuery.extend({
						parent:this,
						dx:this.props.horiz ? this.pos.childdx : 0,
						dy:this.props.horiz ? this.pos.childdy : 0
						},o.pos || {});
					opts=jQuery.extend({
						shadow:this.props.shadow,
						rtl:this.props.rtl ? 1 : 0,
						parentnode:this.props.wnd ? this.props.wnd.top : this.props.parentnode,
						wnd:this.props.wnd,
						onitem: this.props.onitem,
						thispar: this.props.thispar
						},o.opts || {});
					s=new _uMENU('',pos,opts,el[j][1],true);
					el[j][1]=s;
					this.toinit[this.toinit.length]=s;
					was=true;
				}
			}
		}
		return was;
	},
	indexById: function(id) { //find index by string id
		id=String(id);
		for(var i=0;i<this.elems.length;i++)
			if(this.elems[i][2].id==id) return i;
		return -1;
	},
	removeItems: function(idx,nodestroy,norecalc) {
		var i,p,e;
		if(idx==undefined || idx==null) { //remove all
			idx=[];
			for(i=0;i<this.elems.length;i++)idx[i]=this.elems.length-1-i;
		} else if(idx.constructor!=Array) idx=[idx];
		for(i=0;i<idx.length;i++)
			if(idx[i]==undefined || idx[i]==null || idx[i].constructor!=Number) idx[i]=this.indexById(idx[i]);
		idx.sort(function(a,b){return b-a;});
		p=-1;
		for(i=0;i<idx.length;i++) {
			if(idx[i]==p || idx[i]<0 || idx[i]>=this.elems.length) continue;
			e=this.elems[idx[i]];
			if(!nodestroy && e && e.constructor==Array && e[1] && e[1].constructor==_uMENU) e[1].destroy();
			this.elems.splice(idx[i],1);
			this.parts.elems[idx[i]].parentNode.removeChild(this.parts.elems[idx[i]]);
			this.parts.elems.splice(idx[i],1);
			p=idx[i];
		}
		if(!norecalc) this.appendItems(null,0);
	},	
	appendItems: function(el,idx,nocalcsize) {
		if(this.state.destroyed) return;
		if(el && el.constructor==Array && el.length>0)this.add_elems[this.add_elems.length]=el;
		if(!this.state.init)return;
//alert('in appendItems of '+this.idx);
		if(this.createsubmenus()) { //
			setTimeout("var m=_uMENU.all["+this.idx+"];if(m){m._initsubmenus();m.appendItems(null,0,"+nocalcsize+");}",10);
			return;
		}

		if(this.appendtimer)clearTimeout(this.appendtimer);
		this.appendtimer=null;
   		jQuery(this.top).css("visibility",'hidden').css("display",'block');
		if(this.props.horiz) {
			if(!this.props.noabs) jQuery(this.menu).css("width","2100px").css("height","auto");
				else jQuery(this.menu).css("width",this.props.width==0 ? "auto" : this.props.width=='auto' ? 'auto' : this.props.width+"px");
   			jQuery(this.parts.content).css("width","2000px").css("height","auto");
   		} else {
	    	if(jQuery.browser.opera) jQuery(this.top).css('width','2000px');
	   		jQuery(this.menu).css("width","auto").css("height","auto");
   			jQuery(this.parts.content).css("width",jQuery.browser.msie ? "50px" : "auto").css("height","auto");
   		}
		var i;
		for(i=0;i<this.add_elems.length;i++) {
			el=this.add_elems[i];
			var n=el.length;
			for(var j=0;j<n;j++) {
				if(!el[j] || el[j].constructor!=Array) el[j]=[''+el[j]];
				var o={};
				if(el[j][2] && (typeof el[j][2])=='object') o=el[j][2];
					else if((typeof el[j][2])=='number') o={hl:el[j][2]};
					else if((typeof el[j][2])=='string') o={id:el[j][2]};
				el[j][2]=o;
				this.design.append_item(this,el[j],this.props.highlight);
			}
		}
		this.add_elems.splice(0,i);
//		if(onfinish && this.props.wnd)this.onsetsize=onfinish;
		if(!nocalcsize)this.appendtimer=setTimeout("var m=_uMENU.all["+this.idx+"];if(m){m.appendtimer=null;m._setsize();}",10);
	},
	_setsize: function() {
		var d=this.design.calc_size(this);
		this.resizeTo(d.w+this.decor.w,d.h+this.decor.h);

//alert('in setsize of '+this.idx);
		this._calcsizesubmenus();

   		jQuery(this.top).css("display",'none').css("visibility",'visible');
		if(this.pend_show || this.state.visible)this.show(this.pend_show);
//	alert('setsize');
		if(this.props.onreadycreate) {this.props.onreadycreate.call(this.props.thispar,this);this.props.onreadycreate=null;}
		if(this.onsetsize) {
			var a=this.onsetsize;
			this.onsetsize=null;
			if(this.props.wnd)a.apply(this.props.wnd);
		}
	},
	destroy: function() {
		if(this.state.destroyed) return;
		this.removeItems();
	    this.top.parentNode.removeChild(this.top);
	    _uMENU.all[this.idx]=null;
	    this._focus.destroy();
	    this.state.destroyed=true;
    	if(this.props.ondestroy) this.props.ondestroy.apply(this.props.thispar,[this,this.idx]);
	}
};
//inputId - input ID or object
//vals - [[val1,icon1,issel1,text11,text12,...],[val2,icon2,issel2,text21,text22,...]] 
//cols - number of text columns (0 if valX is used as displayed text), place for icons is reserved always
function _uComboBox(name,inputId,opts,cols,vals) {
	this.constructor=_uComboBox;
	this.name=name;
	this.obj=inputId && inputId.tagName ? inputId : document.getElementById(inputId);
	if(!this.obj) return null;
	this.props=jQuery.extend({
//		parentnode: null, //parent node for drop-down list
		design: 'std',
		readonly: false,
		noicons: -1,
		deficon: null,
		listwidth: 'auto',
//		height: 'auto',
		maxlistheight: 'auto',
		colwidth: null,
//		hidden: 0,
//		onshow: null,
//		onhide: null,
//		ondestroy: null,
		onchange: null,
		wnd:null, //parent window determines parentnode for list and this (as wnd.app) for handlers
		rtl:0
	},opts || {});
	this.app=this.props.wnd && this.props.wnd.app ? this.props.wnd.app : null;
	this.textvals=cols > 0 ? cols : 0;
	this.tablecols=cols<=0 ? 1 : cols;
	this.colwidth=this.props.colwidth || [];
	this.destroyed=false;
    this.design=this.props.design && _uComboBox.designs[this.props.design] ? _uComboBox.designs[this.props.design] : _uComboBox.designs['std'];
    this.idx=_uComboBox.nextidx++;
    _uComboBox.all[this.idx]=this;
//    if(this.props.width=='auto') {
//    	this.width=this.obj.offsetWidth || parseInt(this.obj.style.width);
//    	if(!this.width>0) this.width=100;
//    } else this.width=this.props.width;
//    if(this.props.height=='auto') {
//    	this.height=this.obj.offsetHeight || parseInt(this.obj.style.height);
//    	if(!this.height>0) this.height=100;
//    } else this.height=this.props.height;
//	this.zpos=this.props.wnd ? 5 : _uComboBox.defz;
	this.data=[];
	if(!vals) vals=[];
	var defIdx=-1;
	for(var i=0;i<vals.length;i++) {
		if(defIdx<0 && vals[i][2]) defIdx=i;
	}
	this.userInput=this.prevInput=this.obj.value; 
	if(this.props.readonly) {
		this.obj.readOnly=true;
		this.valobj=jQuery('<input type="hidden" name="'+this.obj.name+'" value="">')[0];
		this.obj.removeAttribute('name');
		if(this.obj.form) this.obj.form.appendChild(this.valobj);
		if(defIdx<0 || defIdx>=vals.length) defIdx=0;
	} else {
		this.obj.readOnly=false;
		this.valobj=this.obj; //object which holds form value
		if(defIdx<0 || defIdx>=vals.length) defIdx=-1;
	}
	this.selected=this.userIdx=defIdx;
	this.changetimer=null;


	this.haveicons=this.props.noicons>0;
	if(this.props.noicons<0) 
		for(var i=0;i<vals.length;i++) 
			if(vals[i][1]) {this.haveicons=true;break;}


	this.frame=null;
	this.opened=false;
	this.updated=true;
	this.showtimer=null;
	this.blurtimer=null;
	this.cancelblur=false;
//	var c=document.createElement("div");
//	c.id="_ucombo"+this.idx;
//	this.obj.parentNode.insertBefore(c,this.obj);
	this.combo=null;

	var t=document.createElement("div");
	t.id="_ucombolist"+this.idx;
//	this.combo.appendChild(t);
//	if(!this.props.parentnode) jQuery(jQuery("body")[0]).prepend(t);
//		else this.props.parentnode.appendChild(t);
	this.toplist=t;

   	jQuery(t).css({position:'absolute',zIndex:5,display:'none'});//zIndex:this.zpos,
    if(jQuery.browser.msie && parseFloat(jQuery.browser.version)<8){
		    var html = '<iframe tabindex="-1" '
    		    +'style="display:block; position:absolute; '
			+'filter:Alpha(Opacity=\'0\'); '
			+'width:1px;height:1px;border:0"/>';

	    	this.frame=document.createElement(html);
			t.appendChild(this.frame);
	}

    this.parts=this.design.combo_init(this);
    jQuery(this.obj).attr("autocomplete", "off")
	.bind("focus",this,_uComboBox._onobjfocus)
	.bind("blur",this,_uComboBox._onobjblur)
	.bind("beforedeactivate",this,_uComboBox._onobjdeact)
	.bind("keydown",this,function(e) {return e.data.onkeydown(e);})
	.bind("keyup",this,function(e) {return e.data.onkeyup(e);});

	jQuery(this.combo).bind("mousedown",this,_uComboBox._oncombomousedown);
	jQuery(this.toplist).bind("mousedown",this,_uComboBox._onlistmousedown);
    for(var i=0;i<vals.length;i++) this.appendItem(vals[i]);
	if(!_uComboBox.globalset) {
		_uComboBox.globalset=true;
	    jQuery(document).bind("mousedown",_uComboBox.hideall);
	    jQuery(window).bind("resize",_uComboBox.hideall);
	}
	if(defIdx>=0) this.select(defIdx,false,true);
//	setTimeout("var m=_uComboBox.all["+this.idx+"];if(m)m.showlist();",10);
//	this.showlist();
//	if(!this.props.hidden) this.show();
//	if(!noinit)setTimeout("var m=_uMENU.all["+this.idx+"];if(m)m.init1();",10);
}


_uComboBox.all=[];
_uComboBox.nextidx=0;
_uComboBox.defz=20015;
_uComboBox.globalset=false;
_uComboBox.ignoreclick=null;

_uComboBox.getbyname=function(name) {
    var a=this.all;
    for(var i=0;i<a.length;i++) if(a[i] && a[i].name==name) return a[i];
    return null;
};
_uComboBox.hideall=function() {
    var a=_uComboBox.all;
    for(var i=0;i<a.length;i++) if(a[i] && !a[i].destroyed && _uComboBox.ignoreclick!=a[i]) a[i].hidelist();
	if(_uComboBox.ignoreclick)setTimeout("_uComboBox.ignoreclick=null;",10);
};
_uComboBox.designs={
    std: {
	combo_init: function(o) {
	    var p={};
		o.combo=jQuery('<table id="_ucombo'+o.idx+'" style="position:relative" cellspacing="0" cellpadding="0" border="0" class="x-unselectable u-combo" align="left">'
				+'<tr><td class="u-comboeditcell"></td><td class="u-combobutcell"><div class="u-combobut"></td></tr>'
				+'</table>')[0];
		o.obj.parentNode.insertBefore(o.combo,o.obj);
		if(!o.props.wnd) o.obj.parentNode.insertBefore(o.toplist,o.obj);
			else o.props.wnd.top.appendChild(o.toplist);
//		jQuery(o.combo).attr("class","x-unselectable u-combo").css({position:'relative'}).prepend('<img border="0" class="u-combobut" src="http://src.ucoz.net/img/1px.gif">');
		p.editcell=jQuery(o.combo).find(".u-comboeditcell")[0];
		p.button=jQuery(o.combo).find(".u-combobut")[0];
		p.butcell=jQuery(o.combo).find(".u-combobutcell")[0];
		p.editcell.appendChild(o.obj);
		jQuery(o.obj).attr("class","x-selectable u-comboedit"+(o.haveicons ? " u-comboeditimg" : "")+" "+jQuery(o.obj).attr("class"));
//		if(jQuery.browser.msie) jQuery(o.obj).css({marginTop:'-1px',marginBottom:'-1px'});

	    jQuery(o.toplist).append('<div class="u-combolist"><div style="zoom:1"><table border="0" cellspacing="0" class="x-unselectable u-combocont" width="100%"></table></div></div>');
		p.list=jQuery(o.toplist).find(".u-combolist")[0];
		p.content=jQuery(o.toplist).find(".u-combocont")[0];

		if(!o.props.readonly) jQuery(p.button).bind("mousedown",o,_uComboBox._onbutclick);
			else {
				jQuery(o.obj).bind("mousedown",function(e){e.preventDefault();return 0;});
			}
		jQuery(p.button).bind("mouseover",o.design._onbutmouseover).bind("mouseout",o.design._onbutmouseout);

		p.items=[];
		jQuery(o.toplist).find("div,span,table").andSelf().attr("unselectable","on");
	    return p;
	},
	append_item: function(o,el) {
		var row,cell,t=o.parts.content,i=t.rows.length,txt;
		row=o.parts.items[i]=t.insertRow(i);
		jQuery(row).attr("class","u-comborow").bind("mouseover",o,_uComboBox._onitemmouseover).bind("mouseout",o,_uComboBox._onitemmouseout)
			.bind("click",o,_uComboBox._onitemclick);
		for(var j=0;j<o.tablecols;j++) {
			cell=row.insertCell(j);
			txt= j+3<el.length ? el[j+3] : (j==0 ? el[0] : '&nbsp;');
			if(j==0 && el[1]) txt='<img class="u-comborowicon" border="0" src="'+el[1]+'">'+txt;
			jQuery(cell).attr("class","u-combocell"+j).html(txt);
			if(o.colwidth[j]) jQuery(cell).attr("width",o.colwidth[j]);
		}
		jQuery(row).find("*").andSelf().attr("unselectable","on");
	},
	remove_item: function(o,i) {
		var t=o.parts.content;
		if(i<0 || i>=t.rows.length) return;
		jQuery(t.rows[i]).unbind();
		o.parts.items.splice(i,1);
		t.deleteRow(i);
	},
	calc_size: function(o) {
		var w=0,h=0;
		if(!o.props.horiz) {
			for(var i=0;i<o.parts.elems.length;i++) {
				w=Math.max(w,Math.max(o.parts.elems[i].offsetWidth,o.parts.elems[i].scrollWidth));
				h+=o.parts.elems[i].offsetHeight;
			}
		} else {  //horiz
			for(var i=0;i<o.parts.elems.length;i++) {
				w+=Math.max(o.parts.elems[i].offsetWidth,o.parts.elems[i].scrollWidth);
				h=Math.max(h,o.parts.elems[i].offsetHeight);
			}
		}
		return {w:w,h:h};
	},
	_onitemmouseover: function(e) {
		jQuery(this).addClass("u-comborowhl");
	},
	_onitemmouseout: function(e) {
		jQuery(this).removeClass("u-comborowhl");
	},
	_onbutmouseover: function() {
		jQuery(this).addClass("u-combobuthl");
	},
	_onbutmouseout: function() {
		jQuery(this).removeClass("u-combobuthl");
	},
	_onlistopen: function(o) {
		jQuery(o.combo).addClass("u-comboopen");
	},
	_onlisthide: function(o) {
		jQuery(o.combo).removeClass("u-comboopen");
	},
	_onfocus: function(o,e) {
		jQuery(o.combo).addClass("u-combofocus");
	},
	_onblur: function(o,e) {
		jQuery(o.combo).removeClass("u-combofocus");
	},
	_select: function(o,i) {
		jQuery(o.parts.items[i]).addClass("u-comborowsel");
	},
	_deselect: function(o,i) {
		jQuery(o.parts.items[i]).removeClass("u-comborowsel");
	},
	seticon: function(o,icon) {
		if(icon) jQuery(o.obj).css("background-image","url("+icon+")");
			else jQuery(o.obj).css("background-image",o.props.deficon ? "url("+o.props.deficon+")" : "none")
	}
    }	
};

_uComboBox._onitemmouseover=function(e) {
		e.data.design._onitemmouseover.apply(this,[e]);
};
_uComboBox._onitemmouseout=function(e) {
		e.data.design._onitemmouseout.apply(this,[e]);
};
_uComboBox._onitemclick=function(e) {
		var o=e.data;
		for(var i=0;i<o.parts.items.length;i++)
			if(o.parts.items[i]==this) {
				o.select(i);
				o.hidelist();
				o.obj.focus();
			}
		e.stopPropagation();
		e.preventDefault();
};
_uComboBox._onbutclick=function(e) {
		var o=e.data;
		_uComboBox.ignoreclick=e.data;
		_uWnd.globalmousedown();
		if(o.opened) o.hidelist(); else o.showlist();
		o.obj.focus();
		if(jQuery.browser.msie && !e.data.cancelblur) {
			e.data.cancelblur=true;
			setTimeout("var c=_uComboBox.all["+e.data.idx+"];if(c)c.cancelblur=false;",10);
		}
		e.stopPropagation();
		e.preventDefault();
};
_uComboBox._onlistmousedown=function(e) {
	_uComboBox.ignoreclick=e.data;
	_uWnd.globalmousedown();
	e.data.obj.focus();
	if(jQuery.browser.msie && !e.data.cancelblur) {
		e.data.cancelblur=true;
		setTimeout("var c=_uComboBox.all["+e.data.idx+"];if(c)c.cancelblur=false;",10);
	}
	e.stopPropagation();
	e.preventDefault();
};
_uComboBox._oncombomousedown=function(e) {
	_uComboBox.ignoreclick=e.data;
	_uWnd.globalmousedown();
	if(e.which!=1) return;
	var o=e.data;
	if(o.props.readonly) if(o.opened) o.hidelist(); else o.showlist();
	o.obj.focus();
	if(jQuery.browser.msie && !o.cancelblur) {
		o.cancelblur=true;
		setTimeout("var c=_uComboBox.all["+o.idx+"];if(c)c.cancelblur=false;",10);
	}
	e.stopPropagation();
	if(e.target!=o.obj) e.preventDefault();
};
_uComboBox._onobjfocus=function(e) {
	var o=e.data;
	if(o.blurtimer) clearTimeout(o.blurtimer);
	o.blurtimer=null;
	o.design._onfocus(o,e);
};
_uComboBox._onobjblur=function(e) {
	e.data.onblur();
};
_uComboBox._onobjdeact=function(e) {
	var o=e.data;
	if(o.cancelblur) e.preventDefault();
};

_uComboBox.isUpKey=function (code) {
    return code==38 || code == 63232;
};
_uComboBox.isDownKey=function (code) {
    return code==40 || code == 63233;
};

_uComboBox.prototype={
	_setvalue: function(i) {
	  with(this) {
		if(i>=0 && i<data.length) {
			valobj.value=prevInput=data[i][0];
			if(props.readonly) obj.value=data[i].length>2 ? data[i][2] : data[i][0];
			if(haveicons) design.seticon(this,data[i][1]);
		} else {
			if(props.readonly) {valobj.value='';obj.value=userInput;}
				else {valobj.value=prevInput=userInput;}
			if(haveicons) this.design.seticon(this,null);
		}
	  }
	},
	_selectitem: function(i) {
	  with(this) {
		if(selected>=0 && selected<data.length) {
			design._deselect(this,selected);
			selected=-1;
		}
		if(i>=0 && i<data.length) {
			design._select(this,i);
			selected=i;
		}
	  }
	},
	setvalue: function (v) {
	  with(this) {
	  	var ch=false;
		if(props.readonly) return;
		if(changetimer) clearTimeout(changetimer);
		if(valobj.value!=v) ch=true;
		valobj.value=prevInput=userInput=v;
		if(selected!=-1) ch=true;
		_selectitem(-1);
		userIdx=-1;
		if(haveicons) this.design.seticon(this,null);
		if(props.onchange && ch) props.onchange.apply(this.app || this,[selected,v]);
	  }
	},
	select: function (i,soft) { //
	  with(this) {
		var ch=false;
		if(changetimer) clearTimeout(changetimer);
		changetimer=null;
	  	_selectitem(i);
		_setvalue(selected);
		if(!soft) {
			if(selected!=userIdx) ch=true;
			userIdx=selected;
			if(props.onchange && ch) props.onchange.apply(this.app || this,[selected,valobj.value]);
		}
	  }
	},
	onblur: function (e) {
		if(this.blurtimer) clearTimeout(this.blurtimer);
		this.blurtimer=setTimeout("var c=_uComboBox.all["+this.idx+"];if(c)c._onblur2();",10);
	}, 
	_onblur2: function (e) {
		this.blurtimer=null;
		this.design._onblur(this,e);
	}, 
	onkeydown: function (e) {
	  var i;
	  with(this) {
    	var c=e.keyCode;
    	if(c==27 && opened) {
			select(userIdx,true);
			hidelist();
			return false;
    	}
    	if(data.length==0) return;
	    if(c==13) {
	    	select(selected);
			hidelist();
			obj.focus();
			e.preventDefault();
			e.stopPropagation();
			return false;
	    }
		if(c==9) {
			if(changetimer) select(selected);
			return;
		}
	    if(_uComboBox.isDownKey(c) || _uComboBox.isUpKey(c)) {
			i=selected+(_uComboBox.isDownKey(c) ? 1 : -1);
			if(props.readonly) {
	    		if(i>=data.length) i=0;
					else if(i<0) i=data.length-1;
			} else {
    			if(i>=data.length) i=-1;
					else if(i<-1) i=data.length-1;
			}
			select(i,true); //select clears changetimer
			if(!opened) changetimer=setTimeout("var c=_uComboBox.all["+this.idx+"];if(c)c.select("+i+");",500);
			return false;
    	}
  	  }
	},
	onkeyup: function (e) {
  	  with(this) {
    	if(props.readonly || obj.value==prevInput) return;
		if(changetimer) clearTimeout(changetimer);
    	prevInput=userInput=obj.value;
		_selectitem(-1);
		userIdx=-1;
		if(haveicons) this.design.seticon(this,null);
		if(props.onchange) props.onchange.apply(this.app || this,[-1,obj.value]);
  	  }
	},
	appendItem: function(elem) { //0 - id, 1 - optional icon, 2... - text labels
		this.data[this.data.length]=[elem[0],elem[1],elem[3]];
		this.design.append_item(this,elem);
		this.updated=true;
	},
	removeItem: function(idx) {
		if(idx<0 || idx>=this.data.length) return;
		this.design.remove_item(this,idx);
		this.data.splice(idx,1);
		this.updated=true;
	},
	removeAll: function() {
		for(var i=0;i<this.data.length;i++) this.design.remove_item(this,0);
		this.data.splice(0,this.data.length);
		this.updated=true;
	},
	hidelist: function() {
	    this.toplist.style.display='none';
	    this.opened=false;
		this.design._onlisthide(this);
	},
	showlist: function () {
    	var w,x,y;
		if(!this.props.wnd) {
			x=this.combo.offsetLeft;
			y=this.combo.offsetTop;
		} else {
			var pos=jQuery(this.combo).offset(),ppos=jQuery(this.props.wnd.top).offset();
			x=pos.left-ppos.left;
			y=pos.top-ppos.top;
		}
    	if(this.props.listwidth=='auto') w=this.combo.offsetWidth+'px';
    		else w=this.props.listwidth;
		jQuery(this.toplist).css({left:x+'px',top:(y+this.combo.offsetHeight-1)+'px',width:w,minWidth:this.combo.offsetWidth+'px'});
	    if(this.frame) jQuery(this.frame).css({width:w});
		if(this.updated) {
			jQuery(this.parts.list).css({height:'auto',overflow:'hidden'});
			jQuery(this.toplist).css({visibility:'hidden'}).css({display:'block'});
			if(this.showtimer) clearTimeout(this.showtimer);
			this.showtimer=setTimeout("var c=_uComboBox.all["+this.idx+"];if(c)c._showlist();",10);
		} else {
		    this.toplist.style.display='block';
		}
		this.design._onlistopen(this);
		this.opened=true;
	    this.updated=false;
	},
	_showlist:function(){
		this.showtimer=null;
		var o=this.parts.list,h,maxh,pos,d=_uWnd.getdims();
		h=Math.max(o.scrollHeight,o.offsetHeight);
		pos=jQuery(this.toplist).offset();
		if(this.props.maxlistheight>0) maxh=Math.max(50,Math.min(this.props.maxlistheight,d.clientH-pos.top));
			else maxh=Math.max(50,d.clientH-pos.top);
		if(h>maxh) {
			jQuery(this.parts.list).css({height:maxh+'px',overflow:'auto'});
			h=maxh;
		}
	    if(this.frame) jQuery(this.frame).css({height:h+'px'});

		jQuery(this.toplist).css({visibility:'visible'});
	}
};

//vals in form:
//{'word1':[['word11',col2],['word12',col2]],'word2':[['word21',col2],['word22',col2]]}

//function _uSuggestList(editId,cachegroup,tblClass,normalclass,highclass,minlength,url,vals) {
function _uSuggestList(name,inputId,opts,vals) { //inputId can be ID or object
	this.constructor=_uSuggestList;
	this.name=name;
	this.obj=typeof inputId=='string' ? document.getElementById(inputId) : inputId;
	if(!this.obj) return null;
	this.props=jQuery.extend({
		design: 'std',
		maxlistheight: 'auto',
		colwidth: null,
		cachegroup: 'def',
		minlen: 2,
		url: null,
		separator: null
//		hidden: 0,
//		onshow: null,
//		onhide: null,
//		ondestroy: null,
//		onchange: null,
//		wnd:null,
//		rtl:0
	},opts || {});
	this.colwidth=this.props.colwidth || [];
//	this.destroyed=false;
    this.design=this.props.design && _uSuggestList.designs[this.props.design] ? _uSuggestList.designs[this.props.design] : _uSuggestList.designs['std'];
    this.idx=_uSuggestList.nextidx++;
    _uSuggestList.all[this.idx]=this;
    this.cacheGroup=this.props.cachegroup;
    if(!this.queryCache[this.cacheGroup]) this.queryCache[this.cacheGroup]=[];
    if(vals) this.queryCache[this.cacheGroup]=vals;

    this.visible=true;
    this.hlIndex=-1; //highlighted item
    this.hlRow=null; //highlighted row
    this.blockMouseOver=false;

    this.userInput=this.obj.value; //saved value
    this.previousInput=this.obj.value; //to track changes
    this.resultInput=""; //value for which we have result if result is not empty
    this.requestedInput=""; //to track value which will be requested remotely
    this.ignoreInput=""; //value which must be ignored
	this.sep_pos=-1;

    this.remoteReqTimer=null;
    this.hideTimer=null;
    this.AJAXretries=0;
    this.AJAXTimer=null;


	this.frame=null;


	var t=document.createElement("div");
	t.id="_usuggest"+this.idx;
	this.top=t;
	
	var wnd;
	if(wnd=_uWnd.findparent(this.obj)) {
		this.parent=wnd.top;
		this.parent.appendChild(t);
	} else {
		this.parent=this.obj.parentNode;
		this.parent.insertBefore(t,this.obj);

	}

   	jQuery(t).css({position:'absolute',zIndex:5,display:'none',zoom:1});//zIndex:this.zpos,
    if(0 && jQuery.browser.msie && parseFloat(jQuery.browser.version)<7){
		    var html = '<iframe tabindex="-1" '
    		    +'style="display:block; position:absolute;'
			+'filter:Alpha(Opacity=\'100\'); '
			+'width:1px;height:20px;border:0"/>';

	    	this.frame=document.createElement(html);
			t.appendChild(this.frame);
	}

    this.parts=this.design.suggest_init(this);

    jQuery(this.obj).attr("autocomplete", "off").css("position","relative")
//	.bind("focus",this,_uComboBox._onobjfocus)
	.bind("blur",this,function(e) {return e.data.onblur(e);})
//	.bind("beforedeactivate",this,_uComboBox._onobjdeact)
	.bind("keydown",this,function(e) {return e.data.onkeydown(e);})
	.bind("keyup",this,function(e) {return e.data.onkeyup(e);});

//	jQuery(this.combo).bind("mousedown",this,_uComboBox._oncombomousedown);
//	jQuery(this.toplist).bind("mousedown",this,_uComboBox._onlistmousedown);
	if(!_uSuggestList.globalset) {
		_uSuggestList.globalset=true;
	    jQuery(document).bind("mousedown",_uSuggestList.hideall);
	    jQuery(window).bind("resize",_uSuggestList.hideall);
	}


    this.hide();
//    jQuery(window).bind("resize",this,function(e) {return e.data.positionResult();});

    //attach event to parent form
    if(this.obj.form) {
	jQuery(this.obj.form).bind("submit."+this.idx,this,function(e) {return e.data.onsubmitform(e);});
    }
}

_uSuggestList.all=[];
_uSuggestList.nextidx=0;
_uSuggestList.defz=20015;
_uSuggestList.globalset=false;
_uSuggestList.ignoreclick=null;

_uSuggestList.getbyname=function(name) {
    var a=this.all;
    for(var i=0;i<a.length;i++) if(a[i] && a[i].name==name) return a[i];
    return null;
};
_uSuggestList.hideall=function() {
    var a=_uSuggestList.all;
    for(var i=0;i<a.length;i++) if(a[i] && !a[i].destroyed && _uSuggestList.ignoreclick!=a[i]) a[i].hide();
	if(_uSuggestList.ignoreclick)setTimeout("_uSuggestList.ignoreclick=null;",10);
};

_uSuggestList.designs={
	std: {
	suggest_init: function(o) {
	    var p={};
		jQuery(o.obj).attr("class","x-selectable u-suggedit "+jQuery(o.obj).attr("class"));
//		if(jQuery.browser.msie) jQuery(o.obj).css({marginTop:'-1px',marginBottom:'-1px'});

	    jQuery(o.top).append('<div class="u-sugglist" style="zoom:1"><div style="zoom:1"><table border="0" cellspacing="0" class="x-unselectable u-suggcont" width="100%"></table></div></div>');
		p.list=jQuery(o.top).find(".u-sugglist")[0];
		p.content=jQuery(o.top).find(".u-suggcont")[0];
//		p.items=[];
		jQuery(o.top).find("div,span,table").andSelf().attr("unselectable","on");
	    return p;

		},
	append_row: function(o,item,cols,key) {
		var row=o.parts.content.insertRow(-1);
		jQuery(row).bind("mousedown",o,o._onrowmousedown)
	      .bind("mousemove",o,o._onrowmousemove)
	      .bind("mouseover",o,o._onrowmouseover).addClass('u-suggrow').attr("usuggeststr",item[0]);
		var v=String(item[0]);
	    if(v.toLowerCase().substr(0,key.length)==key.toLowerCase()) v='<span class="u-suggmark">'+v.substr(0,key.length)+'</span>'+v.substr(key.length);
		for(var j=0;j<cols;j++) {
		    jQuery(row).append("<td unselectable='on' class='u-suggcell"+j+"'>"+(j==0?v:item[j])+"</td>");
		}
	},
//	_onlistopen: function(o) {
//		jQuery(o.combo).addClass("u-comboopen");
//	},
//	_onlisthide: function(o) {
//		jQuery(o.combo).removeClass("u-comboopen");
//	}
	_select: function(o,row) {
		jQuery(row).addClass("u-suggrowhl");
	},
	_deselect: function(o,row) {
		jQuery(row).removeClass("u-suggrowhl");
	}

	}

};

_uSuggestList.prototype = {
queryCache: [],
onsubmitform: function (e) {
    if(this.visible) {
	if(this.resultInput!="") this.obj.value=this.userInput;
	this.hide();
    }
},
hide: function () {
  with(this) {
    top.style.display='none';
	visible=false;
	if(hlRow) design._deselect(this,hlRow);
	hlRow=null;
	hlIndex=-1;
  }
},
show: function () {
  with(this) {
    if(!visible && numItems()>0) {
	   	var w=obj.offsetWidth,x,y,off,offp;
	   	off=jQuery(obj).offset();
	   	offp=jQuery(parent).offset();
		jQuery(top).css({left:off.left-offp.left+'px',top:off.top-offp.top+obj.offsetHeight+'px',width:w+'px'});
	    if(frame) jQuery(frame).css({width:w+'px'});
	    top.style.display='block';
		visible=true;
		blockMouseOver=true;
    }
  }
},
numItems: function () {
    return this.parts.content ? this.parts.content.rows.length : 0;
},
onblur: function (e) {
  with(this) {
    if(visible) {
	if(hlIndex>=0) obj.value=userInput;
	hide();
    }
  }
}, 
onkeydown: function (e) {
  with(this) {
    var c=e.keyCode;
    if(c==27 && visible) {
	if(resultInput!="") _setvalue_sep(userInput);
	hide();
	return false;
    }
    if(resultInput=="") return;
    if(c==13 && hlIndex>=0 && visible) {
	previousInput=obj.value;
	userInput=_getvalue_sep();
	resetRequest(userInput);
	hide();
	if(userInput != resultInput) clearResult();
	obj.focus();
	e.preventDefault();
	e.stopPropagation();
	return false;
    }
    if(isDownKey(c)) {
	moveSelection(hlIndex+1);
	return false;
    }
    if(isUpKey(c)) {
	moveSelection(hlIndex-1);
	return false;
    }
  }
},
onkeyup: function (e) {
  with(this) {
    if(ignoreInput!="" && _getvalue_sep()==ignoreInput) return;
    if(obj.value==previousInput) return;
    ignoreInput=resultInput="";
    previousInput=obj.value;
    userInput=_getvalue_sep();
    if(hideTimer) clearTimeout(hideTimer);
    hideTimer=setTimeout("var c=_uSuggestList.all["+idx+"];if(c)c.clearResult(true);",2000);
	
    procRequest(userInput);
  }
},
_setvalue_sep: function(v) {
	with(this){
		if(sep_pos>=0) obj.value=obj.value.substr(0,sep_pos)+props.separator+' '+v;
			else obj.value=v;
	}
},
_getvalue_sep: function() {
  with(this){
  	if(props.separator) sep_pos=obj.value.lastIndexOf(props.separator);
	if(sep_pos>=0) {
		var t=obj.value.substr(sep_pos+props.separator.length);
	    t=t.replace(/^\s+/,"");
	    return t;
	} else return obj.value;
  }
},
moveSelection: function (newi) {
  with(this) {
    if(resultInput=="" && ignoreInput!="" && _getvalue_sep()==ignoreInput) {
	procRequest(_getvalue_sep(),true);
	return;
    }
    if(!visible && resultInput.length>0 && resultInput==_getvalue_sep()) {
	show();
	return;
    }
    if(!visible) return;
    if(hlRow) design._deselect(this,hlRow);
    hlRow=null;
    var cnt=numItems();
    if(newi>=cnt) newi=-1;
	else if(newi<-1) newi=cnt-1;
    if(newi==-1) {
	hlIndex=-1;
	_setvalue_sep(userInput);
	obj.focus();
	return;
    }
    hlIndex=newi;
    hlRow=parts.content.rows[newi];
    design._select(this,hlRow);
    ignoreInput=jQuery(hlRow).attr("usuggeststr");
    _setvalue_sep(ignoreInput);
  }
},	
isUpKey: function (code) {
    return code==38 || code == 63232;
},
isDownKey: function (code) {
    return code==40 || code == 63233;
},
resetRequest: function(txt) {
  with(this) {
    if(remoteReqTimer && requestedInput==txt) return;
    clearTimeout(remoteReqTimer); 	remoteReqTimer=null;
    requestedInput=txt;
    clearTimeout(AJAXTimer); AJAXTimer=null;
    try{if(AJAXObj) AJAXObj.abort(); AJAXObj=null;}catch(e){};
  }
},
procRequest: function (txt,nopause) {
  with(this) {
    resetRequest(txt);

    if(txt.length<props.minlen) {
	clearResult(true);
	return;
    }
    if(queryCache[cacheGroup][txt]) {
	setResult(txt,queryCache[cacheGroup][txt]);
	return;
    }
    AJAXretries=0;
    if(!props.url) return;
    if(nopause) remoteRequest();
	else remoteReqTimer=setTimeout("var c=_uSuggestList.all["+this.idx+"];if(c)c.remoteRequest();",350);
  }
},
remoteRequest: function () {
  with(this) {
    clearTimeout(remoteReqTimer);
    remoteReqTimer=null;
    try{if(AJAXObj) AJAXObj.abort(); AJAXObj=null;}catch(e){};

    if(AJAXretries>1) return;
    clearTimeout(AJAXTimer);
    AJAXTimer=setTimeout("var c=_uSuggestList.all["+this.idx+"];if(c){c.AJAXretries++;c.remoteRequest();}",12000);

    AJAXObj=jQuery.ajax({
	type: "GET",
	dataType: "text",
	cache: false,
	url: props.url,
	data: {tag:requestedInput},
	success: new Function("resp","status","var c=_uSuggestList.all["+this.idx+"];if(c)c.parseRequest(resp,status);"),
	timeout: 10000
    });
  }
},
parseRequest: function (resp,status) {
    clearTimeout(this.AJAXTimer);
    this.AJAXTimer=null;
    this.AJAXretries=0;
    var res=[];
    try {
	res=eval("res="+resp);
    } catch(e) {}
    this.AJAXObj=req=null;
    if(!res || res.length<2 || res[1]<1 || res[1]>10) {
	this.clearResult();
	return;
    }
    var n=res[1],idx=0;
    var data=[];
    for(var i=2;i<res.length;i+=n,idx++) {
	data[idx]=[];
	for(var j=0;j<n;j++) {
	    data[idx][j]=res[i+j];
	}
    }
    this.queryCache[this.cacheGroup][res[0]]=data;
    if(res[0]==this.requestedInput) this.setResult(res[0],this.queryCache[this.cacheGroup][res[0]]);
},
_onrowmousemove: function(e) {
    return e.data.onrowmousemove(e);
},
_onrowmouseover: function(e) {
    return e.data.onrowmouseover(e);
},
_onrowmousedown: function(e) {
    return e.data.onrowmousedown(e);
},
onrowmousemove: function(e) {
    if(this.blockMouseOver) { //mouseOver is disable right after showing results
	this.blockMouseOver=false;
	this.onrowmouseover(e);
    }
},
onrowmouseover: function(e) {
  with(this) {
    if(blockMouseOver) return;
    if(hlRow) design._deselect(this,hlRow);
    hlRow=null;
    hlIndex=-1;
    for(var i=0;i<parts.content.rows.length;i++) {
	if(parts.content.rows[i]==e.target || jQuery.inArray(e.target,jQuery(parts.content.rows[i]).contents())>=0) {
	    hlIndex=i;
	    hlRow=parts.content.rows[i];
	    design._select(this,hlRow);
	    break;
	}
    }
  }
},
onrowmousedown: function(e) {
  with(this) {
    if(numItems()<=0 || !hlIndex<0 || !hlRow) return;
    ignoreInput=userInput=jQuery(hlRow).attr("usuggeststr");
    _setvalue_sep(userInput);
    previousInput=obj.value;
    resetRequest(userInput);
    hide();
    if(userInput != resultInput) clearResult();
    obj.focus();
  }
},

setResult: function (phrase,table) { //array of arrays
  with(this) {
    if(hideTimer) clearTimeout(hideTimer);
    hideTimer=null;
    clearResult();
    hlIndex=-1;
    hlRow=null;
    resultInput=phrase;
    var cnt=table.length;
    if(cnt<=0) {hide();return;}	
    var cols=table[0].length; //determine num cols by first item
    for(var i=0;i<cnt;i++) {
	    design.append_row(this,table[i],cols,phrase);
    }
    show();
  }
},
clearResult: function (hide) {
  with(this) {
    if(hide) hide();
    resultInput="";
    hlRow=null;
    hlIndex=-1;
    while(parts.content.rows.length > 0)parts.content.deleteRow(-1);
  }
}
};
//obj - this for all handlers
function _uDraggable(obj,onmove,oninitdrag,onstartdrag,onstopdrag) {
	this.par=obj;
	this.x=this.y=this.w=this.h=this.m=0;
	this.moved=false;
	this.active=false;
	this.onmove=onmove || _uDraggable.dummy;
	this.oninitdrag=oninitdrag || _uDraggable.dummy;
	this.onstartdrag=onstartdrag || _uDraggable.dummy;
	this.onstopdrag=onstopdrag || _uDraggable.dummy;
        if(!_uDraggable.globalset) {
		_uDraggable.globalset=true;
		jQuery(document).bind("mouseup",_uDraggable.onmouseup);
		jQuery(document).bind("mousemove",_uDraggable.onmousemove);
//		if(jQuery.browser.msie)jQuery(document).bind("mouseout",function(e) {if(!e.relatedTarget && !e.toElement)_uWnd._dragging=null});
	}
}
_uDraggable.dummy=function(){};
_uDraggable.obj=null;
_uDraggable.clkX=0;
_uDraggable.clkY=0;
_uDraggable.scrL=0;
_uDraggable.scrT=0;
_uDraggable.globalset=false;
_uDraggable.onmousemove=function(e) {
  with(_uDraggable) {
    var o=obj;
    if(!o) return;
	o.event=e;
	if(typeof(e.which)!='undefined' && e.which!=1) {o.stop();obj=null;return;}
	e.stopPropagation();
	e.preventDefault();
        var d=_uWnd.getdims();
	if(!o.moved && (e.clientX!=clkX || e.clientY!=clkY)) {o.onstartdrag.apply(o.par,[o.x,o.y,o.w,o.h,o.m]);o.moved=true;}

	o.onmove.apply(o.par,[e.clientX-clkX-(scrL-d.clientLeft),e.clientY-clkY-(scrT-d.clientTop),o.x,o.y,o.w,o.h,o.m]);
	o.event=null;
	return false;
  }
};
_uDraggable.onmouseup=function(e) {
    if(e.which!=1) return;
  with(_uDraggable) {
    var o=obj;
    if(!o) return;
  	if(o.moved)onmousemove(e);
	o.event=e;
  	o.stop();
  	o.event=null;
  	obj=null;
  }
};
_uDraggable.prototype={
	start: function(e,x,y,w,h,m) {
		var r=_uDraggable;
		if(r.obj) {r.obj.event=e;r.obj.onstopdrag.apply(r.obj.par);r.obj.event=null;}
		this.x=x;
		this.y=y;
		this.w=w;
		this.h=h;
		this.m=m;
		this.active=true;
		this.moved=false;
		r.obj=this;
	    var d=_uWnd.getdims();
	    r.clkX=e.clientX;
	    r.clkY=e.clientY;
	    r.scrL=d.clientLeft;
	    r.scrT=d.clientTop;
            this.event=e;
	    this.oninitdrag.apply(this.par,[x,y,w,h,m]);
//window.dump("drag start\n");

	},
	stop: function() {
		var r=_uDraggable;
		if(r.obj==this) r.obj=null;
		if(this.active) {
			this.onstopdrag.apply(this.par,[this.x,this.y,this.w,this.h,this.m]);
			this.active=false;
//window.dump("drag stop \n");
		}
	}

};

//moverID - id or html object of moving object
//type - v or h for vert or horiz
//minV - minimal value returned by slider
//maxV - maximal value
function _uSlider(moverID,type,minV,maxV,opts) {
        if(minV==maxV) return;
        if(typeof(moverID)!='object') this.slider=jQuery('#'+moverID)[0]; else this.slider=moverID;
        if(!this.slider) return;
        this.slider._uslider=this;
        this.type = type.substr(0,1).toLowerCase()=='v' ? 'v' : 'h';

        this.props=jQuery.extend({
            step: 0, //step of value change, if 0 then minimal possible (corresponding to 1 pixel)
            initval: (minV+maxV)/2, //initial value (between min and max)
            disabled: 0, //if initially disabled
            minpos: 0, //offset in pixels which corresponds to minimal value
            maxpos: null, //offset in pixels which corresponds to maximal value, by default size of parent - size of slider
            hotspot: null, //offset of slider hot spot from left (top) edge, by default a half of slider size

            thispar: null, //this for handlers
            onchange: null, //(newvalue,user param,sliderobj)
            param: null //user param for onchange
        },opts || {});

        this.disabled=!!this.props.disabled;

        this.min_value=minV; //min numerical value of slider
        this.max_value=maxV; //max
        this.step=Math.abs(this.props.step); //if zero, than step is minimal possible

        this.value=0;

        this.min_pos=this.props.minpos;
        if(this.props.maxpos!=null) this.max_pos=this.props.maxpos;
                else { //get max pos from parent size
                        var p=this.slider.parentNode;
                        if(p) {
                            this.max_pos=this.type=='h' ? p.offsetWidth-this.slider.offsetWidth : p.offsetHeight-this.slider.offsetHeight;
                        } else this.max_pos=1;
                }
        if(this.props.hotspot!=null) this.hotoff=this.props.hotspot;
                else this.hotoff=this.type=='h' ? Math.floor(this.slider.offsetWidth/2) : Math.floor(this.slider.offsetHeight/2);

        if(this.min_pos==this.max_pos) this.max_pos=this.min_pos+1;

        this.pos=0; //position of slider in parent node
        this.setValue(this.props.initval);
        this.drag=new _uDraggable(this,this._ondrag,null,null,this._ondragstop);
        jQuery(this.slider).unbind('onmousemove mousedown').bind('onmousemove',_uSlider._onmousemove).bind('mousedown',this,_uSlider._onmousedown);
        if(this.slider.parentNode) jQuery(this.slider.parentNode).unbind('mousedown').bind('mousedown',this,_uSlider._onmousedownparent);
}
_uSlider._onmousedownparent=function (e) {
    var o=e.data;
    if(o.disabled) return;
    var x=o.type=='h' ? e.pageX : e.pageY,
        coord=jQuery(this).offset(),
        dx=o.type=='h' ? x-coord.left : x-coord.top;
    o.setPos(dx-o.hotoff);
    e.preventDefault();
};
_uSlider._onmousedown=function (e) {
    var o=e.data;
    if(o.disabled) return;
    o.drag.start(e,o.pos);
    };
_uSlider._onmousemove=function (e) {e.preventDefault();};
_uSlider.prototype={
    setPos:function (newpos) {
                if(newpos<this.min_pos) newpos=this.min_pos;
                        else if(newpos>this.max_pos) newpos=this.max_pos;
                var sign=this.min_value<this.max_value ? 1 : -1;
                var newvalue=(newpos-this.min_pos)/(this.max_pos-this.min_pos)*(this.max_value-this.min_value)+this.min_value;
                if(this.step>0) { //allign value with step
                        newvalue=Math.round((newvalue-this.min_value)/this.step)*this.step+this.min_value;
                        newpos=(newvalue-this.min_value)/(this.max_value-this.min_value)*(this.max_pos-this.min_pos)+this.min_pos;
                        newpos=Math.round(newpos);
                }
                if(newvalue*sign<this.min_value*sign) newvalue=this.min_value;
                        else if(newvalue*sign>this.max_value*sign) newvalue=this.max_value;
                if(newvalue!=this.value) {
                        this.pos=newpos;
                        if(this.type=='h') this.slider.style.left=this.pos+"px";
                                else this.slider.style.top=this.pos+"px";
                        this.value=newvalue;
                        if(this.props.onchange) this.props.onchange.call(this.props.thispar,this.value,this.props.param,this);
                }
    },
    _ondrag:function (dx,dy,p) {
                if(this.disabled) return;
                this.setPos(p+dx);
    },
    setValue:function (V) {
                var sign=this.min_value<this.max_value ? 1 : -1;
                if(V*sign<this.min_value*sign || V*sign>this.max_value*sign) return;
                if(this.step>0) { //allign value with step
                        V=Math.round((V-this.min_value)/this.step)*this.step+this.min_value;
                        if(V*sign<this.min_value*sign) V=this.min_value;
                                else if(V*sign>this.max_value*sign) V=this.max_value;
                }
                var pos=(V-this.min_value)/(this.max_value-this.min_value)*(this.max_pos-this.min_pos)+this.min_pos;
                this.pos=Math.round(pos);
                if(this.type=='h') this.slider.style.left=this.pos+"px";
                        else this.slider.style.top=this.pos+"px";
                this.value=V;
                if(!this.disabled && this.props.onchange) this.props.onchange.call(this.props.thispar,this.value,this.props.param,this);
    }
};


//id - ID of slider in returned code
_uSlider.buildH=function(id,opts) {
    var p=jQuery.extend({
        w: 124, //outer width of slider control in pixels or %
        lw: 0, //width of left image with padding (if any)
        limg: '', //left image
        limgcss: '', //additional css styles for left background prop (like 'no-repeat 0 8px'), url added by limg
        rw: 0, //width of right image with padding (if any)
        rimg: null, //right image
        rimgcss: '', //additional css styles for right background prop (like 'no-repeat 0 8px'), url added by rimg
        title: '', //title for central part if any
        ch: 20, //height of background DIV in pixels
        cw: 120, //width of background DIV in pixels
        cimg: '/img/d/sldBg.gif', //central background image
        cimgcss: 'no-repeat 0 10px', //additional css styles for central background prop (like 'no-repeat 0 8px'), url added by cimg
        simg: '/img/d/sld.gif', //slider image
        simgcss: 'no-repeat 0 0', //additional css styles for slider background prop (like 'no-repeat 0 8px'), url added by simg
        sh: 20, //height of slider DIV in pixels
        sw: 9, //width of slider DIV in pixels
        st: 2 //top css property of slider DIV in pixels
    },opts || {});
    if(String(p.w).indexOf('%')<0 && String(p.w).toLowerCase().indexOf('px')<0) p.w+='px';
    return '<table border="0" cellpadding="1" cellspacing="1" style="width:'+p.w+'"><tr>'
+(p.lw>0 ? '<td width="'+p.lw+'" '+(p.limg || o.limgcss ? 'style="background:'+(p.limg ? 'url('+p.limg+') ' : '')+p.limgcss+'"' : '')+'></td>' : '')
+'<td><div'+(p.title ? ' title="'+p.title+'"' : '')+' style="height:'+p.ch+'px;width:'+p.cw+'px'+(p.cimg || o.cimgcss ? ';background:'+(p.cimg ? 'url('+p.cimg+') ' : '')+p.cimgcss : '')+'">'
+'<div unselectable="on" id="'+id+'" style="cursor:pointer;-moz-user-select:none;height:'+p.sh+'px; width:'+p.sw+'px; position:relative; top:'+p.st+'px'+(p.simg || o.simgcss ? ';background:'+(p.simg ? 'url('+p.simg+') ' : '')+p.simgcss : '')+'"></div></div></td>'
+(p.rw>0 ? '<td width="'+p.rw+'" '+(p.rimg || o.rimgcss ? 'style="background:'+(p.rimg ? 'url('+p.rimg+') ' : '')+p.rimgcss+'"' : '')+'></td>' : '')
+'</tr></table>';

}


//tab options
// id  - id of tab. can be used anywhere instead of tab index. if not specified, then auto generated as '_tc[TCIDX]tb[INDEX]'
// headerc - content of header (string or html obj)
// headerh  - height of header block if it was specified
// footerc - content of footer (string or html obj)
// footerh  - height of header block if it was specified
// hidden - true if tab is initially hidden
// href - URL if tab action is to open URL
// target - target for URL
// close - availability of close button, overrides global option
// icon - URL of tab icon
// markload - alternate markload text, overrides global option
// onload - (tabctrl,idx,tabid,cont,headercont,footercont) after tab content is loaded (reloaded)for the first time (called before onshow), overrides global option
// onshow - (tabctrl,idx,tabid,cont,headercont,footercont) after tab content is loaded into DOM after every tab switch, overrides global option
// onhide - (tabctrl,idx,tabid) when tab looses active state (if tab was closed then this event is called after onbeforeclose if closing was allowed), overrides global option
// onbeforechange - (tabctrl,idx,tabid) before tab is changed to idx. if false, than ignore click, overrides global option
// onchange - (tabctrl,idx,tabid) after tab is changed to idx. if false, than do not load tab contents, overrides global option
// onbeforeclose - (tabctrl,idx,tabid) before tab is closed. if false, than ignore close, overrides global option
// onclose - (idx,tabid) after tab is closed, overrides global option



function _uTabCtrl (name,ntabs,opts,titles,datas,topts) {
	this.constructor=_uTabCtrl;
	this.name=name;
	this.ntabs=ntabs;

	this.props=jQuery.extend({
		parentnode: null, //if null, then just append child
		wnd:null, //use wnd as parent node. window must be already created
		app:opts && opts.wnd && opts.wnd.app || null, //parent app and 'this' for all handlers
		width: 'auto', //outer width
		height: 'auto', //outer height
		min_height: 50, //content height
		active_tab: -1, //if 'auto' then check current url for active tab
		close: 0, //default availability of close button
		design: 'std',
		noinit: false,
		markload: '<div align="left"><div class="myWinLoad"></div></div>', //default mark load text
		emptycontent: '', //content when no active tabs
		
					
		onload: null, //(tabctrl,idx,tabid,cont,headercont,footercont) after tab content is loaded (reloaded)for the first time (called before onshow)
		onshow: null, //(tabctrl,idx,tabid,cont,headercont,footercont) after tab content is loaded into DOM
		onhide: null, //(tabctrl,idx,tabid) when tab looses active state (if tab was closed then this event is called after onbeforeclose if closing was allowed)
		onbeforechange: null, //(tabctrl,idx,tabid) before tab is changed to idx. if false, than ignore click
		onchange: null, //(tabctrl,idx,tabid) after tab is changed to idx. if false, than do not load tab contents
		onbeforeclose: null, //(tabctrl,idx,tabid) before tab is closed. if false, than ignore close
		onclose: null, //(idx,tabid) after tab is closed. 
		ondestroy: null, //(tabctrl,name)
		onresize: null, //(cont_width,cont_height,tabctrl,name)
		rtl:0
	},opts || {});

	this.app=this.props.app;
	this.state={init:false,destroyed:false};
    this.design=this.props.design && _uTabCtrl.designs[this.props.design] ? _uTabCtrl.designs[this.props.design] : _uTabCtrl.designs['std'];
    this.idx=_uTabCtrl.nextidx++;
    _uTabCtrl.all[this.idx]=this;
	this.width=parseInt(this.props.width) || 0; //outer width
	this.height=parseInt(this.props.height) || 0;	//outer height
	this.data=[]; 
	this.pend_show=null;
	this.decor={cdw:0,cdh:0,pdw:0,pdh:0,ph:0}; //
	this.maxid=0;
	this.wnd=null;

	this.active_tab=this.props.active_tab;
	this.scrollpos={tabswidth:0,havewidth:0,pos:null};

	for(var i=0;i<ntabs;i++) {
		var opt=(topts && topts[i]) || {};
		var id=opt.id || '';
		if(!id || id.length==0) id='_tc'+this.idx+'tb'+(this.maxid++);
		this.data[i]={
			id:id, //id of tab. can be used anywhere instead of tab index. if not specified, then auto generated as '_tc[TCIDX]tb[INDEX]'
			title:(titles && titles[i]) || '.', 
			dat:(datas && datas[i]) || '', //tab content struct
                        footer: opt.footerc || null, //content of footer (string or html obj)
                        footerh: opt.footerh && opt.footerh>0 ? opt.footerh : 0, //height of footer block if it was specified
                        header: opt.headerc || null, //content of footer (string or html obj)
                        headerh: opt.headerh && opt.headerh>0 ? opt.headerh : 0, //height of header block if it was specified
                        footercont: null, //real footer content
                        headercont: null, //real footer content
			cont:null, //real tab content (html elem)
                        ismarkload:0, //if tab is in markload state
			markloadcont:null, //markload content obj (html elem)
			obj:null,
			ishidden: opt.hidden,
			clbut:null,
			link:null,
			href:opt.href, //is tab action is to open URL
			target:opt.target, //target for URL
			label:null,
			close:opt.close==undefined ? this.props.close : opt.close,
			icon:opt.icon,
			markload:opt.markload, //alternate markload text
			firstload:0,
			onload:opt.onload==undefined ? this.props.onload : opt.onload,
			onshow:opt.onshow==undefined ? this.props.onshow : opt.onshow,
			onhide:opt.onhide==undefined ? this.props.onhide : opt.onhide,
			onbeforechange:opt.onbeforechange==undefined ? this.props.onbeforechange : opt.onbeforechange,
			onchange:opt.onchange==undefined ? this.props.onchange : opt.onchange,
			onbeforeclose:opt.onbeforeclose==undefined ? this.props.onbeforeclose : opt.onbeforeclose,
			onclose:opt.onclose==undefined ? this.props.onclose : opt.onclose
			};
	}
        this.sesupdate=0;
	if(!_uTabCtrl.globalset) {
		_uTabCtrl.globalset=true;
//	    jQuery(document).bind("mousedown",_uMENU.hideallmenus);
//	    jQuery(window).bind("resize",_uMENU.hideallmenus);
	}
	this.butdown=new _uDraggable(this,null,null,null,function(but,tab){this.design._onbuttonup(this,but,tab);});

	if(!this.props.noinit)this.init();
}


_uTabCtrl.all=[];
_uTabCtrl.nextidx=1;
_uTabCtrl.globalset=false;

_uTabCtrl.getbyname=function(name) {
    var a=this.all;
    for(var i=0;i<a.length;i++) if(a[i] && a[i].name==name) return a[i];
    return null;
};
_uTabCtrl.closeTab=function(name,idx) {
    var w=this.getbyname(name);
    if(w) w.closeTab(idx);
}
_uTabCtrl.content=function(name,idx,c) {
    var w=this.getbyname(name);
    if(w)w.content(idx,c);
}
_uTabCtrl.headerheight=function(name,idx,h) {
    var w=this.getbyname(name);
    if(w)w.headerheight(idx,h);
}
_uTabCtrl.footerheight=function(name,idx,h) {
    var w=this.getbyname(name);
    if(w)w.footerheight(idx,h);
}
_uTabCtrl.setTitle=function(name,idx,t) {
    var w=this.getbyname(name);
    if(w)w.setTitle(idx,t);
}

_uTabCtrl.designs={
    std: {
//    tab_height:'25px',
        content_class: 'u-tabc-content',
        header_class: 'u-tabc-header',
        footer_class: 'u-tabc-footer',
	tabctrl_init: function(o) {
	    var p={};
		jQuery(o.top).attr('class',"x-unselectable u-tabc").html(
 '<div class="u-tabc-p"><div class="u-tabc-listp"><div class="u-tabc-list"></div><div class="u-tabc-pbot"></div></div><div class="u-tabc-scrbut"><div class="u-tabc-tabl"><div class="u-tabc-label"><div class="u-tabc-scrl"></div><div class="u-tabc-scrr"></div></div></div></div></div>'
+'<div class="u-tabc-body"><div class="u-tabc-content" style="height:'+o.props.min_height+'px">'+o.props.emptycontent+'</div></div>'
			);
	    p.pane=jQuery(o.top).find(".u-tabc-p")[0];
	    p.panebot=jQuery(o.top).find(".u-tabc-pbot")[0];
	    p.listp=jQuery(o.top).find(".u-tabc-listp")[0];
	    p.list=jQuery(o.top).find(".u-tabc-list")[0];
	    p.scrbut=jQuery(o.top).find(".u-tabc-scrbut")[0];
	    p.scrl=jQuery(o.top).find(".u-tabc-scrl")[0];
	    p.scrr=jQuery(o.top).find(".u-tabc-scrr")[0];
	    p.body=jQuery(o.top).find(".u-tabc-body")[0];
	    p.emptycontent=p.content=jQuery(o.top).find(".u-tabc-content")[0];
	    jQuery(o.top).find("div,span").andSelf().attr("unselectable","on");
	    jQuery(p.scrl).bind("click mousedown mouseover mouseout",{obj:o,but:0},_uTabCtrl._onscrbutevent);
	    jQuery(p.scrr).bind("click mousedown mouseover mouseout",{obj:o,but:1},_uTabCtrl._onscrbutevent);

	    return p;
	},
	remove_item: function(o,idx) {
		o.parts.list.removeChild(o.data[idx].obj);
	},
	set_title: function(o,idx) {
		jQuery(o.data[idx].label).html(
			(o.data[idx].icon ? '<img class="u-tabc-icon" border="0" src="'+o.data[idx].icon+'">' : (jQuery.browser.msie && jQuery.browser.version<8 ? '<img class="u-tabc-spacer" src="/img/1px.gif" width="1" height="1" border="0">' : ''))
			+o.data[idx].title
		);
	},
	insert_item: function(o,idx) {
		var a=document.createElement('div');
		jQuery(a).attr('class','u-tabc-tab'+(o.data[idx].close ? ' u-tabc-wcl':'')).html(
(o.data[idx].href ?
'<a '+(o.data[idx].target ? 'target="'+o.data[idx].target+'" ':'')+'href="'+o.data[idx].href+'" class="u-tabc-tabl" style="display:block">' :
'<div class="u-tabc-tabl">' )
			+(o.data[idx].close ? '<div class="u-tabc-closebut"></div>' : '')
			+'<div class="u-tabc-tabr"><div class="u-tabc-label">'
			+(o.data[idx].icon ? '<img class="u-tabc-icon" border="0" src="'+o.data[idx].icon+'">' : (jQuery.browser.msie && jQuery.browser.version<8 ? '<img class="u-tabc-spacer" src="/img/1px.gif" width="1" height="1" border="0">' : ''))
			+o.data[idx].title
			+'</div></div>'+(o.data[idx].href ? '</a>' : '</div>')
		);
		o.data[idx].obj=a;
		o.data[idx].label=jQuery(a).find(".u-tabc-label")[0];
		o.data[idx].link=jQuery(a).find(".u-tabc-tabl")[0];
	    if(o.data[idx].close) {
	    	o.data[idx].clbut=jQuery(a).find(".u-tabc-closebut")[0];
			jQuery(o.data[idx].clbut).bind("mouseover",this._onclbutmouseover).bind("mouseout",this._onclbutmouseout).bind("click",{obj:o,tab:o.data[idx]},_uTabCtrl._onclbutclick).bind("mousedown",{obj:o,tab:o.data[idx]},_uTabCtrl._onclbutdown);
	    }
		jQuery(o.data[idx].link).bind("mouseover",this._ontabmouseover).bind("mouseout",this._ontabmouseout).bind("mousedown",{obj:o,tab:o.data[idx]},_uTabCtrl._ontabclick);
		jQuery(a).find("div,span,a,img").andSelf().attr("unselectable","on");
		if(o.data[idx].ishidden) a.style.display='none';

		var nc=o.parts.list.childNodes.length;
//		if(o.props.rtl || window._rtl)
//			o.parts.list.insertBefore(a,nc-idx>=nc ? null : o.parts.list.childNodes[nc-idx]);
//		else
			o.parts.list.insertBefore(a,idx>=nc ? null : o.parts.list.childNodes[idx]);
	},
	_ontabmouseover: function(e) {
		jQuery(this.parentNode).addClass("u-tabc-tab-over");
	},
	_ontabmouseout: function(e) {
		jQuery(this.parentNode).removeClass("u-tabc-tab-over");
	},
	_onclbutmouseover: function(e) {
		jQuery(this).addClass("u-tabc-closebut-over");
	},
	_onclbutmouseout: function(e) {
		jQuery(this).removeClass("u-tabc-closebut-over");
	},
	_onclbutmousedown: function(b,v) {
		if(v) jQuery(b).addClass("u-tabc-closebut-down");
			else jQuery(b).removeClass("u-tabc-closebut-down");
	},
	_ontabactivate: function(o,idx) {
		jQuery(o.data[idx].obj).addClass("u-tabc-tab-act");
	},
	_ontabdeactivate: function(o,idx) {
		jQuery(o.data[idx].obj).removeClass("u-tabc-tab-act");
	},
	_onscrbutactivate: function(o,but,v) { //0-left else right
		if(v) jQuery(!but ? o.parts.scrl : o.parts.scrr).removeClass("u-tabc-scr-dis");
			else jQuery(!but ? o.parts.scrl : o.parts.scrr).addClass("u-tabc-scr-dis");
	},
	_onscrbutover: function(o,but,v) { //0-left else right
		var b=!but ? o.parts.scrl : o.parts.scrr;
		if(v) jQuery(b).addClass("u-tabc-scr-over");
			else jQuery(b).removeClass("u-tabc-scr-over");
	},
	_onscrbutdown: function(o,but,v) { //0-left else right
		var b=!but ? o.parts.scrl : o.parts.scrr;
		if(v) jQuery(b).addClass("u-tabc-scr-down");
			else jQuery(b).removeClass("u-tabc-scr-down");
	},
	_onbuttonup: function(o,but,tab) { //0-left scroll,1-right scroll,2-tab close
		if(but<2) this._onscrbutdown(o,but,0);
			else this._onclbutmousedown(tab.clbut,0);
	}
    }	
};
_uTabCtrl._onclbutdown=function(e) {
		if(e.which==1) {
			e.stopPropagation();
			e.data.obj.design._onclbutmousedown(e.data.tab.clbut,1);
			e.data.obj.butdown.start(e,2,e.data.tab);
		}
		_uWnd.globalmousedown();
};
_uTabCtrl._onscrbutevent=function(e) {
		var o=e.data.obj,but=e.data.but;
		if(e.type=='click' && e.which==1) o.scrollTabPane(but ? 40 : -40);
			else if(e.type=='mouseover') o.design._onscrbutover(o,but,1);
			else if(e.type=='mouseout') o.design._onscrbutover(o,but,0);
			else if(e.type=='mousedown' && e.which==1) {
				o.design._onscrbutdown(o,but,1);
				o.butdown.start(e,but);
			}
};
_uTabCtrl._onclbutclick=function(e) {
		var d=e.data,o=d.obj;
		for(var i=0;i<o.data.length;i++) 
			if(o.data[i]==d.tab) {
				if(typeof o.data[i].onbeforeclose == 'function') if(!o.data[i].onbeforeclose.call(o.app,o,i,o.data[i].id)) break;
				o.closeTab(i);
				break;
			}
		e.preventDefault();
		e.stopPropagation();
};
_uTabCtrl._ontabclick=function(e) {
		var d=e.data,o=d.obj;
		e.preventDefault();
		for(var i=0;i<o.data.length;i++)
			if(o.data[i]==e.data.tab) {
				if(i==o.active_tab) return;
				if(typeof o.data[i].onbeforechange == 'function') if(!o.data[i].onbeforechange.call(o.app,o,i,o.data[i].id)) break;
				o.activateTab(i);
				break;
			}
};

_uTabCtrl.prototype={
        saveSession: function() {
            this.sesupdate=0;
            return {tab: this.active_tab };
        },
	init: function(noinit) {
		var t=document.createElement("div");
		t.id="_utabctrl"+this.idx;
		if(this.props.parentnode) this.props.parentnode.appendChild(t);
			else if(this.props.wnd) {
				var ww=this.props.wnd.parts.wndcont;
				while(ww.firstChild) ww.removeChild(ww.firstChild);
				ww.appendChild(t);
				this.wnd=this.props.wnd;
				this.wnd.tabctrl=this;
				this.wnd.state.loaded=true;
				}
			else jQuery(jQuery("body")[0]).append(t);

		this.top=t;
	        jQuery(t).css({visibility:'hidden',display:'block'});
		if(this.width>0) jQuery(t).css('width',this.width+'px');
//		if(this.height>0) jQuery(t).css('height',this.height+'px');

	    this.parts=this.design.tabctrl_init(this);
	    this.parts.markloadcont=null;
            this.parts.headercontent=null;
            this.parts.footercontent=null;
		this.show();
		if(!noinit)setTimeout("var m=_uTabCtrl.all["+this.idx+"];if(m)m.init1();",10);
	},
	init1: function(noresize) { //noresize used internally by windows
//calculate decor
		var p=this.parts,d=this.decor;
		if(!(this.width>0)) this.width=this.top.offsetWidth;
		if(!(this.height>0)) this.height=this.top.offsetHeight;
		d.cdw=this.top.offsetWidth-p.content.offsetWidth; //content decor w
		d.cdh=this.top.offsetHeight-p.content.offsetHeight; //content decor h
		d.pdw=this.top.offsetWidth-p.listp.offsetWidth; //panelist decor w
                d.hh=0; //active tab header height
                d.fh=0; //active tab footer height
		if(!this.props.wnd && !noresize) {
			jQuery(this.top).css('height',this.height+'px');
			jQuery(p.content).css({width:(this.top.offsetWidth-d.cdw)+'px',height:(this.top.offsetHeight-d.cdh)+'px'});
			jQuery(p.listp).css({width:(this.top.offsetWidth-d.pdw)+'px'});
		}

		jQuery(p.listp).css({overflow:'hidden'});
		jQuery(p.panebot).css('width','4000px');
		jQuery(p.list).css('width','4000px');
		for(var i=0;i<this.ntabs;i++)
			this.design.insert_item(this,i);
		if(this.props.wnd)
			this.resizeTo(this.props.wnd.width-this.props.wnd.decor.w,this.props.wnd.height-this.props.wnd.decor.h);
		var at=this.active_tab;
		this.active_tab=-1;
		if(at=='auto')
			if(this.name && this.name.length>0 && self.location.hash.length>1) {
				var u=self.location.hash.substr(1).split(';');
				var s='T_'+this.name+'=';
				for(var ui=0;ui<u.length;ui++)
					if(u[ui].length>s.length && u[ui].substr(0,s.length)==s) {
						var tb=this.idxbyid(u[ui].substr(s.length));
						if(tb>=0 && tb<this.data.length) {at=tb;break;}
					}
			} else at=-1;
	    jQuery(this.top).css("display",'block').css("visibility",'');
	    if(at==-1 && this.data.length>0) at=0;
	    if(at>=0 && at<this.ntabs) {
			for(var i=0;i<this.ntabs;i++) {
				var j=(at+i)%this.ntabs;
				if(!this.data[j].ishidden && (!this.data[j].onbeforechange || !!this.data[j].onbeforechange(this,j,this.data[j].id))) {
					this.activateTab(j,true);
					break;
				}
			}
		} else this.activateTab(-1,true);

	    jQuery(this.top).css("display",'none').css("visibility",'');
		this.state.init=true;
	    if(pend_show) this.show(pend_show[0]);
	    if(this.props.wnd) this.props.wnd.onexternalload(); //activate autosize 
	},
	scrollTabPane: function(dx) { //>0 - right
		var pos=this.scrollpos.pos+=dx;
		var w=this.scrollpos.tabswidth,havew=this.scrollpos.havewidth;
		if(w<havew) {
			this.design._onscrbutactivate(this,0,0);
			this.design._onscrbutactivate(this,1,0);
			if((this.props.rtl || window._rtl) && this.data.length>0) {
				pos=this.data[this.data.length-1].obj.offsetLeft+havew-w;
			} else pos=0;
			this.scrollpos.pos=pos;
			this.parts.listp.scrollLeft=pos;
		} else {
			if(this.props.rtl || window._rtl) {
				var ol=this.data[this.data.length-1].obj.offsetLeft;
				if(pos<ol) pos=ol;
					else if(pos>ol+w-havew) pos=ol+w-havew;
			} else if(pos<0) pos=0;
					else if(pos>w-havew) pos=w-havew;
			this.scrollpos.pos=pos;
			this.parts.listp.scrollLeft=pos;
			if(pos>0) this.design._onscrbutactivate(this,0,1);
				else this.design._onscrbutactivate(this,0,0);
			if(pos<w-havew) this.design._onscrbutactivate(this,1,1);
				else this.design._onscrbutactivate(this,1,0);
		}
	},
	_setscrolls: function() { //set scroll buttons status accroding to tab widths
		var w=0,havew=this.parts.listp.offsetWidth;
		if(this.parts.listp.clientWidth>0 && this.parts.listp.clientWidth<havew) havew=this.parts.listp.clientWidth;
		if(this.data.length>0) {
			var o=this.data[this.data.length-1];
			if(this.props.rtl || window._rtl) {
				var o2=this.data[0];
				w=o2.obj.offsetLeft+o2.obj.offsetWidth-o.obj.offsetLeft;
			} else w=o.obj.offsetLeft+o.obj.offsetWidth;
		}
		if(havew<w) { //enable scrolls
			this.parts.scrbut.style.display='block';
			havew-=this.parts.scrbut.offsetWidth;
		} else this.parts.scrbut.style.display='none';
			
		if(this.scrollpos.pos==null) {
			if((this.props.rtl || window._rtl) && this.data.length>0) this.scrollpos.pos=this.data[this.data.length-1].obj.offsetLeft+w-havew;
				else this.scrollpos.pos=0;
		} else if(this.scrollpos.havewidth!=havew) 
					if(this.props.rtl || window._rtl) this.scrollpos.pos+=this.scrollpos.havewidth-havew;

		this.scrollpos.havewidth=havew;
		this.scrollpos.tabswidth=w;
		this.scrollTabPane(0);
//		alert(w+','+havew);
	},
	show: function(rel) {
		if(!this.state.init) {pend_show=[rel];return;}
  		jQuery(this.top).show();
	    this.state.visible=true;
//	    if(rel) load();
	},
	resizeTo: function(w,h) {
	  var d=this.decor;
          with(this) {
		width=w;
		height=h;
//		jQuery(this.top).css({height:h+'px',width:w+'px'});
		jQuery(top).width(w).height(h);
		jQuery(parts.content).css({width:(w-d.cdw)+'px',height:(h-d.cdh-d.fh-d.hh)+'px'});
		if(parts.headercontent)jQuery(parts.headercontent).css({width:(w-d.cdw)+'px',height:d.hh+'px'});
		if(parts.footercontent)jQuery(parts.footercontent).css({width:(w-d.cdw)+'px',height:d.fh+'px'});
		jQuery(parts.listp).css({width:(w-d.pdw)+'px'});
		if(props.onresize) props.onresize.call(app,w-d.cdw,h-d.cdh,this,name);
		_setscrolls();
          }
	},
	addTab: function(title,dat,topt,idx) {
		var i= idx!=undefined && idx>=0 && idx<this.data.length ? idx : this.data.length;
		var opt=topt || {};
		var id=opt.id || '';
		if(!id || id.length==0) id='_tc'+this.idx+'tb'+(this.maxid++);
		var data={
			id:id,
			title:title || '.',
			dat:dat || '',
                        footer: opt.footerc || null, //content of header (string or html obj)
                        footerh: opt.footerh && opt.footerh>0 ? opt.footerh : 0, //height of footer block if it was specified
                        header: opt.headerc || null, //content of footer (string or html obj)
                        headerh: opt.headerh && opt.headerh>0 ? opt.headerh : 0, //height of header block if it was specified
                        footercont: null, //real footer content
                        headercont: null, //real footer content
			cont:null,
                        ismarkload:0, //if tab is in markload state
			markloadcont:null,
			obj:null,
			ishidden: opt.hidden,
			clbut:null,
			link:null,
			href:opt.href,
			target:opt.target,
			label:null,
			close:opt.close==undefined ? this.props.close : opt.close,
			icon:opt.icon,
			markload:opt.markload,
			firstload:0,
			onload:opt.onload==undefined ? this.props.onload: opt.onload,
			onshow:opt.onshow==undefined ? this.props.onshow : opt.onshow,
			onhide:opt.onhide==undefined ? this.props.onhide : opt.onhide,
			onbeforechange:opt.onbeforechange==undefined ? this.props.onbeforechange : opt.onbeforechange,
			onchange:opt.onchange==undefined ? this.props.onchange : opt.onchange,
			onbeforeclose:opt.onbeforeclose==undefined ? this.props.onbeforeclose : opt.onbeforeclose,
			onclose:opt.onclose==undefined ? this.props.onclose : opt.onclose
			};

		if(i<this.data.length) this.data.splice(i,0,data);
			else this.data[i]=data;
		this.design.insert_item(this,i);
		this._setscrolls();
		return i;
	},
	tabHidden: function(idx,ishidden) {
		idx=this.idxbyid(idx);
		if(idx<0 || idx>=this.data.length) return;
		var a=this.data[idx];
		a.obj.style.display=ishidden ? 'none' : ''; 
		a.ishidden=!!ishidden;
		this._setscrolls();
	},
	activateTab: function(idx,first) {
		idx=this.idxbyid(idx);
		if(idx<0 || idx>=this.data.length || this.active_tab==idx) if(first && this.data.length>0) idx=0; else return;
		this.design._ontabactivate(this,idx);
		if(this.active_tab>=0 && this.active_tab<this.data.length) {
			if(typeof this.data[this.active_tab].onhide == 'function') this.data[this.active_tab].onhide.call(this.app,this,this.active_tab,this.data[this.active_tab].id);
			this.design._ontabdeactivate(this,this.active_tab);
		}
		this.active_tab=idx;
		this._setscrolls();
                if(!first) this.sesupdate=1;
		if(typeof this.data[idx].onchange == 'function') if(!this.data[idx].onchange.call(this.app,this,idx,this.data[idx].id)) return;
		this.load(idx);
	},
	closeTab: function(idx) {
		idx=this.idxbyid(idx);
		if(idx<0 || idx>=this.data.length) return;
		if(idx==this.active_tab && typeof this.data[idx].onhide == 'function') this.data[idx].onhide.call(this.app,this,idx,this.data[idx].id);

		this.design.remove_item(this,idx);
		var id=this.data[idx].id;
		var f=this.data[idx].onclose;
		this.data.splice(idx,1);
		if(idx==this.active_tab) {
			this.active_tab=-1;
			if(idx==this.data.length) 
				if(idx==0) this._assign_content(-1);
					else this.activateTab(idx-1);
				else this.activateTab(idx);
		} else if(idx<this.active_tab) this.active_tab--;
		this._setscrolls();
		if(f) f(this,idx,id);
	},
	setTitle: function(idx,title) {
		idx=this.idxbyid(idx);
		if(idx<0 || idx>=this.data.length) return;
		this.data[idx].title=title;
		this.design.set_title(this,idx);
		this._setscrolls();
	},
        headerheight: function(idx,fh) {
	    idx=this.idxbyid(idx);
	    if(idx<0 || idx>=this.data.length) return;
            if(typeof(fh)!='number' || isNaN(fh)) return;
            this.data[idx].headerh=fh;
        },
        footerheight: function(idx,fh) {
	    idx=this.idxbyid(idx);
	    if(idx<0 || idx>=this.data.length) return;
            if(typeof(fh)!='number' || isNaN(fh)) return;
            this.data[idx].footerh=fh;
        },
	content: function(idx,c,hc,fc,iserror) { //c - content, hc - header content, fc - footer content
		idx=this.idxbyid(idx);
		if(idx<0 || idx>=this.data.length) return;
		if(!this.data[idx].cont) {
			this.data[idx].cont=document.createElement('DIV');
			jQuery(this.data[idx].cont).addClass(this.design.content_class);
		}
		jQuery(this.data[idx].cont).html(c);
                if(hc) {
    		    if(!this.data[idx].headercont) {
			this.data[idx].headercont=document.createElement('DIV');
			jQuery(this.data[idx].headercont).attr("class",this.design.content_class+' '+this.design.header_class).css("overflow","hidden");
		    }
		    jQuery(this.data[idx].headercont).html(hc);
                } else if(this.data[idx].headercont) jQuery(this.data[idx].headercont).html('');
                if(fc) {
    		    if(!this.data[idx].footercont) {
			this.data[idx].footercont=document.createElement('DIV');
			jQuery(this.data[idx].footercont).attr("class",this.design.content_class+' '+this.design.footer_class).css("overflow","hidden");
		    }
		    jQuery(this.data[idx].footercont).html(fc);
                } else if(this.data[idx].footercont) jQuery(this.data[idx].footercont).html('');
		this.data[idx].firstload=iserror ? 0 : 1;
		this.data[idx].ismarkload=0;
		if(idx==this.active_tab) this._assign_content(idx);
	},
	_assign_content: function(idx) {
                var fh=0,hh=0,i; //footer height, header height
		if(idx<0) {this.parts.content=this.parts.emptycontent;this.parts.headercontent=this.parts.footercontent=null;}
		        else if(this.data[idx].ismarkload) {this.parts.content=this.data[idx].markloadcont;this.parts.headercontent=this.parts.footercontent=null;}
			else {
                            this.parts.content=this.data[idx].cont;
                            this.parts.headercontent=this.data[idx].headercont;
                            hh=this.data[idx].headerh;
                            this.parts.footercontent=this.data[idx].footercont;
                            fh=this.data[idx].footerh;
                        }
                i=0; //current child index
                //set header s first child
                if(this.parts.headercontent) {
		    jQuery(this.parts.headercontent).css({width:(this.width-this.decor.cdw)+'px',height:hh+'px'});
                    if(this.parts.body.childNodes[i]) this.parts.body.replaceChild(this.parts.headercontent,this.parts.body.childNodes[i]);
                        else this.parts.body.appendChild(this.parts.headercontent);
                    this.decor.hh=hh;
                    i++;
                } else this.decor.hh=0;
                //set main content
		jQuery(this.parts.content).css({width:(this.width-this.decor.cdw)+'px',height:(this.height-this.decor.cdh-fh-hh)+'px'});
                if(this.parts.body.childNodes[i]) this.parts.body.replaceChild(this.parts.content,this.parts.body.childNodes[i]);
                        else this.parts.body.appendChild(this.parts.content);
                i++;
                //set footer
                if(this.parts.footercontent) {
		    jQuery(this.parts.footercontent).css({width:(this.width-this.decor.cdw)+'px',height:fh+'px'});
                    if(this.parts.body.childNodes[i]) this.parts.body.replaceChild(this.parts.footercontent,this.parts.body.childNodes[i]);
                        else this.parts.body.appendChild(this.parts.footercontent);
                    this.decor.fh=fh;
                    i++;
                } else this.decor.fh=0;
                //remove excess childs if any
                while(this.parts.body.childNodes[i]) this.parts.body.removeChild(this.parts.body.childNodes[i]);

		if(idx>=0 && !this.data[idx].ismarkload) {
                    if(typeof this.data[idx].onload == 'function' && this.data[idx].firstload) this.data[idx].onload.call(this.app,this,idx,this.data[idx].id,this.parts.content,this.parts.headercontent,this.parts.footercontent);
		    if(typeof this.data[idx].onshow == 'function') this.data[idx].onshow.call(this.app,this,idx,this.data[idx].id,this.parts.content,this.parts.headercontent,this.parts.footercontent);
                }
		if(idx>=0) this.data[idx].firstload=0;
	},
	markload: function(idx,txt) {
		idx=this.idxbyid(idx);
		if(idx<0 || idx>=this.data.length) return;
		var c;
		if(this.data[idx].markload) { //this tab has its own markload page
			if(!this.data[idx].markloadcont) {
				this.data[idx].markloadcont=document.createElement('DIV');
				jQuery(this.data[idx].markloadcont).addClass("u-tabc-content").html(this.data[idx].markload);
			}
//			c=this.data[idx].markloadcont;
		} else {
			if(!this.parts.markloadcont) {
				this.parts.markloadcont=document.createElement('DIV');
				jQuery(this.parts.markloadcont).addClass("u-tabc-content").html(this.props.markload);
			}
                        this.data[idx].markloadcont=this.parts.markloadcont;
//			c=this.parts.markloadcont;
		}
                this.data[idx].ismarkload=1;
                this.data[idx].iserrorload=0;
		if(idx==this.active_tab) this._assign_content(idx);
	},
	idxbyid: function(id) {
		if(typeof id=='string') {
			for(var i=0;i<this.data.length;i++) if(this.data[i].id==id) return i;
			return -1;
		} else return id;
	},
        findintab: function(idx,sel) { //applies css selector to tab content, including footer and header
	    idx=this.idxbyid(idx);
	    if(idx<0 || idx>=this.data.length) return jQuery([]);
            var ob=jQuery(this.data[idx].cont);
            if(this.data[idx].headercont) ob=ob.add(this.data[idx].headercont);
            if(this.data[idx].footercont) ob=ob.add(this.data[idx].footercont);
            return ob.find(sel);
        },
	load: function(idx,reload) {
	    idx=this.idxbyid(idx);
	    if(idx<0 || idx>=this.data.length) return;
	    if(this.data[idx].cont!=null && !reload) { //content is already loaded
		this._assign_content(idx);
		return;
	    }
	    var c=this.data[idx].dat;
	    if(typeof (c) == 'string') this.content(idx,c,this.data[idx].header,this.data[idx].footer);
    	        else if(typeof c == 'function') this.content(idx,c(),this.data[idx].header,this.data[idx].footer);
    	        else if(typeof c == 'object') {
	            this.markload(idx);
                    if(!c.data)c.data={};
                    if(!('_tci' in c.data)) c.data._tci=this.idx;
                    if(!('_ti' in c.data)) c.data._ti=idx;
                    if(this.app && !('_ai' in c.data)) c.data._ai=this.app.pid;
                    if(this.app && !('app' in c)) c.app=this.app.pid;
                    if(this.wnd && !('wnd' in c)) c.wnd=this.wnd.idx;
                    if(!('tabctrl' in c)) c.tabctrl=this.idx;
                    if(!('tabidx' in c)) c.tabidx=idx;
		    if(!c.success && c.xml===false) {c.dataType='text';c.success=new Function("data","st","var w=_uTabCtrl.all["+this.idx+"];if(w)w.content('"+this.data[idx].id+"',data);");}
	   		else if(!c.success && c.xml!==false) c.success=new Function("data","st","var w=_uTabCtrl.all["+this.idx+"];_uParseXML(data,w,'"+this.data[idx].id+"',w.app);");
		    if(!c.error) c.error=new Function("xml","st","er","var w=_uTabCtrl.all["+this.idx+"];if(w)w._onerror('"+this.data[idx].id+"',xml,st,er);");
		    try {
			if(c.form && (c.form.length>0 || c.form.nodeType)) //have form
	   			_uPostForm(c.form,c);
	   		else if(c.url) _uAjaxRequest(c.url,c);
		    } catch(e) {this._onerror(this.data[idx].id,null,'',e);}
    	        }
    	return true;
	},
	_onerror: function(id,xml,st,er) {
		var idx=this.idxbyid(id);
		if(idx<0 || idx>=this.data.length) return;
	    var o=this.props.onerror;
	    if(o && typeof(o)=='function') o.apply(this.app,arguments);
			else this.content(idx,_txt('ErrorLoadTab',this.idx,idx),null,null,1);
	},
	destroy: function() {
		//remove items
		this.data.splice(0,this.data.length);
		this.parts=null;
		this.top.parentNode.removeChild(this.top);
		this.top=null;
		if(this.props.ondestroy) this.props.ondestroy(this,this.name);
	}
};

function _uWnd (name,title,width,height,opts,content,menuitems,app) {
    if(name && name.length>0) {
		var t=_uWnd.getbyname(name);
		if(t) {t.reload(content);return false;}
    }
    this.constructor=_uWnd;
    this.desktop=this.opts && this.opts.desktop || _uWnd.defdesktop;
    this.props=jQuery.extend({
//behavior
	parent: null, //parent window, necessary for modal mode if window is not system modal
	popup: 0, //whether window is closed when clicked outside
	alert: 0, //window has z-index above modal windows
	closeonesc: opts.popup ? 1 : 0, //close window on escape
        nohide: 0, //if true then window is hidden with visibility, not with display when minimized
//position and size
	x: 'auto',
	y: 'auto',
        initstate: null, //'max' or 'min for initially maximized or minimized window regardless of corresponding buttons (session can override this)
        headerh: 0, //height of content header. it is considered to be a part of window's decor
        headerc: null, //content of header, string with html or HTMLElement
        hideheader: 1, //whether header must become invisible when content loading is in progress
        footerh: 0, //height of content footer. it is considered to be a part of window's decor
        footerc: null, //content of footer, string with html or HTMLElement
        hidefooter: 1, //whether header must become invisible when content loading is in progress
        contentsizeprio: opts.footerh>0 || opts.headerh>0 ? 1 : 0,//if true, then preserving content height has priority when changing header or footer height (i.e. outer window height will be changed)
	nomove: 0, //window cannot be moved
        hideonmove: 1, //whether content is hidden when dragging
	center: opts.modal ? 1 : 0, //center window even for desktop (not cascaded)
	session: null, //saved session returned from saveSession
	hidden: 0, //whether initially hidden
	modal: 0, //system modal window if no parent window specified. just disables parent otherwise
	toolwindow: 0, //if true then this window is tool window (so it must have parent) and its activation look depends on parents focus, not own
	resize: opts.toolwindow ? 0 : 1, //whether window can be resized
	fixed: _uWnd.defdesktop || opts.desktop ? 0 : 1, //whether window is fixed to viewport (not actual for desktop)
	minh: 50, //min content height not counting footer and header
	minw: 0, //---same
	maxh: 0, //---same
	maxw: 0, //---same
//autosize
	autosize: opts.toolwindow ? 0 : 1,
	autosizewidth: 0, //whether autosize can change width
	autosizeonimages: 0, //restart auto size on every image load if content has images
	waitimages: 0, //if >0, then do not show content until all images are loaded (timeout is specified value), only during autosize
	hideonresize: 0, //make content invisible during autosize
//visual elements and effects
	icon: '', //icon image url
	header: title ? 1 : 0, //whether window has header
	min: this.desktop && !(opts.notaskbar || opts.parent || opts.toolwindow || opts.alert || opts.modal) ? 1 : 0, //show minimize button
	max: this.desktop && !opts.modal ? 1 : 0, //show maximize/restore button
	close: 1, //show close button
	customButtons: null, //{ 'name': [init_visible,actionfunc(wnd,param),thispar,param || 'name']... }  'xt-name' class, 'xt-name-over' will be used when mouseover
	notaskbar: opts.parent || opts.toolwindow || opts.alert || opts.modal ? 1 : 0,
	align: 'center',
	shadow: opts.toolwindow ? 0 : 1,
	design: 'std',
	fadetype: opts.toolwindow ? 0 : 1, //0 - no fade, 1 - animate opacity, 2 - move down and opacity
	fadespeed: 800,
	fadeclosetype: opts.toolwindow ? 0 : 1, //0 - no fade, 1 - animate opacity, 2 - move down and opacity
	fadeclosespeed: 250,
	havemenu: menuitems ? 1 : 0,
	menuopts: null, //object with menu options, will be passed to menu constructor
	trayicon: null, //just string with icon image URL or object with all optional fields (image must be if window have no icon){title:title, img:image, param:param, thisobj:thisobj, ondown:handler,onclick:handler,onrdown:handler} if thisobj redefined, no standard handlers will be set. param is true for animated show/hide
	traymenu: null, //tray menu which will be set by standard onrdown handler (array with items)
        actlayer: 0, //cover window content and footers with transparent div when window is inactive for ability to activate when content is iframe or flash
//loading
	markload: '<div align="left"><div class="myWinLoad"></div></div>', //default mark load text
	havegrid: 0, //ability to showgrid and hidegrid for setting waiting notification to user.
	markwaitcont: '<div class="myWinLoad"></div>', //default content of waiting notification layer (grided mode)
	markwaitclass: 'myWinGrid', //default class for waiting notification layer

//event handlers
	oninit: null, //(wnd,wndname) called when window finished its initialization and content can be loaded, if returns false, no default content is assigned to window (can be used to assign already created tab control)
	oncontent: null, //(wnd,contElem) called after content is loaded into window and its object became accessible
	onposchange: null, //(wnd) called when window position, size or min/max state is changed
	onactivate: null, //(wnd) called when window is activated (got focus)
	ondeactivate: null, //(wnd) called when window is deactivated (lost focus)
	onerror: null, //(xml,status,error) called when error loading content. if not specified, window is just closed
	onbeforeclose: null, //(wnd) called after user 'close' event appears (button or taskbar). if true, then ignore close
	onclose: null, //(wnd,wndidx) called after window is destroyed
//	onmousemove: null, //(wnd,x,y,{ctrl:0,alt:0,shift:0,meta:0},event) send when mouse is moving over content area, x and y relative to content	

//special
	desktop: null, //
	notabdestroy:0
    }, opts || {});
	if(this.props.modal || this.props.toolwindow) this.props.min=0;
	if(this.props.toolwindow) this.props.max=0;
	if(this.props.session) {
		this.props.x=this.props.session.x;
		this.props.y=this.props.session.y;
	}
//content is string, function returning string or object:
//  url - load window content (on show) by using request to this url, if xml=true, content should be set by _uWnd.content(name,content) or wnd.content(content), otherwise result assumed to be plain html
//  form - send request (for 'url' option) using data from this form ID. if no 'url', use 'action'
//  xml - whether result of remote request is raw html or parsed XML
//  type - 'get' or 'post' methods when 'form' is specified, otherwise use form's 'method' attribute
//  cache - whether to allow browser to cache result of remote request
//  async - whether to do async remote request
//  success - alternative function(data,status) to process successful request (default depends on 'xml')
//  error - handler of error requests. 
//  dataType - data type to return to 'success' function
    jQuery.extend(this,
	{
	name: name,
	title: title ? title : '',
	letsize: 1, //average width of letters in title
	width: width && Math.abs(width)>10 ? width : -300,
	height: height && Math.abs(height)>10 ? height : -200,
        headerh: this.props.headerc && this.props.headerh>=0 ? this.props.headerh : 0,
        footerh: this.props.footerc && this.props.footerh>=0 ? this.props.footerh : 0,
	decor: {w:0, h:0, th:0}, //width of window decoration (it is added to content width to get window width)
	_content: content,
	state: {visible:false,prevvisible:false,minimized:false,maximized:false,loaded:false,init:false,disabled:false,grided:false,noshadow:false,resizing:false,destroyed:false,focused:false},
	grid: null, //grid object for modal windows
	frame: null, //background iframe for IE
	sh: null,//array of shadow objects
	sh_sz: [4,2], //shadow outdent and indent in pixels
	xpos: 0,
	ypos: 0,
	zpos: this.props.modal || this.props.alert ? _uWnd.getModalTopZ() : _uWnd.getTopZ(),
	_drag: new _uDraggable(this,this._ondragmousemove,null,this.onstartdrag,this.onstopdrag),
	_resize: new _uDraggable(this,this._onrsmousemove,null,this.onstartrs,this.onstoprs),
	restRect: null,
//		rsBut: {},
	minheight: this.props.minh,
	minwidth: this.props.minw,
	maxheight: this.props.maxh,
	maxwidth: this.props.maxw,
	pend_show: null,
	autosz: {active:false},
	imgloader: {timer:null,active:false},
	menu: null,
	app: app || null, //parent application. if assigned then all event handlers and content function are called in context of app
	childs: [],
	nchilds: 0, //number of all-level children
	tabctrl: null,
        sesupdate: 0
	});
    this.design=this.props.design && _uWnd.designs[this.props.design] || _uWnd.designs['std'];
    this.idx=_uWnd.nextidx++;
    _uWnd.all[this.idx]=this;
    this._focus=new _uFocus({type:0,owner:this,thispar:this,parent:this.props.parent && this.props.toolwindow ? this.props.parent._focus : (this.app ? this.app._focus : null),onkeydown: this._onkeydown,onkeypress: this._onkeypress,onactivate: this._onactivate,ondeactivate: this._ondeactivate, canactivate: this._canactivate});
    if(!_uWnd.globalset) {
		_uWnd.globalset=true;
//		jQuery(document).bind("click",_uMenu.allmenushide);
//		jQuery(document).bind("mouseup",_uWnd._onmouseup);
		jQuery(document).bind("mousedown",_uWnd.closepopup);
//		jQuery(document).bind("mousemove",_uWnd._onmousemove);
//		if(jQuery.browser.msie)jQuery(document).bind("mouseout",function(e) {if(!e.relatedTarget && !e.toElement)_uWnd._dragging=null});
		jQuery(window).bind("scroll",_uWnd._onscroll);
		jQuery(window).bind("resize",_uWnd._onresize);
		_uWnd.sysmenu=new _uMENU('',{align:'D'},{hidden:1,withmarks:1,onitem:_uWnd._onsysmenuitem},_uWnd.sysmenuitems);

    }
    this.init(menuitems);
}

_uWnd.all={};
_uWnd.nextidx=1;
_uWnd.zchilds=[];
_uWnd.lastz=0;
_uWnd.zstep=2;
_uWnd.minz=10000;
_uWnd.maxz=20000;
_uWnd.lastmodalz=0;
_uWnd.minmodalz=21000;
_uWnd.maxmodalz=25000;
_uWnd.globalset=false;
_uWnd.defdesktop=null;
_uWnd.findactive=function() {
	var t=_uFocus.current;
	while(t) {
		if(t.owner && t.owner.constructor==_uWnd) return t;
		t=t._parent;
	}
	return null;
};
_uWnd.globalmousedown=function(){
	_uMENU.hideallmenus();
	_uSuggestList.hideall();
	_uComboBox.hideall();
	_uWnd.closepopup();
};
_uWnd.messageBoxclose=function(w,wi) {
	var d=w.props._msgdata;
	if(!d.retval) d.retval=d.defc;
	if(d.onsel) d.onsel.call(d.app,d.retval,d.param,w);
};
_uWnd.messageBox=function(txt,title,b,onselect,opts,param,app) { //message text, window title, buttons config
//b - [but1,but2,...]
//butX - string with id or object with props:
//id (mandatory): button id, one of 'ok','yes','not','retry','cancel' or any other (but text must be provided
//t: text label for button (or id will be used)
//def: +1 is button must be focused by default (or first will be used), +2 if button will be returned on ESC or window close (or last will be used)

//opts are stabdard window properties plus:
	var props=jQuery.extend({
		w: 'auto', //width
		h: 'auto', //height
		name: '', //window name
//predefined standard window options
		modal: 1,
		closeonesc: 1,
		align: 'center',
		min: 0,
		max: 0,
		close: 1,
		fadetype: 0,
		fadeclosetype: 0,
		resize:0,
		autosize:0
	},opts || {});
	var d={onsel:onselect,app:app,param:param}; //all messagebox data
	props._msgdata=d;
	props.onclose=_uWnd.messageBoxclose;
	props.oncontent=_uWnd.messageBoxoncont;
	var i,std,w;
	std={'ok':1,'yes':1,'no':1,'retry':1,'cancel':1};
	d.buts=[];
	d.def=null;
	d.defc=null;
	if(!b || b.constructor!=Array) b=['ok'];
	for(i=0;i<b.length;i++) {
		if(typeof b[i]=='string') d.buts.push({id:b[i],t:( std[b[i]] ? _txt(b[i]) : b[i] )});
		else if(typeof b[i]=='object' && b[i].id) {
			d.buts.push({id:b[i].id,t:b[i].t || ( std[b[i].id] ? _txt(b[i].id) : b[i].id ),def:b[i].def});
		} 
	}
	if(d.buts.length==0) return 0;
	w=0;
	for(i=0;i<d.buts.length;i++) { //find default
		w+=60;
		if(d.buts[i].def) {
			if(d.buts[i].def & 1) d.def=d.buts[i].id;
			if(d.buts[i].def & 2) d.defc=d.buts[i].id;
		}
	}
	if(!d.def) d.def=d.buts[0].id;
	if(!d.defc) d.defc=d.buts[d.buts.length-1].id;
	d.txt=txt;
	//
	return new _uWnd(props.name || '',title,!props.w || props.w=='auto' ? -(w+50) : props.w,!props.h || props.h=='auto' ? -100 : props.h,props,_uWnd.messageBoxcont,null,app);
	
};
_uWnd.messageBoxoncont=function(w,c) {
	var d=w.props._msgdata,o;
	o=jQuery(w.parts.content).find('#_uw'+w.idx+"msg"+d.def)[0];
	if(o) o.focus();
};
_uWnd.messageBoxcont=function(w) {
	var cont,d;
	d=w.props._msgdata;
	cont='';
	for(i=0;i<d.buts.length;i++) {
		cont+='<td>'+_uButton('','b',{
			style:1, //d.def==d.buts[i].id ? 0 : 1, 
			text:d.buts[i].t,
			content:'onclick="var w=_uWnd.all['+w.idx+'];if(w){w.props._msgdata.retval=\''+encodeHtmlVal(d.buts[i].id)+'\';w.close()}"',
			id:"_uw"+w.idx+"msg"+d.buts[i].id}
		)+'</td>';
	}
	
	return '<div style="padding:20px 10px 10px 10px">'+d.txt+'</div><div style="padding:0px 10px 10px 10px"><table align="center" callpadding="0" cellspacing="10" border="0"><tr>'+cont+'</tr></table></div>';
};

_uWnd.alerts=null;
_uWnd.alert=function(txt,title,opts,app) {
    opts=jQuery.extend({w:150,h:100,tm:5000,close:1,align:'center',icon:'',name:'',pad:null},opts || {});
    if(opts.pad)txt='<div style="padding:'+opts.pad+'">'+txt+'</div>';
    var d,x,y,by,p,maxy;
    if(!(d=_uWnd.defdesktop)) {
    	d=_uWnd.getdims();
    	x=d.clientW-opts.w-5;
    	by=d.clientH-opts.h-5;//bottom y
    	p=_uWnd.alerts;
    	maxy=0;
    	d=null;
    } else {
		x=d.width-d.calcexclude(1)-opts.w-5;
		by=d.height-d.calcexclude(3)-opts.h-5;
		p=d.alerts;
		maxy=d.calcexclude(2);
    }
   	y=by;
    if(p) {
		if(p.y-(opts.h+5)>=0) y=p.y-(opts.h+5);
		while(p) {if(p.y>maxy) maxy=p.y;p=p.prev;}
		if(maxy>0 && by-maxy>=opts.h+5)y=by;
    }
    var w=new _uWnd(opts.name,title,opts.w,opts.h,{close:opts.close,min:0,max:0,icon:opts.icon,align:opts.align,x:x,y:y,alert:1,autosize:0,fixed:1,shadow:0,resize:0,nomove:1,hidden:0,notaskbar:1,fadetype:2,fadespeed:500,fadeclosetype:2,fadeclosespeed:500,onclose:function(w,idx){_uWnd.rmalert(w,idx);}},txt,null,app);
    if(!d)_uWnd.alerts={prev:_uWnd.alerts,wnd:w,y:y};
    	else d.alerts={prev:d.alerts,wnd:w,y:y};
    if(opts.tm>0) setTimeout("var w=_uWnd.all["+w.idx+"];if(w)w.close();",opts.tm);
    return w;
};
_uWnd.rmalert=function(w,idx) {
    var a=w.desktop ? w.desktop.alerts : _uWnd.alerts,p=null;
    while(a && a.wnd!==w) {p=a;a=a.prev;}
    if(a && a.wnd===w) if(p) p.prev=a.prev; else {
    	if(w.desktop) w.desktop.alerts=a.prev; else _uWnd.alerts=a.prev;
    }
};
_uWnd._onscroll=function(e) {
    if(!jQuery.browser.msie) return;
    var d=_uWnd.getdims(),a=_uWnd.all;
    for(var i in a) 
	if(a[i]) if(a[i].props.fixed || a[i].grid) a[i].moveTo(a[i].xpos,a[i].ypos);
};
_uWnd._onresize=function(e,dsk) { //if dsk then select by desktop, otherwize without desktop
    var d=_uWnd.getdims(),a=_uWnd.all;
    for(var i in a) if(a[i])  if((!dsk && !a[i].desktop) || a[i].desktop==dsk) a[i]._onresize(d);
};
_uWnd.closepopup=function() {
    var a=_uWnd.all;
    for(var i in a)	if(a[i] && a[i].props.popup) a[i].closeevent();
};
_uWnd.findparent=function(elem){ //find parent window for specified element
	var p=elem;
	while(p && p!=document.body) {
		if(p.id && p.id.indexOf && p.id.indexOf('_uwndTop')==0 && p._uwndobj) return p._uwndobj;
		p=p.parentNode;
	}
	return null;
};
_uWnd.getbyname=function(name) {
    var a=this.all;
    for(var i in a) if(a[i] && a[i].name==name) return a[i];
    return null;
};
_uWnd.getTopZ=function(nc,ignw) { //number of all-level child windows for correct account of their influence
//ignw - window which must be ignored when rebuilding z indexes. if its specified then ignw.setZ must follow this function
  if(!nc)nc=0;
  with(_uWnd) {
    var z=lastz;
    if(z<minz) z=minz; else z+=zstep;
    if(z+(1+nc)*zstep>maxz) {
		var x=minz;
		var ar=[];//list of all top-level windows
		for(var i in all) if(all[i] && all[i]!=ignw && all[i].props && !all[i].props.parent) ar[ar.length]=all[i];
		for(var i=0;i<zchilds.length;i++) if(zchilds[i]) ar[ar.length]=zchilds[i];
		ar.sort(function(a,b){return a.zpos-b.zpos});
		for(var i=0;i<ar.length;i++) {
		    ar[i].setZ(x);
		    x+=zstep*(1+(ar[i].nchilds ? ar[i].nchilds : 0));
		}
		z=x;
    }
    lastz=z+(1+nc)*zstep;
    return z;
  }
};
_uWnd.getModalTopZ=function(nc,ignw) {
  if(!nc)nc=0;
  with(_uWnd) {
    var z=lastmodalz;
    if(z<minmodalz) z=minmodalz; else z+=zstep;
    if(z+(1+nc)*zstep>maxmodalz) {
		var x=minmodalz;
		var ar=[];
		for(var i in all) if(all[i] && all[i]!=ignw && all[i].props && !all[i].props.parent && (all[i].props.modal || all[i].props.alert)) ar[ar.length]=all[i];
		ar.sort(function(a,b){return a.zpos-b.zpos});
		for(var i=0;i<ar.length;i++) {
		    ar[i].setZ(x);
		    x+=zstep*(1+(ar[i].nchilds ? ar[i].nchilds : 0));
		}
		z=x;
    }
    lastmodalz=z+(1+nc)*zstep;
    return z;
  }
};
_uWnd.getdims=function() {
    var d=document;
    var s='';
    return {clientW: Math.min(jQuery.browser.opera && window.innerWidth || jQuery(window).width(),jQuery.browser.safari && d.body.clientWidth || jQuery(window).width(),jQuery(window).width()),
	    clientH: Math.min(jQuery.browser.opera && window.innerHeight || jQuery(window).height(),jQuery.browser.safari && d.body.clientHeight || jQuery(window).height(),jQuery(window).height()),
	    clientLeft: jQuery(d).scrollLeft(),
	    clientTop: jQuery(d).scrollTop(),
	    docW: jQuery(d).width(),
	    docH: jQuery(d).height()};
};
_uWnd.csize=function(elem,name) {
    if ( name != "width" && name != "height" ) return 0;
    var val, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
    val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
    var padding = 0, border = 0;
    jQuery.each( which, function() {
	padding += parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
	border += parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
    });
    val -= Math.round(padding + border);
    return Math.max(0, val);
}
_uWnd.close=function(name) {
    var w=_uWnd.getbyname(name);
    if(w) w.close();
}
_uWnd.content=function(name,c) {
    var w=_uWnd.getbyname(name);
    if(w)w.content(c);
}
_uWnd.header=function(name,c) {
    var w=_uWnd.getbyname(name);
    if(w)w.header(c);
}
_uWnd.footer=function(name,c) {
    var w=_uWnd.getbyname(name);
    if(w)w.footer(c);
}
_uWnd.headerheight=function(name,h) {
    var w=_uWnd.getbyname(name);
    if(w)w.headerheight(h);
}
_uWnd.footerheight=function(name,h) {
    var w=_uWnd.getbyname(name);
    if(w)w.footerheight(h);
}
_uWnd.reload=function(name,c) {
    var w=_uWnd.getbyname(name);
    if(w)w.reload(c);
}
_uWnd.setTitle=function(name,t) {
    var w=_uWnd.getbyname(name);
    if(w)w.setTitle(t);
}
_uWnd.activatetopwnd=function(dsk) {
    var maxz=0,maxw=null,a=_uWnd.all;
    for(var i in a) {
		if(!a[i] || (dsk && a[i].desktop!=dsk) || !a[i].state.visible || a[i].state.minimized || a[i].props.alert) continue;
		if(a[i].zpos>maxz){maxz=a[i].zpos;maxw=a[i];}
	}
	if(maxw)maxw.activate();
		else if(dsk) dsk.activate();
};
_uWnd.sysmenu=null;
_uWnd.sysmenuitems=[
	[_txt('Restore'),null,'rest'],
	[_txt('Minimize'),null,'min'],
	[_txt('Maximize'),null,'max'],
	'sep',
	[_txt('Close'),null,'close']	
];
_uWnd._onsysmenuitem=function(idx,m,id) {
	var wnd=m.forwnd;
	if(!wnd)return;
	switch(id) {
		case 'rest':
			if(wnd.state.minimized && wnd.ontaskbar)
				wnd.desktop.wndlist.design.animatewndrestore(wnd.desktop.wndlist,wnd);
				else
				wnd.restore(1);
			break;
		case 'min': 
			wnd.minimize(); 
			break;
		case 'max': 
			if(wnd.state.minimized && wnd.ontaskbar) 
				wnd.desktop.wndlist.design.animatewndmaximize(wnd.desktop.wndlist,wnd); 
				else 
				wnd.maximize(1);
			break;
		case 'close': 
			wnd.closeevent(); 
			break;
	}
};
_uWnd.designs={
    std: {
	sh_sz: [4,2], //design specific shadow config
	altcloseclass: 'xt-close2',

	shadow_init: function(top) {
	    var sh=[];
	    for(var i=0;i<3;i++) {
			sh[i]=document.createElement("div");
			top.appendChild(sh[i]);
			jQuery(sh[i]).attr("class","x-sh").css({position:"absolute",zIndex:1});
	    }
	    jQuery(sh[0]).addClass("xsl").css({width:(this.sh_sz[0]+this.sh_sz[1])+"px",left:(-this.sh_sz[0])+"px",top:"0px"}).html('<div class="xstl"><div class="xsml"></div></div>');
	    jQuery(sh[1]).addClass("xsr").css({width:(this.sh_sz[0]+this.sh_sz[1])+"px",top:"0px"}).html('<div class="xstr"><div class="xsmr"></div></div>');
	    jQuery(sh[2]).addClass("xsb").css({height:(this.sh_sz[0]+this.sh_sz[1])+"px",left:(-this.sh_sz[0])+"px"}).html('<div class="xsbl"><div class="xsbr"><div class="xsbc"></div></div></div>');
	    return sh;
	},
	shadow_resize: function(sh,w,h) {
	    jQuery(sh[0]).css({height:(h-this.sh_sz[1])+"px"});
	    jQuery(sh[1]).css({height:(h-this.sh_sz[1])+"px",left:(w-this.sh_sz[1])+"px"});
	    jQuery(sh[2]).css({width:(w+this.sh_sz[0]*2)+"px",top:(h-this.sh_sz[1])+"px"});
	},
	shadow_hide: function(sh) {
	    jQuery(sh[0]).add(sh[1]).add(sh[2]).hide();
	},
	shadow_show: function(sh) {
	    jQuery(sh[0]).add(sh[1]).add(sh[2]).show();
	},
	custButMargin: 5,
	wnd_init: function(o,wnd,title,align,inith,head,icon,resize,menu) {
		jQuery(wnd).addClass("xw-plain").addClass("x-unselectable");
                if(o.props.alert) jQuery(wnd).addClass("xw-active");
	    if(resize) jQuery(wnd).addClass("xw-resize");
		var custbuts='';
		if(o.props.customButtons) for(var i in o.props.customButtons) 
			custbuts+='<div class="xt xt-'+i+'"></div>';
            jQuery(wnd).html('<div class="xw-disabled" style="display:none;overflow:hidden;position:absolute;z-index:30010"></div>'+
            (head ? '<div class="xw-tl"><div class="xw-tr"><div class="xw-tc">'
+ '<div class="xw-sps"></div><div class="xw-hdr">'
+ '<div class="xt xt-close"></div><div class="xt xt-maxi"></div><div class="xt xt-rest"></div><div class="xt xt-mini"></div>'
+ custbuts
+ (icon ? '<img unselectable="on" onmousedown="return false;" class="xw-icon x-unselectable" src="'+icon+'">' : '')
+ '<span class="xw-hdr-text">'+title+'</span></div></div></div></div>' :
		'<div class="xw-tl"><div class="xw-tr"><div class="xw-tc xw-tsps"></div></div></div>')
+ '<div class="xw-ml"><div class="xw-mr"><div class="xw-mc">'
+ (menu ? '<div class="u-wndmenufr"><div class="u-wndmenu" style="position:relative"></div></div>' : '' )
+ '<div class="xw-body">'
+ (o.props.havegrid ? '<div class="'+o.props.markwaitclass+'" style="display:none;overflow:hidden;position:absolute;z-index:30000"></div>':'')
+ (o.props.headerc ? '<div class="myWinCont myWinHeader" style="overflow:hidden;height:'+o.headerh+'px"></div>' : '')
+ (o.props.actlayer ? '<div class="xw-actlayer" style="overflow:hidden;position:absolute;z-index:30005"></div>':'')
+ '<div style="overflow:scroll;height:'+inith+'px">'
+ '<div class="myWinCont" style="display:none;overflow:hidden"'+(align ? ' align="'+align+'"' : '')+'></div>'
+ '<div class="myWinCont" style="overflow:hidden"'+(align ? ' align="'+align+'"' : '')+'></div></div>'
+ (o.props.footerc ? '<div class="myWinCont myWinFooter" style="overflow:hidden;height:'+o.footerh+'px"></div>' : '')
+ '</div></div></div></div>'
+ '<div class="xw-bl"><div class="xw-br"><div class="xw-bc"></div></div></div>'
+ '<div class="xw-blank" style="display:none"></div>'
	    );
	    var p={},contidx=0;
	    p.markdis=jQuery(wnd).find(".xw-disabled")[0];
	    p.actlayer=jQuery(wnd).find(".xw-actlayer")[0];
	    p.wndmove=jQuery(wnd).find(".xw-blank")[0];
	    p.upper=jQuery(wnd).find(".xw-tl")[0];
	    p.center=jQuery(wnd).find(".xw-ml")[0];
	    p.bottom=jQuery(wnd).find(".xw-bl")[0];
	    p.bottomc=jQuery(wnd).find(".xw-bc")[0];
	    jQuery(wnd).find(".xw-mc").bind("mousedown",o,_uWnd._activateonmousedown);
	    if(o.props.headerc) {
                p.headercont=jQuery(wnd).find(".myWinCont")[contidx];
                jQuery(p.headercont).html(o.props.headerc);
                contidx++;
            }
	    p.markload=jQuery(wnd).find(".myWinCont")[contidx];
	    jQuery(p.markload).html(o.props.markload);
	    p.wndcont=jQuery(wnd).find(".myWinCont")[contidx+1];
	    if(o.props.footerc) {
                p.footercont=jQuery(wnd).find(".myWinCont")[contidx+2];
                jQuery(p.footercont).html(o.props.footerc);
            }
	    if(o.props.havegrid) {
	    	p.markwait=jQuery(wnd).find("."+o.props.markwaitclass)[0];
	    	jQuery(p.markwait).html(o.props.markwaitcont);
	    }
	    p.hwndcont=p.wndcont.parentNode;
	    if(head) {
		p.hdr=jQuery(wnd).find(".xw-hdr")[0];
		p.htitle=jQuery(wnd).find(".xw-hdr-text")[0];
		var buts={cbut:"xt-close",mbut:"xt-mini",xbut:"xt-maxi",rbut:"xt-rest",icon:"xw-icon"};
		for(var i in buts) {
		    p[i]=jQuery(p.hdr).find("."+buts[i])[0];
		}
		p.custom={};
		for(var i in o.props.customButtons) {
			p.custom[i]=jQuery(p.hdr).find(".xt-"+i)[0];
		}
	    } else {
		p.hdr=p.htitle=p.cbut=p.mbut=p.xbut=p.rbut=p.icon=null;
	    }
	    if(menu) p.menu=jQuery(wnd).find(".u-wndmenu")[0]; else p.menu=null;
	    jQuery(wnd).find("div,span").andSelf().attr("unselectable","on");
	    return p;
	},
	onstartautosz: function(o) {
//		o.parts.hwndcont.style.overflow="hidden";
		o.parts.wndcont.style.overflow="hidden";
		o.parts.wndcont.style.height="auto";
	},
	onstopautosz: function(o,onlyscroll) { //onlyscroll true to request restoration if scrool behavior only
//		o.parts.hwndcont.style.overflowY="auto";
		if(!onlyscroll) o.parts.hwndcont.style.visibility="";
		o.parts.wndcont.style.overflow="auto";
		o.parts.wndcont.style.height="100%";
	},
	get_szbuts: function(wnd) {
	    var rs={nw:"xw-tl",n:"xw-sps",ne:"xw-tr",w:"xw-ml",e:"xw-mr",sw:"xw-bl",s:"xw-bc",se:"xw-br"};
	    for(var i in rs) {
			rs[i]=jQuery(wnd).find("."+rs[i])[0];
	    }
	    if(!rs.n) rs.n=jQuery(wnd).find(".xw-tsps")[0];
	    return rs;
	},
	onbuttonover: function(e) {
		var cls=e.data.cls;
		if(e.data.state)
	    	jQuery(this).addClass(cls+'-over');
	    else
	    	jQuery(this).removeClass(cls+'-over');
	},
	onstartdrag: function(o) {
    	jQuery(o.wnd).addClass('xw-dragging');
//	    if(jQuery.browser.msie) o.parts.upper.style.filter="Alpha(Opacity='70')";
//		else o.parts.upper.style.opacity=0.7;
	    if(!o.state.minimized && o.props.hideonmove) {
		o.hideSh();
		jQuery(o.parts.center).add(o.parts.bottom).hide();
		jQuery(o.parts.wndmove).css('width',o.width+'px').css('height',(o.height-o.decor.th)+'px').show();
	    }
	},
	onstopdrag: function(o) {
    	jQuery(o.wnd).removeClass('xw-dragging');
//	    if(jQuery.browser.msie) o.parts.upper.style.filter='';//"Alpha(Opacity='100')";
//		else o.parts.upper.style.opacity=1;
	    if(!o.state.minimized && o.props.hideonmove) {
		o.showSh();
		jQuery(o.parts.center).add(o.parts.bottom).show();
		jQuery(o.parts.wndmove).hide();
	    }
	},
	ondisable: function(o,st) { //st=false to enable
		if(!st) { //show enabled
			jQuery(o.parts.markdis).hide();
			if(o.state.focused || o.props.alert) {jQuery(o.wnd).addClass("xw-active");}
		}
		else { //show disabled
			jQuery(o.parts.markdis).css('width',Math.abs(o.width)+'px').css('height',Math.abs(o.height)+'px').show();
			jQuery(o.wnd).removeClass("xw-active");
		}
	},
	onfocus: function(o,st) {
		if(st && !o.state.disabled) {jQuery(o.wnd).addClass("xw-active");}
			else {jQuery(o.wnd).removeClass("xw-active");}
	},
	onstartresize: function(o) {
	},
	onstopresize: function(o) {
	},
	onminimize: function(o) {
	    jQuery(o.parts.center).css("display","none");
	    jQuery(o.parts.bottomc).addClass("xw-bcm");
	    o.hideSh();
	},
	onrestore: function(o) {
	    jQuery(o.parts.bottomc).removeClass("xw-bcm");
	    jQuery(o.parts.center).css("display","block");
	    o.showSh();
	}
    }	
};

_uWnd.prototype={
showsysmenu: function(x,y) {
	this.activate();
	var m=_uWnd.sysmenu,i;
	if(!m) return;
	if((i=m.indexById('rest'))>=0) {
		if((this.state.minimized && (this.props.max || this.state.beforemin!='max')) || (this.props.max && this.state.maximized)) {
			jQuery(m.parts.elems[i]).removeClass("u-graymenuitem");
			m.elems[i][1]=null;
		} else {
			jQuery(m.parts.elems[i]).addClass("u-graymenuitem");
			m.elems[i][1]=1;
		}
	}
	if((i=m.indexById('min'))>=0) {
		if(this.props.min && !this.state.minimized) {
			jQuery(m.parts.elems[i]).removeClass("u-graymenuitem");
			m.elems[i][1]=null;
		} else {
			jQuery(m.parts.elems[i]).addClass("u-graymenuitem");
			m.elems[i][1]=1;
		}
	}
	if((i=m.indexById('max'))>=0) {
		if((this.props.max || (this.state.minimized && this.state.beforemin=='max')) && !this.state.maximized) {
			jQuery(m.parts.elems[i]).removeClass("u-graymenuitem");
			m.elems[i][1]=null;
		} else {
			jQuery(m.parts.elems[i]).addClass("u-graymenuitem");
			m.elems[i][1]=1;
		}
	}
	m.forwnd=this;
	_uWnd.globalmousedown();
	m.show({pos:{x:x,y:y},parentfocus:this._focus});
},
_foreachchild: function(func,cfirst) { //foreach child executes parent.func(child) and makes recursive call (if cfirst is false) or in reverse order (if cfirst is true)
//when cfirst is false, then func() return value shows whether to stop recursion for that window
	var c;
	for(var i=0;i<this.childs.length;i++) {
		c=this.childs[i];
		if(!c || c.state.destroyed) continue;
		if(cfirst) {
			c._foreachchild(func,cfirst);
			func.call(this,c);
		} else {
			if(!func.call(this,c))
				c._foreachchild(func,cfirst);
		}
	}
},
setZ:function(z) {
  with(this){
    zpos=z;
	var c;
	for(var i=0;i<this.childs.length;i++) {
		c=this.childs[i];
		if(!c || c.state.destroyed) continue;
		z+=_uWnd.zstep;
		c.setZ(z);
		z+=_uWnd.zstep*c.nchilds;
	}    
    jQuery(top).css("z-index",zpos);
    if(grid)jQuery(grid).css("z-index",zpos-1);
  }
},
saveSession: function() { //return data sutable to restore window state
    var r;
    if(this.state.maximized || this.state.minimized) r=this.restRect;
        else r=[this.xpos,this.ypos,this.width,this.height];
    return {
                x:r[0],
                y:r[1],
                w:r[2]-this.decor.w,
                h:r[3]-this.decor.h,
                bm:this.state.minimized ? this.state.beforemin : '',
                s:this.state.maximized ? 'max' : (this.state.minimized ? 'min' : '')
	};
    this.sesupdate=false;
},
moveTo:function(_x,_y,nopos) { //nopos - do not update session (used during init, maximize etc)
    var d=_uWnd.getdims();
  with(this) {
    if(_x=='auto' && _y=='auto' && desktop && !props.fixed && !props.autosize && !props.center) {
    	var p=desktop.getwndcoord(width,height);
    	_x=p.x;
    	_y=p.y;
    } else {
	    if(_x=='auto') {
			if(props.parent) _x=Math.floor((props.parent.width-width)/2)+props.parent.xpos;
				else if(desktop && !props.fixed) _x=Math.floor((desktop.calcwidth()-width)/2)+desktop.calcexclude(0);
				else _x=Math.floor((d.clientW-width)/2)+(props.fixed?0:d.clientLeft);
	    	if(_x<0) _x=0;
	    }
	    if(_y=='auto') {
			if(props.parent) _y=Math.floor((props.parent.height-height)/2)+props.parent.ypos;
				else if(desktop && !props.fixed) _y=Math.floor((desktop.calcheight()-height)/2)+desktop.calcexclude(2);
	    		else _y=Math.floor((d.clientH-height)/2)+(props.fixed?0:d.clientTop);
	    	if(_y<0) _y=0;
	    }
	}
    if(props.fixed && jQuery.browser.msie && parseFloat(jQuery.browser.version)<=8) jQuery(top).css("left",(d.clientLeft+_x)+'px').css("top",(d.clientTop+_y)+'px');
	else jQuery(top).css("left",_x+'px').css("top",_y+'px');
    if(grid) 
	if(jQuery.browser.msie && parseFloat(jQuery.browser.version)<=8) jQuery(grid).css("left",d.clientLeft+'px').css("top",d.clientTop+'px');
	    else jQuery(grid).css("left",'0px').css("top",'0px');
    xpos=_x;
    ypos=_y;
    if(!nopos) sesupdate=1;
    if(this.props.onposchange) this.props.onposchange.apply(this.app,[this]);
  }
},
setTitle:function(t) {
    if(t!=null)this.title=t;
    if(!this.title)this.title='';
  with(this) {
    if(!props.header) return;
    var ts=width-decor.w,l;
    ts-=_countbuttonwidth();
    l=title.length;
    if(l*letsize>ts) l=Math.floor(ts/letsize)-2;
    if(l<title.length) 
		jQuery(parts.htitle).attr("title",title).text(title.substr(0,l)+'...');
		else
		jQuery(parts.htitle).attr("title",'').text(title);
	if(ontaskbar) desktop._onsetwindowtitle(this);
  }
},
resizeTo:function(w,h,center,nopos) { //nopos - do not notify of pos change (used during init, maximize etc)
  with(this) {
  	if(w<0) w=(-w)+decor.w;//content size was specified  
  	if(h<0) h=(-h)+decor.h;//content size was specified  
    if(center) {
        var d=_uWnd.getdims();
		var _x=xpos,_y=ypos;
	    if(props.x=='auto') {
			if(props.parent) _x=Math.floor((props.parent.width-w)/2)+props.parent.xpos;
				else if(desktop && !props.fixed) _x=Math.floor((desktop.calcwidth()-w)/2)+desktop.calcexclude(0);
				else _x=Math.floor((d.clientW-w)/2)+(props.fixed?0:d.clientLeft);
	    	if(_x<0) _x=0;
	    }
	    if(props.y=='auto') {
			if(props.parent) _y=Math.floor((props.parent.height-h)/2)+props.parent.ypos;
				else if(desktop && !props.fixed) _y=Math.floor((desktop.calcheight()-h)/2)+desktop.calcexclude(2);
	    		else _y=Math.floor((d.clientH-h)/2)+(props.fixed?0:d.clientTop);
	    	if(_y<0) _y=0;
	    }
		if(_x!=xpos | _y!=ypos) moveTo(_x,_y,1);
    }
    jQuery(wnd).css("width",w+'px');
    jQuery(parts.wndcont).css("width",(w-decor.w)+'px');
    jQuery(parts.hwndcont).css("height",(h-decor.h)+'px');
    if(parts.markwait) jQuery(parts.markwait).css({height:(h-decor.h+footerh+headerh)+'px',width:(w-decor.w)+'px'});
    if(parts.actlayer) jQuery(parts.actlayer).css({height:(h-decor.h)+'px',width:(w-decor.w)+'px'});
    if(parts.markdis) jQuery(parts.markdis).css("width",w+'px').css("height",h+'px');

    if(menu) jQuery(parts.menu).css("width",(w-decor.w)+'px');

    if(tabctrl) tabctrl.resizeTo(w-decor.w,h-decor.h);
    if(frame) jQuery(frame).css("width",w+'px').css("height",h+'px');
    width=w;
    height=h;
    if(!nopos && !this.state.maximized && !this.state.minimized) sesupdate=1;
    if(this.props.onposchange && !this.state.maximized && !this.state.minimized) this.props.onposchange.apply(this.app,[this]);
    _resizeSh();

    setTitle();
  }
},
_onresize:function(d) {
    if(this.state.maximized) 
		if(this.desktop && !this.props.fixed) {
		    this.moveTo(this.desktop.calcexclude(0),this.desktop.calcexclude(2),1);
	    	this.resizeTo(this.desktop.calcwidth(),this.desktop.calcheight(),0,1);
		} else this.resizeTo(d.clientW,d.clientH);
    if(this.grid) jQuery(this.grid).css("width",d.clientW+'px').css("height",d.clientH+'px');
},
_resizeSh:function() {
    if(this.sh)this.design.shadow_resize(this.sh,Math.abs(this.width),Math.abs(this.height));
},
hideSh:function() {
    if(this.sh)this.design.shadow_hide(this.sh);
},
showSh:function() {
    if(this.sh && !this.state.noshadow)this.design.shadow_show(this.sh);
},
shadow_init:function() {
    this.sh=this.design.shadow_init(this.top);
},
showcustombutton: function(name,state) {
	if(!this.props.customButtons[name]) return;
	this.props.customButtons[name][0]=state;
	if(!this.parts.custom[name]) return; //window not initted yet
	if(!state) jQuery(this.parts.custom[name]).css("display","none");
		else jQuery(this.parts.custom[name]).css("display","block");
},
init: function(menuitems) {
    var p=this.props,t=document.createElement("div"),w;
//top parent for window and optional shadow
	t.id="_uwndTop"+this.idx;
	t._uwndobj=this;
    if(!this.desktop) jQuery(jQuery("body")[0]).prepend(t);
    	else this.desktop.dsk.appendChild(t);
    this.top=t;
    if(!p.fixed || (jQuery.browser.msie && parseFloat(jQuery.browser.version)<=8))	jQuery(t).css("position","absolute"); else jQuery(t).css("position","fixed");
    jQuery(t).css("visibility",'hidden').css("display",'block').css("z-index",this.zpos);
//grid for system modal windows
    if (p.modal && !p.parent){
		var g=document.createElement("div"),d=_uWnd.getdims();
		jQuery(g).addClass('myWinGrid').css("width",d.clientW+'px').css("height",d.clientH+'px').css("z-index",this.zpos-1).hide().bind('mousedown',function(e){e.stopPropagation();e.preventDefault();_uWnd.globalmousedown();});
		if(jQuery.browser.msie && parseFloat(jQuery.browser.version)<=8)	jQuery(g).css("position","absolute"); else jQuery(g).css("position","fixed");
	    if(!this.desktop) jQuery(jQuery("body")[0]).prepend(g);
	    	else this.desktop.dsk.appendChild(g);
		this.grid=g;
    }
    if(jQuery.browser.msie && parseFloat(jQuery.browser.version)<7){
        this.frame=document.createElement("iframe");
        with(this.frame.style) {
            filter="Alpha(Opacity='0')";
            display="block";
            position="absolute";
//            zIndex=0;
            borderWidth=0;
            width=Math.abs(this.width)+'px';
            height=Math.abs(this.height)+'px';
        }
	t.appendChild(this.frame);
    }

    if(p.shadow && !(jQuery.browser.msie && parseFloat(jQuery.browser.version)<7)) {
		this.shadow_init();
		this._resizeSh();
    }

    this.moveTo(0,0,1);
//window
    w=document.createElement("div");
    w.id="_uwndWnd"+this.idx;
    t.appendChild(w);
    this.wnd=w;
    jQuery(w).css({position: "absolute",width: Math.abs(this.width)+'px',zIndex:2,left:0});

	if(!_uWnd._activateonmousedown) _uWnd._activateonmousedown=function(e){e.stopPropagation();e.data.activate(e);_uWnd.globalmousedown();};
	if(!_uWnd._activateonmousedownign) _uWnd._activateonmousedownign=function(e){e.data.activate(e);_uWnd.globalmousedown();};
    this.parts=this.design.wnd_init(this,w,this.title,p.align,60,p.header,p.icon,p.resize,p.havemenu);
    if(p.header) {
		var bb={cbut:"xt-close",mbut:"xt-mini",xbut:"xt-maxi",rbut:"xt-rest"};
		if(!_uWnd._retfalse) _uWnd._retfalse=function(e){return false;};
		if(!_uWnd._onclickcustom) _uWnd._onclickcustom=function(e){var d=e.data;d.wnd.activate(e);d.func.call(d.thispar,d.wnd,d.param);};
		if(!_uWnd._onbuttonclose) _uWnd._onbuttonclose=function(e){e.data.activate(e);e.data.closeevent();};
		if(!_uWnd._onbuttonmin) _uWnd._onbuttonmin=function(e){e.data.activate(e);e.data.minimize()};
		if(!_uWnd._onbuttonmax) _uWnd._onbuttonmax=function(e){e.data.activate(e);e.data.maximize()};
		if(!_uWnd._onbuttonrest) _uWnd._onbuttonrest=function(e){e.data.activate(e);e.data.restore()};
		//set common event handlers for system buttons
		for(var i in bb)
		    jQuery(this.parts[i]).bind("dblclick",_uWnd._retfalse).bind("mouseover",{cls:bb[i],state:1},this.design.onbuttonover).bind("mouseout",{cls:bb[i],state:0},this.design.onbuttonover).bind("mousedown",this,_uWnd._activateonmousedown);
		//set event handlers for custom buttons
		for(var i in p.customButtons) {
		    jQuery(this.parts.custom[i]).bind("dblclick",_uWnd._retfalse).bind("mouseover",{cls:'xt-'+i,state:1},this.design.onbuttonover).bind("mouseout",{cls:'xt-'+i,state:0},this.design.onbuttonover).bind("mousedown",this,_uWnd._activateonmousedown);
			if(!p.customButtons[i][0]) jQuery(this.parts.custom[i]).css("display","none");
			if(p.customButtons[i][1]) jQuery(this.parts.custom[i]).bind("click",{wnd:this,func:p.customButtons[i][1],thispar:p.customButtons[i][2],param:(p.customButtons[i][3] || i)},_uWnd._onclickcustom); //e.stopPropagation
		}
		//set specific event handlers for system buttons
		if(!p.close) jQuery(this.parts.cbut).css("display","none");
		jQuery(this.parts.cbut).bind("click",this,_uWnd._onbuttonclose); //e.stopPropagation
		if(!p.min) jQuery(this.parts.mbut).css("display","none");
		jQuery(this.parts.mbut).bind("click",this,_uWnd._onbuttonmin); //e.stopPropagation
		if(!p.max) jQuery(this.parts.xbut).css("display","none");
		jQuery(this.parts.xbut).bind("click",this,_uWnd._onbuttonmax); //e.stopPropagation
		jQuery(this.parts.rbut).css("display","none").bind("click",this,_uWnd._onbuttonrest); //e.stopPropagation
		if(p.close && !p.min && !p.max && this.design.altcloseclass) {//use alternative class
			jQuery(this.parts.cbut).addClass(this.design.altcloseclass);
		}

		if(!_uWnd._onclicktodrag) _uWnd._onclicktodrag=function(e){
			e.stopPropagation();
			e.data.activate(e);
			_uWnd.globalmousedown();return e.data._ondragmousedown(e);};
		if(!_uWnd._ondclicktomax) _uWnd._ondclicktomax=function(e){var a=e.data;if((a.state.maximized && a.props.max) || a.state.minimized)a.restore();else a.maximize();};

		if(!p.nomove) jQuery(this.parts.hdr).addClass("xw-draggable");
		jQuery(this.parts.hdr).add(this.parts.htitle).bind("mousedown",this,_uWnd._onclicktodrag);

		if(p.max || p.min) jQuery(this.parts.hdr).bind("dblclick",this,_uWnd._ondclicktomax);
    }
	//set event handlers for size box
    var rs=this.design.get_szbuts(w);
    if(!_uWnd._onresizebuttondown) _uWnd._onresizebuttondown=function(e){e.stopPropagation();e.data.w.activate(e);_uWnd.globalmousedown();return e.data.w._onrsmousedown(e,e.data.tp);};
    for(var i in rs) {
		jQuery(rs[i]).bind("mousedown",{w:this,tp:i},_uWnd._onresizebuttondown);
    }
//    jQuery(this.parts.wndcont).bind("mousedown",this,_uWnd._activateonmousedown); ????
//    jQuery(w).bind("mousedown",this,_uWnd._activateonmousedownign); ????
    jQuery(w).bind("mousedown",this,_uWnd._activateonmousedown);
	if(p.havemenu) {
		var mopts=jQuery.extend({
				parentnode:this.parts.menu,
				wnd:this,
				noabs:1,
				horiz:1,
				statical:1,
				width:'auto'
			},p.menuopts || {});
		this.menu=new _uMENU('',{},mopts,menuitems,true);
	}
    if(typeof this._content == 'object' && this._content.constructor==_uTabCtrl) {
    	this.tabctrl=this._content;
    	this.tabctrl.props.parentnode=this.parts.wndcont;
    	this.tabctrl.wnd=this;
    	this.tabctrl.init(true); //tab must be created with noinit property
    } else this.tabctrl=null;
	
	if(p.trayicon && this.desktop) {
		var to={};
		if(p.trayicon.contructor==String) to.img=p.trayicon;
			else if(p.trayicon.contructor!=Object) if(p.icon)to.img=p.icon;
		if(to.img) {
			if(!p.trayicon.thisobj) { //set standard handlers
				to.thisobj=this;
				if(!p.trayicon.ondown) to.ondown=this.ontrayicondown;
				if(!p.trayicon.onrdown) to.onrdown=this.ontrayiconrdown;
				to.param=p.trayicon.param;
			} else jQuery.extend(to,p.trayicon);
			if(!to.title) to.title=this.title || '';
			this.trayicon=this.desktop.addTrayIcon(to.img,to.title,to.thisobj,to.ondown,to.onclick,to.onrdown,to.param);
			if(p.traymenu) {
				this.traymenu=new _uMENU('',{align:'U'},{hidden:1},p.traymenu);
			}
		}
	}
    if(!p.hidden) this.show(false);
    if(this.desktop && !p.popup && !p.alert && p.header && !p.notaskbar) {
    	this.ontaskbar=true;
    	this.desktop._addwindow(this);
    } else this.ontaskbar=false;
    if(p.modal && p.parent) { //disable parent
    	p.parent.disable(1);
    }
    if(this.props.parent) {
    	this.props.parent._onnewchild(this);
    }
    this.activate();
    if(p.header || this.menu) setTimeout("var w=_uWnd.all["+this.idx+"];if(w)w.init1();",10);
	else setTimeout("var w=_uWnd.all["+this.idx+"];if(w)w.init2();",10);
},
init1: function() {
	if(this.props.header) {
  		if(!this.title) this.letsize=8;
    		else
  			with(this) {
    			if(title.length==0) letsize=8;
				else letsize=parts.htitle.offsetWidth/title.length;
    			parts.htitle.innerHTML='...';
  			}
  	}
	if(this.menu) this.menu.init1(true);
    setTimeout("var w=_uWnd.all["+this.idx+"];if(w)w.init2();",10);
},
init2: function() {
  with(this) {
    decor.sbw=parts.hwndcont.offsetWidth-parts.hwndcont.clientWidth; //scroll bar width
    decor.sbh=parts.hwndcont.offsetHeight-parts.hwndcont.clientHeight; //scroll bar width
    decor.w=Math.abs(width)-_uWnd.csize(parts.wndcont,"width")-decor.sbw;
    decor.h=wnd.offsetHeight-60;
    decor.th=parts.upper.offsetHeight;
    parts.hwndcont.style.overflow='hidden';
    if(tabctrl)tabctrl.init1(true); 
    else { //as content() won't be called for tabctrl, enable footer and header here
        if(parts.headercont) parts.headercont.style.visibility='hidden';
        if(parts.footercont) parts.footercont.style.visibility='hidden';
    }
    if(props.session)
        resizeTo(props.session.w+decor.w,props.session.h+decor.h,0,1);
    else
        resizeTo(width,height,0,1);
    moveTo(props.x,props.y,1);
    if(menu)menu._setsize();

    state.init=true;
    var rel=pend_show ? pend_show[0] : false;
    if(props.oninit) if(!props.oninit.call(app,this,name)) rel=false;
    jQuery(top).css("display",'none').css("visibility",'');
    var s=props.session;
    s=(s && s.s) || props.initstate; //initial state state
    if(s=='min') {
            minimize(1);
            state.beforemin=(s && s.bm) || '';
            if(!props.max && props.initstate=='max') state.beforemin='max';
        }
    	else if(s=='max') maximize(0,1);
    	else
    	if(pend_show) show(rel,pend_show[1],pend_show[2]);
    sesupdate=0; //just in case
  }
},
disable: function(v,tmp) { //false to enable, true to disable; tmp - flag that this state change is temporary and must not modify state flag
//console.log("in disable for "+descr(this)+'value '+v+', tmp '+tmp);
	var c;
	if(!tmp) {
		if(v) {
			if(!this.state.disabled) { //disable all direct children
				for(var i=0;i<this.childs.length;i++) {
					c=this.childs[i];
					if(!c || c.state.destroyed || c.props.alert) continue;
					c.state.prevdisabled=c.state.disabled;
					c.disable(1);
				}
			}
			this.state.disabled=true;
		}
		else {
			if(this.state.disabled) { //restore enabled state of all direct children
				for(var i=0;i<this.childs.length;i++) {
					c=this.childs[i];
					if(!c || c.state.destroyed || c.props.alert || typeof c.state.prevdisabled=='undefined') continue;
					c.disable(c.state.prevdisabled);
				}
			}
			this.state.disabled=false;
		}
	}
	this.design.ondisable(this,v);
},
_onkeypress: function(e,un,t) {
    if(!this.state.disabled && this.state.visible && !this.props.toolwindow) { //opera reacts to keypress
		if(e.keyCode==115 && e.ctrlKey && e.shiftKey) { //CTRL-SHIFT-F4
			e.preventDefault();
			return;
		}
    }
},
_onkeydown: function(e,un,t) {
    if(!this.state.disabled && this.state.visible && !this.props.toolwindow) {
		if((e.keyCode==115 && e.ctrlKey && e.shiftKey) || (e.keyCode==27 && this.props.closeonesc)) { //CTRL-SHIFT-F4 or ESC
			this.closeevent();
			return 1;
		}
		if(this.menu && (e.keyCode==17 || e.keyCode==18) && e.ctrlKey && e.altKey && t.owner.constructor!=_uMENU) { //ctrl+alt
			_uWnd.globalmousedown();			
			this.menu.show(null,0);
			this.menu._focus.activate();
			e.preventDefault();
			e.stopPropagation();
			return 1;
		}
	  	if(this.props.header && this.parts.hdr && e.keyCode==32 && e.ctrlKey && !this.state.resizing) { //ctrl space
	  		var of=jQuery(this.parts.hdr).offset();
	  		this.showsysmenu(of.left,of.top+this.parts.hdr.offsetHeight);
			e.preventDefault();
			e.stopPropagation();
	  		return 1;
	  	}
    }
},
_ondeactivate: function(un,t,newf) {
//console.log("ondeactivate in "+descr(this)+", target "+descr(t)+", newfocus "+descr(newf));
//	if(t!=this._focus) return; //ifnore deactivation of child focuses
	if(this.props.toolwindow) { //do nothing if our first non-tool parent remains active
		var p=this.props.parent;
		while(p && p.props.toolwindow) {
			p=p.props.parent;
		}
		if(p && p._focus.isparentof(newf)) return;
	}
	this.state.focused=0;
	this.design.onfocus(this,0);
    if(this.props.ondeactivate) this.props.ondeactivate.apply(this.app,[this]);
	if(!this.props.toolwindow) {
		this._foreachchild( //show all child tool windows as deactivated
			function(c) {
				if(!c.props.toolwindow)return 1;
				c.state.focused=0;
				c.design.onfocus(c,0);
		},0);
	}

},
_canactivate: function(un,old,t) {
//console.log("canactivate in "+descr(this)+", target "+descr(t)+", oldfocus "+descr(old)+',' +this.state.disabled);
//    if(t==this._focus && this.state.disabled) {
//    	console.log('false');
//    	return 0;
//    } //forbid to activate any child focus
	if(this.props.alert || this.state.minimized) return 0;

//    console.log('true');
    return 1;
},
activate: function(e,frommenu) {
//console.log("activate() in "+descr(this)+", event "+e);
	if(this.menu && !frommenu && this.menu._focus.isactive()) {
		this.menu._focus.deactivate(); //deactivate menu if some other focus in window is activated
	}
	if(this.props.alert) return;
	if(this.props.modal && this.props.parent)
		this.props.parent.setforeground();
	return this._focus.activate();
},
_onactivate: function(un,old,t) {
//console.log("onactivate in "+descr(this)+", target "+descr(t)+", oldfocus "+descr(old));
    if(t!=this._focus) {
//console.log('show deactivated');
//		this.design.onactivate(this,0);
    	return; //show as noactive if child activates
    }
    if(!this.state.disabled) {
	    this.state.focused=1;
		this.design.onfocus(this,1);
	    if(this.props.onactivate) this.props.onactivate.apply(this.app,[this]);
		if(!this.props.toolwindow) {
			this._foreachchild( //show all child tool windows as activated
				function(c) {
					if(!c.props.toolwindow)return 1;
					c.state.focused=1;
					c.design.onfocus(c,1);
			},0);
		}
	} else { //try to find enabled direct child
		var c;
		for(var i=0;i<this.childs.length;i++) {
			c=this.childs[i];
			if(!c || c.state.destroyed || c.state.disabled || c.props.alert) continue;
			this.setforeground();
			c.activate();
			return -1;
		}
		return;
	}
	this.setforeground();
},
setforeground: function() {
	if((this.props.modal && !this.props.parent) || this.props.alert) {
	    if(this.zpos+this.nchilds*_uWnd.zstep!=_uWnd.lastmodalz)
    		this.setZ(_uWnd.getModalTopZ(this.nchilds));
	} else {
	    if(this.zpos+this.nchilds*_uWnd.zstep!=_uWnd.lastz)
    		this.setZ(_uWnd.getTopZ(this.nchilds));
	}
},
ontrayicondown:function(anim,icon,e) {
	if(!this.state.visible && !this.state.minimized) if(anim) this.show(false); else this.show(false,0,0);
		else if(this.state.minimized) {
			if(this.state.beforemin=='max')this.maximize(1);
				else this.restore(1);
		} else if(this.props.min && !this.props.notaskbar) this.minimize();
			else if(anim) this.hide(); else this.hide(0,0);

},
ontrayiconrdown:function(pp,icon,e) {
	if(!this.traymenu) return;
	this.traymenu.show({pos:{x:e.pageX,y:e.pageY}});
	_uMENU.ignoreclick=this.traymenu;
},
show: function(rel,fadetp,fadesp) {
  if(arguments.length<2 || fadetp==undefined) fadetp=this.props.fadetype;
  if(arguments.length<3 || fadesp==undefined) fadesp=this.props.fadespeed;
if(!_uWnd._onshowanimend) _uWnd._onshowanimend=function() { //this points to top or wnd
	var w=_uWnd.findparent(this);if(!w)return;
	w.disable(w.state.disabled,1); //restore real disabled state
	w.showSh();
};
  with(this) {
    if(!state.init) {pend_show=[rel,fadetp,fadesp];return;}
	if(state.visible) return;
    state.visible=true;
    if(grid) jQuery(grid).show();
    if(fadetp==1) {
		disable(1,1);
		if(jQuery.browser.safari) hideSh();
		if(jQuery.browser.msie) {
//		    state.noshadow=true;
		    hideSh();
	    	jQuery(wnd).hide();
	    	jQuery(top).show();
	    	jQuery(wnd).css("opacity","0").show().animate( {opacity: 1} , {duration:fadesp,complete:_uWnd._onshowanimend});
		} else jQuery(top).fadeIn(fadesp,_uWnd._onshowanimend);
    } else if(fadetp==2) {
		disable(1,1);
		var endy=parseInt(jQuery(top).css("top"));
		jQuery(top).css("top", (endy+(height>100 ? 100 : height))+'px');
		if(jQuery.browser.msie) {
//		    state.noshadow=true;
		    hideSh();
		    jQuery(wnd).hide();
		    jQuery(top).show().animate( {top: endy+"px"} , {duration:fadesp,complete:_uWnd._onshowanimend});
		    jQuery(wnd).css("opacity","0").show().animate( {opacity: 1} , fadesp);
		} else jQuery(top).css("opacity","0").show().animate( {top: endy+"px", opacity: 1} , {duration:fadesp,complete:_uWnd._onshowanimend});
    } else {
        if(props.nohide) jQuery(top).css({visibility:''});
        jQuery(top).show();
        disable(state.disabled); //restore disabled state as hide() does not restore it
    }
    if(!state.loaded || rel) load();
  }
},
hide: function(fadetp,fadesp,doclose) {
    if(arguments.length<1 || fadetp==undefined) fadetp=this.props.fadeclosetype;
    if(arguments.length<2 || fadesp==undefined) fadesp=this.props.fadeclosespeed;
if(!_uWnd._onhideanimendcl) _uWnd._onhideanimendcl=function() { //this points to top or wnd
	var w=_uWnd.findparent(this);if(!w)return;
	w.close(1);
};
  with(this) {
	if(!state.visible) return 0;
    state.visible=false;
    if(fadetp==1) {
	    disable(1,doclose ? 0 : 1); //if doclose, then disable permanently
		if(jQuery.browser.msie) {
//		    state.noshadow=true;
		    hideSh();
		    jQuery(wnd).animate( {opacity: 0} , {duration:fadesp,complete:doclose ? _uWnd._onhideanimendcl : null});
		} else jQuery(top).fadeOut(fadesp, doclose ? _uWnd._onhideanimendcl : null);
	}
    else if(fadetp==2) {
	    disable(1,doclose ? 0 : 1);
		var endy=parseInt(jQuery(top).css("top"))+(height>100 ? 100 : height);
		if(jQuery.browser.msie) {
//		    state.noshadow=true;
		    hideSh();
		    jQuery(top).animate( {top: endy+"px"} , {duration:fadesp,complete:doclose ? _uWnd._onhideanimendcl : null});
		    jQuery(wnd).animate( {opacity: 0} , fadesp);
		} else jQuery(top).animate( {top: endy+"px", opacity: 0} , {duration:fadesp,complete:doclose ? _uWnd._onhideanimendcl : null});
    } 
    else {
        if(props.nohide) jQuery(top).css({visibility:'hidden'});
            else jQuery(top).hide();
        doclose=false;
    }
    if(grid) jQuery(grid).hide();
	if(_focus.isactive()) {
		_focus.deactivate();
		_uWnd.activatetopwnd(this.desktop);
	}
  }
    if(doclose)return 1;
    return 0;
},
markload: function() {
    with(this) {
        parts.wndcont.style.display='none';
        parts.markload.style.display='block';
        if(parts.headercont && props.hideheader) parts.headercont.style.visibility='hidden';
        if(parts.footercont && props.hidefooter) parts.footercont.style.visibility='hidden';
    }
},
showgrid: function() {
	if(this.parts.markwait) this.parts.markwait.style.display='block';
},
hidegrid: function() {
	if(this.parts.markwait) this.parts.markwait.style.display='none';
},
reload: function(c) {
    this._content=c;
    if(typeof c == 'object' && c.constructor==_uTabCtrl)
    	this.tabctrl=c;
    	else
    	this.tabctrl=null;
    this.state.loaded=false;
//    this.disable(1); ????
    this.load();
    if(!this.props.hidden && !this.state.visible) this.show(false);
},
load: function() {
    if(this.tabctrl) return;
    var c=this._content;
    if(typeof (c) == 'string') this.content(c);
    else if(typeof c == 'function') this.content(c.apply(this.app,[this]));
    else if(typeof c == 'object') {
	this.markload();
        if(!c.data)c.data={};
        if(!('_wi' in c.data)) c.data._wi=this.idx;
        if(this.app && !('_ai' in c.data)) c.data._ai=this.app.pid;
        if(this.app && !('app' in c)) c.app=this.app.pid;
        if(!('wnd' in c)) c.wnd=this.idx;
	if(!c.success && c.xml===false) {c.dataType='text';c.success=new Function("data","st","var w=_uWnd.all["+this.idx+"];if(w)w.content(data);");}
	    else if(!c.success && c.xml!==false) c.success=new Function("data","st","var w=_uWnd.all["+this.idx+"];_uParseXML(data,w,0,w.app);");
	if(!c.error) c.error=new Function("xml","st","er","var w=_uWnd.all["+this.idx+"];if(w)w._onerror(xml,st,er);");
	try {
	if(c.form && c.form.length>0) //have form
	    _uPostForm(c.form,c);
	    else if(c.url) _uAjaxRequest(c.url,c);
	} catch(e) {this._onerror(null,'',e);}
    }
},
_onerror: function(xml,st,er) {
    var o=this.props.onerror;
    if(o && typeof(o)=='function') o.apply(this,arguments);
	else this.close();	
},
header: function(fc) {
    var f=this.parts.headercont;
    if(!f) return;
    if(fc) jQuery(f).html(fc); else jQuery(f).html('');
},
footer: function(fc) {
    var f=this.parts.footercont;
    if(!f) return;
    if(fc) jQuery(f).html(fc); else jQuery(f).html('');
},
headerheight: function(fh) {
    if(!this.parts.headercont || typeof(fh)!='number' || isNaN(fh)) return;
    var prev=this.headerh;
    if(prev==fh) return;
    this.decor.h+=fh-prev;
    this.headerh=fh;
    jQuery(this.parts.headercont).css("height",(fh)+'px');
    this.resizeTo(this.width,this.height+(this.props.contentsizeprio ? fh-prev : 0));
},
footerheight: function(fh) {
    if(!this.parts.footercont || typeof(fh)!='number' || isNaN(fh)) return;
    var prev=this.footerh;
    if(prev==fh) return;
    this.decor.h+=fh-prev;
    this.footerh=fh;
    jQuery(this.parts.footercont).css("height",(fh)+'px');
    this.resizeTo(this.width,this.height+(this.props.contentsizeprio ? fh-prev : 0));
},
_checkimgload: function(load) { //if we wait for all images to be loaded, this func is called after each image to check if we're ready
	var im=this.imgloader;
	var i;
	if(!im.active) return;
	if(load) im.count++; //load means that this func was called from onload or onerror IMG handler
	if(im.images) for(i=0;i<im.images.length;i++)	if(!im.images[i].complete) break; //check that we have incomplete images
	if(i>=im.images.length){this._stopimgload(true);return;} //if none, then finish

	if(im.count>=im.images.length) { //after some events complete was not updated
		if(im.timer) clearTimeout(im.timer);
        	im.timer=setTimeout("var w=_uWnd.all["+this.idx+"];if(w){w.imgloader.timer=null;w._checkimgload();}",100);
	}
	if(jQuery.browser.opera && im.images2) { //for opera check virtual images
	    for(i=0;i<im.images2.length;i++) if(!im.images2[i].complete) return;
	    jQuery(im.images2).unbind(); //all complete
	    im.images2=null;
            with(this) {
	        parts.hwndcont.style.visibility='hidden';
    	        parts.markload.style.display='none';
    	        jQuery(parts.wndcont).css("display","block"); //show content so that normal load handlers get executed
                if(parts.headercont) parts.headercont.style.visibility='';
                if(parts.footercont) parts.footercont.style.visibility='';
            }
	}
},
_stopimgload: function(start) { //start - whether to start autosize, otherwise just reset image waiting
	var im=this.imgloader;
	if(im.active) {
		im.active=false;
		if(im.stoptimer) {clearTimeout(im.stoptimer);im.stoptimer=null;}
		if(im.timer) {clearTimeout(im.timer);im.timer=null;}
		if(im.images) {im.images.unbind();im.images=null;}
		if(im.images2) {jQuery(im.images2).unbind();im.images2=null;}

		if(start) {
                  with(this) {
		    if(props.hideonresize) parts.hwndcont.style.visibility='hidden'; //hide content on autosize
    		    parts.markload.style.display='none';
    		    jQuery(parts.wndcont).css("display","block");
                    if(parts.headercont) parts.headercont.style.visibility='';
                    if(parts.footercont) parts.footercont.style.visibility='';
	    	    autosz.load=true;
	    	    delaychecksize();
                  }
	        }
	}
},
content: function(c) {
    this._stopimgload();
    this.stopautosize();
    var a=this.autosz,w=this.parts.wndcont,p=this.props,im=this.imgloader,f=this.parts.footercont,h=this.parts.headercont;

    this.state.loaded=true;
    if(p.autosize && p.waitimages>0) { //hide content until all images are loaded
        this.markload();
        jQuery(w).html(c);
	if(this.props.oncontent) this.props.oncontent.apply(this.app,[this,w]);

    	im.active=true;
	im.stoptimer=setTimeout("var w=_uWnd.all["+this.idx+"];if(w)w._stopimgload(true);",p.waitimages);
	if(!_uWnd.waitimagesfunc) _uWnd.waitimagesfunc=function(e) {
		var im=e.data.imgloader;
		if(im.timer) clearTimeout(im.timer);
		im.timer=setTimeout("var w=_uWnd.all["+e.data.idx+"];if(w){w.imgloader.timer=null;w._checkimgload(1);}",10);
		};
	im.count=0;
    	im.images=jQuery(w).find("img").bind('error',this,_uWnd.waitimagesfunc).bind('load',this,_uWnd.waitimagesfunc);
	if(jQuery.browser.opera && im.images.length>0) { //opera does not preload hidden images
		im.images2=[];
		for(var i=0;i<im.images.length;i++) im.images2[i]=jQuery("<img>").attr("src",im.images[i].src).bind('load',this,_uWnd.waitimagesfunc)[0];
	}
    	this._checkimgload();
    } else {
        if(p.autosize && p.hideonresize)this.parts.hwndcont.style.visibility='hidden';
        this.parts.markload.style.display='none';
        jQuery(w).css("display","block").html(c);
        if(h) h.style.visibility='';
        if(f) f.style.visibility='';
        a.load=true;
	if(p.autosize) {
	    if(this.props.oncontent) this.props.oncontent.apply(this.app,[this,w]);
	    this.delaychecksize();
	} else {
	    this.design.onstopautosz(this);
	    if(this.props.oncontent) this.props.oncontent.apply(this.app,[this,w]);
	}
    }

},
onexternalload: function() {
  with(this) {
    _stopimgload();
    stopautosize();

    state.loaded=true;
    autosz.load=true;
    if(parts.headercont) parts.headercont.style.visibility='';
    if(parts.footercont) parts.footercont.style.visibility='';
    if(props.autosize) delaychecksize();
  }
},
stopautosize: function() {
	var a=this.autosz;
	if(a.inittimer) {clearTimeout(a.inittimer);a.inittimer=null;}
	if(a.timer){clearTimeout(a.timer);a.timer=null;}
	if(a.images) {jQuery(a.images).unbind();a.images=null;}
	if(a.active)this.design.onstopautosz(this);
	a.active=false;
},
delaychecksize: function(d) { //true if called due to autosizeonimages, delay
	d=d || 10;
	var a=this.autosz;
	if(a.inittimer) clearTimeout(a.inittimer);
    a.inittimer=setTimeout("var w=_uWnd.all["+this.idx+"];if(w);w.checksize();",d);
 
},
_countbuttonwidth: function() {
	var w=0;
	var buts={cbut:1,mbut:1,xbut:1,rbut:1,icon:1};
	for(var i in buts) if(this.parts[i])w+=this.parts[i].offsetWidth;
	for(var i in this.props.customButtons) w+=this.parts.custom[i].offsetWidth+this.design.custButMargin;
	return w;
},
checksize: function(autoload) { //true if called due to autosizeonimages
    var a=this.autosz,w=this.parts.wndcont;
	a.inittimer=null;
    if(this.state.maximized || this.state.minimized) return;
    this.stopdrag();
    this.stopresize();
    if(a.load) {
		a.load=false;
		if(a.images) {jQuery(a.images).unbind();a.images=null;}
		if(this.props.autosizeonimages) {
			if(!_uWnd.autosizeonimagesfunc) _uWnd.autosizeonimagesfunc=function(e) {e.data.delaychecksize();};
			a.images=jQuery(w).find("img").bind('load',this,_uWnd.autosizeonimagesfunc);
		}
    }
    a.active=true;
	this.design.onstartautosz(this);
	if(a.timer){clearTimeout(a.timer);a.timer=null;}

    var d=_uWnd.getdims(),minw=10,minh=10,maxw,maxh;
    if(this.props.header) {
        minw+=this._countbuttonwidth(); //minimal content width must include all header buttons
    }
    minw=Math.max(minw,this.minwidth);
    minh=Math.max(minh,this.minheight); 
    maxw=this.maxwidth;
    if(maxw==0) maxw=d.clientW-this.decor.w;
    maxh=this.maxheight;
    if(maxh==0) maxh=d.clientH-this.decor.h; //decor.h includes footer and header!!!

    var contW,contH; //scrollwidth and height
	if(this.props.autosizewidth) {
		jQuery(this.parts.wndcont).css("width",'10px');
		contW=w.scrollWidth;
		jQuery(this.parts.wndcont).css({width:(this.width-this.decor.w)+'px'});
	} else contW=w.scrollWidth;
    contH=w.scrollHeight+1;
//    a.tw=Math.min(Math.max(w.scrollWidth+scrW,w.offsetWidth,minw),maxw);
//    a.th=Math.min(Math.max(w.scrollHeight+scrH,w.clientHeight ? w.clientHeight : w.offsetHeight,minh),maxh);
	a.tw=Math.max(contW,minw);
	a.th=Math.max(contH,minh);
	var havescroll=false;
	if(a.tw>maxw) {a.tw=maxw;a.th+=this.decor.sbh;havescroll=true;}
	if(a.th>maxh) {
		a.th=maxh;
		if(a.tw+this.decor.sbw<=maxw) {a.tw+=this.decor.sbw;havescroll=true;}
			else a.tw=maxw;
		}
	if(havescroll) this.design.onstopautosz(this,true); //to enable scrolls during resize

    a.timer=setTimeout("try{_uWnd.all["+this.idx+"]._checksize();}catch(e){}",10);
},
_checksize: function() {
    this.stopdrag();
    this.stopresize();
    var a=this.autosz, dw=a.tw-(this.width-this.decor.w), dh=a.th-(this.height-this.decor.h),ws=10,hs=10;
    if(dw!=0) {
		if(dw>0) ws=Math.min(ws,dw);
	    	else if(dw<0) ws=Math.max(-ws,dw);
		this.resizeTo(this.width+ws,this.height,true,1);
    } else if(dh!=0) {
		if(dh>0) hs=Math.min(hs,dh);
	    	else if(dh<0) hs=Math.max(-hs,dh);
		this.resizeTo(this.width,this.height+hs,true,1);
    } else {
    	a.timer=null;
    	a.active=false;
//		if(this.props.hideonresize)
		this.design.onstopautosz(this);
    	return;
    }
    a.timer=setTimeout("try{_uWnd.all["+this.idx+"]._checksize();}catch(e){}",10);
},
closeevent: function() { //close event from user. insertion point for overrides
	if(this.props.onbeforeclose) if(this.props.onbeforeclose.apply(this.app,[this])) return;
	this.close(); //default action
},
close: function(nohide) {
	if(this.state.destroyed) return;
	if(this.state.pendingdestroy) nohide=1;
	var have=0; //if we have live childs
	for(var i=0;i<this.childs.length;i++) {
		var c=this.childs[i];
		if(!c) continue;
		if(c.state.destroyed) {delete this.childs[i];continue;}
		if(this.state.pendingdestroy) {have=1;break;} //second call to close() from hide()
		c.close();
		if(this.childs[i]) have=1;
	}
	this.state.pendingdestroy=true;
    if(this.props.modal && this.props.parent) { //disable parent
    	this.props.parent.disable(0);
    }
    if(!nohide && this.state.visible) {
    	var a=this.hide(this.props.fadeclosetype,this.props.fadeclosespeed,true);
    	if(a==1) return;
    }
	if(!have) this._destroy();
},
_destroy: function() {
	if(this.state.destroyed) return;
	this._focus.destroy();
	if(this.desktop) this.desktop._removewindow(this);
	if(this.menu) this.menu.destroy();
	if(this.traymenu) this.traymenu.destroy();
	if(this.trayicon) this.trayicon.remove();

    this.top.parentNode.removeChild(this.top);
    if(this.grid) this.grid.parentNode.removeChild(this.grid);
	if(this.tabctrl && !this.props.notabdestroy) this.tabctrl.destroy();
    _uWnd.all[this.idx]=null;
    this.state.destroyed=true;
    this.state.visible=false;
    if(this.props.onclose) this.props.onclose.apply(this.app,[this,this.idx]);
    if(this.props.parent) this.props.parent._ondelchild(this);
	if(this.app) this.app._ondestroywnd(this);
//	delete this;
},
_onnewchild:function(w) {
	if(w) this.childs.push(w);
	//recount children
	var n=0;
	this._foreachchild(function(c){n++;});
	this.nchilds=n;
	if(this.props.parent) this.props.parent._onnewchild(null); //recount childs of parent
},
_ondelchild:function(w) {
	var n=0; //count direct childs left
	for(var i=0;i<this.childs.length;i++) {
		if(this.childs[i])
			if(this.childs[i]==w) {
				delete this.childs[i];
				w.props.parent=null;
				if(!this.state.pendingdestroy) break;
			} else n++;
	}
	if(this.state.pendingdestroy && n==0) this._destroy();
		else this._onnewchild(null); //to recount childs

},
minimize: function(frominit) {
  with(this) {
    if(autosz.active || (!props.min && !(frominit && props.initstate=='min')) || !props.header || props.modal || props.alert || props.popup) return;
    state.beforemin=state.maximized ? 'max' : '';
    stopdrag();
    stopresize();
    if(!state.maximized && !state.minimized) restRect=[xpos,ypos,width,height,props.fixed];
    if(!frominit) sesupdate=1;
    if(props.onposchange && !state.minimized) props.onposchange.apply(app,[this]);
    state.maximized=false;
    state.minimized=true;

	//hide all child windows
	this._foreachchild(
		function(w) {
			if(w.props.min)return 1; //do not hide windows which can be minimized manually
			w.state.prevvisible=w.state.visible;
			w.hide(0);
		}
	,0);

    if(!ontaskbar) {
        if(props.min) { //can be false if initstate==min
            jQuery(parts.mbut).css("display","none");
            jQuery(parts.rbut).css("display","block");
        }
   	if(props.max)jQuery(parts.xbut).css("display","block");
   	jQuery(wnd).removeClass("xw-resize");

    	if(!props.fixed && !jQuery.browser.msie) jQuery(top).css("position","fixed");
	    design.onminimize(this);
	    props.fixed=true;
		if(desktop) {
		    moveTo(desktop.calcexclude(0),desktop.calcexclude(2),1);
		} else {
	    	moveTo(0,0,1);
	    }
	    resizeTo(200,30,0,1);
	    if(_focus.isactive()) {
	    	_focus.deactivate();
			if(!frominit) _uWnd.activatetopwnd(desktop);
	    }
	} else {
		if(!frominit) {
			hide(0);
			if(!props.notaskbar)desktop._onwndminimize(this,xpos,ypos,width,height);
		}
	}
  }
},
maximize: function(act,frominit) {
  with(this) {
    if(autosz.active || (!props.max && !(frominit && props.initstate=='max') && !(state.minimized && state.beforemin=='max')) || !props.header) return;
    stopdrag();
    stopresize();
    if(!state.maximized && !state.minimized) restRect=[xpos,ypos,width,height,props.fixed];
    if(!frominit) sesupdate=1;
    if(props.onposchange && !state.maximized) props.onposchange.apply(app,[this]);

    jQuery(parts.hdr).removeClass("xw-draggable");
    jQuery(wnd).removeClass("xw-resize");
    if(props.max) { //can be false if initstate was specified
        jQuery(parts.xbut).css("display","none");
        jQuery(parts.rbut).css("display","block");
    }
    if(props.min)jQuery(parts.mbut).css("display","block");
    if(!desktop && !props.fixed && !jQuery.browser.msie) jQuery(top).css("position","fixed");

    hideSh();
	if(desktop && !props.fixed) {
	    moveTo(desktop.calcexclude(0),desktop.calcexclude(2),1);
	    resizeTo(desktop.calcwidth(),desktop.calcheight(),0,1);
	} else  {
	    var d=_uWnd.getdims();
	    props.fixed=true;
	    moveTo(0,0,1);
	    resizeTo(d.clientW,d.clientH,0,1);
	}
    if(state.minimized) {
    	if(!ontaskbar) 
    		this.design.onrestore(this);
    		else 
    		show(false,0);
    } else if(frominit) {show(false,0);}

    state.maximized=true;
    state.minimized=false;
	//restore all child windows
	_foreachchild(
		function(w) {
			if(w.props.min)return 1; //do not restore windows which can be minimized manually
			if(w.state.prevvisible)w.show(0,0);
		},
	0);
	if(act)activate();
  }
},
restore: function(act) {
  with(this) {
    if(!props.header) return;
    stopdrag();
    stopresize();
    if(this.props.onposchange && (state.minimized || state.maximized)) props.onposchange.apply(app,[this]);

    jQuery(parts.rbut).css("display","none");
    if(props.max) jQuery(parts.xbut).css("display","block");
    if(state.minimized && props.min) jQuery(parts.mbut).css("display","block");
    if(!props.nomove)jQuery(parts.hdr).addClass("xw-draggable");
    if(props.resize)jQuery(wnd).addClass("xw-resize");


    if(state.minimized) {
    	if(!ontaskbar) {
    		this.design.onrestore(this);
    	}
    	else {
    		show(false,0);
//			desktop.onwndrestore(this);
		}
	}
    showSh();
    state.maximized=state.minimized=false;
    var r=restRect;
    if(r) {
		props.fixed=r[4];
		if(!props.fixed && !jQuery.browser.msie) jQuery(top).css("position","absolute");
		moveTo(r[0],r[1],1);
		resizeTo(r[2],r[3],0,1);
    }
	//restore all child windows
	_foreachchild(
		function(w) {
			if(w.props.min)return 1; //do not restore windows which can be minimized manually
			if(w.state.prevvisible)w.show(0,0);
		},
	0);
	if(act)activate();
  }
},
_ondragmousemove: function(dx,dy,x,y) {
    var d=_uWnd.getdims();
	x+=dx;
	y+=dy;
    if(x+this.width<30) x=30-this.width;
    if(y<-5) y=-5;
	if(this.desktop && !this.props.fixed) {
	if(x>this.desktop.width-20) x=this.desktop.width-20;
	if(y>this.desktop.height-20) y=this.desktop.height-20;
	} else if(this.props.fixed) {
	if(x>d.clientW-20) x=d.clientW-20;
	if(y>d.clientH-20) y=d.clientH-20;
    } else {
	if(x>d.docW-20) x=d.docW-20;
	if(y>d.docH-20) y=d.docH-20;
    }
    this.moveTo(x,y);
},
onstartdrag: function() {
	this.design.onstartdrag(this);
},
onstopdrag: function() {
	this.design.onstopdrag(this);
},
_ondragmousedown: function(e) {
  with(this){
	if(e.which==3 && typeof _uDesk=='undefined') return; //no right button without desktop
  	if(e.which==1 && e.ctrlKey) e.which=3;
  	if(e.which==3 && !state.resizing && !state.disabled && !props.toolwindow) {
  		this.showsysmenu(e.pageX,e.pageY);
  		return;
  	}
    if(props.nomove || state.resizing || state.maximized || state.disabled || e.which!=1) return;
	_drag.start(e,xpos,ypos);
    props.x=xpos;
    props.y=ypos;
  }
},
stopdrag: function() {
	this._drag.stop();
},
_onrsmousemove: function(dx,dy,tx,ty,tw,th,m) {
    var x,y,w,h,d=_uWnd.getdims(),minw=10,minh=10,maxw,maxh;
    x=tx;
    y=ty;
    w=tw;
    h=th;
    if(this.props.header) {
		minw+=this._countbuttonwidth();
    }
    minw=Math.max(minw,this.minwidth)+this.decor.w;
    minh=Math.max(minh,this.minheight)+this.decor.h;
    maxw=this.maxwidth;
    if(maxw==0) maxw=d.clientW; else maxw+=this.decor.w;
    maxh=this.maxheight;
    if(maxh==0) maxh=d.clientH; else maxh+=this.decor.h;
//	self.status=minw;
    if(m.indexOf('n')>=0){
	y=ty+dy;
	if(y<0) y=0;
	h=ty+th-y;
	if(h<minh) {h=minh;y=ty+th-h;}
	    else if(h>maxh) {h=maxh;y=ty+th-h;}
    }
    if(m.indexOf('s')>=0){
	h=th+dy;
	if(h<minh) h=minh;
	    else if(h>maxh) h=maxh;
    }
    if(m.indexOf('w')>=0){
	x=tx+dx;
	if(x<0) x=0;
	w=tx+tw-x;
	if(w<minw) {w=minw;x=tx+tw-w;}
	    else if(w>maxw) {w=maxw;x=tx+tw-w;}
    }
    if(m.indexOf('e')>=0){
	w=tw+dx;
	if(w<minw) w=minw;
	    else if(w>maxw) w=maxw;
    }
    this.moveTo(x,y);
    this.resizeTo(w,h);
},
_onrsmousedown: function(e,b) {
  with(this) {
    if(_drag.active || state.disabled || state.maximized || state.minimized || 
	autosz.active || !props.resize || e.which!=1) return;
    props.x=xpos;
    props.y=ypos;
	this.stopautosize();
	_resize.start(e,xpos,ypos,width,height,b);
  }
},
onstartrs: function() {
	this.design.onstartresize(this);
},
onstoprs: function() {
	this.design.onstopresize(this);
},
stopresize: function() {
	this._resize.stop();
}

};

function _txt(sign) { 
		var lng=window._uDeflang,a,p=arguments;
		if(!lng) a=sign;
			else {
				var db=window._uSigns;
				if(!db || !db[lng])a=sign;
					else {
						if(sign in db[lng]) a=db[lng][sign];
							else a=sign;
					}
			}
		function _txtproc(str,param) {
			return p[param];
		}

		if(p.length>1) {
			a=a.replace(/%([1-9])/g,_txtproc);
		}
		return a;
}

function _uColorBox(did,fid){
	var hex_Red=new Array("00","33","66","99","CC","FF");
	var hex_Green=new Array("00","33","66","99","CC","FF");
	var hex_Blue=new Array("00","33","66","99","CC","FF");
	var hex_Gray=new Array("909090","939393","969696","999999","9C9C9C","9F9F9F","C0C0C0","C3C3C3","C6C6C6","C9C9C9","CCCCCC","CFCFF","F0F0F0","F3F3F3","F6F6F6","F9F9F9","FCFCFC","FFFFFF");
	var hexred="00", hexgreen="00", hexblue="00", hexgray="00";
	var red=0, green=0, blue=0;
	var x=0, y=0, z=0;
	var xyz=0;
	var ctable='<table border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">';
	while (y<6){
		ctable+='<tr>';
		var x=0;
		var hexblue=hex_Blue[blue];
		while (x<6){
			var z=0;
			var hexgreen=hex_Green[green];
			while (z<6){
				var hexred=hex_Red[red];
				var hexadecimal=""+hexred+hexgreen+hexblue;
				ctable+='<td style="width:8px;height:8px;cursor:pointer;" onclick="jQuery(\'#'+did+'\').hide(); getElementById(\''+fid+'\').value=\''+hexadecimal+'\'" bgcolor="'+hexadecimal+'"></td>';
				z++;red++;
				if (red==6){red=0;}
			}
			x++;green++;
			if (green==6)green=0;
			xyz++;
			if (xyz==3){ctable+='</tr>'; xyz=0;}
		}
		y++; blue++
		if (blue==6)blue=0;
	}
	for (var i=0;i<hex_Gray.length;i++){
		ctable+='<td style="width:8px;height:8px;cursor:pointer;" onclick="jQuery(\'#'+did+'\').hide(); getElementById(\''+fid+'\').value=\''+hex_Gray[i]+'\'" bgcolor="'+hex_Gray[i]+'"></td>';
	}
	ctable+='</table>';
	jQuery('#'+did).html(ctable);
}

//ucoz specific
//wnd can be _uWnd or _uTabCtrl
function _uParseXML(xml,wnd,tabid,papp) {xml = xml.documentElement;
var tabctrl=null,app=null,footers={}; //footer used to associate footer and header with content of tabctrl, footer and header must come first

if(wnd && wnd.constructor==_uTabCtrl) {tabctrl=wnd;wnd=null;app=papp || tabctrl.app;}
    else if(wnd && wnd.constructor==_uWnd) {app=papp || wnd.app;}
    else {app=papp || null;wnd=null;} //unknown type in wnd

if (xml == null) { alert("Server connection Error. Sorry."); }
for (var i = 0; i < xml.childNodes.length; i++) {
if (xml.childNodes[i].nodeName == "cmd"){ var cmd='',target='',data,height=null;
for (var j=0; j<xml.childNodes[i].attributes.length; j++) {
if (xml.childNodes[i].attributes[j].name == "h")height = parseInt(xml.childNodes[i].attributes[j].value);
if (xml.childNodes[i].attributes[j].name == "p")cmd = xml.childNodes[i].attributes[j].value;
if (xml.childNodes[i].attributes[j].name == "t")target = xml.childNodes[i].attributes[j].value;}
if (xml.childNodes[i].firstChild && xml.childNodes[i].firstChild.data ) data = xml.childNodes[i].firstChild.data; else data = '';
if (cmd=='innerHTML' && target.match(/^layerContent(.+)/)) _uWnd.content(RegExp.$1,data);
if (cmd=='innerHTML' && target.match(/^layerTitle(.+)/)) _uWnd.setTitle(RegExp.$1,data);
if (cmd=='innerHTML') jQuery('#'+target).html(data);
else if (cmd=='+innerHTML') jQuery('#'+target).prepend(data);
else if (cmd=='innerHTML+') jQuery('#'+target).append(data);
else if (cmd=='innerHTMLspanAll') jQuery("span."+target).html(data);
else if (cmd=='innerHTMLdivAll') jQuery("div."+target).html(data);
else if (cmd=='value') jQuery('#'+target).val(data);
else if (cmd=='jsa') includeJSfile(data,target);
else if (cmd=='js') eval(data);
else if (cmd=='content' && target.length>0) {
	var r=target.match(/^([^:]+):(.+)/);
	if(r) {
            if(typeof footers['_uhh'+r]=='number' && footers['_uhh'+r]!=NaN) _uTabCtrl.headerheight(r[1],r[2],footers['_uhh'+r]);
            if(typeof footers['_ufh'+r]=='number' && footers['_ufh'+r]!=NaN) _uTabCtrl.footerheight(r[1],r[2],footers['_ufh'+r]);
            _uTabCtrl.content(r[1],r[2],data,footers['_uhc'+r],footers['_ufc'+r]);
        }
	else _uWnd.content(target,data);
}
else if (cmd=='header' && target.length>0) {
	var r=target.match(/^([^:]+):(.+)/);
        if(r) {
            footers['_uhc'+r]=data;
            footers['_uhh'+r]=height;
        } else {
            if(height!=NaN) _uWnd.headerheight(target,height);
            _uWnd.header(target,data);
        }
}
else if (cmd=='footer' && target.length>0) {
	var r=target.match(/^([^:]+):(.+)/);
        if(r) {
            footers['_ufc'+r]=data;
            footers['_ufh'+r]=height;
        } else {
            if(height!=NaN) _uWnd.footerheight(target,height);
            _uWnd.footer(target,data);
        }
}
else if (cmd=='title' && target.length>0) {
	var r=target.match(/^([^:]+):(.+)/);
	if(r) _uTabCtrl.setTitle(r[1],r[2],data);
	else _uWnd.setTitle(target,data);
}
else if (cmd=='close' && target.length>0) {
	var r=target.match(/^([^:]+):(.+)/);
	if(r) 
		if(!data || isNaN(parseInt(data)))_uTabCtrl.closeTab(r[1],r[2]);else setTimeout("_uTabCtrl.closeTab('"+r[1]+"','"+r[2]+"');",parseInt(data));
	else
		if(!data || isNaN(parseInt(data)))_uWnd.close(target);else setTimeout("_uWnd.close('"+target+"');",parseInt(data));
}
else if(wnd) {
    if(cmd=='content') wnd.content(data);
    else if(cmd=='header') {if(height!=NaN) wnd.headerheight(height);wnd.header(data);}
    else if(cmd=='footer') {if(height!=NaN) wnd.footerheight(height);wnd.footer(data);}
    else if(cmd=='title') wnd.setTitle(data);
    else if(cmd=='close') if(!data || isNaN(parseInt(data)))wnd.close();else setTimeout("var w=_uWnd.all["+wnd.idx+"];if(w)w.close();",parseInt(data));
    }
else if(tabctrl && tabid) {
    if(cmd=='content') {
        if(typeof footers['_uhh']=='number' && footers['_uhh']!=NaN) tabctrl.headerheight(tabid,footers['_uhh']);
        if(typeof footers['_ufh']=='number' && footers['_ufh']!=NaN) tabctrl.footerheight(tabid,footers['_ufh']);
        tabctrl.content(tabid,data,footers['_uhc'],footers['_ufc']);
    }
    else if(cmd=='header') {footers['_uhc']=data;footers['_uhh']=height;}
    else if(cmd=='footer') {footers['_ufc']=data;footers['_ufh']=height;}
    else if(cmd=='title') {tabctrl.setTitle(tabid,data);}
    else if(cmd=='close') if(!data || isNaN(parseInt(data)))tabctrl.closeTab(tabid);else setTimeout("var w=_uTabCtrl.all["+tabctrl.idx+"];if(w)w.closeTab('"+tabid+"');",parseInt(data));
    }
}}
}

var _defAjaxError=function(xmlreq, status, err) {
//this - options for request
//typically only one of status or err (exception) will have info
	try {
		_show_log_form();return;
	} catch(e){}
	window.location.reload();
}
var _hookAjaxError=null; //function(xmlreq, status, err)
//this - options for request

function _uAjaxRequest(url,options) {
    if(!url) return null;
    var o = jQuery.extend({
//noerrorhook:1   - can be used to prevent error hook from being used    	
		app: 0, //application PID
		wnd: 0, //window index
		async: 1,
		cache: true,
		dataType: 'xml',
		error: _defAjaxError,
	        type: 'GET',
		success: _defAjaxSuccess,
		timeout: 25000
// 		data: {f1: val1, f2: val2, f3: [val4,val5]}
    }, options || {});
    if(o.app && o.app.appname && o.app.pid>0) o.app=o.app.pid; //fix if app object was passed instead of PID
    if(o.wnd && o.wnd.constructor==_uWnd) o.wnd=o.wnd.idx; //fix if window object was passed instead of IDX
    if(!o.data)o.data={};
    if(o.app>0 && !('_ai' in o.data)) o.data._ai=o.app;
    if(o.app>0 && _uApp.all[o.app] && _uApp.all[o.app]._admpasscook) o.data._apc=_uApp.all[o.app]._admpasscook;
    if(o.wnd>0 && !('_wi' in o.data)) o.data._wi=o.wnd;

    if(_hookAjaxError && !o.noerrorhook) {o.prev_error=o.error;o.error=_hookAjaxError;}
    o.url=url;
//    if(o.app>0 && !o.nosuccesshook) {o.prev_success=o.success;o.success=_hookAjaxSuccess;}
    return jQuery.ajax(o);
}
//by default use app in options to call success-handler relative to application if it were specified
var _hookAjaxSuccess=function(data, status) {
	if(!this.prev_success) return;
	if(this.app>0 && typeof _uApp!='undefined' && _uApp.all[this.app] && !_uApp.all[this.app].exited && this.prev_success!=_defAjaxSuccess) this.prev_success.call(_uApp.all[this.app],data,status);
		else this.prev_success.call(this,data,status);
};

//default success action for forms and simple _uAjaxRequest
//by default call _uParseXML for xml reply
var _defAjaxSuccess=function(data, status) {
//this - options for request
        var wnd=null,app=null;
	if(this.dataType=='xml') {
		if(this.wnd>0 && _uWnd.all[this.wnd] && !_uWnd.all[this.wnd].state.destroyed) wnd=_uWnd.all[this.wnd];
		if(this.app>0 && typeof _uApp!='undefined' && _uApp.all[this.app] && !_uApp.all[this.app].exited) app=_uApp.all[this.app];
		_uParseXML(data,wnd,0,app);
	}
}

//default error action for _uPostForm
//by default try to find onerror attribute in form and execute it
var _defAjaxFormError=function(xmlreq, status, err) {
//this - options for request
//typically only one of status or err (exception) will have info
	if(!this._formobj) return;
	var a=this._formobj.onerror || this._formobj.getAttribute('onerror');
	if(!a) return;
	if(typeof a == 'string') {
		try {a=new Function(a);} catch(e){return;}
	}
	if(typeof a == 'function' || typeof a=='object') {
		try {a.call(this._formobj,xmlreq, status, err);} catch(e){return;}
	}
}

//by default try to find oncomplete attribute in form and execute it
var _defAjaxFormComplete=function(xmlreq, status) {
//this - options for request
	if(!this._formobj) return;
	var a=this._formobj.oncomplete || this._formobj.getAttribute('oncomplete');
	if(!a) return;
	if(typeof a == 'string') {
		try {a=new Function(a);} catch(e){return;}
	}
	if(typeof a == 'function' || typeof a=='object') {
		try {a.call(this._formobj,xmlreq, status);} catch(e){return;}
	}
}


function _uPostForm(formid,options) {
    if(!formid && options && options.url) { _uAjaxRequest(options.url,options);return;}
    var f;
    if(typeof(formid)!='object') f=jQuery('#'+formid); else f=jQuery(formid);
    if(!f.length) return;
    var o = jQuery.extend({
//noerrorhook:1   - can be used to prevent error hook from being used    	
		app: 0, //pid of application
                wnd: 0, //window index
		url: f.attr('action') || window.location.toString(),
		type: f.attr('method') || 'GET',
		error: _defAjaxFormError,
		success: _defAjaxSuccess,
		complete: _defAjaxFormComplete,
		dataType: 'xml',
		semantic: false //true if form contains type=image
    }, options || {});
    if(o.app && o.app.appname && o.app.pid>0) o.app=o.app.pid; //fix if app object was passed instead of PID
    if(o.wnd && o.wnd.constructor==_uWnd) o.wnd=o.wnd.idx; //fix if window object was passed instead of IDX
    if(!o.data)o.data={};
    if(o.app>0 && !('_ai' in o.data)) o.data._ai=o.app;
    if(o.app>0 && _uApp.all[o.app] && _uApp.all[o.app]._admpasscook) o.data._apc=_uApp.all[o.app]._admpasscook;
    if(o.wnd>0 && !('_wi' in o.data)) o.data._wi=o.wnd;

    if(_hookAjaxError && !o.noerrorhook) {o.prev_error=o.error;o.error=_hookAjaxError;}
    o._formobj=f[0];
    if(o.app>0 && !o.nosuccesshook) {o.prev_success=o.success;o.success=_hookAjaxSuccess;}
    f.ajaxSubmit(o);
}


function includeJSfile(src,id){
if (id && document.getElementById(id)){return;}
var js = document.createElement('script');
js.setAttribute('type','text/javascript');
if(id) js.setAttribute('id',id);
js.setAttribute('src',src);
document.getElementsByTagName('head').item(0).appendChild(js);
}


var _entrRm={};
function _entrRem(bID,u,imgurl,text){
    if (!text){text='Are you sure?';}
    if (!_entrRm[bID] && confirm(text)){_entrRm[bID]=1;
	document.getElementById(bID).src=imgurl+'/img/fr/EmnAjax.gif';
	_uPostForm('',{url:u});
    }	
}


function _coloredTDs(r,c){
    var cl='';
    if (typeof(r)!='object'){r=document.getElementById(r);}
    if (typeof(document.getElementsByTagName)!='undefined'){cl=r.getElementsByTagName('td');}
    else if (typeof(r.cells)!='undefined') {cl=r.cells;}
    else {return false;}
    for (var i=0;i<cl.length;i++) {
	cl[i].className=c;
    }
}

function openLayerB(n,f,url,t,w,h,resize,anyVar2,grid,multypart,align){
    new _uWnd(n,t,w,h,{autosize: resize ? 1 : 0,modal: grid ? 1 : 0,align:align ? align : 'center'},{url:url,form:f,cache:1});
}

function _showOnTop(n,f){
	var z=_uWnd.getTopZ();
        if (f){
                document.getElementById(n).style.zIndex=z+1;
        }
        else {
                document.getElementById('outLayer'+n).style.zIndex=z+1;
        }
}

function encodeHtmlVal(s,quot) { //quot true to encode ' and " only!!! no & will be encoded (if input text is from innerHTML and must be inserted into html attribute)
        if(quot) return String(s).replace(/'/g,'&'+'#39;').replace(/"/g,'&'+'quot;'); //'
	return String(s).replace(/&/g,'&'+'amp;').replace(/'/g,'&'+'#39;').replace(/"/g,'&'+'quot;').replace(/</g,'&'+'lt;').replace(/>/g,'&'+'gt;'); //'
}

//recursively dump SIMPLE objects  (numbers, strings, bool, simple arrays, simple objects)
function dumpObject(o,depth,ex) {
	var tp=typeof o,s;
	if(arguments.length<2) depth=10;
	function hexencode(n,s) {
		var j,d=[0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f'],v='';
		for(j=0;j<s;j++) {v=''+d[n%16]+v;n>>>=4;}
		return v;
	}
	if(tp=='object')
		if(!o) tp='null';
		else if (o.constructor==String) tp='string';
		else if (o.constructor==Number) tp='number';
		else if (o.constructor==Boolean) tp='boolean';
		else if (o.constructor==Array) tp='array';
	switch(tp) {
		case 'number': case 'boolean': case 'null': case 'undefined': return String(o);
		case 'string': 
			return '"'+o.replace(/([\\"])/g,"\\$1").
				replace(/\n/g,"\\n").
				replace(/([\x00-\x1f])/g,function(s,c){return '\\x'+hexencode(c.charCodeAt(0),2);}).
				replace(/([\u2028\u2029])/g,function(s,c){return '\\u'+hexencode(c.charCodeAt(0),4);})+'"';
		case 'array':
			if(depth<=0) return '[?]';
			s='';
			for(var i=0;i<o.length;i++) {
				if(i>0) s+=',';
				s+=dumpObject(o[i]);
			}
			return '['+s+']';
		case 'object':
			if(depth<=0) return '{?}';
			s='';
			for(var i in o) {
				if(ex && ex[i]) continue;
				if(s) s+=',';
				try {
				s+=dumpObject(i)+':'+dumpObject(o[i],depth-1);
				} catch(e) {s+=dumpObject(i)+':?';}
			}
			return '{'+s+'}';
	}
}

function _uHighlightA(parent,url,hlclass) {
	var ls=jQuery(parent).find("a").get(),o=null,l=0;
	for(var j in ls) {
		if (ls[j].href && url.indexOf(ls[j].href)>=0) {
			if (!o || l<ls[j].href.length){
				o=ls[j]; 
				l=ls[j].href.length; 
			} 
		} 
	}
	if(o) jQuery(o).addClass(hlclass);
}
function _uBuildMenu(id,ishoriz,hlurl,hlclass,alignclass,hidetm){
		var p,i,subs,m,al,t,it,f;
		p=jQuery(id)[0];
		if(!p) return;
		hidetm=hidetm || 2000;
		f=function(e){_uMENU.hideallmenus();this.__umenu.show();};
		if(jQuery.browser.msie) p.style.zoom='1';
		subs=jQuery(id + ">ul").children("li").children("ul");
		for(i=0;i<subs.length;i++) {
			m=_ubuild_submenus(subs[i]);
			al=it=subs[i].parentNode;
			if(!ishoriz && alignclass && (t=jQuery(al).children("."+alignclass)[0]) )al=t;
			it.__umenu=new _uMENU('',{alignObj:al,align:ishoriz ? 'D' : (window._rtl ? 'L' : 'R')},{parentnode:p,hidetimer:hidetm},m);
			jQuery(it).bind("mouseover",f);
			it.removeChild(subs[i]);
		}
		if(!hlurl || !hlclass) return;
		setTimeout("_uHighlightA(jQuery('"+id+"')[0],'"+hlurl+"','"+hlclass+"');",100);
	}

function _uReplaceMenu(id,ishoriz,hlurl,hlclass,removeclass){
		var p,m,mm;
		p=jQuery(id)[0];
		if(!p) return;
		if(jQuery.browser.msie) p.style.zoom='1';
		mm=jQuery(id).children("ul")[0];
		if(!mm) return;
		if(removeclass)jQuery(mm).find("."+removeclass).remove();
		m=_ubuild_submenus(mm);
		mm.parentNode.__umenu=new _uMENU('',{},{width:'auto',horiz: ishoriz, statical:1,parentnode:p,noabs:1},m);
		mm.parentNode.removeChild(mm);
		if(!hlurl || !hlclass) return;
		setTimeout("_uHighlightA(jQuery('"+id+"')[0],'"+hlurl+"','"+hlclass+"');",100);
	}

		
function _ubuild_submenus(ul) {
		var ch=jQuery(ul).children("li"),i,res=[],subul,m,ls;
		for(i=0;i<ch.length;i++) {
			subul=jQuery(ch[i]).children("ul")[0];
			if(subul) {
				m=_ubuild_submenus(subul);
				subul.parentNode.removeChild(subul);
				ls=jQuery(ch[i]).find("a").get();
				res[res.length]=[jQuery(ch[i]).html(),m,ls.length>0 ? {action:'a'} : null];
			} else {
				ls=jQuery(ch[i]).find("a").get();
				res[res.length]=[jQuery(ch[i]).html(),ls.length>0 ? 'a' : ''];
			}
		}
		return res;
}

function _uColorBox(did,fid){
var hex_Red=new Array("00","33","66","99","CC","FF");
var hex_Green=new Array("00","33","66","99","CC","FF");
var hex_Blue=new Array("00","33","66","99","CC","FF");
var hex_Gray=new Array("909090","939393","969696","999999","9C9C9C","9F9F9F","C0C0C0","C3C3C3","C6C6C6","C9C9C9","CCCCCC","CFCFCF","F0F0F0","F3F3F3","F6F6F6","F9F9F9","FCFCFC","FFFFFF");
var hexred="00";var hexgreen="00";var hexblue="00";var hexgray="00";
var red=0;var green=0;var blue=0;
var x=0;var y=0;var z=0;
var xyz=0;
var ctable='<table border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">';
while (y<6){
ctable+='<tr>';
var x=0;
var hexblue=hex_Blue[blue];
while (x<6){
var z=0;
var hexgreen=hex_Green[green];
while (z<6){
var hexred=hex_Red[red];
var hexadecimal=""+hexred+hexgreen+hexblue;
ctable+='<td style="width:8px;height:8px;cursor:pointer;" onclick="jQuery(\'#'+did+'\').hide(); getElementById(\''+fid+'\').value=\''+hexadecimal+'\'" bgcolor="'+hexadecimal+'"></td>';
z++;red++;
if (red==6){red=0;}
}
x++;green++;
if (green==6){green=0;}
xyz++;
if (xyz==3){ctable+='</tr>'; xyz=0;}
}
y++; blue++
if (blue==6){blue=0;}
}
for (var i=0;i<hex_Gray.length;i++){
ctable+='<td style="width:8px;height:8px;cursor:pointer;" onclick="jQuery(\'#'+did+'\').hide(); getElementById(\''+fid+'\').value=\''+hex_Gray[i]+'\'" bgcolor="'+hex_Gray[i]+'"></td>';
};
ctable+='</table>';
jQuery('#'+did).html(ctable);
}


function _uButtonExt(frm){ // formID,  generate hidden input-image field for submit buttons
    return '<span style="visibility:hidden"><input type="image" src="/.s/img/1px.gif" style="width:1px" id="subm'+frm+'" /></span>';
}

function _uButton(frm,type,opts){ // formID, type['s','b','r'], options
	var props = jQuery.extend({
        ext: 0, //if true for type=s then input=image is not created, it must be created somewhere inside the form by
	text:'Ok',
	content:'',
	style:0,		//0,1
	id: null //ID for internal A-tag
	},opts || {});

	var tp = {
		's':'onclick="if (this.dis)return; jQuery(this).addClass(\'myBtnDis\'); this.dis=true; jQuery(\'#subm'+frm+'\').click();"',
		'r':'onclick="document.getElementById(\''+frm+'\').reset();"'
	};
    var t = (tp[type]!='undefined') ? tp[type] : '';
	var s = (props.style==1) ? ['myBtnLeft','myBtnCenter','myBtnRight'] : ['myBtnLeftA','myBtnCenterA','myBtnRightA'];

	var hid = (type=='s' && !props.ext) ? '<input type="image" src="/.s/img/1px.gif" style="width:1px" id="subm'+frm+'" />' : '';
	var dis = (props.style==2) ? 'myBtnCont x-unselectable myBtnDis' : 'myBtnCont x-unselectable';

	var bt = '<table border="0" cellpadding="0" cellspacing="0" onmousedown="this.className=\'downBtn\'" onmouseover="this.className=\'overBtn\'" onmouseout="this.className=\'outBtn\'">'
+'<tr><td class="'+s[0]+'"><img border="0" src="/.s/img/1px.gif"></td>'
+'<td class="'+s[1]+'"><div class="'+dis+'" unselectable="on" '+t+' '+props.content+'><a '+(props.id ? 'id="'+props.id+'" ': '')+'href="javascript://" onclick="return false;">'+props.text+'</a></div></td>'
+'<td class="'+s[2]+'"><img border="0" src="/.s/img/1px.gif"></td>'
+'<td style="visibility:hidden;">'+hid+'</td></tr></table>'
	return bt;	
}

function _uButtonEn(id,enable) { //button id or object, enable  - whether to enable or disable
        var o=typeof(id)=='string' ? jQuery("#"+id)[0] : id;
        if(!o)return;
        if(enable) {
            o.dis=false;
            jQuery(o).removeClass('myBtnDis');
        } else {
            o.dis=true;
            jQuery(o).addClass('myBtnDis');
        }
}
