var InfoToolTip = new Class({

	initialize: function(options) {
		this.panelElemId = options.panelElemId;
		this.delayInterval = options.delay;
		this.loadingTitle = options.loadingTitle;
		this.url = options.url;
		this.saveMovieFunction = options.saveMovieFunction;
		this.sendEmailFunction = options.sendEmailFunction;
		this.currentId = 0;
		this.movieLocation = 1;
		this.typeId = 0;
		this.userId = 0;
		this.reviewId = 0;
		this.currentRecommendationId = 0;
		this.hideTimer = null;
		this.panelElem = null;
		this.offsetX = options.offsetX;
		this.activeMode = true;
		this.messageBoxId = 'tooltipMsg';
		this.linkElem = null;
		this.recomTmp = 0;
		this.newAddRecom = false;
		this.emAr = new Array(12);
		this.emLen = 0;
		this.authErrorMsg = 'auth_error';
		this.loginUrl = 'login.htm';
		
		
		for(var i = 0; i < 12; i++)
			this.emAr[i] = 0;
		
		this.mainDivName = 'actionbox';
		this.formId  = options.formId;
		this.formSubmitBtnId = options.formSubmitBtnId;
		
		window.addEvent('domready', function() {
			this.panelElem = $(this.panelElemId);
		}.bind(this));

		window.addEvent('keydown', function(event) {  
			var event = new Event(event);
		    if (event.key == 'esc') {
		        this.close();      
		    }			
		}.bind(this));				
	},
	
	setUrl: function(url) {
		this.url = url; 
	},
	
	show: function(linkElemId, id, recomId, movieLocation, typeId, userId, reviewId, url) {
		this.hide();
		
		if (!this.activeMode) return;
		
		if (url)
			this.setUrl(url);

		this.linkElem = $(linkElemId);
		this.currentId = id;	
		this.movieLocation = movieLocation;	
		this.typeId = typeId;	
		this.userId = userId;	
		this.reviewId = reviewId;	
		this.currentRecommendationId = recomId;
				
		var panel = this.panelElem;
		panel.innerHTML = this.loadingTitle;
		var pos = this.linkElem.getPosition();
		panel.style.top = (pos.y-32) + 'px';
		panel.style.left = this.offsetX + 'px';
		panel.style.display = '';
		var url = this.prepareUrl();
		
		new Ajax(url, {
			method: 'get',
			onComplete: function(data) {
			
				this.panelElem.innerHTML = data;
				var popupLinks = glob.property['@mymovieteach.web.view.ViewDataResolver@POPUP_MENU_KEYS'];
				if(popupLinks){
					for(var i =0; i < popupLinks.length; i++){
						var link = $(popupLinks[i]);
						if(link){
							link.addEvent("click", this.close.bind(this), false);
						}
					}
				}

				var closeImg = $('tooltipCloseImg');
				if (closeImg)
					closeImg.addEvent('click', this.close.bind(this), false);
					
				if (emulateCheckbox)
					emulateCheckbox();		
				
				if($('captcha')){
					$('captcha').style.display = 'none';
				}
				
				if($(this.mainDivName)){
					$(this.mainDivName).setStyle('top', panel.style.top);
					$(this.mainDivName).setStyle('left', panel.style.left);
				}
				
				//--------
				this.prepareForm();
				//---------
				
			}.bind(this)
		}).request();
	},
	
	prepareUrl: function() {
		var url = this.url.replace('%id%', this.currentId);
		if (this.url.indexOf('recomId') > 0) {
			url = url.replace('%recomId%', this.currentRecommendationId);
		}
		if (this.url.indexOf('movieLoc') > 0) {
			url = url.replace('%movieLoc%', this.movieLocation);
		}
		if (this.url.indexOf('typeId') > 0) {
			url = url.replace('%typeId%', this.typeId);
		}
		if (this.url.indexOf('userId') > 0) {
			url = url.replace('%userId%', this.userId);
		}
		if (this.url.indexOf('reviewId') > 0) {
			url = url.replace('%reviewId%', this.reviewId);
		}
		return url;
	},

	sendEmail: function(movieId, recomendId, newAddRecom){
		if(recomendId && parseInt(recomendId) > 0 && newAddRecom){
			this.sendEmailFunction(movieId, recomendId, Class.empty);		
		}
	},
	
	close: function(){
		this.sendEmail(this.currentId, this.recomTmp, this.newAddRecom);
		this.hide();
	},
	
	hide: function() {
		this.panelElem.style.display = 'none';
		this.recomTmp = 0;
	},	
	
	onActionBoxOpen: function() {
		this.hide();
		this.activeMode = false;
	},
	
	onActionBoxClose: function() {
		this.activeMode = true;
	}, 

	onDragStart: function() {
		this.hide();
		this.activeMode = false;
	},
	
	onDragStop: function() {
		this.activeMode = true;
	},
	
	saveMovie: function(movieListTypeId, checked, recomId, newAddRecom) {
		this.recomTmp = recomId ? recomId : 0;
		this.newAddRecom = newAddRecom ? newAddRecom : false;
		this.saveMovieFunction(this.currentId, movieListTypeId, (recomId ? recomId : 0), checked, this.saveMovieFunctionCallBack.bind(this));
	},
	
	saveMovieFunctionCallBack: function(wrapper) {
		if (wrapper.movieId == this.currentId && wrapper.message.length > 0){
			$(this.messageBoxId).setHTML(glob.text['dynamic.movie.saved_or_removed']);
			var bcgChange = new Fx.Style(this.messageBoxId, 'background-color',{duration:1000});
			bcgChange.start('#fc6', '#fed');
		}
		var mlMenuItem = new MovieListMenuItem({typeId : wrapper.typeId});
		mlMenuItem.changeCount(wrapper.change);
	},
	
	prepareForm: function() {
		if($defined(this.formId) && this.formId.length > 0) {
			this.prepareFormEvents();
		}
	},
	
	prepareFormEvents: function() {
		$(this.formSubmitBtnId).addEvent('click', function() {
				$(this.formSubmitBtnId).disabled = true; 
				if (this.onSubmitEvent)
					this.onSubmitEvent();
				this.submitForm();
			}.bind(this));
	},
	
	setOnSubmitEvent: function(func){	
		this.onSubmitEvent = func;
	},
	
	submitForm: function() {
		var frm = $(this.formId);
		var postData =  frm.toQueryString();
		var ajax = new Ajax(frm.action, 
		{
			method: 'post', 
			data: postData,
			onComplete: function(data) {
				if (data.trim().length < 1) {
					$(this.mainDivName).empty();
					this.refreshPage();
				} else if (data.trim() == this.authErrorMsg) {
					location.href = this.loginUrl;
				} else {
					this.hide();	
					$(this.mainDivName).setHTML(data);
					/*var bcgChange = new Fx.Style($(this.mainDivName), 'background-color', {duration:1000});
					bcgChange.start('#fc6', '#fd6');*/
					
					$(this.formSubmitBtnId).disabled = false;

					this.prepareForm();
				}
			}.bind(this)
		}
		).request();
	},
	
	refreshPage: function() {
		var url = window.location.href;
		var ps = url.lastIndexOf('#');
		if (ps >= 0) 
			url = url.substring(0, ps);
		window.location.href = url;
	},
	

	showCaptcha: function(id){
//		alert(id)
		var em = $('emails['+id+'].value');
		var captcha = $('captcha');
		if(em){
			this.emLen -= this.emAr[id];
			this.emLen += em.value.trim().length;
			this.emAr[id] = em.value.trim().length;
			if(captcha){
				if(this.emLen > 0){
					captcha.style.display = '';
				}else{
					captcha.style.display = 'none';
				}
			}
			
		}
		
	}
	
	
});