var efUI = new Class({

	Implements: [Events, Options],

	options:{
		classNamePrefix: 'efUI-',
		overlayOpacity:.3
	},

	initialize: function(options){
		this.setOptions(options);

		this.build();

		this.popup = {
			title:"Some title",
			content:"<p>Some html</p><input type=\"button\" value=\"button\" />",
			mayClose:true,
			defaultWidth: this.popupEl.getStyle('width').toInt(),
			defaultTop: this.popupEl.getStyle('top').toInt(),
			width: this.popupEl.getStyle('width'),
			loading:false
		};
		
		this.prevPopup = null;
		//alert($(this.cssStr('popup').getParent()));
	},

	build: function(){
		//build overlay
		this.overlayEl = new Element('div',{
			'id':this.cssStr('overlay'),
			'styles':{
				'opacity':0
			}
		}).inject(document.body);

		//build popup
		this.popupEl = new Element('table',{
			'id':this.cssStr('popup'),
			'styles':{
				'opacity':0
			},
			'cellpadding':0,
			'cellspacing':0
		}).adopt(new Element('tbody').adopt(
			new Element('tr').adopt(
				new Element('td',{
					'id':this.cssStr('popup-cornerTL')
				}),
				new Element('td',{
					'id':this.cssStr('popup-cornerT')
				}),
				new Element('td',{
					'id':this.cssStr('popup-cornerTR')
				})
			),
			new Element('tr').adopt(
				new Element('td',{
					'id':this.cssStr('popup-cornerL')
				}),
				new Element('td',{
					'id':this.cssStr('popup-center')
				}).adopt(
					new Element('div',{
						'id':this.cssStr('popup-header')
					}).adopt(
						this.popupTitleEl = new Element('div',{
							'id':this.cssStr('popup-title')
						}),
						this.popupCloseEl = new Element('div',{
							id:this.cssStr('popup-closeBtn'),
							'events':{
								'click':this.onCloseBtn_click.bind(this)
							}
						}),
						this.popupLoaderEl = new Element('div',{
							'id':this.cssStr('popup-loader'),
							'html':'Loading',
							'styles':{'opacity':0}
						})
					),
					this.popupContentEl = new Element('div',{
						'id':this.cssStr('popup-content'),
						'text':'dummy content'
					})
				),
				new Element('td',{
					'id':this.cssStr('popup-cornerR')
				})
			),
			new Element('tr').adopt(
				new Element('td',{
					'id':this.cssStr('popup-cornerBL')
				}),
				new Element('td',{
					'id':this.cssStr('popup-cornerB')
				}),
				new Element('td',{
					'id':this.cssStr('popup-cornerBR')
				})
			)
		));
		this.popupEl.inject(document.body);

		//create a loading div
		this.loaderEl = new Element('div',{
			'id':this.cssStr('loaderTop'),
			'html':'Loading',
			'styles':{'opacity':0}
		}).inject(document.body);
	},

	showOverlay: function(){
		this.overlayEl.fade(this.options.overlayOpacity);
	},
	hideOverlay: function(){
		this.overlayEl.fade(0);
	},
	refreshPopup: function(){
		this.log('refreshing popup...');
		this.popupTitleEl.set('text',this.popup.title);
		this.popupContentEl.set('html',this.popup.content);
		this.popupEl.setStyle('width', this.popup.width);
		this.popupEl.setStyle('margin-left', (this.popup.width/2)*-1);
		this.popupLoaderEl.setStyle('opacity',this.popup.loading?1:0);

		if(!this.popup.mayClose){
			this.popupCloseEl.setStyle('opacity',.2).addClass('disabled');
		}else{
			this.popupCloseEl.setStyle('opacity',1).removeClass('disabled');
		}
	},
	showPopup: function(noRefresh,onCloseCallBack){
		if(!noRefresh) this.refreshPopup();
		this.showOverlay();
		this.popupEl.fade(1);
		this.onCloseCallBack = onCloseCallBack;
	},
	hidePopup: function(keepOverlay){
		
		if(this.onCloseCallBack){
			this.onCloseCallBack();
			this.onCloseCallBack = null;
		}
		
		if(!this.prevPopup){
			new Fx.Tween(this.popupEl,{
				property:'opacity',
				duration:300,
				onComplete:function(){ui.popupEl.setStyle('width', ui.popup.defaultWidth);}
			}).start(0);
			
			new Fx.Tween(this.popupEl,{
				property:'top',
				duration:300,
				onComplete:function(){ui.popupEl.setStyle('top', ui.popup.defaultTop)}
			}).start(this.popup.defaultTop+100);
	
			if(!keepOverlay) this.hideOverlay();
			//
			this.popup.width = this.popup.defaultWidth;
		}
		
		if(this.prevPopup) {
			openEfPopup(this.prevPopup[0],this.prevPopup[1],this.prevPopup[2],this.prevPopup[3],this.prevPopup[4],this.prevPopup[5]);
		}
	},
	showLoader: function(){
		this.loaderEl.fade(1);
	},
	hideLoader: function(){
		this.loaderEl.fade(0);
	},
	scrollToTop: function(){
		var scroller = new Fx.Scroll(window);
		scroller.toTop();
	},
	scrollTo: function(x,y){
		var scroller = new Fx.Scroll(window);
		scroller.start(x,y);
	},
	scrollToElement: function(el){
		var scroller = new Fx.Scroll(window);
		scroller.toElement(el);
	},
	onCloseBtn_click:function(e){
		e.stop();
		if(this.popup.mayClose){
			this.hidePopup();
		}
	},
	popup_ajaxize: function(callBackFunc,typeNum){
		//console.log(this);
		if(this==window) return;
		this.popup_hideLoad();
		var typeNumValue = $chk(typeNum)?typeNum:40;
		var isAjaxValue = typeNumValue==0 ? 1 : 0;
		var self = this;
		this.popupContentEl.getElements('a').each(function(aTag){
			if(aTag.getProperty("target")=="_top" || aTag.getProperty("target")=="_blank") return;
			aTag.addEvent('click',function(e){
				e.stop();
				new Request.HTML({
					url:aTag.getProperty('href'),
					method:'get',
					update:self.popupContentEl,
					evalScripts: true,
					data:{
						type:typeNumValue,
						isAjax:isAjaxValue,
						'L':window.easyFairs.lid
					},
					onComplete:function(r){
						//self.popupContentEl.set('html',r);
						self.popup_ajaxize(callBackFunc,typeNumValue);
						self.popup_hideLoad();
						if(callBackFunc) callBackFunc();
					}
				}).send();
				self.popup_showLoad();
			});
		});
		this.popupContentEl.getElements('form').each(function(formTag){
			formTag.addEvent('submit',function(e){
				e.stop();
				if(this.getProperty("do_not_post")=="true") return;
				this.adopt(new Element('input',{'type':'hidden','name':'type','value':typeNumValue}));
				this.adopt(new Element('input',{'type':'hidden','name':'isAjax','value':isAjaxValue}));
				new Request.HTML({
					url:formTag.getProperty('action'),
					method:formTag.getProperty('method'),
					update:self.popupContentEl,
					evalScripts: true,
					onComplete:function(r){
						//self.popupContentEl.set('html',r);
						self.popup_ajaxize(callBackFunc,typeNumValue);
						self.popup_hideLoad();
						if(callBackFunc) callBackFunc();
					},
					data:this
				}).send({L:window.easyFairs.lid});
				/*
				this.set('send',{
					url:formTag.getProperty('action'),
					method:formTag.getProperty('method'),
					onComplete:function(r){
						self.popupContentEl.set('html',r);
						self.popup_ajaxize(callBackFunc);
						self.popup_hideLoad();
						if(callBackFunc) callBackFunc();
					}
				}).send();
				*/
				self.popup_showLoad();
			});
		});
	},
	popup_showLoad: function(){
		this.popup.loading = true;
		this.popupLoaderEl.fade(1);
	},
	popup_hideLoad: function(){
		this.popup.loading = false;
		this.popupLoaderEl.fade(0);
	},
	popup_checkHeight: function(){
		contentSize = this.popupContentEl.getSize();
		if(contentSize.y > 350){
			this.popupContentEl.setStyles({
				height: 350,
				overflow: 'auto'
			});

		}
	},
	cssStr: function(str){
		return this.options.classNamePrefix+str;
	},
	log: function(whatever){
		console.log('efUI:',whatever);
	},
	setPrevPopup: function(a){
		this.prevPopup = a;
	}
});
