/*modal.js 0.2*/

(function () {
	/*
	 * @param (object) body  			si passa l oggetto da utilizzare dentro la modal
	 * @param (object) iframe 			in alternativa a body di passa iframe:{url:'',width:'',height:''}. 
	 * 				  					HEIGHT può essere tralasciata ed in questo caso da dentro l iframe occorre fare
	 * 				  					window.frameElement.modal.setBodyHeight(document.body.offsetHeight);
	 * @param (bool) logo				se si vuole visualizzare il logo in alto a sinistra
	 * @param (bool) autosize 			(default: true)
	 * @param (bool) noClose
	 * 
	 * Eventi
	 * onClose
	 * onResize
	 * onRende
	 * 
	 * @method centerBody()		centra il body. se il body è piu alto del browser allora si mette la scollbar verticale nel DIV con l'opacità
	 * 							se la pagina che contiene la modal ha una sua scrollbar attiva allora si usa direttamente quella.
	 * 							Per evitare di usare la scollbar della pagina, il body della pagina deve avere la classe css no-scroll
	 * @method setBodyHeight(h) alza il body
	 * */
MEB.Popup = function(configs) 
{
	if (window.frameElement) //&& window.frameElement.modal
	{
		this._document=parent.document;
		this._Y=parent.Y;
		this._MEB=parent.MEB;
	}
	else
	{
		this._Y=Y;
		this._MEB=MEB;
		this._document=document;
	}
	this.id=configs.id;
	this.body=configs.body;
	this.title=configs.title;
	this.logo=configs.logo;
	this.iframe=configs.iframe;
	this.container=configs.container;
	this.autoSize=configs.autoSize;
	this.polling=configs.polling;
	this._contest=configs.contest?configs.contest:'std';
	this.type=configs.type;
	this.noClose=configs.noClose;
	this._animTime = configs.animTime?configs.animTime:0.5;
	this.onClose=new this._Y.util.CustomEvent( 'ModalCloseEvent' , window , true , YAHOO.util.CustomEvent.FLAT  );
	this.onMove=new this._Y.util.CustomEvent( 'ModalMoveEvent' , window , true , YAHOO.util.CustomEvent.FLAT  ); 
	
	this.onResize=new this._Y.util.CustomEvent( 'ModalResizeEvent' , window , true , YAHOO.util.CustomEvent.FLAT  ); 
	this.onRender=new this._Y.util.CustomEvent( 'ModalRenderEvent' , window , true , YAHOO.util.CustomEvent.FLAT  ); 
	this._anim=new Array();
	this._cAnim=0;
};
MEB.Popup.prototype={
	_back:null,_body:null,container:null,htmlObj:null,autoSize:true,width:null,height:null,logo:null,
	getEnv:function()
	{
		return {document:this._document,Y:this._Y,MEB:this._MEB};
	},
	setTitle:function(tit)
	{
		this._tit.innerHTML=tit;
	},
	setLogo:function(site)
	{
		if(site)
		{
			if(this.logo)
				return;
			Y.util.Dom.addClass(this._logo,'modal-logo-'+(Y.lang.isString(site)?site:'net'));
		}
		else
			this._logo.className = 'modal-logo';
		this.logo=site;
	},
	setBody:function(body)
	{
		this.body=body;
	},
	switchContest:function(s)
	{
		Y.util.Dom.removeClass(this.htmlObj,this._contest);
		this._contest=s;
		Y.util.Dom.addClass(this.htmlObj,this._contest);
	},
	_createContainer:function()
	{	
		this._container=this._document.createElement('table');
		this._container.className="modal-container";
		this._tr1 = this._container.insertRow(0);
		this._td1 = this._tr1.insertCell(0);
		this._td1.className = "modal-td1";
		this._td2 = this._tr1.insertCell(1);
		this._td2.className = "modal-td2";
		this._td3 = this._tr1.insertCell(2);
		this._td3.className = "modal-td3";
		
		this._tr2 = this._container.insertRow(1);
		this._td4 = this._tr2.insertCell(0);
		this._td4.className = "modal-td4";
		this._td5 = this._tr2.insertCell(1);
		this._td5.className = "modal-td5";
		this._td6 = this._tr2.insertCell(2);
		this._td6.className = "modal-td6";

		this._tr3 = this._container.insertRow(2);
		this._td7 = this._tr3.insertCell(0);
		this._td7.className = "modal-td7";
		this._td8 = this._tr3.insertCell(1);
		this._td8.className = "modal-td8";
		this._td9 = this._tr3.insertCell(2);
		this._td9.className = "modal-td9";
			
		if(this.body)
		{
			this._body=this._document.createElement('DIV');
			this._body.style.visibility='hidden';
			if (Y.lang.isObject(this.body))
				this._body.appendChild(this.body);
			else
				this._body.innerHTML+=this.body;
		}
		else
		{
			this._body=this._document.createElement('IFRAME');
			this._body.frameBorder='0';
			this.bw=this.iframe.width;
			if (this.iframe.height)
			{
				this.bh=this.iframe.height;
				this._body.height=this.bh+'px';
			}
			else
			{
				this.bh=100;
				this._body.height='100px';
				
			}
			this._body.width=this.bw+'px';
			this._body.style.visibility='hidden';
			this._body.src=this.iframe.url;
		}
		
		this._body.popup=this;
		this._body.className="modal-body";
		
		this._td5.appendChild(this._body);
		this._extContainer.appendChild(this._container);		
	},
	_createBar: function()
	{
		this._logo=this._document.createElement('DIV');
		this._logo.className="modal-logo";
		this._extContainer.appendChild(this._logo);
		if(this.logo)
			this.setLogo(this.logo);
			
		this._tit=this._document.createElement('DIV');
		this._tit.className="modal-title";
		this._extContainer.appendChild(this._tit);
		if(this.title)
			this.setTitle(this.title);
		
		this._btnCloseCont=this._document.createElement('DIV');
		this._btnCloseCont.className='modal-btnCloseCont';
		this._btnCloseCont.style.display='none';
		this._extContainer.appendChild(this._btnCloseCont);
		
		this._btnHomeCont=this._document.createElement('DIV');
		this._btnHomeCont.className='modal-btnProfile';
		this._btnHomeCont.style.display='none';
		this._extContainer.appendChild(this._btnHomeCont);
	},
	render:function()
	{
		cl.popupOpened(this);
		this.htmlObj=this._document.createElement('DIV')
		this.htmlObj.className=this._contest+" modal";
		this.htmlObj.style.zIndex=this.zIndex;
		
		this._extContainer=this._document.createElement('DIV')
		this._extContainer.className="modal-ext";
		this.htmlObj.appendChild(this._extContainer);
		
		this._createContainer();
		this._createBar();	
		
		if(!this.container)
		{
			if (!this.noClose)
			{
				this._btnClose=new this._MEB.Button({container:this._btnCloseCont,className:'modal-btnClose'});
				this._btnClose.onClick.subscribe(this.close,null,this);
			}
			this._document.body.appendChild(this.htmlObj);
		}
		else
			this.container.appendChild(this.htmlObj);
		
		if (this.body)
		{
			var r=this._Y.util.Dom.getRegion(this._body.firstChild);
			if (r)
			{
			    this.bw=r.right-r.left;
			    this.bh=r.bottom-r.top;
			    this._body.style.width=this.bw+'px';
			    this._body.style.height=this.bh+'px';
			}
		}
		
		this.onRender.fire();
		this.resize();
		this._body.style.visibility='visible';
		if (!this.noClose)
			this._btnCloseCont.style.display='block';
		if (this.autoSize || !this.container)
			this._Y.util.Event.addListener(window,'resize',this.resize,null,this);
	},
	resize:function()
	{
		this.vw=this._Y.util.Dom.getViewportWidth();
		this.vh=this._Y.util.Dom.getViewportHeight();
			
		if (!this.container)
		{
			this.width=this.vw;
			this.height=this._Y.util.Dom.getDocumentHeight();
		}
		else
		{
			var cr=this._Y.util.Dom.getRegion(this.container);
			this.width=cr.right-cr.left;
			this.height=cr.bottom-cr.top;
		}
		this.htmlObj.style.width=this.width+'px'; 
		this.htmlObj.style.height=this.height+'px'; 
		this.onResize.fire();
		this.centerBody(false,{w:this.bw,h:this.bh});
	},
	startPooling:function()
	{
		this.stopPooling();
		if (this._body.contentWindow)
			this._poolTime=this._Y.lang.later(700,this,this._poolw,null,true);
		else
			this._poolTime=this._Y.lang.later(700,this,this._pool,null,true);
	},
	stopPooling:function()
	{
		if (!this._poolTime)
			return;
		this._poolTime.cancel();
		this._poolTime=null;
	},
	_poolw:function()
	{
		try
		{
			var doPool=false,b=this._body.contentWindow.document.body;
			if (b.offsetWidth!=this.bw || b.offsetHeight!=this.bh && b.offsetHeight>0)
				this.setBodyDim(b.offsetWidth,b.offsetHeight);
		}
		catch(e)
		{
			//this.stopPooling();
		}
	},
	_pool:function()
	{
		try
		{
			var doPool=false;
			
			if (this._body.firstChild.offsetWidth!=this.bw || this._body.firstChild.offsetHeight!=this.bh)
				this.setBodyDim(this._body.firstChild.offsetWidth,this._body.firstChild.offsetHeight);
		}
		catch(e)
		{
			//this.stopPooling();
		}
	},
	setBodyHeight:function(h)
	{
		alert('deprecato');
	},
	setBodyDim:function(w,h)
	{
		this._anim[this._anim.length]={w:w,h:h};
		if (!this._anim2)
			this._nextAnim();
	},
	_nextAnim:function()
	{
		if (!this._anim[this._cAnim])
		{
			this._anim1=null;
			this._anim2=null;
			this._animEnd();
			return;
		}
		var p={w:this.bw,h:this.bh};
		this.bh=this._anim[this._cAnim].h;
		this.bw=this._anim[this._cAnim].w;
		this._cAnim++;
		this.centerBody(true,p);
		this._anim2 = new this._Y.util.Anim(this._body, {height: {  to: this.bh },width: {  to: this.bw }}, this._animTime, Y.util.Easing.easeInStrong	);
		this._anim2.animate();
		this._anim2.onComplete.subscribe(this._nextAnim,null,this);
	},
	_animEnd: function()
	{
	
	},
	getCenter:function()
	{
		var r=Y.util.Dom.getRegion(this._body);
		return {x:(r.right+(r.right-r.left)/2),y:(r.top+(r.bottom-r.top)/2)};
	},
	centerBody:function(anim,previous)
	{
		var f,l,t,dt=this._Y.util.Dom.getDocumentScrollTop(); 
		l=this._Y.util.Dom.getDocumentScrollLeft()+(this.vw-this.bw)/2-30;
		if (this.bh+85>this.height)
		{
			if (this._Y.util.Dom.hasClass(this._document.body,'no-scroll'))
			{
				this.htmlObj.style.overflow='auto';
				this.htmlObj.style.overflowY='scroll';
				this.htmlObj.style.overflowX='hidden';
			}
			t=0;
			
		}
		if (this.bh+85<=this.height || previous)
		{
			//f=this.bh>400?3:3;
			if (!previous || this.bh+85<this.height)
				t=dt+(this.vh-this.bh)/3;
			else
			{
				//centro rispetto la posizione/dimensione precedente
				var r=Y.util.Dom.getRegion(this._extContainer);
				t=r.top+(previous.h-this.bh)/2;
				dt=0; //non vado a considerare lo scrollTop
			}
		}
		if (t<dt+25)
			t=dt+25;
		if (l<0)
			l=0;
		
		if (anim)
		{
			var a = new this._Y.util.Anim(this._extContainer, {top: {  to: t },left:{ to: l }}, this._animTime, Y.util.Easing.easeIn	);
			a.onComplete.subscribe(this._moved,null,this);
			a.animate();
		}
		else
		{
			this._extContainer.style.left=l+'px';
			this._extContainer.style.top=t+'px';
			this.onMove.fire();
			
		}
		/*
		if (!this.container && !this.noClose)
		{
			this._btnCloseCont.style.left=(l+r.right-r.left)+'px';
			this._btnCloseCont.style.top=t+'px';
		}
		*/
	},
	_moved:function()
	{
		this.onMove.fire();
	},
	close:function(a)
	{
		if (this._body.src)
			this._body.src=MEB.MODULES_BASE+'blank.html';
		
		this.htmlObj.parentNode.removeChild(this.htmlObj);
		this.stopPooling();
		this._Y.util.Event.removeListener(window,'resize',this.resize);
		
		
		if (!this.container)
		{
		    if (this._oldScrollX)
		    	this._document.body.style.overflowX=this._oldScrollX;
		}
		
		this.onClose.fire(a); 
		cl.popupClosed(this);
	}
};

MEB.ErrorPopup=function(configs)
{
	if (!configs.title)
		configs.title=MEB.lang.common.error;
	
	configs.contest = 'error';
	configs.body='<div class="modal-default-body">'+configs.body+'</div>';
	MEB.ErrorPopup.superclass.constructor.call(this,configs);
	this.render();
}
Y.lang.extend(MEB.ErrorPopup, MEB.Popup);

MEB.InvitePopup=function(configs)
{
    /*
    var m='<fb:serverfbml id="invitePopup">';

        m+='<script type="text/fbml">';

        m+='<fb:fbml>';

        m+='<fb:request-form action="'+(!configs.action?MEB.MODULES_BASE+'closePopup.php':configs.action)+'" method="GET" invite="true" type="'+configs.type+'" content="'+configs.content+'">';

        m+='<fb:multi-friend-selector bypass="cancel" target="_self" showborder="false" rows="4" actiontext="'+configs.actionText+'">';

        m+='</fb:request-form>';

        m+='</fb:fbml>';

        m+='</script>';

    m+='</fb:serverfbml>';
	configs.body='<div class="invite-body">'+m+'</div>';
	*/
	configs.iframe={url:MEB.MODULES_BASE+'invitePopup.php?check='+MEB.CHECK+'&action='+escape(!configs.action?MEB.MODULES_BASE+'closePopup.php':configs.action)+'&content='+escape(configs.content)+'&actiontext='+configs.actionText+'&type='+configs.type,width:650,height:500};
	MEB.InvitePopup.superclass.constructor.call(this,configs);
	this.render();
}
Y.lang.extend(MEB.InvitePopup, MEB.Popup);

MEB.ImagePopup=function(configs)
{
	configs.contest = 'image';
	configs.animTime = 0.1; 
	MEB.ImagePopup.superclass.constructor.call(this,configs);
	this.body = this._document.createElement('DIV');
	this.body.className = "modal-image-loading";
	this.render();
	this.startPooling();
	this._showLoading();
	this.setImage(configs.image);
}
Y.lang.extend(MEB.ImagePopup, MEB.Popup,{
	_loading:null,_htmlContImg:null,
	_createContainer:function()
	{	
		this._container=this._document.createElement('table');
		this._container.className="modal-container";
		this._tr1 = this._container.insertRow(0);
		this._td1 = this._tr1.insertCell(0);
		this._td1.className = "modal-td1";
		this._td2 = this._tr1.insertCell(1);
		this._td2.className = "modal-td2";
		this._td3 = this._tr1.insertCell(2);
		this._td3.className = "modal-td3";
		
		this._tr2 = this._container.insertRow(1);
		this._td4 = this._tr2.insertCell(0);
		this._td4.className = "modal-td4";
		this._td5 = this._tr2.insertCell(1);
		this._td5.className = "modal-td5";
		this._td6 = this._tr2.insertCell(2);
		this._td6.className = "modal-td6";

		this._tr3 = this._container.insertRow(2);
		this._td7 = this._tr3.insertCell(0);
		this._td7.className = "modal-td7";
		this._td8 = this._tr3.insertCell(1);
		this._td8.className = "modal-td8";
		this._td9 = this._tr3.insertCell(2);
		this._td9.className = "modal-td9";
		
		this._body=this._document.createElement('DIV');
		this._body.style.visibility='hidden';
		this._body.appendChild(this.body);
		this._body.popup=this;
		this._body.className="modal-body";
		
		this._td5.appendChild(this._body);
		this._extContainer.appendChild(this._container);		
	},
	_animEnd:function()
	{
		if(this._loaded)
		{
			this._imageHtml.style.visibility = 'visible';
		}
	},
	_createBar:function()
	{
		this._tit=this._document.createElement('DIV');
		this._tit.className="modal-title";
		this._extContainer.appendChild(this._tit);
		if(this.title)
			this.setTitle(this.title);
		
		this._btnCloseCont=this._document.createElement('DIV');
		this._btnCloseCont.className='modal-btnCloseCont';
		this._btnCloseCont.style.display='none';
		this._extContainer.appendChild(this._btnCloseCont);
	},
	_showLoading: function()
	{
		if(!this._loading)
			this._loading = new MEB.Loading(this.body,'c');
		this._loading.show();
	},
	close: function(a)
	{
		MEB.ImagePopup.superclass.close.call(this,a);
		if(this._loading.visible)
			this._loading.hide();
	},
	setImage:function(src)
	{
		this._loaded = false;
		this.image = new Image();
		Y.util.Event.addListener(this.image,'load', this._onLoadImage,null,this);
		Y.util.Event.addListener(this.image,'error',this._loadImageError,null,this);
		Y.util.Event.addListener(this.image,'abort',this._loadImageError,null,this);
		this.image.src = src;
	},
	_onLoadImage:function()
	{
		this._loaded = true;
		this._imageHtml = document.createElement('IMG');
		this._imageHtml.src = this.image.src;
		this._imageHtml.style.visibility = 'hidden';
		this._body.innerHTML = '';
		this._body.appendChild(this._imageHtml);
		this._loading.hide();
	},
	_loadImageError:function()
	{
		this._loaded = false;
		this.body.className = 'modal-image-error';
		this.body.innerHTML = MEB.lang.errors[10];
		this._loading.hide();
	}
});

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