/**
 * piccola classe per gestione segnalazioni al CAT
 */

Libero.CAT=Class.create();
Libero.CAT.prototype = {
	/**
	 * inizializzazione classe
	 * @param {String} url url action del form
	 */
	initialize: function(url,logged){
		this.logged=logged;
		this.objContainer=$('segnala-container');
		this.objClicked=null;
		this.open={status:false,id:null};
		this.okMessContainer = $('segnala-okmessage');
		this.radioItems=null;
		this.formContainer=null;
		if (logged == 1) {
			this.submitUrl = url;
			this.divErrorObj = $('cat-error');
			this.formObj = $('formsegnala');
			this.radioItems = this.formObj.marks_type;
			this.submitButton = $('submitFormCat');
			this.formContainer = $('segnala-form');
			this.preloadObj = $('cat-area-ajaxpreload');
		}
		this.timeout=null;
	},
	/**
	 * visualizza il form se 
	 * @param {object} objClicked oggetto cliccato
	 * @param {Int} postId id del post (naturalmente se si sta segnalando un post)
	 * @param {Int} level valore da passare al cat 0 utente, 1 blog,  2 post
	 */
	
	
	showForm: function(objClicked, blogId,postId,objUrl,blogTitle,postTitle,level){
		if(this.timeout){
			clearTimeout(this.timeout)
		}
		if(this.open.status){
			if(this.objClicked){
				if(objClicked.id==this.open.id){
					this.hideForm();
					return false;
				}else{
					this.hideForm();
				}
			}
		}
		this.objClicked=objClicked.parentNode;
		this.objContainer.style.position='absolute';
		this.objContainer.style.zIndex=1000;
		coords=getPageCoords (this.objClicked);
		this.objContainer.style.top=(coords.y + 20)+'px';
		this.objContainer.style.left=coords.x+'px';
		this.objContainer.style.width=Libero.Utils.prototype.getWidth(this.objClicked)+'px';
		this.objClicked.style.height=(Libero.Utils.prototype.getHeight(this.objContainer))+'px';
		this.objContainer.style.display='block';
		if (this.logged == 1) {
			this.setLevel(level);
			
			if (level == 0) {
				$('marked_obj_url').value = objUrl;
			}
			else 
				if (level == 1) {// segnalazione blog
					$('marked_obj_id').value = blogId;
					$('view_name').value = blogTitle;
					$('marked_obj_url').value = objUrl;
				}
				else 
					if (level == 2) {// segnalazione post
						$('marked_obj_id').value = postId;
						$('obj_id_lev_1').value = blogId;
						$('view_name').value = postTitle;
						$('marked_obj_url').value = objUrl;
						$('obj_text_lev_1').value = blogTitle;
					}
			$('catUseAjax').value = 1;
		}
		this.open.status=true;
		this.open.id=objClicked.id;
	},
	/**
	 * nasconde il form
	 */
	hideForm: function(){
		this.objContainer.style.position='';
		this.objContainer.style.display='none';
		if(this.objClicked){
			this.objClicked.style.height='auto';
		}
		this.objClicked=null;
		this.open.status=false;
		this.open.id=null;
		this._hideError();
		this.reset();

		
	},
	/**
	 * setta nel form appropriato il valore nel campo level  0 utente, 1 blog,  2 post
	 * @param {Int} level 
	 */
	setLevel: function(level){
		this.formObj.level.value=level;
	},
	/**
	 * Invia il modulo di segnalazione dopo aver effettuato il controllo
	 * che sia stato selezionato almento un radiobutton
	 */
	submitForm: function(form){
		vr=false;
		for(i=0;i<this.radioItems.length;i++){
			if(this.radioItems[i].checked){
				vr=true
				break;
			}
		}
		if(vr){ // invio il form in ajax
			this.submitButton.disabled=true;
			this.preloadObj.style.dispay='block';
			createPageVeil(0.3,'#FFFFFF');
			var _this=this;
	 		var hxr=new Libero.Request({
				url: form.action,
				method: 'post',
					secure : true, 
					requestType: 'json',
				data : {}
			});
			hxr.onSuccess = function(){
				removePageVeil();
				_this.preloadObj.style.dispay='none';
				if(json.status==true){
					_this.formContainer.style.display='none';
					_this.okMessContainer.style.display='';
					_this.okMessContainer.innerHTML=json.errormessage;
					_this.timeout=window.setTimeout(function(){
							_this.hideForm();
					}, 4000);
				}else{
					_this._showError(json.errormessage);
				}
			}
			hxr.onFailure = function(){
				removePageVeil();
				_this.preloadObj.style.dispay='none';
				this._showError('<h2>Servizio momentaneamente non raggiungibile!</h2> <p>Si è verificato un errore durante la verifica. Prego riprovare più tardi</p>');
				this.submitButton.disabled=false;
			}.bind(this);
			hxr.send(form);
		}else{
			this.preloadObj.style.dispay='none';
			this._showError('<p>Selezionare almeno un motivo per questa segnalazione.</p>');
		}
		this.submitButton.disabled=false;
		if($('catUseAjax').value==1){
			return false;
		}else{
			return true;
		}
		;
	},
	/**
	 * reset del radiobuttom precedentemente checcato
	 */
	reset:function(){
		if(this.radioItems){
			for(i=0;i<this.radioItems.length;i++){
				this.radioItems[i].checked=false;
			}
			$('marked_obj_id').value='';
			$('obj_id_lev_1').value='';
			$('view_name').value='';
			$('marked_obj_url').value='';
			$('obj_text_lev_1').value='';
			$('level').value='';
		}
	if(this.formContainer){
		this.formContainer.style.display='';
		this.okMessContainer.style.display='none';
	}

	},
	/**
	 * mostra gli errori all'utente
	 * @param {String} message messaggio
	 */
	_showError:function(message){
		this.divErrorObj.style.display='block';
		if (typeof message!='undefined') {
			this.divErrorObj.innerHTML=message;
		}
		h1=Libero.Utils.prototype.getHeight(this.objContainer);
		this.objClicked.style.height=((h1))+'px';
		
		var _this=this;
		_this.timeout=window.setTimeout(function(){
			_this._hideError();
		}, 5000);
	},	
	/**
	 * nasconde e "svuota il div di errore"
	 */
	_hideError: function(){
		if(this.divErrorObj){
			this.divErrorObj.style.display='none';
			this.divErrorObj.innerHTML='';
		}
		h1=Libero.Utils.prototype.getHeight(this.objContainer);
		if(this.objClicked){
			this.objClicked.style.height=(h1)+'px';
		}

	}
	
}