/*controls.js 0.2*/

(function () {
MEB.OverlayMgr = function()
{
	this._layers=new Array();
	this.current=-1;
}
MEB.OverlayMgr.prototype =
{
	add:function(menu)
	{
		var i=this._layers.length;
		this._layers[i]=menu;
		return i;
	},
	remove:function(index)
	{
		if (this.current==index)
		{
			Y.util.Event.removeListener(document, 'mousedown', this.close);
			this.current=null;
		}
		this._layers[index]=null;
	},
	opened:function(index)
	{
		if (this.current>=0 && this._layers[this.current])
		{
			try
			{
				this._layers[this.current].close(true);
			}
			catch(e)
			{
			}
			finally
			{
				
				this.current=index;
			}
		}
		else
		{
			this.current=index;
			Y.util.Event.addListener(document, 'mousedown', this.close,null,this);
		}
	},
	closed:function()
	{
		this.current=-1;
		Y.util.Event.removeListener(document, 'mousedown', this.close);
	},
	close:function(e)
	{
		if (this.current>=0 && this._layers[this.current])
			this._layers[this.current].close();
		this.current=-1;
		Y.util.Event.removeListener(document, 'mousedown', this.close);
	}
}
MEB.overlayMgr=new MEB.OverlayMgr();

/*
* 	@class Scroll
*	@requires yahoo-dom-event
* 	
*	@info: 	Classe per applicare lo scroll ad un div utilizzando due bottoni
*
* 	@param (string) target 	l'oggetto html del div che deve scrollare
*
*	@param (object) buttons 	t,b opzionalmente anche l,r (oggetti MEB.Buttons oppure HTML)
*	@param (int) 	thick 		il passo di incremento dello scroll da bottone (default 1)
*	@param (int)	time 		il tempo in ms ogni quanto modifica lo scroll da bottone (default 10)
*
*	@param (int) 	kthick 		il passo di incremento da keyboard (default 5)
*
*	@param (int) 	mthick 		il passo di incremento da wheel
*
*	@param (int) 	area 		spessore dell area
*	@param (object) areaRegion 	t,l,w,h del htmlObj da scrollare
*	@param (int)	speed 		velocita. speed*area= scostamento massimo ogni 60ms 
*
*	@param (string) cursor		path a una directory con le immagini per i cursori
*
*	METODI
*	bindWheel()
*	unbindWheel()
*	bindKeys(over)				over da utilizzare se si vuole che i keys vengano rilevati solo con il mouse dentro l'AREA
*	unbindKeys()
*	bindArea()
*	unbindArea()
*	
*	setupArea(t,l,w,h)
*	getScroll()
*	setScroll(left,top)
*	
*	ESEMPI: 
*
//var topBut=new MEB.Button({id:'topButton',content:'up!'});
//var scroll=new MEB.Scroll({target:viewport.get(),area:40,areaRegion:{t:20,l:0,w:500,h:400}});
//var scroll=new MEB.WindowScroll({area:40});

var viewport=new MEB.Viewport({margin:{t:100,l:30,r:30,b:30}});
var s=viewport.attachScroll(new MEB.Scroll({area:40}));
viewport.get().innerHTML='<div style="position:absolute;left:100px;top:100px;width:4000px;height:4000px;background-color:black"></div><div style="position:absolute;left:1000px;top:200px;width:200px;height:300px;background-color:red"></div>';
s.bindHand();
s.bindArea();
s.bindWheel();
s.bindKeys();
*/
MEB.Scroll = function(configs)
{
	this._scrolling={x:0,y:0};
	this._scrollTimer = null;
	this._htmlObj = configs.target;
	this._buttons = configs.buttons;
	this._thick = configs.thick ? configs.thick : 1;
	this._mthick = configs.mthick ? configs.mthick : this._thick;
	this._kthick = configs.kthick ? configs.kthick : 5; 
	
	this._time = configs.time ? configs.thick : 10;
	this._area = configs.area;
	this._aregion=configs.areaRegion;
	this._speed = configs.speed? configs.speed : 1;

	this._cursor=configs.cursor;
	if (this._buttons)
	{
		if (this._buttons.t)
		{
			if (this._buttons.t.onActivate)
			{
				this._buttons.t.onActivate.subscribe(this._buttonScroll,{x:0,y:-1},this);
				this._buttons.t.onDeactivate.subscribe(this._scrollStop,null,this);
			}
			else
			{
				YAHOO.util.Event.addListener(this._buttons.t,"mouseover",this._buttonScroll,{x:0,y:-1},this);
				YAHOO.util.Event.addListener(this._buttons.t,"mouseout",this._scrollStop,null,this);
			}
			if (this._buttons.b.onActivate)
			{
				this._buttons.b.onActivate.subscribe(this._buttonScroll,{x:0,y:1},this);
				this._buttons.b.onDeactivate.subscribe(this._scrollStop,null,this);
			}
			else
			{
				YAHOO.util.Event.addListener(this._buttons.b,"mouseover",this._buttonScroll,{x:0,y:1},this);
				YAHOO.util.Event.addListener(this._buttons.b,"mouseout",this._scrollStop,null,this);
			}
		}
		if (this._buttons.l)
		{
			if (this._buttons.l.onActivate)
			{
				this._buttons.l.onActivate.subscribe(this._buttonScroll,{x:-1,y:0},this);
				this._buttons.l.onDeactivate.subscribe(this._scrollStop,null,this);
			}
			else
			{
				YAHOO.util.Event.addListener(this._buttons.l,"mouseover",this._buttonScroll,{x:-1,y:0},this);
				YAHOO.util.Event.addListener(this._buttons.l,"mouseout",this._scrollStop,null,this);
			}
			if (this._buttons.r.onActivate)
			{
				this._buttons.r.onActivate.subscribe(this._buttonScroll,{x:1,y:0},this);
				this._buttons.r.onDeactivate.subscribe(this._scrollStop,null,this);
			}
			else
			{
				YAHOO.util.Event.addListener(this._buttons.r,"mouseover",this._buttonScroll,{x:1,y:0},this);
				YAHOO.util.Event.addListener(this._buttons.r,"mouseout",this._scrollStop,null,this);
			}
		}
	}
	this.onScroll=new Y.util.CustomEvent( 'scrollEvent' , this , true , Y.util.CustomEvent.FLAT  ); 
}
MEB.Scroll.prototype = {
	destroy:function()
	{
		this.unbindHand();
		this.unbindArea();
		this.unbindKeys();
		this.unbindWheel();
	},
	setTarget:function(t)
	{
		this._htmlObj=t;
	},
	getTarget:function()
	{
		return this._htmlObj;
	},
	setScroll:function(l,t,noSignal)
	{
		this._htmlObj.scrollLeft=l;
		this._htmlObj.scrollTop=t;
		if (!noSignal)
			this.onScroll.fire();
	},
	setVScroll:function(t,noSignal)
	{
		this._htmlObj.scrollTop=t;
		
		if (!noSignal)
			this.onScroll.fire();
	},
	setHScroll:function(l,noSignal)
	{
		this._htmlObj.scrollLeft=l;
		if (!noSignal)
			this.onScroll.fire();
	},
	getScroll:function()
	{
		return {x:parseInt(this._htmlObj.scrollLeft),y:parseInt(this._htmlObj.scrollTop)};
	},
	bindHand: function()
	{
		this._ks=new Y.util.KeyListener(document, {keys:32},{fn:this._handDown,scope:this,correctScope:true},Y.util.KeyListener.KEYDOWN);
		this._ks.enable();
	},
	unbindHand: function()
	{
		if (this._ks)
		{
			this._ks.disable();
			delete this._ks;
			this._ks=null;
		}
	},
	_handDown:function(et,o)
	{
		if (this._hd==true)
			return;
		
		var element = Y.util.Event.getTarget(o[1]);
		if(element && (element.tagName=="INPUT" || element.tagName=="TEXTAREA"))
			return;
		Y.util.Event.stopEvent(o[1]);
		this._hd=true;
		if (this._ao)
		{
			this._rebindArea=true;
			this.unbindArea();
		}
		this._hand=null;
		this._ksup=new Y.util.KeyListener(document, {keys:32},{fn:this._handUp,scope:this,correctScope:true},Y.util.KeyListener.KEYUP);
		this._ksup.enable();
		Y.util.Event.addListener(this._htmlObj,"mousemove",this._handScroll,null,this);
		document.body.style.cursor='move';
	},
	_handUp:function(et,o)
	{
		if (!this._hd)
			return;
		var element = Y.util.Event.getTarget(o[1]);
		if(element && (element.tagName=="INPUT" || element.tagName=="TEXTAREA"))
			return;
		Y.util.Event.stopEvent(o[1]);
		Y.util.Event.removeListener(this._htmlObj,"mousemove",this._handScroll);
		this._ksup.disable();
		if (this._rebindArea)
		{
			this._rebindArea=false;
			this.bindArea();
		}
		document.body.style.cursor='default';
		delete this._ksup;
		this._hd=false;
		
	},
	_handScroll:function(e)
	{
		var p={x:Y.util.Event.getPageX(e)-Y.util.Dom.getDocumentScrollLeft(),y:Y.util.Event.getPageY(e)-Y.util.Dom.getDocumentScrollTop()};
		if (!this._hand)
		{
			this._hand=p;
			return true;
		}
		this._scrolling={x:this._hand.x-p.x,y:this._hand.y-p.y};
		this._hand=p;
		this._scroll();
		
	},
	bindWheel: function()
	{
		Y.util.Event.addListener(this._htmlObj,Y.env.ua.gecko?'DOMMouseScroll':"mousewheel",this._wheelScroll,null,this);
	},
	unbindWheel: function()
	{
		Y.util.Event.removeListener(this._htmlObj,Y.env.ua.gecko?'DOMMouseScroll':"mousewheel");
		this._scrollStop();
	},
	bindArea: function()
	{
		this._ao=true;
		Y.util.Event.addListener(this._htmlObj,"mouseover",this._bindArea,null,this);
		if (!this._outBinded)
		{
			Y.util.Event.addListener(this._htmlObj,"mouseout",this._out,null,this);
			this._outBinded=true;
		}
	},
	_bindArea:function(e)
	{
		if (this._ab)
			return;
		
		Y.util.Event.addListener(this._htmlObj,"mousemove",this._areaScroll,null,this);
		this._ab=true;
	},
	unbindArea: function(){
		this._ao=false;
		Y.util.Event.removeListener(this._htmlObj,"mouseover",this._bindArea);
		this._unbindArea();
	},
	_unbindArea:function()
	{
		Y.util.Event.removeListener(this._htmlObj,"mousemove",this._areaScroll);
		this._scrollStop();
		this._ab=false;
	},
	_out: function(e){
		p={x:Y.util.Event.getPageX(e)-Y.util.Dom.getDocumentScrollLeft()-this._aregion.l,y:Y.util.Event.getPageY(e)-Y.util.Dom.getDocumentScrollTop()-this._aregion.t};
		if (!e || p.x<0 || p.x>this._aregion.w || p.y<0 || p.y>this._aregion.h)
		{
			if (this._ab)
				this._unbindArea();
			
			if (this._ka)
				this._unbindKeys();
		}
	},
	_bindKeys:function(e)
	{
		if (this._ka)
			return;
		
		this._kd.enable();
		this._ku.enable();
		this._ka=true;
	},
	bindKeys: function(over){
		this._kOver=over;
		this._kd=new Y.util.KeyListener(document, {keys:[37,38,39,40]},{fn:this._keyScroll,scope:this,correctScope:true},Y.util.KeyListener.KEYDOWN);
		this._ku=new Y.util.KeyListener(document, {keys:[37,38,39,40]},{fn:this._keyUp,scope:this,correctScope:true},Y.util.KeyListener.KEYUP);
		
		if (!over)
		{
			this._kd.enable();
			this._ku.enable();
		}
		else
		{
			Y.util.Event.addListener(this._htmlObj,"mouseover",this._bindKeys,null,this);
			if (!this._outBinded)
			{
				Y.util.Event.addListener(this._htmlObj,"mouseout",this._out,null,this);
				this._outBinded=true;
			}
		}
			
	},
	_unbindKeys:function()
	{
		this._ka=false;
		if (this._kd)
		{
			this._kd.disable();
			this._ku.disable();
			delete this._kd;
			delete this._ku;
			this._kd=this._ku=null;
		}
		
		this._scrollStop();
	},
	unbindKeys: function()
	{
		if (this._kOver)
			Y.util.Event.removeListener(this._htmlObj,"mouseover",this._enableKeys);
		this._unbindKeys();		
	},
	_keyScroll:function(et,o)
	{
		var element = Y.util.Event.getTarget(o[1]);
		if(element && (element.tagName=="INPUT" || element.tagName=="TEXTAREA"))
			return;
		
		Y.util.Event.stopEvent(o[1]);
		switch(o[0])
		{
			case(37):
				this._scrolling.x=-this._kthick;
			break;
			case(38):
				this._scrolling.y=-this._kthick;
			break;
			case(39):
				this._scrolling.x=this._kthick;
			break;
			case(40):
				this._scrolling.y=this._kthick;
			break;
				
		}
		if (!this._scrollTimer)
			this._scrollTimer=YAHOO.lang.later(this._time,this,this._scroll,null,true);
	},
	_keyUp:function(e,o)
	{
		var element = Y.util.Event.getTarget(o[1]);
		if(element && (element.tagName=="INPUT" || element.tagName=="TEXTAREA")){
			return;
		}
		switch(o[0])
		{
			case(37):
				if (this._scrolling.x<0)
					this._scrolling.x=0;
			break;
			case(38):
				if (this._scrolling.y<0)
					this._scrolling.y=0;
			break;
			case(39):
				if (this._scrolling.x>0)
					this._scrolling.x=0;
			break;
			case(40):
				if (this._scrolling.y>0)
					this._scrolling.y=0;
			break;
				
		}
		if (this._scrolling.x==0 && this._scrolling.y==0)
			this._scrollStop()
	},
	_wheelScroll: function(e)
	{
		Y.util.Event.stopEvent(e);
		var delta;
		e=Y.util.Event.getEvent(e);
		if (e.wheelDelta) 
		{
			delta = -e.wheelDelta/120;
			if (window.opera)
				delta = delta;
		} 
		else if (e.detail) 
		{ 
			/** Mozilla case. */
			/** In Mozilla, sign of delta is different than in IE.
			 Also, delta is multiple of 3.
			*/
			delta = e.detail/3;
		}
		this._scrolling={x:0,y:delta*this._mthick};
		this._scroll();
	},
	_scrollStop:function()
	{
		this._scrolling={x:0,y:0};
		if (this._scrollTimer) 
			this._scrollTimer.cancel();
		this._scrollTimer=null;
	},
	_buttonScroll:function(e,v)
	{
		this._scrolling={x:v.x*this._thick,y:v.y*this._thick};
		this._scrollTimer=Y.lang.later(this._time,this,this._scroll,null,true);
	},
	_scroll:function()
	{
		this._htmlObj.scrollTop=parseInt(this._htmlObj.scrollTop)+this._scrolling.y;
		this._htmlObj.scrollLeft=parseInt(this._htmlObj.scrollLeft)+this._scrolling.x;
		this.onScroll.fire();
	},
	setupArea:function(t,l,w,h)
	{
		this._aregion={t:t,l:l,w:w,h:h};
	},
	_areaScroll:function(e)
	{
		var ico='',p={x:Y.util.Event.getPageX(e)-Y.util.Dom.getDocumentScrollLeft()-this._aregion.l,y:Y.util.Event.getPageY(e)-Y.util.Dom.getDocumentScrollTop()-this._aregion.t};
		if (p.x>this._area && p.x<this._aregion.w-this._area && p.y>this._area && p.y<this._aregion.h-this._area)
		{
			if (this._ico) 
			{
				document.body.style.cursor='default';
				this._ico='';
			}
			this._scrollStop();
			return true;  
		}
		this._scrolling={x:0,y:0};
		if (p.x<this._area)
		{
			this._scrolling.x=-(this._area-p.x)*this._speed;
			ico+='n';
		}
		else if (p.x>this._aregion.w-this._area)
		{
			this._scrolling.x=(p.x-this._aregion.w+this._area)*this._speed;
			ico+='p';
		}
		else
			ico='0';
		if (p.y<this._area)
		{
			this._scrolling.y=-(this._area-p.y)*this._speed;
			ico+='n';
		}
		else if (p.y>this._aregion.h-this._area)
		{
			this._scrolling.y=(p.y-this._aregion.h+this._area)*this._speed;
			ico+='p';
		}
		else
			ico='0';

		if (this.cursor && this._ico!=ico) 
		{
			document.body.style.cursor='url('+this.cursor+'c'+ico+'.cur), auto';
			this._ico=ico;
		}
		
		if (!this._scrollTimer)
			this._scrollTimer=Y.lang.later(60,this,this._scroll,null,true);
	}
}
MEB.WindowScroll=function(configs)
{
	configs.target=window;
	MEB.WindowScroll.superclass.constructor.call(this,configs);
	this.setupArea();
	Y.util.Event.addListener(window,"resize",this.setupArea,null,this);
}
Y.lang.extend(MEB.WindowScroll, MEB.Scroll,
{
	getScroll:function()
	{
		return {x:Y.util.Dom.getDocumentScrollLeft(),y:Y.util.Dom.getDocumentScrollTop()};
	},
	setScroll:function(l,t)
	{
		window.scrollTo(l,t);
	},
	_scroll:function()
	{
		window.scrollTo(Y.util.Dom.getDocumentScrollLeft() + this._scrolling.x,Y.util.Dom.getDocumentScrollTop()+ this._scrolling.y);
	},
	setupArea:function()
	{
		this._aregion={t:0,l:0,w:Y.util.Dom.getViewportWidth(),h:Y.util.Dom.getViewportHeight()};
	},
	bindArea: function()
	{
		MEB.WindowScroll.superclass._bindArea.call(this);	
	},
	unbindArea: function()
	{
		MEB.WindowScroll.superclass._unbindArea.call(this);
	},
	bindKeys:function()
	{
		MEB.WindowScroll.superclass.bindKeys.call(this,false);
	}
	
});
/*
* 	@class Viewport
*	@requires yahoo-dom-event
* 	
*	@info: 	Classe per creare un div che si auto resiza con la finestra e si collega allo scroll
*
* 	@param (string) id 			id del div
*	@param (string) container 	id o htmlObj del contenitore, senza il div va nel BODY
*	@param (obj) 	margin 		{t,r,b,l} per fare in modo che il div stia a tot margine dalla finestra e attivi l'autosize
*	@param (obj) 	size 		{t,l,w,h} per fare in modo che il contenitore abbia dimensione fissa
*	@param (int)	time 		il tempo in ms ogni quanto modifica lo scroll da bottone (default 10)
*
*	ATTRIBUTI
*	htmlObj
*	top,left,width,height 	
*	scroll (dopo attachScroll)
*
*	METODI
*	get()							ritorna l oggetto htmlObj
*	autosize()
*	attachScroll(MEB.Scroll obj)	ritorna l'oggetto scroll collegato
*	destroy()
*/
MEB.Viewport=function(config)
{
	this.htmlObj=document.createElement('DIV');
	this.htmlObj.className='viewport';
	this.htmlObj.id=config.id;
	if (!config.container)
		document.body.appendChild(this.htmlObj);
	else
		Y.util.Dom.get(container).appendChild(this.htmlObj);
	
	this.onResize=new Y.util.CustomEvent( 'resizeEvent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 

	this.margin=config.margin;
	this.size=config.size;
	
	this.resize();
	Y.util.Event.addListener(window,"resize",this.autosize,null,this);
		
}
MEB.Viewport.prototype={
	get:function()
	{
		return this.htmlObj;
	},
	resize:function()
	{		
		if (this.margin)
		{
			if (this.margin.t)
				this.htmlObj.style.top=this.margin.t+'px';
			else
				this.htmlObj.style.bottom=this.margin.b+'px';
		
			if (this.margin.l)
				this.htmlObj.style.left=this.margin.l+'px';
			else
				this.htmlObj.style.right=this.margin.r+'px';
		
			this._autosize();
			
		}
		else
		{
			this.top=this.size.t;
			this.left=this.size.l;			
			this.height=this.size.h;
			this.width=this.size.w;
			this.htmlObj.style.top=this.size.t+'px';
			this.htmlObj.style.left=this.size.l+'px';
			this.htmlObj.style.width=this.size.w+'px';
			this.htmlObj.style.height=this.size.h+'px';

		}
	},
	autosize:function()
	{
		this._autosize();
		this.onResize.fire(this.width,this.height);
		
	},
	_autosize:function()
	{
		this.width=Y.util.Dom.getViewportWidth()-this.margin.l-this.margin.r;
		this.height=Y.util.Dom.getViewportHeight()-this.margin.t-this.margin.b;
		if (this.margin.t)
			this.top=this.margin.t;
		else
			this.top=this.margin.b-this.height;
		if (this.margin.l)
			this.left=this.margin.l;
		else
			this.right=this.margin.r-this.width;

		this.htmlObj.style.width=this.width+'px';
		this.htmlObj.style.height=this.height+'px';
		if (this.scroll)
			this.scroll.setupArea(this.top,this.left,this.width,this.height);
	},
	attachScroll:function(scroll)
	{
		this.scroll=scroll;
		this.scroll.setTarget(this.htmlObj);
		this.scroll.setupArea(this.top,this.left,this.width,this.height);
		return this.scroll;
	},
	destroy:function()
	{
		Y.util.Event.removeListener(window,"resize",this.autosize);
		if (this.scroll)
			this.scroll.destroy();
		this.htmlObj.parentNode.removeChild(this.htmlObj);
	}
}
MEB.Overlay = function (configs)
{
	
	this.id=configs.id;
	this.container=configs.container;
	this.content=configs.content;
	this.alignment=configs.alignment;
	this.x=configs.x;
	this.y=configs.y;
	this.w=configs.w;
	this.h=configs.h;
	
	this.htmlObj=document.createElement('div');
	this.htmlObj.className='menu';
	
	if (this.id)
		this.htmlObj.id=this.id;
	if (this.content)
		this.htmlObj.innerHTML=this.content;
	if (this.container)
		this.container.appendChild(this.htmlObj);
	else
		Y.util.Event.onDOMReady(function(){ document.body.appendChild(this.htmlObj); },null,this);
	this.onOpen=new Y.util.CustomEvent( 'openevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onClose=new Y.util.CustomEvent( 'closeevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this._index=MEB.overlayMgr.add(this);
}

MEB.Overlay.prototype =
{
	id:null,from:null,htmlObj:null,container:null,content:null,x:null,y:null,w:null,h:null,alignment:null,direction:{x:'r',y:'b'},opened:false,_index:null,
		
	_stopEvents:function()
	{
		Y.util.Event.addListener(this.htmlObj, "mousedown", this._stopEvent, null,this); 
		Y.util.Event.addListener(this.htmlObj, "click", this._stopEvent, null,this); 
	},
	_stopEvent:function(e)
	{
		Y.util.Event.stopEvent(e);
	},
	open:function(from)
	{
		if (this.opened)
			return true;
		this.opened=true; 
		
		MEB.overlayMgr.opened(this._index);
		this.htmlObj.style.zIndex=MEB.zIndex++;
		this.htmlObj.style.display='block';	
		this.from=from;
		this.onOpen.fire();
		this.updateView();
	},
	_align:function(alignment,w,h)
	{
		if (alignment.obj.tagName)
			var reg=Y.util.Dom.getRegion(alignment.obj);
		else
			var reg=alignment.obj.getRegion();
		
		switch(alignment.context)
		{
			case('tl'):
				this.x=reg.left;
				this.y=reg.top-h;
				this.direction={x:'r',y:'t'};
			break;
			case('tr'):
				this.x=reg.right-w;
				this.y=reg.top-h;
				this.direction={x:'l',y:'t'};
				
			break;
			case('bl'):
				this.x=reg.left;
				this.y=reg.bottom;
				this.direction={x:'r',y:'b'};
			break;
			case('br'):
				this.x=reg.right-w;
				this.y=reg.bottom;
				this.direction={x:'l',y:'t'};
			break;
		}
	},
	align:function(alignment)
	{
		this.alignment=alignment;
		this.updateView();
	},
	updateView:function()
	{
		var cr=Y.util.Dom.getClientRegion();
		var h,w;
		if (!this.h || !this.w)
		    var r=Y.util.Dom.getRegion(this.htmlObj);
		
		if (!this.h)
		    h=r.bottom-r.top;
		else
		    h=this.h;
		    
		if (!this.w)
		    w=r.right-r.left;
		else
		    w=this.w;
		
		if (this.alignment)
			this._align(this.alignment,w,h);
		
		this._checkPos(cr,w,h);
		
		if (this.w!=undefined)
			this.htmlObj.style.width=this.w+'px';
		if (this.h!=undefined)
			this.htmlObj.style.height=this.h+'px';
		if (this.x!=undefined)
			this.htmlObj.style.left=this.x+'px';
		if (this.y!=undefined)
			this.htmlObj.style.top=this.y+'px';
		
	},
	_checkPos:function(cr,w,h)
	{
		if (this.direction.x=='r')
		{
			if(this.x+w>cr.right)
				this.x=	cr.right-w;
		}
		else
		{
			if(this.x<cr.left)
				this.x=cr.left;
		}
		if (this.direction.y=='b')
		{
			if(this.y+h>cr.bottom)
				this.y=cr.bottom-h;
		}
		else
		{
			if(this.y<cr.top)
				this.y=cr.top;
		}
	},
	close:function(silent)
	{
		if (!this.opened)
			return false;
		
		this.htmlObj.style.display='none';
		MEB.overlayMgr.closed(this._index);
		this.opened=false;
		if (this.from)
			this.from.menuClosed();
		if (!silent)
			this.onClose.fire();
	},
	destroy:function()
	{
		this.onOpen.unsubscribeAll(); 
		this.onClose.unsubscribeAll();
		MEB.overlayMgr.remove(this._index);
		//alert(this.htmlObj.id);
		this.htmlObj.parentNode.removeChild(this.htmlObj);
	}
}
MEB.Menu = function(configs)
{
	this.selected=configs.selected!=undefined?configs.selected:-1;
	
	MEB.Menu.superclass.constructor.call(this,configs);
	
	Y.util.Event.addListener(this.htmlObj, "mousedown", this.mousedown, null,this); 
	Y.util.Event.addListener(this.htmlObj, "mouseover", this.mouseover, null,this); 
	
	Y.util.Event.addListener(this.htmlObj, "click", this._stopEvent, null,this); 
	
	this._k=new Y.util.KeyListener(document, { keys:[38] },{ fn:this.prevItem, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );  
	this._k2=new Y.util.KeyListener(document, { keys:[40] },{ fn:this.nextItem, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );  
	this._k3=new Y.util.KeyListener(document, { keys:[13] },{ fn:this.enterItem, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );  
	
	this.onItemSelected=new Y.util.CustomEvent( 'itemselectedevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	
	this.items=configs.items?configs.items:[];
	
	this._create();
	
	this.onKeyOut=new Y.util.CustomEvent( 'keyout' , this , true , Y.util.CustomEvent.FLAT  ); 
	
}
Y.lang.extend(MEB.Menu, MEB.Overlay,{
	items:null,_oItemsUl:null,_oItems:null,selected:-1,
	_create:function()
	{
		var i;
		
		this._oScrollUp=document.createElement('DIV');
		this._oScrollUp.className='scroll';
		this.htmlObj.appendChild(this._oScrollUp);
		
		this._oScrollBar=document.createElement('DIV');
		this._oScrollBar.className='scrollBar';
		this.htmlObj.appendChild(this._oScrollBar);
		
		this._oItemsUl=document.createElement('UL');
		this._oItemsUl.className='items';
		this.htmlObj.appendChild(this._oItemsUl);
		this._oItems=new Array();
		for(i=0;i<this.items.length;i++)
		{
			this._appendItem(i);
		}
		this._oScrollDown=document.createElement('DIV');
		this._oScrollDown.className='scroll';
		this.htmlObj.appendChild(this._oScrollDown);
	},
	_createScrollButtons:function()
	{
		if (this._oScrollUpBtn)
			return;
		this._oScrollUpBtn=new MEB.OverButton({container:this._oScrollUp,className:'scrollup'});
		this._oScrollDownBtn=new MEB.OverButton({container:this._oScrollDown,className:'scrolldown'});
		if (!this._scroll)
		{
			this._scroll=new MEB.Scroll({target:this._oItemsUl,buttons:{t:this._oScrollUpBtn,b:this._oScrollDownBtn},thick:30});
			this._scroll.bindWheel();
		}
	},
	_hideScrolls:function()
	{
		this._oScrollDown.style.display='none';
		this._oScrollUp.style.display='none';
		this._oScrollBar.style.display='none';
		Y.util.Dom.removeClass(this._oItemsUl,'wscroll');
	},
	_showScrolls:function()
	{
		this._oScrollDown.style.display='block';
		this._oScrollUp.style.display='block';
		Y.util.Dom.addClass(this._oItemsUl,'wscroll');
		this._oScrollBar.style.display='block';
	},
	_getHFHeight:function()
	{
		var rf=Y.util.Dom.getRegion(this._oScrollDown),rh=Y.util.Dom.getRegion(this._oScrollUp);
		
		return (rf.bottom-rf.top)+(rh.bottom-rh.top);
	},
	_scrollToSelected:function()
	{
		if (this.selected>=0 && this._scroll)
		{	
			this._scroll.setVScroll(0);
			var r=Y.util.Dom.getRegion(this._oItems[this.selected]);
			this._scroll.setVScroll(r.top-this.y-30);
		}
	},
	_scrollToOver:function()
	{
		if (this._current>=0 && this._scroll)
		{	
			this._scroll.setVScroll(0);
			var r=Y.util.Dom.getRegion(this._oItems[this._current]);
			this._scroll.setVScroll(r.top-this.y-30);
		}
	},
	open:function(from)
	{
		if (this.opened)
			return true;
		
		this._current=-1;
		
		if (!this.h)
		{
			this.htmlObj.style.left='-10000px';
			this.htmlObj.style.top='-10000px';
			this._oItemsUl.style.height='auto';
		}
		MEB.Menu.superclass.open.call(this,from);
		if (!this.h)
			Y.util.Event.addListener(window, "resize", this.updateView, null,this); 
	},
	_checkPos:function(cr,w,h)
	{
		if (this.direction.x=='r')
		{
			if(this.x+w>cr.right)
				this.x=	cr.right-w;
		}
		else
		{
			if(this.x<cr.left)
				this.x=	cr.left;
		}
		if (!this.h)
		{
			if (this.direction.y=='b')
			{
				if (this.y+h>cr.bottom)
				{
					this._createScrollButtons();
					this._showScrolls();
					this._ulLimit=cr.bottom-this.y-this._getHFHeight()-15;
					this._oItemsUl.style.height=this._ulLimit+'px';
					
					this._initScrollbar();
				}
				else
				{
					this._oItemsUl.style.height='auto';
					this._hideScrolls();
				}
			}
			else
			{
				if (this.y<cr.top)
				{
					this._createScrollButtons();
					this._showScrolls();
					var rfheight=this._getHFHeight();
					var diff=cr.top-this.y+rfheight+15;
					this.y=cr.top+15;
					this._ulLimit=h-diff;
					this._oItemsUl.style.height=this._ulLimit+'px';
				
					this._initScrollbar();
				}
				else
				{
					this._oItemsUl.style.height='auto';
					this._hideScrolls();
				}
			}
		}
	},
	_initScrollbar:function()
	{
		if (!this._scrollBar)
			this._scrollBar=new MEB.ScrollBar({scroll:this._scroll,axis:'y',container:this._oScrollBar});
		else
			this._scrollBar.init();
	},
	close:function(silent)
	{
		if (!this.opened)
			return false;
		Y.util.Event.removeListener(window, "resize", this.updateView); 
		this.blur();
		MEB.Menu.superclass.close.call(this,silent);
	},
	updateView:function()
	{
		MEB.Menu.superclass.updateView.call(this);
		this._scrollToSelected();
	},
	mousedown:function(e)
	{
		Y.util.Event.stopEvent(e);
		var t=Y.util.Event.getTarget(e),i;
		if (t)
		{
			if (Y.util.Dom.hasClass(t,'item-over') || Y.util.Dom.hasClass(t,'item-selected'))
				i=t;
			else
			{
				i=Y.util.Dom.getAncestorByClassName(t,'item-over');
				if (!i)
					i=Y.util.Dom.getAncestorByClassName(t,'item-selected');
			}
			if (i)
				this.setSelectedByIndex(i.itemIndex);	
		}
	},
	mouseover:function(e)
	{
		Y.util.Event.stopEvent(e);
		var t=Y.util.Event.getTarget(e),i;
		if (t)
		{
			if (Y.util.Dom.hasClass(t,'item') || Y.util.Dom.hasClass(t,'item-selected'))
				i=t;
			else
			{
				i=Y.util.Dom.getAncestorByClassName(t,'item');
				if (!i)
					i=Y.util.Dom.getAncestorByClassName(t,'item-selected');
			}
			if (i)
				this._overItem(i.itemIndex);	
		}
	},
	setSelectedByIndex:function(index,silent)
	{
		if (this.selected>=0)
			Y.util.Dom.removeClass(this._oItems[this.selected],'item-selected');
		
		this.selected=index;
		Y.util.Dom.addClass(this._oItems[index],'item-selected');
		if (!silent)
			this.onItemSelected.fire(index);
	},
	getSelectedIndex:function()
	{
		return this.selected;
	},
	empty:function()
	{
		this.selected=-1;
		var n=this.items.length;
		for(var i=0;i<n;i++)
		{
			this.removeItem(0);
		}
	},
	removeItem:function(index)
	{
		this._oItems[index].parentNode.removeChild(this._oItems[index]);
		this._oItems.splice(index,1);
		this.items.splice(index,1);
		
	},
	addItem:function(item)
	{
		var i=this.items.length;
		this.items[i]=item;
		this._appendItem(i);
	},
	_appendItem:function(index)
	{
		this._oItems[index]=document.createElement('LI');
		this._oItems[index].className='item';
		if (this.selected==index)
			this._oItems[index].className+=' item-selected';
		
		this._oItems[index].innerHTML=this.items[index].html?this.items[index].html:this.items[index].text;
		this._oItems[index].itemIndex=index;
		this._oItemsUl.appendChild(this._oItems[index]);
	},
	focus:function()
	{
		this._k.enable();
		this._k2.enable();
		this._k3.enable();
		if (this.selected>=0)
			this._overItem(this.selected);
		else
			this._overItem(0);
	},
	blur:function()
	{
		if (this._current>=0)
			Y.util.Dom.removeClass(this._oItems[this._current],'item-over');
		this._k.disable();
		this._k2.disable();
		this._k3.disable();
	},
	_overItem:function(i)
	{
		if (this._current>=0)
			Y.util.Dom.removeClass(this._oItems[this._current],'item-over');
		
		this._current=i;
		Y.util.Dom.addClass(this._oItems[this._current],'item-over');
	},
	enterItem:function(e,a)
	{
		Y.util.Event.stopEvent(a[1]);
		this.setSelectedByIndex(this._current);
	},
	prevItem:function(e,a)
	{
		Y.util.Event.stopEvent(a[1]);
		Y.util.Dom.removeClass(this._oItems[this._current],'item-over');
		this._current--;
		if (this._current<0)
		{
			this.blur();
			this.onKeyOut.fire();
		}
		else
		{
			this._scrollToOver();
			Y.util.Dom.addClass(this._oItems[this._current],'item-over');
		}
	},
	nextItem:function(e,a)
	{
		Y.util.Event.stopEvent(a[1]);
		if (this._current>=0)
			Y.util.Dom.removeClass(this._oItems[this._current],'item-over');
		this._current++;
		if (this._current>=this._oItems.length)
			this._current=this._oItems.length-1;
		
		this._scrollToOver();
		Y.util.Dom.addClass(this._oItems[this._current],'item-over');
		
	},
	destroy:function()
	{
		this.onItemSelected.unsubscribeAll();
		MEB.Menu.superclass.destroy.call(this);
	}
});

MEB.Button = function(configs) 
{
	this.id=configs.id!=undefined?configs.id:Y.util.Dom.generateId();
	this.disabled=configs.disabled;
	this.className=configs.className?configs.className:'button';
	this.focusLock=configs.focusLock;
	if (configs.on_off)
	{
		this.active=configs.active;
		this.on_off=true;
	}
	if (configs.container)
	{
		this.container=Y.util.Dom.get(configs.container);
		this.htmlObj=document.createElement('button');
		if (configs.submit)
			this.htmlObj.setAttribute("type","submit");
		else
			this.htmlObj.setAttribute("type","button");
		this.htmlObj.id=this.id;
		this.container.appendChild(this.htmlObj);
	}
	else
		this.htmlObj=Y.util.Dom.get(this.id);
	if (configs.content!=undefined)
		this.setContent(configs.content);
		
	this._focus=false;
	this.htmlObj.hideFocus=true;
	this.htmlObj.className=this.className;
	
	if (this.active)
		Y.util.Dom.addClass(this.htmlObj,this.className+'-active');
	
	if (!this.disabled)
		this._bind();
	else
	{
		this.htmlObj.disabled='disabled';
		Y.util.Dom.addClass(this.htmlObj,this.className+'-disabled');
	}
	this.onClick=new Y.util.CustomEvent( 'clickevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onMouseDown=new Y.util.CustomEvent( 'mousedownevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onMouseUp=new Y.util.CustomEvent( 'mouseupevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onActivate=new Y.util.CustomEvent( 'activateevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onDeactivate=new Y.util.CustomEvent( 'deactivateevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
};
MEB.Button.prototype={
	htmlObj:null,container:null,id:null,on_off:false,active:false,disabled:false,_focus:false,focusLock:false,
	setContent:function(content)
	{
		if (this.htmlObj.firstChild)
			this.htmlObj.removeChild(this.htmlObj.firstChild);
	
		var t=document.createElement('table');
		var r=t.insertRow(0);
		var c=r.insertCell(0);
		c.className=this.className+'-space';
		var c2=r.insertCell(1);
		c2.className=this.className+'-content';
		c2.id=this.id+'_content';
		c2.innerHTML=content;
		
		//var str='<table><tr><td class="'+this.className+'-space"></td><td class="'+this.className+'-content" id="'+this.id+'_content">'+content+'</td></tr></table>';
		//this.htmlObj.innerHTML=str; 
		this.htmlObj.appendChild(t);
	},
	getDocument:function()
	{
		return document;
	},
	getRegion:function()
	{
		return Y.util.Dom.getRegion(this.htmlObj);
	},
	_bind:function()
	{
		if (this._binded)
			return;
		this._binded=true;
		Y.util.Event.addListener(this.htmlObj, "click", this.click, null,this);  
		Y.util.Event.addFocusListener (this.htmlObj,  this.focus, null,this);  
		Y.util.Event.addBlurListener (this.htmlObj,  this.blur, null,this);  
		Y.util.Event.addListener(this.htmlObj, "mousedown", this.mousedown, null,this);  
		Y.util.Event.addListener(this.htmlObj, "mouseup", this.mouseup, null,this);  
		Y.util.Event.addListener(this.htmlObj, "mouseover", this.mouseover, null,this);
		Y.util.Event.addListener(this.htmlObj, "mouseout", this.mouseout, null,this);  
		if (!this._kl1)
			this._kl1=new Y.util.KeyListener(this.htmlObj, { keys:[13,32] },{ fn:this.mousedown, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );   
		this._kl1.enable();
		
		if (!this.on_off)
		{
			if (!this._kl2)
				this._kl2=new Y.util.KeyListener(this.htmlObj, { keys:[13,32] },{ fn:this.deactivate, scope:this, correctScope:true},Y.util.KeyListener.KEYUP );  
			if (!this._kl3)
				this._kl3=new Y.util.KeyListener(this.htmlObj, { keys:[27] },{ fn:this.deactivate, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );
			
			this._kl2.enable();
			this._kl3.enable();
		}
	},
	_unbind:function()
	{
		if (!this._binded)
			return;
		this._binded=false;
			
		Y.util.Event.removeListener(this.htmlObj, "click", this.click);  
		Y.util.Event.removeFocusListener (this.htmlObj,  this.focus);  
		Y.util.Event.removeBlurListener (this.htmlObj,  this.blur);  
		Y.util.Event.removeListener(this.htmlObj, "mousedown", this.mousedown);  
		Y.util.Event.removeListener(this.htmlObj, "mouseup", this.mouseup);  
		Y.util.Event.removeListener(this.htmlObj, "mouseover", this.mouseover);
		Y.util.Event.removeListener(this.htmlObj, "mouseout", this.mouseout);  
		
		this._kl1.disable();
		if (!this.on_off)
		{
			this._kl2.disable();  
			this._kl3.disable();  
		}
	},
	click:function()
	{
		this.onClick.fire();
	},
	focus:function()
	{
		if (this._focus)
			return false;
		this._focus=true;
		Y.util.Dom.addClass(this.htmlObj,this.className+'-over');
		if (this.active)
			Y.util.Dom.addClass(this.htmlObj,this.className+'-active-over');
		
	},
	enable:function()
	{
		this.disabled=false;
		this.htmlObj.disabled='';
		Y.util.Dom.removeClass(this.htmlObj,this.className+'-disabled');
		this._bind();
	},
	disable:function()
	{
		if (this.active)
			this.deactivate();
		this.htmlObj.blur();
		this._unbind();
		this.disabled=true;
		Y.util.Dom.addClass(this.htmlObj,this.className+'-disabled');
		this.htmlObj.disabled='disabled';
	},
	blur:function()
	{
		if (!this._focus)
			return false;
		this._focus=false;
		Y.util.Dom.removeClass(this.htmlObj,this.className+'-over');
		if (this.active)
			Y.util.Dom.removeClass(this.htmlObj,this.className+'-active-over');
	},
	activate:function(noSignal)
	{
		this.active=true;
		Y.util.Dom.addClass(this.htmlObj,this.className+'-active');
		if (this._focus)
			Y.util.Dom.addClass(this.htmlObj,this.className+'-active-over');
		if (!noSignal)
			this.onActivate.fire();
	},
	deactivate:function(noSignal)
	{
		this.active=false;
		Y.util.Dom.removeClass(this.htmlObj,this.className+'-active');
		Y.util.Dom.removeClass(this.htmlObj,this.className+'-active-over');
		if (this._focus)
			Y.util.Dom.addClass(this.htmlObj,this.className+'-over');
		if (!noSignal)
			this.onDeactivate.fire();
		
	},
	mouseover:function(e)
	{
		if (!this._focus || !this.focusLock)
		{
			Y.util.Dom.addClass(this.htmlObj,this.className+'-over');
			if (this.active)
				Y.util.Dom.addClass(this.htmlObj,this.className+'-active-over');
			
		}
	},
	mouseout:function(e)
	{
		if (!this._focus || !this.focusLock)
		{
			Y.util.Dom.removeClass(this.htmlObj,this.className+'-over');
			if (this.active)
				Y.util.Dom.removeClass(this.htmlObj,this.className+'-active-over');
		}
		if (!this.on_off && this.active)
			this.deactivate();
		
	},
	mousedown:function(e)
	{
		if (!this._focus)
			this.htmlObj.focus();
		if (!this.active)
		{
			this.activate();
			this.onMouseDown.fire();
		}
		else if (this.on_off)
		{
			this.deactivate();
		}
	},
	mouseup:function(e)
	{
		if (!this.on_off)
		{
			this.deactivate();
			this.onMouseUp.fire();
		}
	},
	destroy:function()
	{
		this._unbind();
		this.onClick.unsubscribeAll(); 
		this.onMouseDown.unsubscribeAll(); 
		this.onMouseUp.unsubscribeAll(); 
		this.onActivate.unsubscribeAll(); 
		this.onDeactivate.unsubscribeAll(); 
		this.htmlObj.parentNode.removeChild(this.htmlObj);
	}
};
MEB.OverButton = function(configs) 
{
	MEB.OverButton.superclass.constructor.call(this,configs);
}
Y.lang.extend(MEB.OverButton, MEB.Button,{
	mouseover:function()
	{
		MEB.OverButton.superclass.activate.call(this);
	},
	mouseout:function()
	{
		MEB.OverButton.superclass.deactivate.call(this);
	},
	mousedown:function()
	{
		return true;
	},
	mouseup:function()
	{
		return true;
	}
});
/*
 * @param menu (MEB.Overlay)
 * 
 * 
 * */
MEB.MenuButton = function(configs) 
{
	this._menu=configs.menu;
	configs.on_off=true;
	this.width=configs.width;
	configs.className=(configs.className)?configs.className:'menuButton';
	MEB.MenuButton.superclass.constructor.call(this,configs);
	this.onMenuClose=new Y.util.CustomEvent( 'menucloseevent' , this , true , YAHOO.util.CustomEvent.FLAT  );
}
Y.lang.extend(MEB.MenuButton, MEB.Button,{
	setContent:function(content)
	{
		if (this.htmlObj.firstChild)
			this.htmlObj.removeChild(this.htmlObj.firstChild);
		this.htmlObj.innerHTML='<table'+(this.width?' style="width:'+this.width+'px"':'')+'><tr><td nowrap="1" class="'+this.className+'-content" id="'+this.id+'_content">'+content+'</td><td id="'+this.id+'_menuArrow" class="arrow"></td></tr></table>';
	},
	disable:function()
	{
		MEB.MenuButton.superclass.disable.call(this);
		if (this._menu)
			this._menu.close(true);
	},
	focus:function()
	{
		MEB.MenuButton.superclass.focus.call(this);
	},
	blur:function()
	{
		MEB.MenuButton.superclass.blur.call(this);
	},
	activate:function()
	{
		MEB.MenuButton.superclass.activate.call(this);
		this._menu.open(this);
	},
	deactivate:function()
	{
		this.closeMenu();
	},
	closeMenu:function()
	{
		this._menu.close();
	},
	menuClosed:function()
	{
		MEB.MenuButton.superclass.deactivate.call(this);
		this.htmlObj.focus();
		this.onMenuClose.fire();
	},
	mousedown:function(e)
	{
		Y.util.Event.stopEvent(e);
		MEB.MenuButton.superclass.mousedown.call(this,e);
	},
	setMenu:function(menu)
	{
		this._menu=menu;
	},
	getMenu:function()
	{
		return this._menu;
	},
	destroy:function()
	{
		this.onMenuClose.unsubscribeAll(); 
		if (this._menu)
			this._menu.destroy();
		MEB.MenuButton.superclass.destroy.call(this);
	}
});
MEB.Checkbox = function(configs) 
{
	configs.on_off=true;
	configs.className='checkbox';
	configs.content=undefined;
	MEB.Checkbox.superclass.constructor.call(this,configs);
	this.htmlObj.name=this.name=configs.name;
	this.onChange = new Y.util.CustomEvent('changeevent',this,true,Y.util.CustomEvent.FLAT); 
}
Y.lang.extend(MEB.Checkbox, MEB.Button,{
	name:null,
	activate:function(silent)
	{
		MEB.Checkbox.superclass.activate.call(this);
		if(!silent)
			this.onChange.fire(this.active);
	},
	disable:function()
	{
		this.htmlObj.blur();
		this._unbind();
		this.disabled=true;
		if (this.active)
			Y.util.Dom.addClass(this.htmlObj,this.className+'-disabled-active');
		else
			Y.util.Dom.addClass(this.htmlObj,this.className+'-disabled');
	},
	deactivate:function(silent)
	{
		MEB.Checkbox.superclass.deactivate.call(this);
		if(!silent)
			this.onChange.fire(this.active);
	},
	destroy:function()
	{
		this.onChange.unsubscribeAll(); 
		MEB.Checkbox.superclass.destroy.call(this);
	}
});

MEB.Select = function(configs) 
{
	MEB.Select.superclass.constructor.call(this,configs);
	
	this.htmlObj.name=this.name=configs.name;
	this.selectedIndex=configs.content!=undefined?-1:0;
	
	if (configs.selectedIndex!=undefined)
	{
		this.selectedIndex=configs.selectedIndex;
		if (this.selectedIndex>=0)
			this.value=configs.items[this.selectedIndex].value;
	}
	else if (configs.value!=undefined)
		this._selectedByValue(configs.value,configs.items,true);

	this.createMenu(configs.items);
	
	if (this.selectedIndex>=0 && this._menu.items[this.selectedIndex])
		this.setContent(this._menu.items[this.selectedIndex].text);
	
	this.onChange = new Y.util.CustomEvent('changeevent',this,true,Y.util.CustomEvent.FLAT); 
}
Y.lang.extend(MEB.Select, MEB.MenuButton,{
	name:null,value:null,selectedIndex:null,
	createMenu: function(items){
		if (!items || !items.length)
			return;
		if(this._menu)
			this._menu.destroy();
		this._menu=new MEB.Menu({alignment:{obj:this.htmlObj,context:'bl'},items:items,selected:this.selectedIndex});
		this._menu.onItemSelected.subscribe(this._itemSelected,null,this);
		this._menu.onKeyOut.subscribe(this.closeMenu,null,this);
		this._k=new Y.util.KeyListener(this.htmlObj, { keys:[40] },{ fn:this.focusMenu, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );  
	  	this._k.enable();
	},
	focusMenu:function(eName,e,b)
  	{
  		Y.util.Event.stopEvent(e[1]);
  		
  		if (!this._menu)
  			return;
  		if (!this._menu.opened)
  			this.activate();
  		this._focus=false;
  		this.htmlObj.blur();
  		this._focus=true;
  		
  		this._menu.focus(); 
  	},
  	_selectedByValue:function(v,items)
  	{
		for (var i=0;i<items.length;i++)
		{
			if (v==items[i].value)
			{
				this.value=v;
				this.selectedIndex=i;
				return true;
			}
		}
  	},
	_itemSelected:function(index)
	{
		if (index!=this.selectedIndex)
		{	
			this.setSelectedIndex(index);
			this.onChange.fire(this.value);
		}
		this._menu.close();
	},
	setSelectedIndex:function(index)
	{
		this.value=this._menu.items[index].value;
		this.selectedIndex=index;
		this._menu.setSelectedByIndex(index,true);
		this.setContent(this._menu.items[this.selectedIndex].text);
	},
	setValue:function(value)
	{
		if (this._selectedByValue(value,this._menu.items))
		{
			this._menu.setSelectedByIndex(this.selectedIndex,true);
			this.setContent(this._menu.items[this.selectedIndex].text);
		}
	},
	getText: function()
	{
		return this._menu.items[this.selectedIndex].text;
	},
	destroy:function()
	{
		this.onChange.unsubscribeAll(); 
		MEB.Select.superclass.destroy.call(this);
	}
});
MEB.Country = function(configs)
{
	configs.items=new Array();
	var i=0;
	for (var key in MEB.lang.countries)
	{
		configs.items[i]=new Object();
		configs.items[i].value=key;
		configs.items[i].text=MEB.lang.countries[key];
		i++;
	}
	MEB.Country.superclass.constructor.call(this,configs);
}
Y.lang.extend(MEB.Country, MEB.Select);

MEB.Admin1 = function(configs)
{
	configs.items=new Array();
	this.value=configs.value;
	MEB.Admin1.superclass.constructor.call(this,configs);
	this.onInit = new Y.util.CustomEvent('initevent',this,true,Y.util.CustomEvent.FLAT); 
}
Y.lang.extend(MEB.Admin1, MEB.Select,{
	init: function(country){
		this._country= country;
		new MEB.Connect('POST', MEB.AJAX_BASE+'meb-core.php',{ ok:this._init ,scope:this} ,'cmd=admin1&country='+country, {t:this.htmlObj, p:'r'});
	},
	_init: function(root){
		var items= new Array();
		var nodes = root.getElementsByTagName('item');
		for(var i=0;i<nodes.length;i++){
			items[i]=new Object();
			items[i].value=nodes[i].getAttribute('id');
			items[i].text= nodes[i].firstChild.nodeValue;
			if (this.value && this.value==items[i].value)
				this.selectedIndex=i;
		}
		this.createMenu(items);
		
		if (this.selectedIndex>=0 && this._menu.items[this.selectedIndex])
			this.setContent(this._menu.items[this.selectedIndex].text);
		this.onInit.fire();
	}
});

MEB.Admin2 = function(configs)
{
	configs.items=new Array();
	this.value=configs.value;
	
	MEB.Admin2.superclass.constructor.call(this,configs);
	this.onInit = new Y.util.CustomEvent('initevent',this,true,Y.util.CustomEvent.FLAT); 
}
Y.lang.extend(MEB.Admin2, MEB.Select,{
	init: function(country,admin1){
		this._country= country;
		this._admin1= admin1;
		if (this._admin1)
			this.value.admin1=this.admin1;
		new MEB.Connect('POST', MEB.AJAX_BASE+'meb-core.php',{ ok:this._init ,scope:this} ,'cmd=admin2&country='+country+(this._admin1?'&admin1='+this._admin1:''), {t:this.htmlObj, p:'r'});
	},
	_init: function(root){
		var items= new Array();
		var nodes = root.getElementsByTagName('item');
		for(var i=0;i<nodes.length;i++){
			items[i]=new Object();
			items[i].value={admin2:nodes[i].getAttribute('id'),admin1:(this._admin1?this._admin1:nodes[i].getAttribute('admin1'))};
			items[i].text= nodes[i].firstChild.nodeValue;
			if (this.value && this.value.admin1==items[i].value.admin1 && this.value.admin2==items[i].value.admin2)
				this.selectedIndex=i;
		}
		this.createMenu(items);
		
		if (this.selectedIndex>=0 && this._menu.items[this.selectedIndex])
			this.setContent(this._menu.items[this.selectedIndex].text);
		this.onInit.fire();
	},
	_selectedByValue:function(v,items)
  	{
  		this.value=v;
		for (var i=0;i<items.length;i++)
		{
			if (this.value.admin1==items[i].value.admin1 && this.value.admin2==items[i].value.admin2)
			{
				this.value=v;
				this.selectedIndex=i;
				return true;
			}
		}
  	}
});

MEB.Language = function(configs)
{
	configs.items=new Array();
	var i=0;
	for (var key in MEB.lang.languages)
	{
		configs.items[i]=new Object();
		configs.items[i].value=key;
		configs.items[i].text=MEB.lang.languages[key];
		i++;
	}
	MEB.Language.superclass.constructor.call(this,configs);
}
Y.lang.extend(MEB.Language, MEB.Select);

MEB.Birthdate = function(oConfigs) 
{
	this.container=oConfigs.container;
	this.id=oConfigs.id;
	this.name=oConfigs.name;
	this.skipYears=oConfigs.skipYears!=undefined?oConfigs.skipYears:0;
	this.value=oConfigs.value;
	this.htmlObj=document.createElement('span');
	this.render(this.container);
	
	this.onChange = new Y.util.CustomEvent('changeevent',this,true,Y.util.CustomEvent.FLAT); 
};
MEB.Birthdate.prototype=
{
	_day:null,_month:null,_year:null,
	_updateDays:function(limit)
	{
		var days = new Array(),m=this._day.getMenu(),i,day;
		
		if (this._day.value> limit) 
			this._day.setValue(limit); 
		if (m.items.length > limit) 
		{
			for (i = m.items.length-1; i >=limit ; i--) 
			{
				m.removeItem(i);
			}
		}
		else if (m.items.length < limit) 
		{
			for (i = m.items.length+1; i <= limit; i++) 
			{
				day = new Object();
				day.text = i  + '';
				day.value = i ;
				m.addItem(day);
			}
		}
	},
	_makeDays:function(limit)
	{
		var i;
		var days = new Array();
		for (i = 0; i < limit; i++) 
		{
			days[i]=new Object();
			days[i].text=(i+1)+'';
			days[i].value=i+1;
		}  
		this._day = new MEB.Select({ 
			content:  this._day?this._day:MEB.lang.time.day,    
			items:  days, 
            container: this.htmlObj,
			selectedIndex:parseInt(this._day)-1
			}); 
		
		this._day.onChange.subscribe(this._updateValue,null,this);
	},
	_makeMonths:function()
	{
		var i,months = new Array();
			
		for (i = 0; i < 12; i++) 
		{
			months[i]=new Object();
			months[i].text=(i+1)+'';
			months[i].value=i+1;
			
		}  
		this._month = new MEB.Select({ 
			content:  this._month?this._month:MEB.lang.time.month,    
			items:  months, 
            container: this.htmlObj,
            selectedIndex:parseInt(this._month)-1
			}); 
		this._month.onChange.subscribe(function(value)
		{
			this._updateDays(32 - new Date(this._year.value?this._year.value:2000, value-1, 32).getDate());
			this._updateValue();
		},null,this);
	}, 
	_makeYears: function()
	{
		var years = new Array();
		var selectedYear;
		var date=new Date();
		
		for (x=0,i = date.getFullYear()-this.skipYears; i > 1900; i--,x++) 
		{
			years[x]=new Object();
			years[x].text=i+'';
			years[x].value=i;
			if (i==this._year)
				selectedYear=x;
		}  
		this._year = new MEB.Select({ 
			content:  this._year?this._year:MEB.lang.time.year,    
			items:  years, 
            container: this.htmlObj,
            selectedIndex:selectedYear
		}); 
		this._year.onChange.subscribe(function(value)
		{
			this._updateDays(32 - new Date(value, this._month.value?this._month.value-1:0, 32).getDate());
			this._updateValue();
		},null,this);
	},
    render: function (parentEl) 
	{ 
		var i,l,v=this.value;
		
		parentEl =	Y.util.Dom.get(parentEl);
		if (!parentEl) 
		    return null;
		
		if (v)
		{
			this._day = v.substr(6, 2);
			if (this._day.substr(0, 1) == '0') 
				this._day = this._day.substr(1, 1);
			
			this._month = v.substr(4, 2);
			if (this._month.substr(0, 1) == '0') 
				this._month = this._month.substr(1, 1);
			this._year = v.substr(0, 4);
			
			l=32 - new Date(this._year, this._month?this._month-1:0, 32).getDate();
		}
		else
			l=31;
		switch (MEB.lang.time.DATE_TYPE)
		{
			case ('2'):
				this._makeDays(l);
				this._makeMonths();
				this._makeYears();
			break;
			default:
			case ('1'):
				this._makeMonths();
				this._makeDays(l);
				this._makeYears();
			break;
		}
		
		parentEl.appendChild(this.htmlObj);
		var input=document.createElement('input');
		input.name = this.name;
		input.id = this.id || input.name;
		input.value = v;
		input.type='hidden';
		input.inputH=this;
		this.htmlObj.appendChild(input);
	},
	getRegion:function()
	{
		return Y.util.Dom.getRegion(this.htmlObj);
	},
	_updateValue:function ()
	{
		var day=this._day.value,month=this._month.value,v;
		if (day<10)
			day='0'+day;
		if (month<10)
			month='0'+month;
		
		v=this._year.value+''+month+''+day;
		
		this.value=v;
		Y.util.Dom.get(this.id|| this.name).value=v;
		this.onChange.fire();
	},
    destroy: function () 
	{ 
		this._day.destroy();
		this._month.destroy();
		this._year.destroy();
	}
}

MEB.IntInput = function(configs){
	this.id=configs.id;
	this.htmlObj = document.getElementById(this.id);
	Y.util.Event.addListener(this.htmlObj,'keydown',this._keyControl,null,this);
	Y.util.Event.addListener(this.htmlObj,'keyup',this._keyUp,null,this);
}
MEB.IntInput.prototype={
	_controlActive:false,
	_keyControl:function(e)
	{
		var key = Y.util.Event.getEvent(e).keyCode;
		if( key==17 || key==224){
			this._controlActive = true;
			return true;
		}
		if( (key>47 && key<58) || (key>95 && key<106) || key==8 || key==9 || key==13 || key==46 || (key>=37 && key<=40) || (this._controlActive && key==86))
			return true;
		else
		{
			Y.util.Event.stopEvent(e);
			return false;
		}
	},
	_keyUp:function(e)
	{
		var key = Y.util.Event.getEvent(e).keyCode;
		if( key==17 || key==224){
			this._controlActive = false;
		}
		if(this.htmlObj.value!='')
			this.htmlObj.value=parseInt(this.htmlObj.value);
	}
}
MEB.PhoneInput = function(configs){
	this.id=configs.id;
	this.htmlObj = document.getElementById(this.id);
	Y.util.Event.addListener(this.htmlObj,'keydown',this._keyControl,null,this);
	Y.util.Event.addListener(this.htmlObj,'keyup',this._keyUp,null,this);
}
MEB.PhoneInput.prototype={
	_controlActive:false,
	_keyControl:function(e){
		var key = Y.util.Event.getEvent(e ).keyCode;
		if( key==17 || key==224){
			this._controlActive = true;
			return true;
		}
		if( (key>47 && key<58) || (key>95 && key<106) || key==8 || key==9 || key==13 || key==46 || (key>=37 && key<=40) || key==32 || key==107 || key==111 || key==43 || (this._controlActive && key==86))
			return true;
		else
		{
			Y.util.Event.stopEvent(e);
			return false;
		}
	},
	_keyUp:function(e)
	{
		var key = Y.util.Event.getEvent(e).keyCode;
		if( key==17 || key==224){
			this._controlActive = false;
		}
	}
}



MEB.Textarea = function(configs){
	
	this.id=configs.id;
	this.max=configs.max;
	
	this.htmlObj = document.getElementById(this.id);
	this.min=this.htmlObj.clientHeight; //this.htmlObj.rows;
	this.htmlObj.wrap="hard";

	//Y.util.Event.addListener(this.htmlObj,'keydown',this.size,null,this);
	Y.util.Event.addListener(this.htmlObj,'keyup',this.size,null,this);
	Y.util.Event.addListener(this.htmlObj,'keydown',this._keydown,null,this);
	
	Y.util.Event.addListener(this.htmlObj,'change',this.size,null,this);
	this.htmlObj.style.overflow = 'hidden';
	this.size();
}
MEB.Textarea.prototype={
	_keydown:function(e)
	{
		var k=Y.util.Event.getEvent(e ).keyCode;
		if (k==13)
			this.size(e);
		return true;
	},
	size: function(e)
	{
	/*
		 if (!this.htmlObj.rows || this.htmlObj.rows < this.min) 
			 this.htmlObj.rows = this.min;
	     while ((this.htmlObj.clientHeight >= this.htmlObj.scrollHeight && this.htmlObj.rows > 1 && this.htmlObj.rows <= this.max) || this.htmlObj.rows > this.max) 
	    	 this.htmlObj.rows -= 1;
	     while ((this.htmlObj.clientHeight < this.htmlObj.scrollHeight || this.htmlObj.rows < this.min) && this.htmlObj.rows < this.max) 
	    	 this.htmlObj.rows += 1;
	    
	     if (this.htmlObj.rows == this.max && this.htmlObj.clientHeight < this.htmlObj.scrollHeight) 
	     	 this.htmlObj.style.overflow = 'auto';
	     else 
	     {
	    	 this.htmlObj.style.overflow = 'hidden';
	    	 this.htmlObj.style.overflowX = 'auto';
	     }
	     */
	     var key = Y.util.Event.getEvent(e ).keyCode,adjustedHeight = this.htmlObj.clientHeight;
	     if (key==8 || key==46)
	     {
	    	if (adjustedHeight>this.min && this.htmlObj.scrollHeight<=adjustedHeight)   
		    {     
	    		this.htmlObj.style.overflow = 'hidden';
	    		this.htmlObj.style.height = this.min + "px"; 
	    		
	    		if (this.htmlObj.scrollHeight>this.min)
	    			this.htmlObj.style.height = this.htmlObj.scrollHeight + "px"; 
		    }
	     }
	     else
	     {
	    	 if (!this.max ||this.max > adjustedHeight )   
			 {     
	    		 this.htmlObj.style.overflow = 'hidden';
	    		 adjustedHeight = Math.max(this.htmlObj.scrollHeight, adjustedHeight);  
	    		 
	    		 if (this.max )         
	    			adjustedHeight = Math.min(this.max, adjustedHeight);      
	    		 if (adjustedHeight > this.htmlObj.clientHeight)         
					this.htmlObj.style.height = adjustedHeight + "px";   
			}
			else
				this.htmlObj.style.overflow = 'auto';
	     }
	}
}

MEB.Color = function(init) {	

	if (init.hex)

		this.setHex(init.hex);

	else if (init.r)

		this.setRgb(init.r, init.g, init.b);

	else 

		this.setHsv(init.h, init.s, init.v);			

};

MEB.Color.prototype = {

    r: 0,g: 0,b: 0,h: 0,s: 0,v: 0,hex: '',

    setRgb: function(r, g, b) {

		this.r = r;

		this.g = g;

		this.b = b;

					

		var newHsv = MEB.Color.rgbToHsv(this);

		this.h = newHsv.h;

		this.s = newHsv.s;

		this.v = newHsv.v;

		

		this.hex = MEB.Color.rgbToHex(this);					

	},

		

	setHsv: function(h, s, v) {

		this.h = h;

		this.s = s;

		this.v = v;

		

		var newRgb = MEB.Color.hsvToRgb(this);

		this.r = newRgb.r;

		this.g = newRgb.g;

		this.b = newRgb.b;	

		

		this.hex = MEB.Color.rgbToHex(newRgb);	

	},

	

	setHex: function(hex) {

		this.hex = hex;

		var newRgb = MEB.Color.hexToRgb(this.hex);

		this.r = newRgb.r;

		this.g = newRgb.g;

		this.b = newRgb.b;

		

		var newHsv = MEB.Color.rgbToHsv(newRgb);

		this.h = newHsv.h;

		this.s = newHsv.s;

		this.v = newHsv.v;			

	}

}

MEB.Color.hexToRgb= function(hex) {

	//hex = MEB.Color.validateHex(hex);



	var r='00', g='00', b='00';

	

	/*

	if (hex.length == 3) {

		r = hex.substring(0,1);

		g = hex.substring(1,2);

		b = hex.substring(2,3);

	} else if (hex.length == 6) {

		r = hex.substring(0,2);

		g = hex.substring(2,4);

		b = hex.substring(4,6);

	*/

	if (hex.length == 6) {

		r = hex.substring(0,2);

		g = hex.substring(2,4);

		b = hex.substring(4,6);	

	} else {

		if (hex.length > 4) {

			r = hex.substring(4, hex.length);

			hex = hex.substring(0,4);

		}

		if (hex.length > 2) {

			g = hex.substring(2,hex.length);

			hex = hex.substring(0,2);

		}

		if (hex.length > 0) {

			b = hex.substring(0,hex.length);

		}					

	}

	

	return { r:MEB.Color.hexToInt(r), g:MEB.Color.hexToInt(g), b:MEB.Color.hexToInt(b) };

}

MEB.Color.htmlHex=function(htmlhex)

{

    if (htmlhex.length>6)

        return htmlhex.substring(1,7);

    return htmlhex;

}

MEB.Color.validateHex= function(hex) {

	hex = new String(hex).toUpperCase();

	hex = hex.replace(/[^A-F0-9]/g, '0');

	if (hex.length > 6) hex = hex.substring(0, 6);

	return hex;

}

MEB.Color.webSafeDec= function (dec) {

	dec = Math.round(dec / 51);

	dec *= 51;

	return dec;

}

MEB.Color.hexToWebSafe= function (hex) {

	var r, g, b;



	if (hex.length == 3) {

		r = hex.substring(0,1);

		g = hex.substring(1,1);

		b = hex.substring(2,1);

	} else {

		r = hex.substring(0,2);

		g = hex.substring(2,4);

		b = hex.substring(4,6);

	}

	return intToHex(MEB.Color.webSafeDec(MEB.Color.hexToInt(r))) + MEB.Color.intToHex(MEB.Color.webSafeDec(MEB.Color.hexToInt(g))) + MEB.Color.intToHex(MEB.Color.webSafeDec(MEB.Color.hexToInt(b)));

}

MEB.Color.rgbToWebSafe= function(rgb) 

{

	return {r: MEB.Color.webSafeDec(rgb.r), g: MEB.Color.webSafeDec(rgb.g), b: MEB.Color.webSafeDec(rgb.b) };

}

MEB.Color.rgbToHex= function (rgb) 

{

	return MEB.Color.intToHex(rgb.r) + MEB.Color.intToHex(rgb.g) + MEB.Color.intToHex(rgb.b);

}

MEB.Color.intToHex= function (dec)

{

	var result = (parseInt(dec).toString(16));

	if (result.length == 1)

		result = ("0" + result);

	return result.toUpperCase();

}

MEB.Color.hexToInt= function (hex)

{

	return(parseInt(hex,16));

}

MEB.Color.rgbToHsv= function (rgb) 

{

	var r = rgb.r / 255;

	var g = rgb.g / 255;

	var b = rgb.b / 255;



	hsv = {h:0, s:0, v:0};



	var min = 0

	var max = 0;



	if (r >= g && r >= b) {

		max = r;

		min = (g > b) ? b : g;

	} else if (g >= b && g >= r) {

		max = g;

		min = (r > b) ? b : r;

	} else {

		max = b;

		min = (g > r) ? r : g;

	}



	hsv.v = max;

	hsv.s = (max) ? ((max - min) / max) : 0;



	if (!hsv.s) {

		hsv.h = 0;

	} else {

		delta = max - min;

		if (r == max) {

			hsv.h = (g - b) / delta;

		} else if (g == max) {

			hsv.h = 2 + (b - r) / delta;

		} else {

			hsv.h = 4 + (r - g) / delta;

		}



		hsv.h = parseInt(hsv.h * 60);

		if (hsv.h < 0) {

			hsv.h += 360;

		}

	}

	

	hsv.s = parseInt(hsv.s * 100);

	hsv.v = parseInt(hsv.v * 100);



	return hsv;

}

MEB.Color.hsvToRgb= function (hsv) {



	rgb = {r:0, g:0, b:0};

	

	var h = hsv.h;

	var s = hsv.s;

	var v = hsv.v;



	if (s == 0) {

		if (v == 0) {

			rgb.r = rgb.g = rgb.b = 0;

		} else {

			rgb.r = rgb.g = rgb.b = parseInt(v * 255 / 100);

		}

	} else {

		if (h == 360) {

			h = 0;

		}

		h /= 60;



		// 100 scale

		s = s/100;

		v = v/100;



		var i = parseInt(h);

		var f = h - i;

		var p = v * (1 - s);

		var q = v * (1 - (s * f));

		var t = v * (1 - (s * (1 - f)));

		switch (i) {

			case 0:

				rgb.r = v;

				rgb.g = t;

				rgb.b = p;

				break;

			case 1:

				rgb.r = q;

				rgb.g = v;

				rgb.b = p;

				break;

			case 2:

				rgb.r = p;

				rgb.g = v;

				rgb.b = t;

				break;

			case 3:

				rgb.r = p;

				rgb.g = q;

				rgb.b = v;

				break;

			case 4:

				rgb.r = t;

				rgb.g = p;

				rgb.b = v;

				break;

			case 5:

				rgb.r = v;

				rgb.g = p;

				rgb.b = q;

				break;

		}



		rgb.r = parseInt(rgb.r * 255);

		rgb.g = parseInt(rgb.g * 255);

		rgb.b = parseInt(rgb.b * 255);

	}



	return rgb;

}



MEB.ColorPicker = function(configs)
{
	this.id!=undefined?this.id:Y.util.Dom.generateId();
	this._container=Y.util.Dom.get(configs.container);
	this._className=configs.className!=undefined?configs.className:'colorPicker';
	this.htmlObj=document.createElement('div');
	this.htmlObj.className=this._className;
	if (this.id)
		this.htmlObj.id=this.id;
	this.color=new MEB.Color({hex:configs.value});	
	this.value=this.color.hex;
	
	this.bcontrol=new MEB.Slider2D({container:this.htmlObj,className:this._className+'-base',size:[256,256],min:[0,0],max:[100,100],ticks:[256,256],value:[this.color.s,100-this.color.v]});
	this.hcontrol=new MEB.Slider({container:this.htmlObj,className:this._className+'-hue',axis:'y',length:256,min:0,max:359,ticks:256,value:(359-this.color.h)});;
	
	this._updateBase();
	this._container.appendChild(this.htmlObj);
	
	this.onChange = new Y.util.CustomEvent('changeEvent',this,true,Y.util.CustomEvent.FLAT); 
	this.bcontrol.onChange.subscribe(this._baseChange,null,this);
	this.hcontrol.onChange.subscribe(this._hueChange,null,this);
}
MEB.ColorPicker.prototype =
{
	htmlObj:null,base:null,bcontrol:null,hcontrol:null,_container:null,_className:null,value:0,color:null,
	_updateBase:function()
	{
	    var v=new MEB.Color({h:this.color.h,s:100,v:100});
	    Y.util.Dom.setStyle(this.bcontrol.htmlObj,'background-color','#'+v.hex); 
	    delete v;
	},
	_baseChange: function (value) 
	{ 
	    this.color.setHsv(this.color.h,value[0],100-value[1]);
	    this._setValue(this.color.hex);
       	this.onChange.fire(this.value);
       	
	},
	_hueChange: function (value) 
	{ 
	    this.color.setHsv(359-value,this.color.s,this.color.v);
	   
	    this._updateBase();
        this._setValue(this.color.hex);
       	this.onChange.fire(this.value);
	},
	_setValue:function(value)
	{
		this.value=value; 
	},
	setValue:function(value)
	{
	    this.color.setHex(value);
		this.bcontrol.setValue([this.color.s,this.color.v]);
		this.hcontrol.setValue(this.color.h);
		this._updateBase();
		this._setValue(this.color.hex);
	},
	destroy:function()
	{
	    this.onChange.unsubscribeAll(); 
	    this.bcontrol.destroy();
	    this.hcontrol.destroy();
	}
}


MEB.ColorPickerButton = function(configs) 
{
	MEB.ColorPickerButton.superclass.constructor.call(this,configs);
	this.setContent('<div class="selectedColor" id="'+this.id+'_color"></div>');
	this.htmlObj.name=this.name=configs.name;
		
	this._menu=new MEB.Menu({alignment:{obj:this.htmlObj,context:'bl'}});
	this._content=Y.util.Dom.get(this.id+'_color');
	this._setValue(configs.value);
	 	
	this._menu.onOpen.subscribe(this._open,null,this);
	this.onChange = new Y.util.CustomEvent('changeEvent',this,true,Y.util.CustomEvent.FLAT); 
}
Y.lang.extend(MEB.ColorPickerButton, MEB.MenuButton,{
	name:null,_content:null,value:null,_colorPicker:null,
	_open:function()
	{
		if (!this._colorPicker)
		{
			this._colorPicker = new MEB.ColorPicker({container:this._menu.htmlObj,value:this.value});
			this._colorPicker.onChange.subscribe(this._onChange,null,this);
		}
    },
	_onChange: function () 
	{ 
        this._setValue(this._colorPicker.value);
       	this.onChange.fire(this._colorPicker.value);
	},
	_setValue:function(value)
	{
	    this.value=value; 
		Y.util.Dom.setStyle(this._content, "background-color", '#'+value); 
	},
	setValue:function(value)
	{
		this._colorPicker.setValue(value);
		this._setValue(this._colorPicker.value);
	},
	destroy:function()
	{
		this._menu.destroy();
		this.onChange.unsubscribeAll();
		MEB.ColorPickerButton.superclass.destroy.call(this);
	}
});
MEB.Slider = function(configs)
{
	this.id!=undefined?this.id:Y.util.Dom.generateId();
	this._container=Y.util.Dom.get(configs.container);
	this._length=configs.length;
	this._noActiveBase=configs.noActiveBase!=undefined?configs.noActiveBase:false;
	this._axis=configs.axis;
	this._className=configs.className!=undefined?configs.className:'slider-'+this._axis;
	this.htmlObj=document.createElement('div');
	this.htmlObj.className=this._className;
	if (this.id)
		this.htmlObj.id=this.id;
	
	this.base=document.createElement('div');
	this.base.className=this._className+'-base';
	if (!this._noActiveBase)
	{
		this.activeBase=document.createElement('div');
		this.activeBase.className=this._className+'-active';
	}
	this.htmlObj.appendChild(this.base);
	this.base.appendChild(this.activeBase);
	
	this._size();
	
	this.htmlObj.appendChild(this.base);
	
	this.control=document.createElement('div');
	this.control.className=this._className+'-control';
	this.base.appendChild(this.control);
	
	this.control.unfocusable=true;
	this.base.unfocusable=true;
	
	this._container.appendChild(this.htmlObj);
	
	Y.util.Event.addListener(this.base, "mousedown", this.mousedown, null,this); 
	Y.util.Event.addListener(this.htmlObj, "click", this._stopEvent, null,this); 
	if (configs.values)
		this.initByValues(configs.values,configs.value!=undefined?configs.value:configs.values[0]);
	else
		this.init(configs.min!=undefined?configs.min:0,configs.max!=undefined?configs.max:0,configs.ticks!=undefined?configs.ticks:configs.length,configs.value!=undefined?configs.value:configs.min)
	this.onChange=new Y.util.CustomEvent('changeevent', this , true , Y.util.CustomEvent.FLAT  ); 
	this.onDragStart=new Y.util.CustomEvent('dragstartevent', this , true , Y.util.CustomEvent.FLAT  ); 
	this.onDragStop=new Y.util.CustomEvent('dragstopevent', this , true , Y.util.CustomEvent.FLAT  ); 
}
MEB.Slider.prototype =
{
	htmlObj:null,base:null,control:null,_container:null,_reg:null,_className:null,_values:null,_length:null,_ticks:null,_min:null,_max:null,_range:null,_axis:null,_dragOffset:0,_controlLength:0,
	_stopEvent:function(e)
	{
		Y.util.Event.stopEvent(e);
	},
	init:function(min,max,ticks,value)
	{
		this._min=min;
		this._max=max;
		this._ticks=ticks;
		
		this._tickSize=Math.round(this._length/(this._ticks-1));
		this._range=this._max-this._min;
		this.setValue(value);
	},
	initByValues:function(values,value)
	{
		this._values=values;
		this._ticks=values.length;
		
		this._tickSize=Math.round(this._length/(this._ticks-1));
		this.setValue(value);
	},
	mousedown:function(e)
	{
		var pos=Y.util.Event.getXY(e);
		this._reg=Y.util.Dom.getRegion(this.base);
		var cr=Y.util.Dom.getRegion(this.control),cp;
		
		if (this._axis=='x')
			cp=cr.left-this._reg.left;
		else
			cp=cr.top-this._reg.top; 
		
		Y.util.Dom.addClass(this.control,'hover');
		
		this._checkPos(pos,cp);
		this.base.style.cursor='pointer';
		this.onDragStart.fire();
		MEB.disableSelection(document.body);
		Y.util.Event.addListener(document, "mouseup", this.mouseup, null,this);   
 		Y.util.Event.addListener(document, "mousemove", this.mousemove, null,this); 
 		Y.util.Event.stopEvent(e);
	},
	_size:function()
	{
		if (this._axis=='x')
			this.htmlObj.style.width=this.base.style.width=this._length+'px';
		else
			this.htmlObj.style.height=this.base.style.height=this._length+'px';
	},
	_checkPos:function(pos,first)
	{
		var v,p;
		if (this._axis=='x')
			p=pos[0]-this._reg.left;
		else
			p=pos[1]-this._reg.top; 
		if (this._controlLength)
		{
			if (first!=undefined)
			{
				if (p>=first && p<first+this._controlLength)
				{
					this._dragOffset=p-first; 
					return true;
				}
				else
				{
					p-=this._controlOffset;
					if (p<0)
						this._dragOffset=this._controlOffset+p;
					else if (p>this._length)
						this._dragOffset=this._controlOffset+(p-this._length);
					else
						this._dragOffset=this._controlOffset;
				}
			}
			else
				p-=this._dragOffset;
		}
		if (p<0)
			p=0;
		var tick=Math.round(p/this._tickSize);
		p=tick*this._tickSize;
		
		if (p>this._length)
		{
			p=this._length;
			if (this._values)
				tick=this._values.length-1;
		}
		if (!this._values)
			v=(p*this._range)/(this._length)+this._min;
		else
			v=this._values[tick];
		
		if (v!=this.value)
		{
			this._setValue(v);
			this._updateView(p);
			this.onChange.fire(v);
		}
	},
	_setValue:function(v)
	{
		this.value=v;
	},
	_updateView:function(p)
	{
		try
		{
			if (this._axis=='x')
				this.control.style.left=p+'px';
			else
				this.control.style.top=p+'px';
			if (!this._noActiveBase)
				this._updateActiveBase(p);
		}
		catch(e)
		{
			
			
		}
	},
	_updateActiveBase:function(p)
	{
		if (this._axis=='x')
		{
			this.activeBase.style.width=p+'px';
		}
		else
		{
			this.activeBase.style.height=p+'px';
		}
	},
	setValue:function(v)
	{
		this.value=v;
		if (!this._values)
			this._updateView(((v-this._min)*this._length)/this._range);
		else
		{
			for(var i=0;i<this._values.length;i++)
			{
				if (v==this._values[i])
					break;
			}
			var p=i*this._tickSize;
			//se v è rotto, allora aggiusto la p ma non il v stesso (stica)
			if (p>this._length)
				p=this._length;
			this._updateView(p);
		}
	},
	mouseup:function(e)
	{
		Y.util.Event.removeListener(document, "mousemove", this.mousemove); 
		Y.util.Event.removeListener(document, "mouseup", this.mouseup);   
		Y.util.Dom.removeClass(this.control,'hover');
		this.base.style.cursor='default';
		this._dragOffset=0;
		this._stopEvent(e);
		MEB.enableSelection(document.body);
		this.onDragStop.fire();
	},
	mousemove:function(e)
	{
		var pos=Y.util.Event.getXY(e);
		this._checkPos(pos);
	},
	destroy:function()
	{
	    this.htmlObj.parentNode.removeChild(this.htmlObj);
		this.onChange.unsubscribeAll(); 
		this.onDragStart.unsubscribeAll(); 
		this.onDragStop.unsubscribeAll(); 
		
	}
}
//configs: scroll, axis
MEB.ScrollBar=function(configs)
{
	if (!configs.className)
		configs.className='scrollbar-'+configs.axis;
	configs.className.noActiveBase=true;
	this._scroll=configs.scroll;
	this._target=this._scroll.getTarget();
	MEB.ScrollBar.superclass.constructor.call(this,configs);
	this._scroll.onScroll.subscribe(this._scrolled,null,this);
}
Y.lang.extend(MEB.ScrollBar, MEB.Slider,{
	_size:function()
	{
	},
	mousedown:function(e)
	{
		this._scroll.onScroll.unsubscribe(this._scrolled);
		MEB.ScrollBar.superclass.mousedown.call(this,e);
	},
	mouseup:function(e)
	{
		this._scroll.onScroll.subscribe(this._scrolled,null,this);
		MEB.ScrollBar.superclass.mouseup.call(this,e);
	},
	init:function()
	{
		
		var r=Y.util.Dom.getRegion(this._target);
		if (this._axis=='x')
		{
			this._scrollLength=this._target.scrollWidth;
			this._tlength=r.right-r.left;
		}
		else
		{
			this._scrollLength=this._target.scrollHeight;
			this._tlength=r.bottom-r.top;
		}
		
		this._controlLength=Math.round(this._tlength*this._tlength/this._scrollLength);
		this._controlOffset=this._controlLength/2;
		this._length=this._tlength-this._controlLength;
		if (this._axis=='x')
		{
			this.htmlObj.style.width=this._tlength+'px';
			this.base.style.width=this._tlength+'px';
			this.control.style.width=this._controlLength+'px';
		}
		else
		{
			this.htmlObj.style.height=this._tlength+'px';
			this.base.style.height=this._tlength+'px';
			this.control.style.height=this._controlLength+'px';
		}
		this._min=0;
		this._max=this._scrollLength-this._tlength;
		this._ticks=this._scrollLength-this._tlength+1;
		
		this._tickSize=Math.round(this._length/(this._ticks-1));
		if (this._tickSize<1)
		{
			this._tickSize=1;
			this._ticks=this._length;
		}
		
		this._range=this._max-this._min;
		var c=this._scroll.getScroll();
		this.setValue(c[this._axis]);
	},
	_scrolled:function()
	{
		var c=this._scroll.getScroll();
		this.setValue(c[this._axis]);
	},
	_setValue:function(v)
	{
		this.value=v;
		
		if (this._axis=='x')
			this._scroll.setHScroll(this.value);
		else
			this._scroll.setVScroll(this.value);
	}
});
MEB.SliderDouble = function(configs)
{
	this.id!=undefined?this.id:Y.util.Dom.generateId();
	this._container=Y.util.Dom.get(configs.container);
	this._length=configs.length;
	this._axis=configs.axis;
	this._className=configs.className!=undefined?configs.className:'slider-'+this._axis;
	this.htmlObj=document.createElement('div');
	this.htmlObj.className=this._className;
	if (this.id)
		this.htmlObj.id=this.id;
	
	this.base=document.createElement('div');
	this.base.className=this._className+'-base';
	this.activeBase=document.createElement('div');
	this.activeBase.className=this._className+'-active';
	if (this._axis=='x')
		this.htmlObj.style.width=this.base.style.width=this._length+'px';
	else
		this.htmlObj.style.height=this.base.style.height=this._length+'px';
	this.htmlObj.appendChild(this.base);
	this.base.appendChild(this.activeBase);
	
	this.controlSx=document.createElement('div');
	this.controlSx.className=this._className+'-control-sx';
	this.base.appendChild(this.controlSx);
	this.controlDx=document.createElement('div');
	this.controlDx.className=this._className+'-control-dx';
	this.base.appendChild(this.controlDx);
	this._container.appendChild(this.htmlObj);
	
	Y.util.Event.addListener(this.controlSx, "mousedown", this.mousedown, {who:'sx'},this); 
	Y.util.Event.addListener(this.controlDx, "mousedown", this.mousedown, {who:'dx'},this); 
	Y.util.Event.addListener(this.htmlObj, "click", this._stopEvent, null,this); 
	
	if (!configs.values)
		this.init(configs.min!=undefined?configs.min:0,configs.max!=undefined?configs.max:0,configs.ticks!=undefined?configs.ticks:configs.length,configs.valueSx!=undefined?configs.valueSx:configs.min,configs.valueDx!=undefined?configs.valueDx:configs.max)
	else
		this.initByValues(configs.values,configs.valueSx!=undefined?configs.valueSx:configs.values[0],configs.valueDx!=undefined?configs.valueDx:configs.values[configs.values.length-1]);
		
	this.onChange=new Y.util.CustomEvent('changeevent', this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onDragStart=new Y.util.CustomEvent('dragstartevent', this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onDragStop=new Y.util.CustomEvent('dragstopevent', this , true , YAHOO.util.CustomEvent.FLAT  ); 
}
MEB.SliderDouble.prototype =
{
	htmlObj:null,base:null,controlSx:null,controlDx:null,_container:null,_reg:null,_className:null,_length:null,_ticks:null,_min:null,_max:null,_range:null,_axis:null,
	_stopEvent:function(e)
	{
		Y.util.Event.stopEvent(e);
	},
	init:function(min,max,ticks,valueSx,valueDx)
	{
		this._min=min;
		this._max=max;
		this._ticks=ticks;
		
		this._tickSize=Math.round(this._length/(this._ticks-1));
		this._range=this._max-this._min;
		
		this._initValue('sx',valueSx);
		this._initValue('dx',valueDx);
		this._updateActiveBase();
	},
	initByValues:function(values,valueSx,valueDx)
	{
		this._values=values;
		this._ticks=values.length;
		
		this._tickSize=Math.round(this._length/(this._ticks-1));
		this._initValue('sx',valueSx);
		this._initValue('dx',valueDx);
		this._updateActiveBase();
	},
	mousedown:function(e,arg)
	{
		var pos=Y.util.Event.getXY(e);
		this._reg=Y.util.Dom.getRegion(this.base);
		this._checkPos(arg.who,pos);
		this.base.style.cursor='pointer';
		this.onDragStart.fire(arg.who);
		
		if(arg.who=='sx')
			Y.util.Dom.addClass(this.controlSx,'hover');
		else
			Y.util.Dom.addClass(this.controlDx,'hover');
			
		Y.util.Event.addListener(document, "mouseup", this.mouseup, {who: arg.who},this);   
 		Y.util.Event.addListener(document, "mousemove", this.mousemove, {who: arg.who},this); 
		Y.util.Event.stopEvent(e);
	},
	_checkPos:function(who,pos)
	{
		var v,p,other;
		if (this._axis=='x')
			p=pos[0]-this._reg.left;
		else
			p=pos[1]-this._reg.top; 
		if (p<0)
			p=0;
		
		var tick=Math.round(p/this._tickSize);
		p=tick*this._tickSize;
		if (p>this._length)
			p=this._length;
		
		if(who=='sx')
		{
			if(p>=this._p_dx)
				p=this._p_dx-this._tickSize;
		}
		else
		{
			if(p<=this._p_sx)
				p=this._p_sx+this._tickSize;
		}
		if (!this._values)
			v=(p*this._range)/(this._length)+this._min;
		else
			v=this._values[tick];
		
		if(who=='sx' && v!=this.valueSx)
		{
			this._setValue(who,v);
			this._updateView(who,p);
			this.onChange.fire({who:who,value:v});
			return;
		}
		if(who=='dx' && v!=this.valueDx)
		{
			this._setValue(who,v);
			this._updateView(who,p);
			this.onChange.fire({who:who,value:v});
			return;
		}
	},
	_setValue:function(who,v)
	{
		if(who=='sx')
			this.valueSx=v;
		if(who=='dx')
			this.valueDx=v;
		return;
	},
	_updateView:function(who,p,noUpdateActiveBase)
	{
		if (who=='sx')
			this._p_sx=p;
		else
			this._p_dx=p;
		if (this._axis=='x'){
			if(who=='sx')
				this.controlSx.style.left=p+'px';
			if(who=='dx')
				this.controlDx.style.left=p+'px';
			if (!noUpdateActiveBase)
				this._updateActiveBase();
		}else{
			if(who=='sx')
				this.controlSx.style.top=p+'px';
			if(who=='dx')
				this.controlDx.style.top=p+'px';
			if (!noUpdateActiveBase)
				this._updateActiveBase();
		}	
	},
	_updateActiveBase:function()
	{
		if (this._axis=='x')
		{
			this.activeBase.style.left=this._p_sx+'px';
			this.activeBase.style.width=(this._p_dx-this._p_sx)+'px';
		}
		else
		{
			this.activeBase.style.top=this._p_sx+'px';
			this.activeBase.style.height=(this._p_dx-this._p_sx)+'px';
		}
	},
	setValue:function(who,v)
	{
		if (!this._values)
		{
			this._setValue(who,v);
			this._updateView(who,((v-this._min)*this._length)/this._range);
		}
		else
		{
			var p=this._findValue(v)*this._tickSize;
			if (p>this._length)
				p=this._length;
		
			//se v è rotto, allora aggiusto la p ma non il v stesso (stica)
			if(who=='sx')
			{
				if(p>=this._p_dx)
					p=this._p_dx-this._tickSize;
			}
			else
			{
				if(p<=this._p_sx)
					p=this._p_sx+this._tickSize;
			}
			this._setValue(who,v);
			this._updateView(who,p);
		}
	},
	_findValue:function(v)
	{
		for(var i=0;i<this._values.length;i++)
		{
			if (v==this._values[i])
				return i;
		}
		return i;
	},
	_initValue:function(who,v)
	{
		if (!this._values)
		{
			this._setValue(who,v);
			this._updateView(who,((v-this._min)*this._length)/this._range,true);
		}
		else
		{
			var p=this._findValue(v)*this._tickSize;
			if (p>this._length)
				p=this._length;
			
			//se v è rotto, allora aggiusto la p ma non il v stesso (stica)
			if(who=='sx')
			{
				if(p>=this._p_dx)
					p=this._p_dx-this._tickSize;
			}
			else
			{
				if(p<=this._p_sx)
					p=this._p_sx+this._tickSize;
			}
			this._setValue(who,v);
			this._updateView(who,p,true);
		}
	},
	mouseup:function(e,arg)
	{
		Y.util.Event.removeListener(document, "mousemove", this.mousemove); 
		Y.util.Event.removeListener(document, "mouseup", this.mouseup);   
		this.base.style.cursor='default';
		if(arg.who=='sx')
			Y.util.Dom.removeClass(this.controlSx,'hover');
		else
			Y.util.Dom.removeClass(this.controlDx,'hover');
		this.onDragStop.fire(arg.who);
	},
	mousemove:function(e,arg)
	{
		var pos=Y.util.Event.getXY(e);
		this._checkPos(arg.who,pos);
	},
	destroy:function()
	{
	    this.htmlObj.parentNode.removeChild(this.htmlObj);
		this.onChange.unsubscribeAll(); 
		this.onDragStart.unsubscribeAll(); 
		this.onDragStop.unsubscribeAll(); 
		
	}
}
MEB.Slider2D = function(configs)
{
	this.id!=undefined?this.id:Y.util.Dom.generateId();
	this._container=Y.util.Dom.get(configs.container);
	this._size=configs.size;
	
	this._className=configs.className!=undefined?configs.className:'slider2d';
	this.htmlObj=document.createElement('div');
	this.htmlObj.className=this._className;
	if (this.id)
		this.htmlObj.id=this.id;
	
	this.base=document.createElement('div');
	this.base.className=this._className+'-base';
	this.htmlObj.style.width=this.base.style.width=this._size[0]+'px';
	this.htmlObj.style.height=this.base.style.height=this._size[1]+'px';
	this.htmlObj.appendChild(this.base);
	
	this.control=document.createElement('div');
	this.control.className=this._className+'-control';
	
	this.base.appendChild(this.control);
	this._container.appendChild(this.htmlObj);
	
	Y.util.Event.addListener(this.base, "mousedown", this.mousedown, null,this); 
	Y.util.Event.addListener(this.htmlObj, "click", this._stopEvent, null,this); 
	
	this.init(configs.min!=undefined?configs.min:[0,0],configs.max!=undefined?configs.max:[0,0],configs.ticks!=undefined?configs.ticks:configs.length,configs.value!=undefined?configs.value:configs.min)
	this.onChange=new Y.util.CustomEvent('changeevent', this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onDragStart=new Y.util.CustomEvent('dragstartevent', this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onDragStop=new Y.util.CustomEvent('dragstopevent', this , true , YAHOO.util.CustomEvent.FLAT  ); 
}
MEB.Slider2D.prototype =
{
	htmlObj:null,base:null,control:null,_container:null,_reg:null,_className:null,_size:null,_ticks:null,_min:null,_max:null,_range:null,_axis:null,
	_stopEvent:function(e)
	{
		Y.util.Event.stopEvent(e);
	},
	init:function(min,max,ticks,value)
	{
		this._min=min;
		this._max=max;
		this._ticks=ticks;
		
		this._tickSize=[Math.round(this._size[0]/(this._ticks[0]-1)),Math.round(this._size[1]/(this._ticks[1]-1))];
		this._range=[this._max[0]-this._min[0],this._max[1]-this._min[1]];
		
		this.setValue(value);
	},
	mousedown:function(e)
	{
		var pos=Y.util.Event.getXY(e);
		this._reg=Y.util.Dom.getRegion(this.base);
		
		this._checkPos(pos);
		this.htmlObj.style.cursor='pointer';
		this.onDragStart.fire();
		
		Y.util.Event.addListener(document, "mouseup", this.mouseup, null,this);   
 		Y.util.Event.addListener(document, "mousemove", this.mousemove, null,this); 
		Y.util.Event.stopEvent(e);
		
	},
	_checkPos:function(pos)
	{
		var v,p;
		p=[pos[0]-this._reg.left,pos[1]-this._reg.top];
		if (p[0]<0)
			p[0]=0;
		if (p[0]>this._size[0])
			p[0]=this._size[0];
		if (p[1]<0)
			p[1]=0;
		if (p[1]>this._size[1])
			p[1]=this._size[1];
		
		p=[Math.round(p[0]/this._tickSize[0])*this._tickSize[0],Math.round(p[1]/this._tickSize[1])*this._tickSize[1]];
		v=[(p[0]*this._range[0])/this._size[0]+this._min[0],(p[1]*this._range[1])/this._size[1]+this._min[1]];
		if (v!=this.value)
		{
			this._setValue(v);
			this._updateView(p);
			this.onChange.fire(v);
		}
	},
	_setValue:function(v)
	{
		this.value=v;
	},
	_updateView:function(p)
	{
		this.control.style.left=p[0]+'px';
		this.control.style.top=p[1]+'px';
			
	},
	setValue:function(v)
	{
		this._setValue(v);
		this._updateView([((v[0]-this._min[0])*this._size[0])/this._range[0],((v[1]-this._min[1])*this._size[1])/this._range[1]]);
	},
	mouseup:function(e)
	{
		Y.util.Event.removeListener(document, "mousemove", this.mousemove); 
		Y.util.Event.removeListener(document, "mouseup", this.mouseup);   
		this.base.style.cursor='default';
		this._stopEvent(e);
		this.onDragStop.fire();
	},
	mousemove:function(e)
	{
		var pos=Y.util.Event.getXY(e);
		this._checkPos(pos);
	},
	destroy:function()
	{
	    this.htmlObj.parentNode.removeChild(this.htmlObj);
		this.onChange.unsubscribeAll(); 
		this.onDragStart.unsubscribeAll(); 
		this.onDragStop.unsubscribeAll(); 
	}
}
MEB.SliderButton = function(configs) 
{
	MEB.SliderButton.superclass.constructor.call(this,configs);
	this.setContent(null);
	this.htmlObj.name=this.name=configs.name;
	Y.util.Dom.addClass(this.htmlObj,'slider-button');
	if (configs.width!=undefined)
	{
		this._width=configs.width;
		this.htmlObj.style.width=configs.width;
	}
	this._unit=configs.unit;
	
	this._length=configs.length!=undefined?configs.length:100;
	this._min=configs.min!=undefined?configs.min:0;
	this._max=configs.max!=undefined?configs.max:100;
	this._ticks=configs.ticks!=undefined?configs.ticks:this._length;
	
	this._menu=new MEB.Menu({alignment:{obj:this.htmlObj,context:'bl'}});
	this._content=Y.util.Dom.get(this.id+'_content');
	
	this._setValue(configs.value);
	this._menu.onOpen.subscribe(this._open,null,this);
	this.onChange = new Y.util.CustomEvent('changeevent',this,true,Y.util.CustomEvent.FLAT); 
}
Y.lang.extend(MEB.SliderButton, MEB.MenuButton,{
	name:null,value:null,_slider:null,
	_open:function()
	{
		if (!this._slider)
		{
			this._slider = new MEB.Slider({container:this._menu.htmlObj,axis:'x',length:this._length,min:this._min,max:this._max,ticks:this._ticks});
			this._slider.onChange.subscribe(this._onChange,null,this);
		}
		this._slider.setValue(this.value);
    },
	_onChange: function (value) 
	{ 
        this._setValue(value);
       	this.onChange.fire(value);
	},
	_setValue:function(value)
	{
		this.value=value; 
		this._content.innerHTML=this.value+' '+this._unit;
	},
	setValue:function(value)
	{
		this._slider.setValue(value);
		this._setValue(value);
	},
	destroy:function()
	{
		this._menu.destroy();
		this.onChange.unsubscribeAll(); 
		MEB.SliderButton.superclass.destroy.call(this);
	}
});

/*
* 	@class ImageLoader
*	@requires yahoo-dom-event
* 	
*	@info: 	classe che esegue il loading delle immagini, al termine esegue l'evento onLoad passandogli il numero delle immagini caricate
*
* 	@param (array())   images 	  array contenente l'URL delle immagini da caricare
    @param (function)  fn     	  callback
    @param (mixed)     arg        argomenti da passare alla callback
    @param (object)    scope      oggetto di scope per la callback
*/
MEB.ImageLoader = function(images,fn,arg,scope)
{
	this.onLoad = new Y.util.CustomEvent('onLoad',this,true,Y.util.CustomEvent.FLAT);
	if (fn)
	    this.onLoad.subscribe(fn,arg,scope);
	this.nLoaded = 0;
	this.nProcessed = 0;
	this.aImages = new Array;
	
	this.nImages = images.length;
	
	for ( var i = 0; i < images.length; i++ )
		this._preload(images[i]);
}

MEB.ImageLoader.prototype = {
   
   _preload: function(image)
   {
		var oImage = new Image;
		this.aImages.push(oImage);
	 	
	 	oImage.onload = this._onLoad;
	 	oImage.onerror = this._onError;
	 	oImage.onabort = this._onAbort;
	 	oImage.oImagePreloader = this;
	 	oImage.bLoaded = false;
	
	 	oImage.src = image;
	},

	_onComplete: function()
	{
   		this.nProcessed++;
   		if ( this.nProcessed == this.nImages ){
      		this.onLoad.fire(this.nLoaded);
      	}
	},
	_onLoad: function()
	{
   		this.bLoaded = true;
   		this.oImagePreloader.nLoaded++;
   		this.oImagePreloader._onComplete();
	},

	_onError: function()
	{
   		this.bError = true;
  	 	this.oImagePreloader._onComplete();
	},
	
	_onAbort: function()
	{
   		this.bAbort = true;
   		this.oImagePreloader._onComplete();
	}
}

/*
* 	@class Tooltip
*	@requires yahoo-dom-event, animation, imageloader
* 	
*	@info: 	Classe Tooltip
*
*	@param (string) 	code			codice del tooltip: alla visualizzazione viene chiuso (se esiste) un altro tooltip con lo stesso codice 
* 	@param (string) 	type			tipo di tooltip, valori ammessi error o tip.
	@param (htmlObj)  	target			oggetto html del quale farà riferimento il tooltip
	@param (string)		content			Contenuto del tooltip, anche html
	@param (int)  		width			larghezza del tooltip, default nessuna, si adatta al contenuto 
	@param (string)  	align			poisione in riferimento al target, sul quale viene posizionato il tooltip
										valori ammessi : lt, t, ttl, ttr, rt, r, br, b, bl, l
	@param (bool)		closeButton		true or false se si vuole vedere il bottone di chiusura del tooltip, default (true).
	@param (bool)		closeOthers		true or false se si che il bottone prima di visualizzarsi chiuda gli altri tooltip aperti dello stesso type (error o tip)
	@param (bool)		closeOnChange	true or false se si vuole che il bottone si chiuda con l'evento onChange del target: se non c'è closebutton closeOnChange=true, default (true).
*/
MEB.Tooltip = function (oConfigs)
{
	this.code = oConfigs.code;
	this._type = oConfigs.type?oConfigs.type:'default';
	this._target = oConfigs.target;
	this._content = oConfigs.content;
	this._align = oConfigs.align ? oConfigs.align : 't';
	this._firstAlign = this._align;
	this._width = oConfigs.width ? parseInt(oConfigs.width) : null;
	this._animTime = oConfigs.animTime ? oConfigs.animTime : 0.4;
	this._tipLatency = oConfigs.tipLatency ? oConfigs.tipLatency : 300;
	this._localEnv = oConfigs.localEnv?oConfigs.localEnv:false;
	if(this._type!='tip')
	{
		this._closeButton = oConfigs.closeButton!=null ? oConfigs.closeButton : true;
		this._closeOthers = oConfigs.closeOthers!=null ? oConfigs.closeOthers : true;
		this._closeOnChange = oConfigs.closeOnChange!=null ? true : false;
		this._focus = oConfigs.focus!=null ? oConfigs.focus : true;
		this._onChangeArray = (oConfigs.closeOnChange && oConfigs.closeOnChange.constructor == Array) ? oConfigs.closeOnChange : null;
	}
		
	if (window.frameElement && parent.Y && !this._localEnv)
	{
		this._Y = parent.Y;
		this._MEB = parent.MEB;
		this._document = parent.document;
		this._frame=true;
	}
	else
	{
		this._offsetLeft = this._offsetTop = 0;
		this._document = document;
		this._Y = Y;
		this._MEB = MEB;
	}
	
	if(this._onChangeArray){
		for(var i=0;i<this._onChangeArray.length;i++){
			this._addCloseOnChange(this._onChangeArray[i]);
		}
	}else if(!this._closeButton || this._closeOnChange){
		this._addCloseOnChange(this._target);
	}
	
	if(window.frameElement)
		MEB.onUnload.subscribe(this._unloaded,null,this);

	this.onClose = new Y.util.CustomEvent('closeEvent',this,true,Y.util.CustomEvent.FLAT); 
	
	this._alignTried = new Array();
	this._alignTried[this._firstAlign] = false;
	this._alignTried['t'] = false;
	this._alignTried['ttl'] = false;
	this._alignTried['ttr'] = false;
	this._alignTried['rt'] = false;
	this._alignTried['r'] = false;
	this._alignTried['rb'] = false;
	this._alignTried['b'] = false;
	this._alignTried['lb'] = false;
	this._alignTried['l'] = false;
	this._alignTried['lt'] = false;
	
	if(this._type=='tip')
	{
		Y.util.Event.addListener(this._target,'mouseover',this.openTip,null,this);
		Y.util.Event.addListener(this._target,'mouseout',this.closeTip,null,this);
	}
	else
	{
		this.open();
	}
}

MEB.Tooltip.prototype = {
	visible:false,_isBind:false,
	getEnv:function()
	{
		return {document:this._document,Y:this._Y,MEB:this._MEB};
	},
	getType:function()
	{
		return this._type;
	},
	isAutoclose:function()
	{
		return this._closeButton?false:true;
	},
	_addCloseOnChange:function(el)
	{
		if (el.tagName)
		{
			if( el.tagName=="INPUT" || el.tagName=="TEXTAREA" ) 
				Y.util.Event.addListener(el,'keydown',this.close,null,this);
		}
		else
		{
			if (el.onChange)
				el.onChange.subscribe(this.close,null,this);
			else if (el.onActivate)
				el.onActivate.subscribe(this.close,null,this);
		}
	},
	openTip:function()
	{
		if(this.visible)
			return;
		if(!this._timer)
			this._timer = Y.lang.later(this._tipLatency,this,this.open,null);
	},
	open:function(noanim)
	{
		if(this._closeOthers)
		{
			switch(this._type)
			{
				case('error'):
					cl.tooltipCloseByType('error');
				break;
				case('tip'):
					cl.tooltipCloseByType('tip');
				break;
				default:
					cl.tooltipCloseByType('default');
			}
		}
		
		var o= cl.tooltipAdd(this);
		this._zIndex=o.zIndex;
		this._mgrIndex=o.i;
		
		if(!this.htmlObj)
			this._create();
		else
		{
			this._Y.util.Dom.setStyle(this.htmlObj,'display','block');
			this.htmlObj.style.zIndex=this._zIndex;
			if(this._align!=this._firstAlign){
				for(key in this._alignTried)
					this._alignTried[key]=false;
				this._align=this._firstAlign;
				this._position();
			}else
				this._checkPosition();
		}
				
		if(!noanim)
			this._show();
		else
			this._Y.util.Dom.setStyle(this.htmlObj,'opacity', 1.0);

		if(this._timer)
			this._timer = null;
		this.visible=true;
		
		if(!this._isBind)
			this._bind();
		
		if(this._focus)
		{
			if (this._target.tagName)
			{
				try
				{
					if( this._target.tagName=="INPUT" || this._target.tagName=="TEXTAREA" ) 
						this._target.focus();
				}
				catch(e)
				{
					
				}
			}
		}
	},
	_create: function()
	{		
		this.htmlObj = this._document.createElement('div');
		
		this._container=this._document.createElement('table');
		this._container.className="tooltip-container";
		this._tr1 = this._container.insertRow(0);
		this._td1 = this._tr1.insertCell(0);
		this._td1.className = "tooltip-td1 no-bg";
		this._td2 = this._tr1.insertCell(1);
		this._td2.className = "tooltip-td2 no-bg";
		this._td3 = this._tr1.insertCell(2);
		this._td3.className = "tooltip-td3 no-bg";
		
		this._tr2 = this._container.insertRow(1);
		this._td4 = this._tr2.insertCell(0);
		this._td4.className = "tooltip-td4 no-bg";
		this._td5 = this._tr2.insertCell(1);
		this._td5.className = "tooltip-td5";
		this._td6 = this._tr2.insertCell(2);
		this._td6.className = "tooltip-td6 no-bg";

		this._tr3 = this._container.insertRow(2);
		this._td7 = this._tr3.insertCell(0);
		this._td7.className = "tooltip-td7 no-bg";
		this._td8 = this._tr3.insertCell(1);
		this._td8.className = "tooltip-td8 no-bg";
		this._td9 = this._tr3.insertCell(2);
		this._td9.className = "tooltip-td9 no-bg";
		
		this._setWidth();
			
		this._setContent();
		this.htmlObj.appendChild(this._container);
		this._Y.util.Dom.setStyle(this.htmlObj,'opacity', 0.0);
		this.htmlObj.style.zIndex=this._zIndex;
		this._document.body.appendChild(this.htmlObj);	
				
		if(this._closeButton)
		{
			this._oDivClose = this._document.createElement('div');
			this._oDivClose.className='tooltip-btn-close '+this._type;
			this.htmlObj.appendChild(this._oDivClose);
			this._Y.util.Event.addListener(this._oDivClose, "click", this._closeBtn, null, this);
		}
				
		this._position();					
	},
	_setWidth: function()
	{
		if(this._width){
			this.htmlObj.style.width = this._width+'px';
		}else{
			this._Y.util.Dom.setStyle(this._td5,'white-space','nowrap');
		}
	},
	_setContent: function()
	{
		this._td5.innerHTML = '';
		if(this._content.tagName)
			this._td5.appendChild(this._content);
		else
			this._td5.innerHTML = this._content;	
	},
	_position: function()
	{
		this.htmlObj.className='tooltip '+this._type;
		
		var r= this._Y.util.Dom.getRegion(this.htmlObj);
		this._twidth = Math.abs(r.right - r.left);
		this._theight = Math.abs(r.bottom - r.top);
		
		if(!this._width)
			this.htmlObj.style.width = (r.right - r.left)+'px';
		
		if(this._target.tagName)
			this._elRegion = Y.util.Dom.getRegion(this._target);
		else
			this._elRegion = this._target.getRegion();
		var targetWidth = Math.abs(this._elRegion.right - this._elRegion.left);
		var targetHeight = Math.abs(this._elRegion.bottom - this._elRegion.top);
		
		if(this._oDivArrow)
		{
			this._oDivArrow.parentNode.removeChild(this._oDivArrow)
			this._oDivArrow = null;
		}
		
		switch(this._align)
		{
			case 'lt':
				this._top  = this._elRegion.top-this._theight;
				this._left = this._elRegion.left-this._twidth;
				this._Y.util.Dom.addClass(this.htmlObj, 'lt');
				break;
			case 't':
				this._top  = this._elRegion.top-this._theight;
				this._left = this._elRegion.left+Math.round(targetWidth/2-this._twidth/2);
				this._Y.util.Dom.addClass(this.htmlObj, 't');
				this._oDivArrow = this._document.createElement('div');
				this._oDivArrow.className='tooltip-btn-arrow no-bg';
				this. _td8.appendChild(this._oDivArrow);
				var tdReg = this._Y.util.Dom.getRegion(this._td8);
				var arrowReg = this._Y.util.Dom.getRegion(this._oDivArrow);
				this._oDivArrow.style.marginLeft = (tdReg.width/2)-(arrowReg.width/2)+"px";
				break;
			case 'ttl':
				this._top  = this._elRegion.top-this._theight;
				this._left = this._elRegion.left;
				this._Y.util.Dom.addClass(this.htmlObj, 'ttl');
				this._oDivArrow = this._document.createElement('div');
				this._oDivArrow.className='tooltip-btn-arrow no-bg';
				this. _td8.appendChild(this._oDivArrow);
				break;
			case 'ttr':
				this._top  = this._elRegion.top-this._theight;
				this._left = this._elRegion.left+targetWidth-this._twidth;
				this._Y.util.Dom.addClass(this.htmlObj, 'ttr');
				this._oDivArrow = this._document.createElement('div');
				this._oDivArrow.className='tooltip-btn-arrow no-bg';
				this. _td8.appendChild(this._oDivArrow);
				var tdReg = this._Y.util.Dom.getRegion(this._td8);
				var arrowReg = this._Y.util.Dom.getRegion(this._oDivArrow);
				this._oDivArrow.style.marginLeft = this._oDivArrow.offsetLeft+(tdReg.width)-(arrowReg.width)+"px";
				break;
			case 'rt':
				this._top  = this._elRegion.top-this._theight;
				this._left = this._elRegion.right;
				this._Y.util.Dom.addClass(this.htmlObj, 'rt');
			break;
			case 'r':
				this._top  = this._elRegion.top+Math.round(targetHeight/2-this._theight/2);
				this._left = this._elRegion.right;
				this._Y.util.Dom.addClass(this.htmlObj, 'r');
				this._oDivArrow = this._document.createElement('div');
				this._oDivArrow.className='tooltip-btn-arrow no-bg';
				this. _td4.appendChild(this._oDivArrow);
			break;
			case 'rb':
				this._top  = this._elRegion.bottom;
				this._left = this._elRegion.right;
				this._Y.util.Dom.addClass(this.htmlObj, 'rb');
				break;
			case 'b':
				this._top  = this._elRegion.bottom;
				this._left = this._elRegion.left+Math.round(targetWidth/2-this._twidth/2);
				this._Y.util.Dom.addClass(this.htmlObj, 'b');
				this._oDivArrow = this._document.createElement('div');
				this._oDivArrow.className='tooltip-btn-arrow no-bg';
				this. _td2.appendChild(this._oDivArrow);
				var tdReg = this._Y.util.Dom.getRegion(this._td2);
				var arrowReg = this._Y.util.Dom.getRegion(this._oDivArrow);
				this._oDivArrow.style.marginLeft = (tdReg.width/2)-(arrowReg.width/2)+"px";
				break;
			case 'lb':
				this._top  = this._elRegion.bottom;
				this._left = this._elRegion.left-this._twidth;
				this._Y.util.Dom.addClass(this.htmlObj, 'lb');
			break;
			case 'l':
				this._top  = this._elRegion.top+Math.round(targetHeight/2-this._theight/2);
				this._left = this._elRegion.left-this._twidth;
				this._Y.util.Dom.addClass(this.htmlObj, 'l');
				this._oDivArrow = this._document.createElement('div');
				this._oDivArrow.className='tooltip-btn-arrow no-bg';
				this. _td6.appendChild(this._oDivArrow);				
				break;
		}
		
		if(this._frame)
		{
			this._offsetChanged();
		}
		else
		{
			this.htmlObj.style.top=this._top+'px';
			this.htmlObj.style.left= this._left+'px';
		}	
		
		this._checkPosition();
	},
	_bind:function()
	{
		this._isBind = true;
		if(this._frame)
		{
			if(window.frameElement.modal)
				window.frameElement.modal.onMove.subscribe(this._offsetChanged,null,this);
			else
				this._Y.util.Event.addListener(parent,'resize',this._offsetChanged,null,this);
		}
		else
		{
			Y.util.Event.addListener(window,'resize',this._winResized,null,this);
		}
	},
	_unbind:function()
	{
		this._isBind = false;
		if(this._frame)
		{
			if(window.frameElement.modal)
				window.frameElement.modal.onMove.unsubscribe(this._offsetChanged);
			else
				this._Y.util.Event.removeListener(parent,'resize');
		}
		else
		{
			Y.util.Event.removeListener(window,'resize');
		}
	},
	_winResized: function()
	{
		for(key in this._alignTried)
			this._alignTried[key]=false;
		
		if(this._align!=this._firstAlign){
			this._align = this._firstAlign;
			this._position();
		}else
			this._checkPosition();
	},
	_checkPosition:function()
	{
		var vw = this._Y.util.Dom.getViewportWidth()+this._Y.util.Dom.getDocumentScrollLeft();
		var vh = this._Y.util.Dom.getViewportHeight()+this._Y.util.Dom.getDocumentScrollTop();
		if((this._left+this._twidth)>vw || (this._top+this._theight)>vh || (!this._frame && (this._left<0 || this._top<0)))
		{
			for(key in this._alignTried)
			{
				if(!this._alignTried[key])	
				{
					this._alignTried[key] = true;
					this._align = key;
					this._position();
					break;
				}	
			}
		}
	},
	_offsetChanged:function()
	{
		var offset=this._Y.util.Dom.getRegion(window.frameElement);
		this.htmlObj.style.top=this._top+offset.top+'px';
		this.htmlObj.style.left= this._left+offset.left+'px';
		this._checkPosition();
	},
	_show: function()
	{
		var anim = new this._Y.util.Anim(this.htmlObj, {opacity: {  to: 1.0 }}, this._animTime, Y.util.Easing.easeOut	);
		anim.animate();
	},
	refresh: function(oDiv,align,width)
	{
		if(!this.htmlObj)
			return;
		this._content = oDiv;
		this._width = width?width:this._width;
		this._align = align?align:this._align;
		
		this._zIndex= this._MEB.zIndex++;
		this.htmlObj.style.zIndex=this._zIndex;
		
		this._setWidth();
		this._setContent();
		this._position();
	},
	_closeBtn:function(e)
	{
		this.close();
	},
	closeTip:function(e,noanim)
	{
		if(this._timer){
			this._timer.cancel();
			this._timer = null;
		}
		if(this.htmlObj)
		{
			if(this._isBind)
				this._unbind();
			cl.tooltipRemove(this._mgrIndex);
			if(noanim)
				this._Y.util.Dom.setStyle(this.htmlObj,'opacity', 0);
			else{
				var anim = new this._Y.util.Anim(this.htmlObj, {opacity: {  to: 0 }}, this._animTime, Y.util.Easing.easeOut);
				anim.onComplete.subscribe(this._closeTip,null,this);
				anim.animate();
			}
			this.visible=false;
		}
	},
	_closeTip:function()
	{
		this._Y.util.Dom.setStyle(this.htmlObj,'display','none');
	},
	close: function(nosignal,noanim)
	{			
		if(!this.htmlObj)
			return false;
		
		if(this._oDivClose)
			this._Y.util.Event.removeListener(this._oDivClose, "click", this.destroy);
		
		cl.tooltipRemove(this._mgrIndex);
		this.visible=false;
		
		if(noanim){
			this._close(null,null,nosignal);
		}
		else{
			var anim = new this._Y.util.Anim(this.htmlObj, {opacity: {  to: 0 }}, this._animTime, Y.util.Easing.easeOut	);
			anim.onComplete.subscribe(this._close,nosignal,this);
			anim.animate();
		}
	
		if (this._target.tagName)
		{
			if((this._target.tagName=="INPUT" || this._target.tagName=="TEXTAREA") && (!this._closeButton || this._closeOnChange))
				Y.util.Event.removeListener(this._target,'keydown',this.close);
		}
		else if (!this._closeButton || this._closeOnChange)
		{
			if (this._target.onChange)
				this._target.onChange.unsubscribe(this.close);
			else if (this._target.onActivate)
				this._target.onActivate.unsubscribe(this.close);
		}
	},
	_close:function(e,a,nosignal)
	{
		if(!nosignal)
			this.onClose.fire();
		this.destroy();
	},
	disableCloseButton: function()
	{
		if(this._oDivClose)
			this._Y.util.Event.removeListener(this._oDivClose, "click");
	},
	enableCloseButton: function()
	{
		if(this._oDivClose)
			this._Y.util.Event.addListener(this._oDivClose, "click", this.close, null, this);
	},
	_unloaded:function()
	{
		cl.tooltipRemove(this._mgrIndex);
		this.destroy();
	},
	destroy: function()
	{
		if(this.htmlObj)
		{
			if(this._isBind)
				this._unbind();
			this.htmlObj.parentNode.removeChild(this.htmlObj);
			this.htmlObj=null;
		}
	}
}

/*INNER TOOLTIP
align => before, after, inner
arrow => t, r, b, l
*/
MEB.InnerTooltip = function (oConfigs)
{
	this._type = oConfigs.type?oConfigs.type:'tip';
	this._target = oConfigs.target;
	this._content = oConfigs.content;
	this._width = oConfigs.width ? parseInt(oConfigs.width) : null;
	this._height = oConfigs.height ? parseInt(oConfigs.height) : null;
	
	this._align = oConfigs.align ? oConfigs.align : 'inner';
	this._arrow = oConfigs.arrow ? oConfigs.arrow : null;
	this._closeButton = oConfigs.closeButton!=undefined ? oConfigs.closeButton : true;
	this._closeOthers = oConfigs.closeOthers!=undefined ? oConfigs.closeOthers : false;
	this._closeOnChange = oConfigs.closeOnChange!=undefined ? oConfigs.closeOnChange : false;
	this._noAnim = oConfigs.noAnim!=undefined ? oConfigs.noAnim : false;
	
	if (this._target.tagName)
	{
		this._htmlTarget=this._target;
		if((this._target.tagName=="INPUT" || this._target.tagName=="TEXTAREA") && (!this._closeButton || this._closeOnChange))
			Y.util.Event.addListener(this._target,'keydown',this.close,null,this);
	}
	else
	{
		this._htmlTarget=this._target.htmlObj;
		if (!this._closeButton || this._closeOnChange)
		{
			if (this._target.onChange)
				this._target.onChange.subscribe(this.close,null,this);
			else if (this._target.onActivate)
				this._target.onActivate.subscribe(this.close,null,this);
		}
	}
	
	this.onClose = new Y.util.CustomEvent('closeEvent',this,true,Y.util.CustomEvent.FLAT); 
	this._create();
}

MEB.InnerTooltip.prototype = {
	getType:function()
	{
		return this._type;
	},
	isAutoclose:function()
	{
		return this._closeButton?false:true;
	},
	_create: function()
	{
		this.htmlObj = document.createElement('div');
		Y.util.Dom.addClass(this.htmlObj, 'inner-tooltip');
		
		if(!this._arrow){
			/*if(this._align=='after')
				this._arrow = 't';
			else if(this._align=='before')
				this._arrow = 'b';*/
		}
		if (this._arrow)
		{
			this._oDivArrow = document.createElement('div');
			Y.util.Dom.addClass(this._oDivArrow,'inner-tooltip-arrow-'+this._type);
			Y.util.Dom.addClass(this._oDivArrow,this._arrow);
		}
		this._oDivContainer = document.createElement('div');
		Y.util.Dom.addClass(this._oDivContainer, 'inner-tooltip-'+this._type);
		switch(this._arrow)
		{
			case 't':
				Y.util.Dom.addClass(this._oDivContainer, 'margin-t');
				this.htmlObj.appendChild(this._oDivArrow);
				break;
			case 'r':
				Y.util.Dom.addClass(this._oDivContainer, 'margin-r');
				break;
			case 'b':
				Y.util.Dom.addClass(this._oDivContainer, 'margin-b');
				break;
			case 'l':
				Y.util.Dom.addClass(this._oDivContainer, 'left');
				Y.util.Dom.addClass(this._oDivContainer, 'margin-l');
				this.htmlObj.appendChild(this._oDivArrow);
				break;
		}
		this.htmlObj.appendChild(this._oDivContainer);
		
		if (this._closeButton)
		{
			this._oDivClose = document.createElement('div');
			this._oDivClose.className='inner-tooltip-btn-close-'+this._type;
			this._oDivContainer.appendChild(this._oDivClose);
			Y.util.Event.addListener(this._oDivClose, "click", this._closeBtn, null, this);
		}
		this._oDivText = document.createElement('div');
		Y.util.Dom.addClass(this._oDivText, 'inner-tooltip-text');
		this._oDivContainer.appendChild(this._oDivText);
		
		if(this._width)
			this.htmlObj.style.width=this._width+'px';
		if(this._height)
			this.htmlObj.style.height=this._height+'px';
		
		this._setContent();
		
		if(this._arrow && this._arrow!='t' && this._arrow!='l')
			this.htmlObj.appendChild(this._oDivArrow);
		
		if (!this._noAnim)
		{
			this.htmlObj.style.height='0px';
			Y.util.Dom.setStyle(this.htmlObj,'opacity',0.0);
		}
		
		switch(this._align)
		{
			case 'before':
				this._htmlTarget.parentNode.insertBefore(this.htmlObj,this._htmlTarget);
				break;
			case 'after':
				this._htmlTarget.parentNode.insertBefore(this.htmlObj,this._htmlTarget.nextSibling );
				break;
			case 'inner':
				this._htmlTarget.appendChild(this.htmlObj);
			break;
		}
		if (!this._noAnim)
		{
			this._height=this.htmlObj.scrollHeight;
			this.show();
		}
	},
	setContent:function(c)
	{
		this._oDivText.innerHTML ='';
		this._content=c;
		this._setContent();
		r=Y.util.Dom.getRegion(this.htmlObj);
		if (!this._noAnim)
		{
			this._height=this.htmlObj.scrollHeight;
			this.show();
		}
	},
	_setContent:function()
	{
		if(this._content.tagName)
			this._oDivText.appendChild(this._content);
		else
			this._oDivText.innerHTML = this._content;
	},
	show: function()
	{
		var anim = new Y.util.Anim(this.htmlObj, {opacity: { to: 1.0 },height: { to: this._height+1 }  }, 0.4, Y.util.Easing.easeOut	);
		anim.animate();
	},
	_closeBtn:function(e)
	{
		this.close();
	},
	close: function(nosignal)
	{
		if (!this.htmlObj)
			return;
		
		Y.util.Event.removeListener(this._oDivClose, "click", this.destroy);
		
		var anim = new Y.util.Anim(this.htmlObj, {opacity: {  to: 0.0 } , height: {  to: 0 }}, 0.4, Y.util.Easing.easeOut	);
		anim.onComplete.subscribe(this._close,nosignal,this);
		anim.animate();
	
		if (this._target.tagName)
		{
			if((this._target.tagName=="INPUT" || this._target.tagName=="TEXTAREA")&& (!this._closeButton || this._closeOnChange))
				Y.util.Event.removeListener(this._target,'keydown',this.close);
		}
		else if (!this._closeButton || this._closeOnChange)
		{
			if (this._target.onChange)
				this._target.onChange.unsubscribe(this.close);
			else if (this._target.onActivate)
				this._target.onActivate.unsubscribe(this.close);
		}
	},
	_close:function(e,a,nosignal)
	{
		if(!nosignal)
			this.onClose.fire();
		this.destroy();
	},
	disableCloseButton: function()
	{
		if(this._oDivClose)
			Y.util.Event.removeListener(this._oDivClose, "click");
	},
	enableCloseButton: function()
	{
		if(this._oDivClose)
			Y.util.Event.addListener(this._oDivClose, "click", this.close, null, this);
	},
	destroy: function()
	{
		this.htmlObj.parentNode.removeChild(this.htmlObj);
	}
}
/*
 * ESEMPIO 
var viewport=new MEB.Viewport({margin:{t:100,l:30,r:30,b:30}});
var s=viewport.attachScroll(new MEB.Scroll({area:40}));
viewport.get().innerHTML='<div style="position:absolute;left:0px;top:0px;width:4000px;height:4000px;background:url(http://www.myeverybodysworld.com/map/4-32.gif)"></div><div id="prova" style="position:absolute;left:1000px;top:205px;width:200px;height:300px;background-color:red"></div>';
s.bindHand();
s.bindArea();
s.bindWheel();
s.bindKeys();

var t=new MEB.DragDrop({element:document.getElementById('prova'),scroll:viewport.get(),thick:10,limits:{t:100,b:500,l:50,r:500},center:true});
t.bind();
*/
MEB.DragDrop=function(config)
{
	this._ddObj=config.element;
	this._handleObj=config.handle?config.handle:this._ddObj;
	this._scrollObj=config.scroll?config.scroll:null;
	this._limits=config.limits?config.limits:null;		
	this._center=config.center;		
	this.size=config.size;
	this._thick=config.thick?config.thick:null;		

	this.onStart=new Y.util.CustomEvent( 'startEvent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onEnd=new Y.util.CustomEvent( 'endEvent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onDrag=new Y.util.CustomEvent( 'dragEvent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onCheck=new Y.util.CustomEvent( 'checkEvent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 	
}
MEB.DragDrop.prototype={
	bind:function()
	{
		Y.util.Event.addListener(this._handleObj,"mousedown",this._start,null,this);
	},
	unbind:function()
	{
		Y.util.Event.removeListener(this._handleObj,"mousedown",this._start);
	},
	_start:function(e)
	{
		this._c={x:Y.util.Event.getPageX(e),y:Y.util.Event.getPageY(e)};
		
		this._tLeft=this.left=this._ddObj.style.left?parseInt(this._ddObj.style.left):0;
		this._tTop=this.top=this._ddObj.style.top?parseInt(this._ddObj.style.top):0;
		
		var r=Y.util.Dom.getRegion(this._ddObj);
		if (!this.size)	
			this.size={w:r.right-r.left,h:r.bottom-r.top};
		
		if (this._center)
		{
			this.left+=this._c.x-(r.left+this.size.w/2);
			this.top+=this._c.y-(r.top+this.size.h/2);
			this._tLeft=this.left;
			this._tTop=this.top;
		}
		this.moveBy(0,0);
		this._ddObj.style.left=this.left+'px';
		this._ddObj.style.top=this.top+'px';
		this.onStart.fire(this);		

		if (this._scrollObj)
		{
			this._scroll={x:parseInt(this._scrollObj.scrollLeft),y:parseInt(this._scrollObj.scrollTop)};
			Y.util.Event.addListener(this._scrollObj,"scroll",this._scrolled,null,this);
		}
		
		Y.util.Event.addListener(document,"mouseup",this.end,null,this);
		Y.util.Event.addListener(document,"mousemove",this._move,null,this);		
	},
	end:function()
	{
		Y.util.Event.removeListener(document,"mousemove",this._move);
		Y.util.Event.removeListener(document,"mouseup",this.end);
		if (this._scrollObj)
			Y.util.Event.removeListener(this._scrollObj,"scroll",this._scrolled);
		this.onEnd.fire(this);
	},
	_move:function(e)
	{
		var c={x:Y.util.Event.getPageX(e),y:Y.util.Event.getPageY(e)};
		
		if (this.moveBy(c.x-this._c.x,c.y-this._c.y))
		{
		this._ddObj.style.left=this.left+'px';
		this._ddObj.style.top=this.top+'px';
		this.onDrag.fire({x:this.left,y:this.top}); 
		}
		this._c.x=c.x;
		this._c.y=c.y;
	},
	_scrolled:function()
	{
		var s={x:parseInt(this._scrollObj.scrollLeft),y:parseInt(this._scrollObj.scrollTop)};
		
		if (this.moveBy(s.x-this._scroll.x,s.y-this._scroll.y))
		{
		this._ddObj.style.left=this.left+'px';
		this._ddObj.style.top=this.top+'px';
		this.onDrag.fire(this); 
		}
		this._scroll.x=s.x;
		this._scroll.y=s.y;
	},
	moveBy:function(x,y)
	{
		this.oldTop=this.top
		this.oldLeft=this.left;

		if (this._thick)
		{
			this._tLeft+=x;
			this._tTop+=y;
			this.left=Math.round(this._tLeft/this._thick)*this._thick;
			this.top=Math.round(this._tTop/this._thick)*this._thick;
		}
		else
		{
			this.left+=x;
			this.top+=y;
		}
		if (this._limits)
		{
			if (this._limits.t!=undefined && this.top<this._limits.t)
				this.top=this._limits.t;
			if (this._limits.l!=undefined && this.left<this._limits.l)
				this.left=this._limits.l;
			if (this._limits.r!=undefined && this.left+this.size.w>this._limits.r)
				this.left=this._limits.r-this.size.w;
			if (this._limits.b!=undefined && this.top+this.size.h>this._limits.b)
				this.top=this._limits.b-this.size.h;
		}
		this.onCheck.fire(this); 
		if (this.oldTop!=this.top || this.oldLeft!=this.left)
			return true;
		else 
			return false;
	}
}
MEB.InputFile=function(config)
{
	if (config.container)
		this._container=Y.lang.isObject(config.container)?config.container:Y.util.Dom.get(config.container);
	this.id=config.id?config.id:Y.util.Dom.generateId();
	
	this.htmlObj=document.createElement('div');
	this.htmlObj.className='inputFile';
	
	this._btn=new MEB.Button({container:this.htmlObj,content:'sfoglia'});
	
	if (this._container)
	{
		this._input=document.createElement('input');
		this._input.type='file';
		this._input.name=config.name;
		this._input.id=this.id;
		this._container.appendChild(this.htmlObj);
	}
	else
	{
		this._input=document.getElementById(this.id);
		this._container=this._input.parentNode;
		this._container.replaceChild(this.htmlObj,this._input);
	}
	if (Y.env.ua.gecko)
	{
		this._input.size='1';
		this._input.style.left='-24px';
	}
	this.htmlObj.appendChild(this._input);
	Y.util.Dom.addClass(this.htmlObj,'active');
	this._input.className='realInput';
	
	Y.util.Event.addListener(this._input, "change", this._change, null,this); 
	
	this.onChange=new Y.util.CustomEvent( 'changeEvent' , this , true , Y.util.CustomEvent.FLAT  ); 
}
MEB.InputFile.prototype={
	_change:function()
	{
		this.onChange.fire();
	},
	disable:function()
	{
		this._btn.disable();
		Y.util.Dom.addClass(this.htmlObj,'active');
		this._input.style.display='none';
	},
	enable:function()
	{
		this._btn.enable();
		Y.util.Dom.addClass(this.htmlObj,'active');
		
		this._input.style.display='block';
	},
	getRegion:function()
	{
		return Y.util.Dom.getRegion(this.htmlObj);
	},
	getDocument:function()
	{
		return document;
	}
}

/*
* 	@class Uploader
*	@requires 
* 	
*	@info: 	Classe Uploader
*
*	@param (obj) 		container		 
* 	@param (string) 	id				id del form che verrà creato(optional)
* * @param (string) 	action			url dove inviare l immagine

	@method showInput					disegna l imput nascondendo il loading
	@method submit						uploading del file
	@method destroy
	
* 	@event onUploaded					spara come arg la risposta della pagina action chiamata
* 		
	*/
MEB.Uploader=function(config)
{
	this.container=Y.lang.isObject(config.container)?config.container:Y.util.Dom.get(config.container);
	this.id=config.id?config.id:Y.util.Dom.generateId();
	this.action=config.action;
	
	this.data=config.data?config.data:null;
	this.htmlObj=document.createElement('div');
	this.htmlObj.innerHTML='<form id="'+this.id+'-form" name="'+this.id+'-form" method="post" enctype="multipart/form-data"><div class="uploader-input-cont" id="'+this.id+'-input-cont"></div><div class="uploader-text" id="'+this.id+'-text"></div></form>';
	this.htmlObj.className='uploader';
	this.htmlObj.id=this.id;
	this.container.appendChild(this.htmlObj);
	
	this.form=document.getElementById(this.id+'-form');
	this.input=new MEB.InputFile({name:config.name,container:document.getElementById(this.id+'-input-cont')});
	this.input.onChange.subscribe(this._inputFileChange,null,this);
	
	this.onSubmit=new Y.util.CustomEvent( 'submitEvent' , this , true , Y.util.CustomEvent.FLAT  );
	this.onChange=new Y.util.CustomEvent( 'changeEvent' , this , true , Y.util.CustomEvent.FLAT  ); 
	this.onUploading=new Y.util.CustomEvent( 'uploadingEvent' , this , true , Y.util.CustomEvent.FLAT  ); 
}
MEB.Uploader.prototype=
{
	getDocument:function()
	{
		return document;
	},
	getRegion:function()
	{
		return Y.util.Dom.getRegion(this.htmlObj);
	},
	_uploaded:function()
	{
		this._loading.destroy();
		Y.util.Dom.addClass(document.getElementById(this.id+'-text'),'uploaded');
		document.getElementById(this.id+'-text').innerHTML='caricato';
		this.input.enable();
	},
	_uploading:function()
	{
		this.input.disable();
		Y.util.Dom.removeClass(document.getElementById(this.id+'-text'),'uploaded');
		document.getElementById(this.id+'-text').innerHTML='';
		
		this._loading=new MEB.Loading(document.getElementById(this.id+'-text'),'inside','caricamento');
		this._loading.show();
		this.onUploading.fire();
	},
	_inputFileChange:function()
	{
		if (this.submit())
			this._uploading();
	},
	submit:function(e)
	{
		if (!this.onSubmit.fire())
		{
			Y.util.Event.stopEvent(e);
			return false;
		}

		this._n = this.id+'-f-' + Math.floor(Math.random() * 100000);
		this._div=document.createElement('div');
		this._div.style.display='none';
		this._div.innerHTML='<iframe id="'+this._n+'" name="'+this._n+'" src="about:blank"></iframe>';
		document.body.appendChild(this._div);
		this._frame=document.getElementById(this._n);
		
		this.form.target=this._n;
		Y.util.Event.addListener(this._frame, "load", this._loaded, null,this); 

		this._t=MEB.jsonT++;
		this.form.action=this.action+'?check='+MEB.CHECK+'&l='+encodeURIComponent(document.domain)+'&jsonT='+this._t+'&r='+Math.floor(Math.random()*100000)+'&'+(this.data?this.data:'');
		this.form.submit();
		return true;
	},
	_loaded:function()
	{
		Y.util.Event.removeListener(this._frame, "load"); 
		var e;
		try
		{
			//alert(window.frames[this._n].frames[0].location.hash.substring(1));   
			eval('e='+decodeURIComponent(window.frames[this._n].frames[0].location.hash.substring(1)));
		}
		catch(a)
		{
			e={err:1};
		}
		if (!this.onChange.fire(e) && e.err)
		{
			MEB.Connect.handleError(e.err,e.text);
			this.input.enable();
			this._loading.destroy();
		}
		else
			this._uploaded();
		
	},
	destroy:function()
	{
		this._loading.destroy();
		this.onChange.unsubscribeAll();
		this.onUploading.unsubscribeAll();
		this.onSubmit.unsubscribeAll();
		if(this._div)
			this._div.parentNode.removeChild(this._div);
		this.htmlObj.parentNode.removeChild(this.htmlObj);
	}
}
/*
* 	@class Pages
*	@requires 
* 	
*	@info: 	Classe Uploader
*
	@param (obj) 			container
	@param (obj) 			id	
	@param (obj) 			tot
	@param (obj) 			current
	@param (obj) 			n=11	

	@method setPage			setpage
	@method destroy
	
* 	@event onChange		
* 		
*/
MEB.Pages=function(config)
{
	if (config.container)
		this._container=Y.lang.isObject(config.container)?config.container:Y.util.Dom.get(config.container);
	this.id=config.id;
	this.current=config.current;
	this.tot=config.tot;
	this.n=config.n?config.n:11;
	
	this.htmlObj=document.createElement('table');
	this.htmlObj.align = "center";
	this.htmlObj.id=this.id;
	this._row = this.htmlObj.insertRow(0);
	this._cell = this._row.insertCell(0);
	this._cell.className='pages';
	
	this._container.appendChild(this.htmlObj);
	
	this.onChange=new Y.util.CustomEvent( 'changeEvent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this._create();
	
}
MEB.Pages.prototype={
	pageCont:null,
	_create:function()
	{
		var start,diff,end,l,s;
		var start=this.current-(this.n-1)/2;
		if (start<1)
		{
			diff=1-start;
			start=1;
		}
		else 
			diff=0;
		
		end=this.current+(this.n-1)/2+diff;
		if (end>this.tot)
		{
			diff=end-this.tot;
			start-=diff;
			if (start<1)
				start=1;
			end=this.tot;
		}
		//freccia sinistra
		if (this.current>1) 
		{
			l=document.createElement('div');
			l.className = 'pageNav-left left';
			Y.util.Event.addListener(l, "click", this.setPage,this.current-1,this); 
			this._cell.appendChild(l);
		}
		else 
		{
			s=document.createElement('div');
			s.className ='pageNav-left-disable left';
			this._cell.appendChild(s);
		}
		//pagine
		this.pageCont=document.createElement('div');
		this.pageCont.className ='left';
		this._cell.appendChild(this.pageCont);
		if (start==1)
			this._addPage(1);
		if (start>1)
		{
			s=document.createElement('span');
			s.className = 'pagePPP';
			s.innerHTML='...';
			this.pageCont.appendChild(s);
		}
		for (var i=start+1;i<=end;i++)
		{
			this._addPage(i);
		}
		if (end<this.tot-1)
		{
			s=document.createElement('span');
			s.className = 'pagePPP';
			s.innerHTML='...';
			this.pageCont.appendChild(s);
		}
		if (end<this.tot)
			this._addPage(this.tot);
		//freccia destra
		if (this.current<this.tot)
		{
			l=document.createElement('div');
			l.className = 'pageNav-right left';
			Y.util.Event.addListener(l, "click", this.setPage,this.current+1,this);
			this._cell.appendChild(l);
		}
		else
		{
			s=document.createElement('div');
			s.className = 'pageNav-right-disable left';
			this._cell.appendChild(s);
		}
	},
	_addPage:function(i)
	{
		var l=document.createElement('a');
		l.setAttribute('href','javascript:void(0)');
		l.innerHTML=i;
		if (i==this.current){
			l.className='currentPage';
		}
		else{
			Y.util.Event.addListener(l, "click", this.setPage,i,this); 	
		}
		this.pageCont.appendChild(l);
	},
	_empty:function()
	{
		this._cell.innerHTML='';
	},
	setPage:function(e,p)
	{
		this.onChange.fire(p);
		this.current=p;
		this._empty();
		this._create();
	},
	destroy:function()
	{
		this.htmlObj.parentNode.removeChild(this.htmlObj);
	}
}

/*
 AUTOCOMPLETE
 */
 MEB.AutoComplete = function(configs) 
 {
  	this.id=configs.id;
  	this._className=configs.className?configs.className:'autocomplete';
  	this.width=configs.width;
  	if (configs.container)
  	{
  		this.container=Y.util.Dom.get(configs.container);
  		this.htmlObj=document.createElement('div');
  		this.htmlObj.className=this._className+' field-input';
  		this.input=document.createElement('input');
  		this.input.setAttribute('autocomplete','off');
  		if(configs.text)
  			this.input.value = configs.text;
  		if(this.width)
  			this.input.style.width = this.width+'px';
  		this.input.id=this.id!=undefined?this.id:Y.util.Dom.generateId();
  		this.htmlObj.appendChild(this.input)
  		this.container.appendChild(this.htmlObj);
  	}
  	else
  	{
  		this.htmlObj=Y.util.Dom.get(this.id);
  		this.input=this.htmlObj.firstChild;
  		if(configs.text)
  			this.input.value = configs.text;
  	}
  	if (configs.dataHandler)
  		this._dataHandler=configs.dataHandler;
  	if (configs.getTextHandler)
  		this._getTextHandler=configs.getTextHandler;
  	this._autoSelectFirstItemOnly=configs.autoSelectFirstItemOnly!=undefined?configs.autoSelectFirstItemOnly:true;
  	this._menuTarget=configs.menuTarget?configs.menuTarget:this.input;
  	
  	this.value = (configs.value)?configs.value:null;
  	this.text = (configs.text)?configs.text:null;
  	this.ajaxUrl = configs.ajaxUrl;
  	this.ajaxData = configs.ajaxData? configs.ajaxData:null;
  	this.opt = configs.opt;
  	this.queryLimit = (configs.queryLimit)?configs.queryLimit:'20';
  	this.menu = null;
  	Y.util.Event.addListener(this.input,"keyup",this._keyUp,null,this);
  	Y.util.Event.addBlurListener(this.input,this._onBlur,null,this);
  	Y.util.Event.addFocusListener(this.input,this._onFocus,null,this);
  	
  	
  	this._k=new Y.util.KeyListener(this.input, { keys:[13] },{ fn:this.selectCurrentText, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );  
  	this._k.enable();
  	this._k2=new Y.util.KeyListener(this.input, { keys:[40] },{ fn:this.focusMenu, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );  
  	this._k2.enable();
  	
  	this.onNotFound = new Y.util.CustomEvent( 'notfoundevent' , this , true , Y.util.CustomEvent.FLAT  ); 
  	this.onChange = new Y.util.CustomEvent( 'changeevent' , this , true , Y.util.CustomEvent.FLAT  ); 
  	this.onEmpty  = new Y.util.CustomEvent( 'emptyevent' , this , true , Y.util.CustomEvent.FLAT  );
  	this.onBlankBackspace = new Y.util.CustomEvent( 'blankbackevent' , this , true , Y.util.CustomEvent.FLAT  ); 
  	this.onBlur   = new Y.util.CustomEvent( 'blurevent' , this , true , Y.util.CustomEvent.FLAT  ); 
  	this.onFocus  = new Y.util.CustomEvent( 'blurevent' , this , true , Y.util.CustomEvent.FLAT  ); 
 } 
 MEB.AutoComplete.prototype={
  	menu:null,_key:null,_request:null,
  	getRegion:function()
  	{
  		return Y.util.Dom.getRegion(this.input);
  	},
  	disable:function()
  	{
  		this.input.disabled='disabled';
  		this._k.disable();
  		this._k2.disable();
  	},
  	enable:function()
  	{
  		this.input.disabled='';
  		this._k.enable();
  		this._k2.enable();
  	},
  	_onFocus:function(e)
  	{
  		if (this._hasFocus)
  			return;
  		
  		this._hasFocus=true;
  		this.onFocus.fire();
  	},
  	_onBlur:function(e)
  	{
  		if (!this._hasFocus)
  			return;
  		e=Y.util.Event.getEvent(e);
  		if (e.toElement && e.toElement.unfocusable)
  		{
  			this.input.focus();
  			return;
  		}
  		
  		this._hasFocus=false;
  		this.selectCurrentText();
  		this.onBlur.fire();
  	},
  	hasFocus:function()
  	{
  		return this._hasFocus;
  	},
  	focus:function()
  	{
  		this._hasFocus=true;
  		this.input.focus();
  		this.onFocus.fire();
  	},
  	blur:function(nosignal)
  	{
  		this._hasFocus=false;
  		this.input.blur();
  		if (!nosignal)
  			this.onBlur.fire();
  		
  	}, 
  	focusMenu:function(eName,e,b)
  	{
  		Y.util.Event.stopEvent(e[1]);
  		if (!this.menu || !this.menu.opened)
  			return;
  		
  		this._hasFocus=false;
  		this.input.blur();
  		this._hasFocus=true;
  		this.menu.focus(); 
  	},
  	getCurrentText:function()
  	{
  		return this.input.value;
  	},
  	selectCurrentText:function()
  	{
  		var v=Y.lang.trim(this.input.value);
  		if (v && (this.value && this.text.city==v))
  			return;
  		if (!v)
  		{
  			this.value = null;
  			this.text = null;
  			this.onChange.fire({value: this.value, text:this.text});
  			return;
  		}
  		
  		if (this._kTimer)
  		{
  			this._select=true;
  			return;
  		}
  		if(this._request){
  			this._select=true;
  			return;
  			/*
  			this._request.abort();
  			delete this._request;
  			this._request=null;
  			*/
  		}
  		
  		
  		if (this.menu)
  		{
  			try
  			{
  				if (this.menu.items.length)
  				{
  					var to=this._autoSelectFirstItemOnly?0:this.menu.items.length-1;
  					for (var x=0;x<=to;x++)
  					{
	  					var text=this._getTextHandler?this._getTextHandler.fn.call(this._getTextHandler.scope,x):this.menu.items[x].text;
	  					if (text.toLowerCase()==v.toLowerCase())
	  					{
	  						this.menu.setSelectedByIndex(x);
	  						this._select=false;
	  						return;
	  					}
  					}
  				}
  				
  			}
  			catch(e)
  			{
  				
  			}
  			finally
  			{
  				this.menu.empty(); 
  				this.menu.close(true);
  			}
  		}
  		this._select=false;
  		this.value = null;
  		this.text = v;
  		this.onChange.fire({value: this.value, text:this.text});
  		if (this._empty)
  			this.empty();
  	},
  	_keyUp:function(e)
  	{
  		if(this._key && this.input.value && this.input.value.toLowerCase()==this._key.toLowerCase())
  			return; 
  		if (!this._key && this.input.value=='' && Y.util.Event.getCharCode(e)==8)
  			this.onBlankBackspace.fire();
  		if (this._kTimer)
  		{
  			this._kTimer.cancel();
  			this._kTimer=null;
  		}
  		if(this._request){
  			this._request.abort();
  			delete this._request;
  			this._request=null;
  		}
  		this._empty=this._select=null;
  		
  		if(this.input.value==""){
  			if (this.menu)
  			{
	  			this.menu.empty();
	  			this.menu.close();
  			}
  			this._key='';
  			
  			this.onEmpty.fire(); 
  			return;
  		}
  		this._key = this.input.value;
  		this._kTimer=Y.lang.later(200,this,this._ajaxCallback,e);
  	},
  	_ajaxCallback :function(e){
  		this._kTimer=null;
  		if(!this.menu){
  			this.menu = new MEB.Menu({ alignment: {context: 'bl', obj: this._menuTarget} ,w : this.input.offsetWidth-2});
  			this.menu.onItemSelected.subscribe(this._selectItem,null,this);
  			this.menu.onClose.subscribe(this._closeMenu,null,this);
  			this.menu.onKeyOut.subscribe(this.focus,null,this);
  		}
  		
  		this._request = new MEB.Connect('POST', this.ajaxUrl,{ ok:this._getData, error: this._getDataErr,scope:this} , 'search='+this._key+'&limit='+this.queryLimit+'&opt='+this.opt+'&'+this.ajaxData , {t:this.input, p:'r'});
  	},
  	_getData :function(root){
  		this.lastList=null;
  		this.menu.empty();
  		this.menu.close(true);
  		this.value=null;
  		this.text=null;
  		
  		if (this._dataHandler)
  		{
  			var item;
  			for(i=0;i<root.childNodes.length;i++)
  			{
  				item = this._dataHandler.fn.call(this._dataHandler.scope,root.childNodes[i]);
  				this.menu.addItem(item);
  			}
  		}
  		else
  		{
  			for(i=0;i<root.childNodes.length;i++)
  			{
  				var item = '<div class="'+this._className+'-item"><div class="'+this._className+'-name">'+root.childNodes[i].firstChild.firstChild.data+'</div>';
  				if(root.childNodes[i].childNodes[1])
  					item += '<div class="'+this._className+'-detail">'+root.childNodes[i].childNodes[1].firstChild.data+'</div>';
  				item +='</div>'; 
  				this.menu.addItem({ value: root.childNodes[i].getAttribute('id') ,actext:root.childNodes[i].firstChild.firstChild.data, text: root.childNodes[i].firstChild.firstChild.data , html: item});
  			}
  		}
  		this._request = null;
  		//if(root.attributes['n'].value>20)
  			//this.menu.innerHTML += 'Altri '+(root.attributes['n'].value-20)+' risultati...';
  		if (this._select)
  		{
  			this.selectCurrentText();
  			return;
  		}
  		if(root.getAttribute('n')!=0)
  			this.menu.open();	
  		else
  		{
  			this.value=null;
  			this.text=this._key;
  			this.onNotFound.fire({text: this.input.value});
  		}
  	},
  	_getDataErr : function(e){
  	},
  	_selectItem: function(i){
  		
  		this.value = this.menu.items[i].value;
  		this.text = this.menu.items[i].text;
  		this.input.value = this.menu.items[i].actext;
  		this._key=this.menu.items[i].actext;

  		this.lastList=this.menu.items;
  		
  		if (this.menu && this.menu.opened)
  			this.menu.close(true); 
  		if (!this._select)
  			this.focus();
  		this.onChange.fire({value: this.value, text:this.text});
  	},
  	_closeMenu: function(){
  		if(!this.menu.items.length)
  			return;
  		var to=this._autoSelectFirstItemOnly?0:this.menu.items.length-1;
		for (var x=0;x<=to;x++)
		{
			var text=this._getTextHandler?this._getTextHandler.fn.call(this._getTextHandler.scope,x):this.menu.items[x].text;
			if (text.toLowerCase()==this.input.value.toLowerCase())
			{
				this._selectItem(x);
				return;
			}
		}
  	},
  	empty:function()
  	{
  		if (this._select)
  		{
  			this._empty=true;
  			return;
  		}
  		this._empty=false;
  		this.value=null;
  		this.text='';
  		this._select=null;
  		this.input.value="";
  		this.lastList=null;
  		if (this.menu)
  		{
  			this.menu.empty();
  			this.menu.close();
  		}
  		this._key='';
  	},
  	setOpt: function(opt){
  		if(this.opt!=opt){
  			this.input.value="";
  			this._key='';
  			this.lastList=null;
  			if(this.menu){
  				this.menu.empty();
  				this.menu.close();
  			}		
  		}
  		this.opt=opt;
  	},
  	setWidth: function(w){
  		this.width = w;
  		this.input.style.width = w+'px';
  	},
  	destroy: function(){
  		if(this._request)
  			this._request.abort();
  		this.htmlObj.parentNode.removeChild(this.htmlObj);
  	}
  	
  }
  MEB.MultiAutoComplete = function(configs){
  	this._container = Y.util.Dom.get(configs.container);
  	this.id=configs.id;
  	this._className= configs.className?configs.className:'multiAutoComplete';
  	
  	this.htmlObj = document.createElement("div");
  	this.htmlObj.className =this._className;
  	this.htmlObj.id=this.id;
  	this._container.appendChild(this.htmlObj);

  	var r=Y.util.Dom.getRegion(this.htmlObj);
  	this.width=r.right-r.left;
  	this.left=r.left;
  	this.valuesObj=new Array();
  	this.values=new Array();
  	
  	this._autocomplete = new MEB.AutoComplete({width:(this.width-10),container: this.htmlObj,menuTarget:this.htmlObj,ajaxUrl:configs.ajaxUrl,ajaxData:configs.ajaxData,dataHandler:(configs.dataHandler?configs.dataHandler:null)}); 
  	this._autocomplete.onChange.subscribe(this._found,null,this);
  	this._autocomplete.onBlankBackspace.subscribe(this._enBackspace,null,this);
  	this._autocomplete.onFocus.subscribe(this._acFocus,null,this);
  	
  	if (configs.values)
  	{
  		for (var i=0;i<configs.values.length;i++)
  		{
  			this._addItem(configs.values[i]);
  		}
  	}
  	Y.util.Event.addListener(this.htmlObj,'mousedown',this.focus,null,this);
  	
  	this._k=new Y.util.KeyListener(document, { keys:[8] },{ fn:this._backspace, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );  
  	this._k2=new Y.util.KeyListener(document, { keys:[37] },{ fn:this._fprevItem, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );  
  	this._k3=new Y.util.KeyListener(document, { keys:[39] },{ fn:this._fnextItem, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );  
  	this._k4=new Y.util.KeyListener(document, { keys:[9] },{ fn:this.blur, scope:this, correctScope:true},Y.util.KeyListener.KEYDOWN );  

  	this.onChange = new Y.util.CustomEvent( 'changeevent' , this , true , Y.util.CustomEvent.FLAT  );
  	
  	if (configs.disabled)
  		this.disable();
 }

 MEB.MultiAutoComplete.prototype={
  	_n:0,_i:0,_fitem:null,
  	getRegion:function()
  	{
  		return Y.util.Dom.getRegion(this.htmlObj);
  	},
  	disable:function()
  	{
  		this._disabled=true;
  		this._fitem=null;
  		this._k.disable();
  		this._k2.disable();
  		this._k3.disable();
  		this._k4.disable();
  		this._autocomplete.disable();
  		Y.util.Event.removeListener(document,'mousedown',this.blur);
  		Y.util.Event.removeListener(this.htmlObj,'mousedown',this.focus);
  		this._focus=false;
  		for(var k in this.valuesObj)
  		{
  			this._disableItem(k);
  		}
  	},
  	enable:function()
  	{
  		
  		this._autocomplete.enable();
  		Y.util.Event.addListener(this.htmlObj,'mousedown',this.focus,null,this);
  		for(var k in this.valuesObj)
  		{
  			this._enableItem(k);
  		}
  		this._disabled=false;
  	},
  	getNumber:function()
  	{
  		return this._n;
  	},
  	empty:function()
  	{
  		this._n=0;
  		this._autocomplete.empty();
  		this.valuesObj=new Array();
  		this.values=new Array();
  		while (this._autocomplete.htmlObj.previousSibling)
  		{
  			this.htmlObj.removeChild(this._autocomplete.htmlObj.previousSibling);
  		}
  		this._updateWidth(this._autocomplete.htmlObj.previousSibling);
  	},
  	toPost:function(varName)
  	{
  		var d=varName+'=';
  		for(var i in this.values)
  		{
  			if (d)
  				d+=',';
  			if (this.values[i].value)
  				d+=this.values[i].value;
  			else
  				d+=encodeURIComponent(this.values[i].text);
  		}
  		return d;
  	},
  	focus:function(e)
  	{
  		
  		if (e)
  			Y.util.Event.stopEvent(e);
  		if (this._fitem)
  			this._blurItem(null,this._fitem);

  		this._autocomplete.focus();
  	},
  	_acFocus:function()
  	{
  		if (this._fitem)
  			this._blurItem(null,this._fitem);

  		if (!this._focus)
  		{
  			this._autocomplete.onBlur.subscribe(this.blur,null,this);
  			Y.util.Event.addListener(document,'mousedown',this.blur,null,this);
  		}
  		this._k.disable();
  		this._k2.disable();
  		this._k3.disable();
  		this._k4.disable(); 
  		this._focus=true;
  	},
  	blur:function(e)
  	{
  		if (!this._focus)
  			return;
  		if (this._fitem)
  			this._blurItem(null,this._fitem);
  		this._autocomplete.selectCurrentText();
  		this._autocomplete.onBlur.unsubscribe(this.blur);
  		this._autocomplete.empty();
  		
  		Y.util.Event.removeListener(document,'mousedown',this.blur);
  		
  		this._k.disable();
  		this._k2.disable();
  		this._k3.disable();
  		this._k4.disable();
  		this._focus=false;
  	},
  	_enBackspace:function()
  	{
  		if (!this._autocomplete.htmlObj.previousSibling)
  			return;
  		this._autocomplete.blur(true);
  		this._k.enable();
  		this._k2.enable();
  		this._k3.enable();
  		this._k4.enable();
  		this._backspace();
  	},
  	_backspace:function(k,e)
  	{
  		if (this._fitem)
  			this._delItem(null,this._fitem);
  		else
  		{
  			if (this._autocomplete.htmlObj.previousSibling)
  				this._focusItem(null,this._autocomplete.htmlObj.previousSibling.ref);
  		}
  	},
  	_exblurItem:function(e,i)
  	{
  		this._blurItem(e,i);
  		this.blur();
  	},
  	_exfocusItem:function(e,i)
  	{
  		this._focusItem(e,i);

  		if (!this._focus)
  		{
  			Y.util.Event.addListener(document,'mousedown',this.blur,null,this);
  			this._k.enable();
  			this._k2.enable();
  			this._k3.enable();
  			this._k4.enable();
  			this._focus=true;
  		}
  	},
  	_blurItem:function(e,i)
  	{
  		this._fitem=null;
  		Y.util.Dom.removeClass(this.valuesObj[i].obj,this._className+'-item-over');
  	},
  	_focusItem:function(e,i)
  	{
  		if (this._fitem)
  			this._blurItem(null,this._fitem);
  		this._fitem=i;
  		Y.util.Dom.addClass(this.valuesObj[i].obj,this._className+'-item-over');
  	},
  	_outItem:function(e,i)
  	{
  		if (this._disabled)
  			return;
  		if (i==this._fitem)
  			return;
  		Y.util.Dom.removeClass(this.valuesObj[i].obj,this._className+'-item-over');
  	},
  	_overItem:function(e,i)
  	{
  		if (this._disabled)
  			return;
  		if (i==this._fitem)
  			return;
  		Y.util.Dom.addClass(this.valuesObj[i].obj,this._className+'-item-over');
  	},
  	_fprevItem:function()
  	{
  		if (!this._fitem)
  		{
  			if (this._autocomplete.htmlObj.previousSibling)
  				this._focusItem(null,this._autocomplete.htmlObj.previousSibling.ref);
  		}
  		else if (this.valuesObj[this._fitem].obj.previousSibling)
  			this._focusItem(null,this.valuesObj[this._fitem].obj.previousSibling.ref);
  	},
  	_fnextItem:function()
  	{
  		if (!this._fitem)
  			this._autocomplete.focus();
  		else if (this.valuesObj[this._fitem].obj.nextSibling && this.valuesObj[this._fitem].obj.nextSibling.ref)
  			this._focusItem(null,this.valuesObj[this._fitem].obj.nextSibling.ref);
  		else
  			this._autocomplete.focus();
  	},
  	_found: function(arg)
  	{
  		if (!arg.value && !arg.text)
  			return false;
  		
  		this._autocomplete.empty();
  		
  		if (!this.validate(arg))
  			return false;
  		
  		
  		this._addItem(arg);

  		if (!this._focus)
  			this.focus();
  		this.onChange.fire();
  		
  	},
  	validate:function()
  	{
  		return true;
  	},
  	_enableItem:function(i)
  	{
  		this.valuesObj[i].obj.tabindex='0'; 
  		this.valuesObj[i].btn.enable();
  	},
  	_disableItem:function(i)
  	{
  		this.valuesObj[i].obj.tabindex=''; 
  		this.valuesObj[i].btn.disable();
  	},
  	_addItem:function(arg)
  	{
  		var i='i'+this._i,r;
  		
  		this._autocomplete.setWidth(10);
  		
  		this.values[i]=arg; //{value:arg.value,text:arg.text}
  		this.valuesObj[i]=new Object();
  		var d=this.valuesObj[i].obj=document.createElement('DIV');
  		d.className=this._className+'-item'; 
  		d.innerHTML=arg.text+'<button type="button" id="'+this.id+'-'+i+'-btn"></button>';
  		d.tabindex='0';
  		d.ref=i;
  		//Y.util.Event.addListener(d,'keydown',function(e){alert('assadad');},i,this);
  		Y.util.Event.addListener(d,'mouseover',this._overItem,i,this);
  		Y.util.Event.addListener(d,'mouseout',this._outItem,i,this);
  		this.htmlObj.insertBefore(d,this._autocomplete.htmlObj);
  		Y.util.Event.addBlurListener(d,this._exblurItem,i,this);
  		Y.util.Event.addFocusListener(d,this._exfocusItem,i,this);
  		
  		this.valuesObj[i].btn=new MEB.Button({id:this.id+'-'+i+'-btn',content:'x'}); //className:this._className+'-delbtn'
  		this.valuesObj[i].btn.onClick.subscribe(this._delItem,i,this);
  		this.valuesObj[i].index=i;
  		
  		this._updateWidth(d);
  		
  		this._i++;
  		this._n++;
  		
  	},
  	_updateWidth:function(d)
  	{
  		var newWidth;
  		if (d)
  		{
  			r=Y.util.Dom.getRegion(d); 
  			if (r.left-this.left<10 && d.previousSibling)
  				d.previousSibling.style.clear='right';
  			newWidth=this.width-(r.right-this.left)-10;
  			if (newWidth<30)
  			{
  				d.style.clear='right';
  				newWidth=this.width-10;
  			}
  		}
  		else
  			newWidth=this.width-10;
  		this._autocomplete.setWidth(newWidth);
  	},
  	_delItem:function(e,i)
  	{
  		var prev=this.valuesObj[i].obj.previousSibling,next=this.valuesObj[i].obj.nextSibling;
  		
  		if (prev && prev.style.clear)
  			prev.style.clear='none';
  		
  		delete this.values[i];
  		this.valuesObj[i].btn.destroy();
  		this.htmlObj.removeChild(this.valuesObj[i].obj);
  		this._autocomplete.setWidth(10);
  		delete this.valuesObj[i];
  		
  		this._updateWidth(this._autocomplete.htmlObj.previousSibling);
  		
  		this._fitem=null;
  		if (!this._autocomplete.htmlObj.previousSibling || (!prev && !next.ref))
  			this._autocomplete.focus();
  		else 
  			this._focusItem(null,prev?prev.ref:next.ref);
  		
  		this._n--;
  		this.onChange.fire();
  	},
  	
  	destroy: function(){
  		if(this._autocomplete)
  			this._autocomplete.destroy();
  		this._container.removeChild(this.htmlObj);
  	}
 }

 MEB.City = function(configs){
 	this._container = Y.util.Dom.get(configs.container);
 	this.country=configs.country;
 	this._noflag=configs.noflag?true:false;
 	this._countries=configs.countries!=undefined?configs.countries:['IT','GB','US','ES','FR','DE'];

 	this.htmlObj = document.createElement("div");
 	this._className = (configs.className)?configs.className:'city';
 	this.htmlObj.className = this._className;
 	this._container.appendChild(this.htmlObj);
 	
 	if(!this._noflag){
 		if(this._countries)
 		{
	 		this.items = new Array();
 			for (var i=0;i<this._countries.length;i++)
 				this.items.push({text: ('<img src="'+MEB.IMG_BASE+'flag/'+this._countries[i].toLowerCase()+'.png" class="location-flag" />&nbsp;'+MEB.lang.countries[this._countries[i]]), value: this._countries[i] });
	 		for (var i=0;i<this.items.length;i++)
 			{
 				if (this.country==this.items[i].value)
 				{
 					this._selectedIndex=i;
 					break;
 				}
 			}
 			this._menu=new MEB.Menu({alignment:{obj:this.htmlObj,context:'bl'},items: this.items, selected: this._selectedIndex});
 			this._menu.onItemSelected.subscribe(this._countrySelected,null,this);
 			this._menuButton = new MEB.MenuButton({container: this.htmlObj, menu: this._menu, className: this._className+'-countries'});
 			this._menuButton.setContent('<img src="'+MEB.IMG_BASE+'flag/'+this.country.toLowerCase()+'.png" class="location-flag" />');
 		}
 		else
 		{
 			this._countryHtml = document.createElement('div');
 			this._countryHtml.className = this._className+'-country';
 			this._countryHtml.innerHTML = '<img src="'+MEB.IMG_BASE+'flag/'+this.country.toLowerCase()+'.png" class="location-flag" />';
 			this.htmlObj.appendChild(this._countryHtml);
 		}
 	}
 	
 	this._autocomplete = new MEB.AutoComplete({container: this.htmlObj,ajaxUrl:MEB.AJAX_BASE+'meb-core.php?cmd=city', opt: this.country, autoSelectFirstItemOnly:false,text: (configs.text?configs.text.city:null), value: (configs.value?configs.value.city:null), dataHandler:{fn:this._dataHandler,scope:this},getTextHandler:{fn:this._getTextHandler,scope:this},className: this._className+'-autocomplete'});
 	this._autocomplete.onChange.subscribe(this._cityFound,null,this);
 	this._autocomplete.onEmpty.subscribe(this._empty,null,this);
 	this._autocomplete.onNotFound.subscribe(this._cityNotFound,null,this);
 	
 	this.value = configs.value?configs.value:null;
 	this.text = configs.text?configs.text:{};
 	
 	this.onCountryChange=new Y.util.CustomEvent( 'countrychange' , this , true , Y.util.CustomEvent.FLAT  ); 
 	this.onChange=new Y.util.CustomEvent( 'changeevent' , this , true , Y.util.CustomEvent.FLAT  ); 
 	this.onNotFound=new Y.util.CustomEvent( 'notFound' , this , true , Y.util.CustomEvent.FLAT  ); 
 }

 MEB.City.prototype={
 	getRegion:function()
 	{
 		return Y.util.Dom.getRegion(this.htmlObj);
 	},
 	_getTextHandler:function(x)
 	{
 		return this._autocomplete.menu.items[x].text.city;  
 	},
 	_dataHandler:function(el)
 	{
 		var item = '<div class="'+this._className+'-autocomplete-item"><div class="'+this._className+'-autocomplete-name">'+el.childNodes[0].firstChild.data+'</div>';
 			item += '<div class="'+this._className+'-autocomplete-detail">';
 			
		var regText=el.childNodes[1].firstChild?el.childNodes[1].firstChild.nodeValue:null;
		var provText=el.childNodes[2].firstChild?el.childNodes[2].firstChild.nodeValue:null;
		
		if (regText && provText)
			item +=regText+', '+provText;
		else if (regText)
			item +=regText;
		else if (provText)
			item +=provText
		item +='</div></div>';
 		return { value: {city:el.childNodes[0].getAttribute('id'),admin1:el.childNodes[1].getAttribute('id'),admin2:el.childNodes[2].getAttribute('id')} ,actext:el.childNodes[0].firstChild.data, text:{city:el.childNodes[0].firstChild.data,admin1:regText,admin2:provText} , html: item};
 	},
 	_countrySelected: function(index)
 	{
 		if (index!=this._selectedIndex)
 		{	
 			this.country=this._menu.items[index].value;
 			this._selectedIndex=index;
 			this._menu.setSelectedByIndex(index,true);
 			this._menuButton.setContent('<img src="'+MEB.IMG_BASE+'flag/'+this.country.toLowerCase()+'.png" class="location-flag" />');
 			this.onCountryChange.fire(this.country);
 		}
 		this._menu.close();
 		this._autocomplete.setOpt(this.country);
 	},
 	_cityFound: function(arg)
 	{
 		this.value = arg.value;
 		if (arg.text && !arg.text.city)
 			arg.text={city:arg.text};
 		else if (!arg.text)
 			arg.text={};
 		
 		this.text = arg.text;
 		this.onChange.fire({text: this.text, value:this.value});
 	},
 	_empty:function()
 	{
 		this.value=null;
 		this.text={};
 		this.onChange.fire({text: this.text, value:this.value});
 	},
 	_cityNotFound:function(t)
 	{
 		this.onNotFound.fire({text: t.text});
 	},
 	findInList:function(adm1,adm2,text)
 	{
 		if (this._autocomplete.lastList)
 		{
 			for (var i=0;i<this._autocomplete.lastList.length;i++)
 			{
 				if (this._autocomplete.lastList[i].value.admin1==adm1 && ((adm2 && this._autocomplete.lastList[i].value.admin2==adm2) || !adm2) && text.toLowerCase()==this._autocomplete.lastList[i].text.city.toLowerCase())
 				{
 					this.value = this._autocomplete.lastList[i].value;
 					this.text = this._autocomplete.lastList[i].text;
 					return true;
 				}
 			}
 			this.value = null;
 		}
 		else
 			return false;
 	},
 	getCity: function()
 	{	
 		return {text: this.text, value:this.value};
 	},
 	destroy: function()
 	{
 		if(this._menuButton)
 			this._menuButton.destroy();
 		if(this._autocomplete)
 			this._autocomplete.destroy();
 		this._container.removeChild(this.htmlObj);
 	}
}

MEB.Bury = function(configs){
	this._title = configs.title?configs.title:'Bury';
	this._tooltipCode = configs.tooltipCode?configs.tooltipCode:'meb-bury';
	this._opt = configs.opt;
	this._ajaxURL = configs.ajaxURL;
	this._ajaxPOST = configs.ajaxPOST;
	this._className = (configs.className)?configs.className:'bury';
	this._target = configs.target.t;
	this._position = configs.target.p;
	
	this.htmlObj = document.createElement("div");
	this.htmlObj.className = this._className;
	this.htmlObj.innerHTML = '<div class="'+this._className+'-title">'+this._title+'</div>';
	
	this.buryItem = new Array();
	var i=0;
	for(value in this._opt){
		this.buryItem[i] = new Object();
		this.buryItem[i].htmlObj = document.createElement('div');
		this.buryItem[i].htmlObj.className = this._className+'-item';
		this.buryItem[i].htmlObj.innerHTML = this._opt[value];
		this.buryItem[i].type = value;
		this.htmlObj.appendChild(this.buryItem[i].htmlObj);
		i++;
	}
	
	this.onBury = new Y.util.CustomEvent( 'onbury' , this , true , Y.util.CustomEvent.FLAT  ); 
	this.enableBuryBtn();
}

MEB.Bury.prototype={
	open: function(e){
		if(this._target.tagName)
			Y.util.Event.removeListener(this._target, 'click');
		else
			this._target.onClick.unsubscribe(this.open);
		this._tooltip =new MEB.Tooltip({code:this._tooltipCode,target:this._target,align:this._position, content: this.htmlObj,closeButton:true});
		this._tooltip.onClose.subscribe(this.enableBuryBtn,null,this);
		this._createTooltip();
	},
	
	_createTooltip: function(){
		for(var i=0;i<this.buryItem.length;i++){
			Y.util.Event.addListener(this.buryItem[i].htmlObj,"click",this._clickBury,this.buryItem[i].type,this);
		}
	},
	
	_clickBury: function(e,arg){
		if(!cl.logged){
			cl.loginForm();
			this.enableBuryBtn();
			return;
		}
		for(var i=0;i<this.buryItem.length;i++){
			Y.util.Event.removeListener(this.buryItem[i].htmlObj,"click");
		}
		this._ajaxPOST += '&type='+arg;
		Y.util.Dom.setStyle(this.htmlObj,'opacity',0.6);
		this._tooltip.disableCloseButton();
		new MEB.Connect('POST', this._ajaxURL,{ ok:this._sendBury, error: this._sendBuryError,scope:this} , this._ajaxPOST , {t:this.htmlObj});
	},
	
	_sendBury: function(root){
		this.onBury.fire();
		this._tooltip.close(true);
	},
	
	_sendBuryError: function(e){
		this._tooltip.close();
	},
	enableBuryBtn:function(){
		if(this._target.tagName)
			Y.util.Event.addListener(this._target, 'click', this.open,null,this);
		else
			this._target.onClick.subscribe(this.open,null,this);
	},
	destroy: function(){
		if(this._tooltip)
			this._tooltip.destroy();
	}
}

MEB.Share = function(configs){
	this._title = configs.title?configs.title:'share';
	this._tooltipCode = configs.tooltipCode?configs.tooltipCode:'meb-share';
	this._shareURL = configs.shareURL;
	this._ajaxURL = configs.ajaxURL;
	this._ajaxPOST = configs.ajaxPOST;
	this._className = (configs.className)?configs.className:'share';
	this._target = configs.target.t;
	this._position = configs.target.p;
	
	this.onShare = new Y.util.CustomEvent( 'onshare' , this , true , Y.util.CustomEvent.FLAT); 
	this.enableShareBtn();
	cl.onLogout.subscribe(this.closeTooltip,null,this);
}

MEB.Share.prototype={
	_shareFBbtn:null,
	open: function(e){
		if(this._target.tagName)
			Y.util.Event.removeListener(this._target, 'click');
		else
			this._target.onClick.unsubscribe(this.open);
			
		this.htmlObj = document.createElement("div");
		this.htmlObj.className = this._className;
		
		if(cl.logged){
			if(cl.logged.data.type==0 || cl.logged.data.type==2){//solo fb 
				this._shareFBbtn = document.createElement('div');
				this._shareFBbtn.id = this._className+'-fb-icon';
				this._shareFBbtn.className = this._className+'-icon';
				this.htmlObj.appendChild(this._shareFBbtn);
			}
			if(cl.logged.data.type==1 || cl.logged.data.type==2){//solo meb
				this._shareNETbtn = document.createElement('div');
				this._shareNETbtn.id = this._className+'-net-icon';
				this._shareNETbtn.className = this._className+'-icon';
				this.htmlObj.appendChild(this._shareNETbtn);
			}
		}
		
		this._tooltip =new MEB.Tooltip({code:this._tooltipCode,target:this._target,align:this._position, content: this.htmlObj,closeButton:true});
		this._tooltip.onClose.subscribe(this.enableShareBtn,null,this);
		this._bind();
	},
	
	_bind: function(){
		if(this._shareFBbtn)
			Y.util.Event.addListener(this._shareFBbtn, 'click', this._clickShareFB,null,this);		
		if(this._shareNETbtn)
			Y.util.Event.addListener(this._shareNETbtn, 'click', this._clickShareFB,null,this);		
	},
	
	_clickShareFB: function(e,arg){
		var f = this;
		u=this._shareURL;
		t=document.title;
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
		this.closeTooltip();
		//FB.Connect.showShareDialog(this._shareURL,function(){ f.closeTooltip() });
	},
	closeTooltip:function(){
		if(this._tooltip)
		{
			this._tooltip.close();
			Y.util.Event.removeListener(this._shareFBbtn, 'click');
		}
	},
	enableShareBtn: function(){
		if(this._target.tagName)
			Y.util.Event.addListener(this._target, 'click', this.open,null,this);
		else
			this._target.onClick.subscribe(this.open,null,this);
	},
	destroy: function(){
		if(this._tooltip)
			this._tooltip.destroy();
	}
}

/*-----------------PIC MANAGER
param => container, id, classname, addurl, del url, pics, preview
*/
MEB.PicManager = function(configs)
{
	this._container = configs.container;
	this._className = configs.className?configs.className:'picManager';
	this.id=configs.id?configs.id:Y.util.Dom.generateId();
	this._preview = configs.preview?configs.preview:true;
	this._addURL = configs.addURL; 
	this._delURL = configs.delURL; 
	
	this.htmlObj=document.createElement('DIV');
	this.htmlObj.className = this._className;
	this._container.appendChild(this.htmlObj);
	
	this._uploaderHTML = document.createElement('div');
	this._uploaderHTML.className = this._className+"-add-photo";
	this._noImageObj = document.createElement('div');
	this._noImageObj.className = this._className+'-no-photos hidden';
	this._noImageObj.innerHTML = MEB.lang.common['no_photos'];
	this._picContainer = document.createElement('div');
	this._picContainer.className = this._className+'-photos-container';
	this.htmlObj.appendChild(this._uploaderHTML);
	this.htmlObj.appendChild(this._noImageObj);
	this.htmlObj.appendChild(this._picContainer);
	
	this._picUploader=new MEB.Uploader({container:this._uploaderHTML,action: this._addURL});
	this._picUploader.onUploaded.subscribe(this._picUploadComplete,null,this);
	this._picUploader.onSubmit.subscribe(this._picUploadStart,null,this);
	
	var tmpAnt=false;
	if(configs.pics){
		for(i=0;i<configs.pics.length;i++){
			this._addPic(configs.pics[i].src);
			if(configs.pics[i].ant)
				tmpAnt=i;
		}
	}
	if(this._preview && tmpAnt)
		this._changeAnt(true,{n:tmpAnt});
		
	this._showNoImage();
}

MEB.PicManager.prototype = {
	_picCount:0,_pic:new Array(),_picAnt:-1,_picReal:0,
	_showNoImage: function()
	{
		if(!this._picReal)
			Y.util.Dom.removeClass(this._noImageObj,'hidden');
	},
	_hideNoImage: function()
	{
		if(!Y.util.Dom.hasClass(this._noImageObj,'hidden'))
			Y.util.Dom.addClass(this._noImageObj,'hidden');
	},
	_picUploadStart:function(){},
	_picUploadComplete: function(obj)
	{
		this._picUploader.showInput();
		if(obj.err){
			if (obj.err==210)
			{
				this._picTooltip = new MEB.Tooltip({code:this._className+'-tooltip-error',target:this._picUploader,align:'t',type:'error',content:MEB.lang.errors[210],closeButton:true,closeOnChange:true});
				return true;
			}
			else
				return false;
		}
		this._addPic(obj.src);
		return true;
	},
	_addPic: function(src)
	{
		this._pic[this._picCount] = new Object();
		var cont = document.createElement('div');
		cont.className = this._className+'-img-container';
		this._pic[this._picCount].cont = cont;
		var del = document.createElement('div');
		del.className = this._className+'-del-icon';
		this._pic[this._picCount].del = del;
		if(this._preview)
		{
			var ant = document.createElement('div');
			ant.className = this._className+'-ant-icon';
			ant.innerHTML = '<button type="button" id="ant-'+this._picCount+'"></button>';
		}
		var img = document.createElement('div');
		img.className = this._className+'-img';
		img.innerHTML = '<img src="'+src+'" />';
		this._pic[this._picCount].img = src;
		cont.appendChild(del);
		cont.appendChild(ant);
		cont.appendChild(img);
		this._picContainer.appendChild(cont);
		if(this._preview)
		{
			this._pic[this._picCount].ant = new MEB.Checkbox({id:'ant-'+this._picCount});
			if(this._picReal==0){
				this._picAnt = this._picCount;
				this._pic[this._picCount].ant.activate(true);
			}
			this._pic[this._picCount].ant.onChange.subscribe(this._changeAnt,{n:this._picCount},this);
		}
		Y.util.Event.addListener(del,'click',this._delPicCall,{n:this._picCount},this);
		this._picCount++;
		this._picReal++;
		this._hideNoImage();
	},
	_delPicCall: function(e,arg)
	{
		new MEB.Connect('POST', this._delURL,{ ok:this._delPic, scope:this} ,'id='+arg.n, {t:this._pic[arg.n].cont});
	},
	_delPic: function(root)
	{
		var id = root.firstChild.nodeValue;
		if(this._preview)
		{
			if(this._picReal>1 && id==this._picAnt){
				for(var i=0;i<this._picCount;i++)
					if(id!=i && this._pic[i]){
						this._changeAnt(null,{n:i});
						break;	
					}
			}else if(this._picReal==1){
				this._picAnt = -1;
			}
			this._pic[id].ant.destroy();
		}	
		Y.util.Event.removeListener(this._pic[id].del,'click');
		this._picContainer.removeChild(this._pic[id].cont);
		this._pic[id] = null;
		this._picReal--;
		this._showNoImage();
	},
	_changeAnt:function(o,arg)
	{
		if(o==false){
			this._pic[arg.n].ant.activate(true);
			return;	
		}
		if(this._picAnt>-1){
			this._pic[this._picAnt].ant.deactivate(true);
		}
		this._picAnt = arg.n;
		this._pic[this._picAnt].ant.activate(true);
	},
	getAnt: function()
	{	
		return this._picAnt;
	},
	destroy: function()
	{
		if(this._picUploader)
			this._picUploader.destroy();
		if(this.htmlObj)
			this.htmlObj.parentNode.removeChild(this.htmlObj);
	}
}

/*----------------LOCATOR------------------
param => container , className, request, *zoom, *mapType, *disableMapUI, *markerImage, *caption

*request = stringa oppure oggetto{country,admin2,city,address}
dopo la creazione richiamare init()

accuracy:
	ROOFTOP => The returned result reflects a precise geocode.
	RANGE_INTERPOLATED => The returned result reflects an approximation (usually on a road) interpolated between two precise points (such as intersections). Interpolated results are generally returned when rooftop geocodes are unavilable for a street address.
	GEOMETRIC_CENTER => The returned result is the geometric center of a result such a line (e.g. street) or polygon (admin1).
	APPROXIMATE => The returned result is approximate.
*/
MEB.Locator = function(configs)
{
	this._geocoder = new google.maps.Geocoder();
	
	this._container = configs.container;
	this._className = configs.className?configs.className:'locator';
	this._zoom = configs.zoom?configs.zoom:16;
	this._mapType = configs.mapType?configs.mapType:google.maps.MapTypeId.ROADMAP;
	this._disableMapUI = configs.disableMapUI?configs.disableMapUI:true;
	this._markerImage = configs.markerImage?configs.markerImage:MEB.IMG_BASE+'marker.png';
	this._caption = configs.caption?configs.caption:true;
	this._autoShowMap = configs.autoShowMap!=undefined?configs.autoShowMap:true;
	
	this.onBeforeShow = new Y.util.CustomEvent( 'beforeshowevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onLoad = new Y.util.CustomEvent( 'loadevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onChange = new Y.util.CustomEvent( 'onmarkerchangeevent' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	
	if(configs.request)
		this._request = configs.request;
	else{
		this.latlng = configs.latlng;
		this.accuracy = configs.accuracy;
	}
}

MEB.Locator.prototype = {
	marker:null,map:null,latlng:null,accuracy:null,htmlObj:null,mapObj:null,_captionObj:null,
	getRegion: function()
	{
		return Y.util.Dom.getRegion(this._mapContainer);
	},
	init: function()
	{
		if(this._request)
			return this.request();
		else
		{
			/*
			this._createMap();
			this._setMarker();
			this.onLoad.fire({status: 'ok', lat:this.latlng.lat, lng:this.latlng.lng, accuracy: this.accuracy, requestStep: 2});
			if(this._caption)
				this._refreshCaption();
			*/
			this._requestStep=2;
			this._canRefresh=true;
			this.onBeforeShow.fire();
			if (this._autoShowMap)
				this.refreshMap();
			return true;
		}
	},
	disable:function()
	{
		if (this._caption)
			this.editCaption('');
		this.latlng = null;
		this.accuracy = null;
		this._mapObj.style.visibility='hidden';
	},
	enable:function()
	{
		if (this._caption)
			this._refreshCaption();
		this._mapObj.style.visibility='visible';
	},
	request:function(r,force)
	{
		var str
		if(Y.lang.isString(r))
		{
			str = r;
			if(r==this.request)
				return false;
			else
				this._request=r;
		}
		else
		{
			if (r)
			{
				if(this._request && !force && r.adm==this._request.adm && r.city==this._request.city && r.address==this._request.address)
					return false;
				else{
					this._request = {};
					this._request.country=r.country;	
					this._request.adm=r.adm;	
					this._request.city=r.city;
					this._request.address=r.address;
				}
			}
			str = (this._request.country?this._request.country+', ':'')+(this._request.adm?this._request.adm+', ':'')+(this._request.city?this._request.city+', ':'')+(this._request.address?this._request.address:'');
		}

		this._requestStep=2;
		var self = this;
		this._geocoder.geocode( { address: str}, function(results,status){self._geocoderResponse(results, status);});
		return true;
	},
	_searchCity:function()
	{
		if(!this._request.city)
			return false;
		str = (this._request.country?this._request.country+',':'')+(this._request.adm?this._request.adm+',':'')+this._request.city;
		var self = this;
		this._geocoder.geocode( { address: str}, function(results,status){self._geocoderResponse(results, status);});
	},
	_searchAdm:function()
	{
		if(!this._request.adm)
			return false;
		str = (this._request.country?this._request.country+',':'')+this._request.adm;
		var self = this;
		this._geocoder.geocode( { address: str}, function(results,status){self._geocoderResponse(results, status);});
	},
	refreshMap:function()
	{
		if (!this._canRefresh)
			return;
		if (!this.htmlObj)
			this._createMap();
		this.enable();
		this._setMarker();
		this.onLoad.fire({status: 'ok', lat:this.latlng.lat, lng:this.latlng.lng, accuracy: this.accuracy, requestStep: this._requestStep});
		if(this._caption)
			this._refreshCaption();
		this._canRefresh=false;
	},
	_geocoderResponse: function(results, status)
	{
		this._canRefresh=false;
		if (status == google.maps.GeocoderStatus.OK && results.length && ((this._requestStep==2 && results[0].geometry.location_type != google.maps.GeocoderLocationType.APPROXIMATE) || this._requestStep<2)) 
		{
			this.latlng = {lat: results[0].geometry.location.lat(), lng:results[0].geometry.location.lng()};
			this.bounds = results[0].geometry.viewport;			
			if(this._requestStep!=2 || results[0].geometry.location_type == google.maps.GeocoderLocationType.GEOMETRIC_CENTER)
				this.accuracy = 1;
			else 
				this.accuracy = 0;
			
			this._canRefresh=true;
        	if (!this.htmlObj || !this.marker)
			{
				this.onBeforeShow.fire();
				if (this._autoShowMap && !this.htmlObj)
					this._createMap();
				else if (!this._autoShowMap)
					return;
			}
			else
				this.marker.setVisible(false);
				
			this.refreshMap();
		}
		else if(this._requestStep)
		{
			switch(this._requestStep)
			{
				case 2:
					this._searchCity();
					this._requestStep--;
					break;
				case 1:
					this._searchAdm();
					this._requestStep--;
					break;	
			}
    	}
		else
		{
			this.latlng = null;
			this.accuracy = null;
			this.onLoad.fire({status: 'err', error: status});
		}
	},
	_createMap: function()
	{
		this.htmlObj=document.createElement('DIV');
		this.htmlObj.className = this._className;
		this._container.appendChild(this.htmlObj);
		
		this._mapBackObj=document.createElement('DIV');
		this._mapBackObj.className= this._className+'-back';
		this.htmlObj.appendChild(this._mapBackObj);
		
		this._mapObj=document.createElement('DIV');
		this._mapObj.className= this._className+'-map';
		this._mapBackObj.appendChild(this._mapObj);
			
		this._captionObj=document.createElement('DIV');
		this._captionObj.className= this._className+'-caption';
		this.htmlObj.appendChild(this._captionObj);

		var option = {zoom: this._zoom, mapTypeId: this._mapType, disableDefaultUI: this._disableMapUI, navigationControl: true };
		
		this.map = new google.maps.Map(this._mapObj, option);
	},
	_setMarker: function()
	{
		var point = new google.maps.LatLng(this.latlng.lat,this.latlng.lng);
		if(this.accuracy==0)
		{
			this.map.setCenter(point);
			this.map.setZoom(this._zoom);
		}
		else if(this.bounds)
			Y.lang.later(0,this,function(){this.map.fitBounds(this.bounds);});	//bug che non si può chiamare subito la funzione fitBounds altrimenti non la prende!
		else
			this.map.setCenter(point);
		
   		this.marker = new google.maps.Marker({position: point,	map: this.map, icon: this._markerImage, draggable: true});
		var self = this;
		google.maps.event.addListener(this.marker, 'dragend', function(){self._markerPositionChanged();});
	},
	_markerPositionChanged:function(){
		this.latlng = {lat: this.marker.get_position().lat(), lng:this.marker.get_position().lng()};
		this.accuracy=2;
		this._requestStep=2;
		this.onChange.fire({lat: this.latlng.lat, lng: this.latlng.lng, accuracy: this.accuracy});
		if(this._caption)
			this._refreshCaption();
	},
	_refreshCaption:function()
	{
		if(this._requestStep==1)
			this.editCaption(MEB.lang.locator['caption_city']);
		else if(this._requestStep==0) 
			this.editCaption(MEB.lang.locator['caption_adm']);
		else
		{
			if(this.accuracy==0)
				this.editCaption(MEB.lang.locator['caption_best']);
			else if(this.accuracy==1)
				this.editCaption(MEB.lang.locator['caption_good']);
			else
				this.editCaption(MEB.lang.locator['caption_personalize']);
		}
	},
	editCaption:function(txt)
	{
		this._captionObj.innerHTML = txt;
	},
	destroy: function() 
	{
		delete this.marker;
		delete this.map;
		delete this._geocoder;
		if(this.htmlObj)
			this.htmlObj.parentNode.removeChild(this.htmlObj);
	}
}

/*----------------LOCATION------------------
param => container, id, className, markerImage, country, value {city, city_name, admin1, admin2, address, latitude, longitude, accuracy}
*/
MEB.Location = function(configs)
{
	this._container = configs.container;
	this._className = configs.className?configs.className:'location';
	this.id=configs.id?configs.id:Y.util.Dom.generateId();
	this._markerImage = configs.markerImage?configs.markerImage:MEB.IMG_BASE+'marker.png';
	this.value = configs.value?configs.value:{};
	this._addressField = configs.addressField!=undefined?configs.addressField:true;
	this._zipcodeField = configs.zipcodeField!=undefined?configs.zipcodeField:false;
	this.country = configs.country;
	this._countries=configs.countries!=undefined?configs.countries:undefined;
	this._create();
	this._initAdmin();

	this._cityAutocomplete = new MEB.City({container: Y.util.Dom.get(this.id+'-city'), country: this.country, text: {city:this.value.city_name},value: {city:this.value.city,admin2:this.value.admin2,admin1:this.value.admin1}, countries: this._countries});
	this._cityAutocomplete.onCountryChange.subscribe(this._countryChange,null,this);
	this._cityAutocomplete.onChange.subscribe(this._cityChange,null,this);
	this._cityAutocomplete.onNotFound.subscribe(this._cityNotFound,null,this);
	
	/*
	if(configs.value && configs.value.city==0){
		Y.util.Dom.removeClass(this._admin1Row,'hidden');
		Y.util.Dom.removeClass(this._admin2Row,'hidden');
		if (this._useAdmin1)
		{
			this._admin1Select = new MEB.Admin1({container:this.id+'-admin1',country:this.country});
			this._admin1Select.onChange.subscribe(this._admin1Change,null,this);
			this._admin1Select.onInit.subscribe(function(){this._admin1Select.setValue(this.value.admin1);this._admin1Select.onInit.unsubscribeAll();},null,this);
		}
		if (this._useAdmin2)
		{
			this._admin2Select = new MEB.Admin2({container:this.id+'-admin2',country:this.country,admin1: (this.value.admin1?this.value.admin1:null)});
			this._admin2Select.onChange.subscribe(this._admin2Change,null,this);
			this._admin2Select.onInit.subscribe(function(){this._admin2Select.setValue({admin2:this.value.admin2,admin1:this.value.admin1});this._admin2Select.onInit.unsubscribeAll();},null,this);
		}
	}
	*/
	if(this._addressField)
		this._addressInput = Y.util.Dom.get(this.id+'-address');
	if(this._zipcodeField)
		this._zipcodeInput = Y.util.Dom.get(this.id+'-zipcode');
	this.onChange = new Y.util.CustomEvent( 'onchanget' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onBeforeMapShow = new Y.util.CustomEvent( 'onBeforeMapShow' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onSendRequest = new Y.util.CustomEvent( 'onSendRequest' , this , true , YAHOO.util.CustomEvent.FLAT  ); 
}

MEB.Location.prototype = {
	_admin1Select:null,_admin2Select:null,htmlObj:null,
	initMap:function(configs)
	{
		this._map=true;
		this._autoShowMap=configs.autoShowMap;
		this._addressNotRequired=configs.addressNotRequired;
		this._mapContainer = document.getElementById(this.id+'-locator');
		this._btnRefreshMap = document.getElementById(this.id+'-refresh-map');
		Y.util.Event.addListener(this._btnRefreshMap,'click',this._addressChanged,true,this);
		Y.util.Event.addListener(this._addressInput,'keyup',this._addressKeyUp,null,this);
		
		if(this.value.hasOwnProperty('accuracy'))
		{
			this._locator = new MEB.Locator({container: this._mapContainer,autoShowMap:this._autoShowMap,latlng: {lat :this.value.latitude, lng: this.value.longitude}, accuracy: this.value.accuracy, markerImage: this._markerImage});
			this._locator.onBeforeShow.subscribe(this._beforeShowMap,null,this);
			this._locator.onLoad.subscribe(this._showMapRequest,null,this);
			this._locator.onChange.subscribe(this._changeLatLng,null,this);
			this._locator.init();
			if (this.value.accuracy==2) 
				Y.util.Dom.removeClass(this._btnRefreshMap,'hidden');
		}
	},
	_initAdmin:function()
	{
		this._useAdmin2=this._useAdmin1=false;
		
		if (this._admin1Select)
		{
			this._admin1Select.destroy();
			this._admin1Select=null;
		}
		if (this._admin2Select)
		{
			this._admin2Select.destroy();
			this._admin2Select=null;
		}
		
		switch(this.country)
		{
			case('US'):
				this._useAdmin1=MEB.lang.fields['state']; 
				this._useAdmin1_sel=MEB.lang.fields['select_state'];
				this._useAdmin1_pre=MEB.lang.fields['select_pre_state'];
				if (document.getElementById(this.id+'-admin1-tit'))
					document.getElementById(this.id+'-admin1-tit').innerHTML=this._useAdmin1;
			break;
			default:
				this._useAdmin2=MEB.lang.fields['province'];
				this._useAdmin2_sel=MEB.lang.fields['select_province']; 
				if (document.getElementById(this.id+'-admin2-tit'))
					document.getElementById(this.id+'-admin2-tit').innerHTML=this._useAdmin2; 
		}
		if (this._useAdmin1)
		{
			
			Y.util.Dom.removeClass(this._admin1Row,'hidden');
			this._admin1Select = new MEB.Admin1({container:this.id+'-admin1',value:this.value.admin1,content: this._useAdmin1_sel});
			this._admin1Select.onChange.subscribe(this._admin1Change,null,this);
			this._admin1Select.init(this.country);
			if (!this.value.city && !this.value.city_name)
				this._admin1Select.disable();
			
			
		}
		else 
			Y.util.Dom.addClass(this._admin1Row,'hidden');
		
		if (this._useAdmin2)
		{
			Y.util.Dom.removeClass(this._admin2Row,'hidden');
			this._admin2Select = new MEB.Admin2({container:this.id+'-admin2',value:{admin1:this.value.admin1,admin2:this.value.admin2},content: (this._useAdmin1?this._useAdmin1_pre:this._useAdmin2_sel), disabled:(this._useAdmin1?true:false)});
			this._admin2Select.onChange.subscribe(this._admin2Change,null,this);
			if (!this._useAdmin1)
				this._admin2Select.init(this.country,null);
			else if (this.value.admin2)
				this._admin2Select.init(this.country,this.value.admin1);
			
			if (!this.value.city && !this.value.city_name || (this._useAdmin1 && !this.value.admin1))
				this._admin2Select.disable();
		}
		else
			Y.util.Dom.addClass(this._admin2Row,'hidden');
	},
	_create: function()
	{
		this.htmlObj=document.createElement('DIV');
		this.htmlObj.className = this._className;
		this._container.appendChild(this.htmlObj);
		
		this._cityRow = document.createElement('DIV');
		this._cityRow.className= 'field field-city';
		this._cityRow.innerHTML = '<div class="field-label">'+MEB.lang.fields['city']+'</div><div id="'+this.id+'-city"></div>';
		this.htmlObj.appendChild(this._cityRow);
		
		if(this._zipcodeField)
		{
			this._zipcodeRow = document.createElement('DIV');
			this._zipcodeRow.className= 'field field-zipcode';
			this._zipcodeRow.innerHTML = '<div class="field-label">'+MEB.lang.fields['zipcode']+'</div><div class="field-input"><input id="'+this.id+'-zipcode" name="zip" type="text" value="'+(this.value.zipcode?this.value.zipcode:'')+'"></div>';
			this.htmlObj.appendChild(this._zipcodeRow);
		}
		
		this._admin1Row = document.createElement('DIV');
		this._admin1Row.className= 'field hidden field-admin1';
		this._admin1Row.innerHTML = '<div class="field-label" id="'+this.id+'-admin1-tit">'+this._useAdmin1+'</div><div id="'+this.id+'-admin1" class=""></div>';
		this.htmlObj.appendChild(this._admin1Row);

		this._admin2Row = document.createElement('DIV');
		this._admin2Row.className= 'field hidden field-admin2';
		this._admin2Row.innerHTML = '<div class="field-label" id="'+this.id+'-admin2-tit">'+this._useAdmin2+'</div><div id="'+this.id+'-admin2" class=""></div>';
		this.htmlObj.appendChild(this._admin2Row);
		if(this._addressField)
		{
			this._addressRow = document.createElement('DIV');
			this._addressRow.className= 'field field-address';
			this._addressRow.innerHTML = '<div class="field-label ">'+MEB.lang.fields['address']+'</div><div class="field-input"><input id="'+this.id+'-address" name="address" type="text" value="'+(this.value.address?this.value.address:'')+'"></div>'+
										'<div id="'+this.id+'-refresh-map" class="refresh-map hidden">'+MEB.lang.locator['refresh_map']+'</div>';
			this.htmlObj.appendChild(this._addressRow);
		}
		this._locatorRow = document.createElement('DIV');
		this._locatorRow.id = this.id+'-locator';
		this._locatorRow.className= 'locator-container';
		this.htmlObj.appendChild(this._locatorRow);
	},
	_countryChange:function()
	{
		this.country = this._cityAutocomplete.country; 
		this.value.city=null;
		this.value.city_name=null;
		this.value.admin1=null;
		this.value.admin2=null;
		//this._hideAdmins();
		this._initAdmin();
		this._isAllComplete();
	},
	_cityNotFound:function(t)
	{
		if (this._useAdmin1)
			this._admin1Select.enable();
		if (this._useAdmin2)
			this._admin2Select.enable();

		this._isAllComplete(false,t.text);
	},
	_cityChange:function()
	{
		var locationCity = this._cityAutocomplete.getCity();
		if(!locationCity.value && locationCity.text.city)
		{
			this.value.city = 0;
			this.value.city_name = locationCity.text.city;
			
			if (this._useAdmin2 || this._useAdmin1)
			{	
				//Y.lang.later(0,this,function(){ if (this._useAdmin1) this._admin1Select.htmlObj.focus(); else this._admin2Select.htmlObj.focus();});
				if (this._useAdmin1)
				{
					this._admin1Select.enable();
					this._admin1Select.htmlObj.focus();
				}
				if (this._useAdmin2)
				{
					this._admin2Select.enable();
					this._admin2Select.htmlObj.focus();
				}
			}
			else
				this._isAllComplete();
		}
		else if(locationCity.value)
		{
			this.value.city = locationCity.value.city;
			this.value.city_name = locationCity.text.city;
			this.value.admin1 = locationCity.value.admin1;
			this.value.admin2 = locationCity.value.admin2;	
			this.value.admin2_name = locationCity.text.admin2;	
			if (this._useAdmin1)
			{
				this._admin1Select.enable();
				this._admin1Select.setValue(this.value.admin1);
			}
			if (this._useAdmin2)
			{
				this._admin2Select.enable();
				this._admin2Select.setValue({admin1:this.value.admin1,admin2:this.value.admin2});
			}
			this._addressChanged();
		}
		else
		{
			if (this._useAdmin1)
				this._admin1Select.disable();
			if (this._useAdmin2)
				this._admin2Select.disable();
			this.value.city = null;
			this.value.city_name = null;
			this.value.admin2 = null;
			this.value.admin1 = null;
		}
		this._isAllComplete();
	},
	_hideAdmins: function()
	{
		Y.util.Dom.addClass(this._admin1Row,'hidden');
		Y.util.Dom.addClass(this._admin2Row,'hidden');
		if(this._admin1Select)
		{
			this._admin1Select.destroy();
			this._admin1Select=null;
		}
		if(this._admin2Select)
		{
			this._admin2Select.destroy();
			this._admin2Select=null;
		}
	},
	_admin1Change:function()
	{
		this.value.admin1 = this._admin1Select.value;
		this.value.admin2 = null;
		if (this._useAdmin2)
		{
			this._admin2Select.init(this.country,this.value.admin1);
			this._admin2Select.setContent(this._useAdmin2_sel);
			this._admin2Select.enable();
		} 
		else
			this._checkCity();
		this._isAllComplete();
	},
	_admin2Change: function()
	{
		this.value.admin2 = this._admin2Select.value.admin2;
		if (!this._useAdmin1)
			this.value.admin1 = this._admin2Select.value.admin1;
		
		this.value.admin2_name = this._admin2Select.getText();
		
		this._checkCity();

		this._isAllComplete();
	},
	_checkCity:function()
	{
		if (this._cityAutocomplete.findInList(this.value.admin1,this.value.admin2,this.value.city_name))
		{
			this.value.city = this._cityAutocomplete.value.city;
			this.value.city_name =  this._cityAutocomplete.text.city;
		}
		else
			this.value.city= null;
	},
	_isAllComplete: function(force,cityPreview)
	{
		if (!this._map )
			return;
		if((!this.value.address && !this._addressNotRequired) || (!this.value.admin1 && this._useAdmin1) || (!this.value.admin2 && this._useAdmin2))
		{
			this.value.latitude = null;
			this.value.longitude = null;
			this.value.accuracy = null;
			this.value.requestStep = null;
			if (this._locator)
				this._locator.disable();
			this._hideRefreshBtn();
			return false;
		}
		if(this.value.city_name || cityPreview)
		{
			this._sendRequest(force,cityPreview);
			return true;
		}
		else
		{
			this.value.latitude = null;
			this.value.longitude = null;
			this.value.accuracy = null;
			this.value.requestStep = null;
			if (this._locator)
				this._locator.disable();
			this._hideRefreshBtn();
			return false;
		}
	},
	_addressKeyUp:function()
    {
    	if (this._aTimer)
    		this._aTimer.cancel();
   		this._aTimer=Y.lang.later(800,this,this._addressChanged);
    },
	_addressChanged:function(e,arg)
	{
		this._aTimer = null;
		this.value.address = Y.util.Dom.get(this.id+'-address').value;
		this._isAllComplete(arg);
	},
	_sendRequest: function(force,cityPreview){
		if(this._loading)
		{
			this._loading.destroy();
			this._loading = null;
		}
		this._loading = new MEB.Loading(this._addressInput,'r');
		this._loading.show();
		this.onSendRequest.fire();
		var hide=true;
		if(!this._locator)
		{
			this._locator = new MEB.Locator({container: this._mapContainer,autoShowMap:this._autoShowMap, request: {country: MEB.lang.countries[this.country], adm: (this._useAdmin2?this.value.admin2_name:(this._useAdmin1?this.value.admin1_name:null)), city: cityPreview?cityPreview:this.value.city_name, address: this.value.address}, markerImage: this._markerImage});
			this._locator.onBeforeShow.subscribe(this._beforeShowMap,null,this);
			this._locator.onLoad.subscribe(this._showMapRequest,null,this);
			this._locator.onChange.subscribe(this._changeLatLng,null,this);
			hide = this._locator.init();
		}
		else
			hide = this._locator.request({country: MEB.lang.countries[this.country], adm: (this._useAdmin2?this.value.admin2_name:(this._useAdmin1?this.value.admin1_name:null)), city: cityPreview?cityPreview:this.value.city_name, address: this.value.address},force);
		if(!hide)
			this._loading.hide();
	},
	_beforeShowMap:function()
	{
		this.onBeforeMapShow.fire();
	},
	refreshMap:function()
	{
		if (this._locator)
			this._locator.refreshMap();
	},
	_hideRefreshBtn:function()
	{
		if(!Y.util.Dom.hasClass(this._btnRefreshMap,'hidden')){
			//Y.util.Event.addListener(this._addressInput,'keyup',this._addressKeyUp,null,this);
			Y.util.Dom.addClass(this._btnRefreshMap,'hidden');
		}
	},
	_showMapRequest: function(arg)
	{
		this._hideRefreshBtn();
		if(arg.status=='ok')
		{
			this.value.latitude = arg.lat;
			this.value.longitude = arg.lng;
			this.value.accuracy = arg.accuracy;
			this.value.requestStep = arg.requestStep;
		}
		else
		{
			this.value.latitude = null;
			this.value.longitude = null;
			this.value.accuracy = null;
			this.value.requestStep = null;
			this._locator.destroy();
			this._locator=null;
		}
		this._loading.hide();
		this.onChange.fire();
		if(this._tooltip){
			this._tooltip.close();
			this._tooltip= null;
		}
	},
	_changeLatLng: function(arg)
	{
		if(Y.util.Dom.hasClass(this._btnRefreshMap,'hidden')){
			//Y.util.Event.removeListener(this._addressInput,'keyup');
			Y.util.Dom.removeClass(this._btnRefreshMap,'hidden');
		}
		this.value.latitude = arg.lat;
		this.value.longitude = arg.lng;
		this.value.accuracy = arg.accuracy;
		this.value.requestStep = 2;
		this._locator.editCaption(MEB.lang.locator['caption_personalize']);
		this.onChange.fire();
		if(this._tooltip){
			this._tooltip.close();
			this._tooltip= null;
		}
	},
	updateValues:function()
	{
		if(!this._map)
			this.value.address = Y.util.Dom.get(this.id+'-address').value;
		if (this._zipcodeField)
		this.value.zipcode = Y.util.Dom.get(this.id+'-zipcode').value;
	},
	check: function(skip,errors)
	{
		this.updateValues();
		if (!skip)
			skip={};
		if (!errors)
			errors={};
		if (!skip.city)
		{
			if(!this.value.city && !this.value.city_name){
				this._tooltip = new MEB.Tooltip({code:'location-tooltip-error',target:this._cityAutocomplete,align:'t',type:'error',content:(errors.city?errors.city:MEB.lang.errors[200]),closeButton:false,closeOnChange:true});
				return false;
			}
			if (!skip.address)
			{
				if(!this.value.address || this.value.address==""){
					this._tooltip = new MEB.Tooltip({code:'location-tooltip-error',target:this._addressInput,align:'t',type:'error',content:(errors.address?errors.address:MEB.lang.errors[200]),closeButton:false,closeOnChange:true});
					return false;
				}
				
			}
			if (!skip.zipcode && this._zipcodeField)
			{
				if(!this.value.zipcode || this.value.zipcode==""){
					this._tooltip = new MEB.Tooltip({code:'location-tooltip-error',target:this._zipcodeInput,align:'t',type:'error',content:(errors.zipcode?errors.zipcode:MEB.lang.errors[200]),closeButton:false,closeOnChange:true});
					return false;
				}
				
			}
		}
		if (this._map && !skip.geo && (this.value.city || this.value.city_name) && this.value.address )
		{
			if(!this.value.latitude){
				this._tooltip = new MEB.Tooltip({code:'location-tooltip-error',target:this._addressInput,align:'t',type:'error',content:(errors.geo?errors.geo:MEB.lang.errors[251]),closeButton:true,closeOnChange:true,width:250});
				return false;
			}
			if(this.value.requestStep!=2){
				this._tooltip = new MEB.Tooltip({code:'location-tooltip-error',target:this._mapContainer,align:'t',type:'error',content:(errors.geo?errors.geo:MEB.lang.errors[252]),closeButton:true,width:250});
				return false;
			}
		}
		return true;
	},
	
	destroy: function()
	{
		if(this._tooltip)
			this._tooltip.close();
		if(this._cityAutocomplete)
			this._cityAutocomplete.destroy();
		if(this._locator)
			this._locator.destroy();
		if(this.htmlObj)
			this.htmlObj.parentNode.removeChild(this.htmlObj);
	}
}

})();
Y.register('meb-controls', MEB.Button, {version: "1.0", build: '1'});
