var UserActionsOverlay = function (id) {
	this.overlay = new Overlay(id, {	width: '300px',
										draggable: true,
										visible: false,
										zindex: 1000,
										fixedcenter: true,
										constraintoviewport: true
	});

	// Chiamata Ajax
	this.overlay.scriptUrl = "http://" +  location.hostname + "/gui_manager/foto_video";
	
	// Metodo di connessione
	this.overlay.connectionMethod = 'POST';
	
	// Identifica un overlay a pił passi 
	this.overlay.stepOverlay = true;
	
	// Se true nasconde il terzo passo della finestra, default false
	// this.hideAfterLoad = false;
		
	this.show = function (anchorShow, text, anchorHide) {
		this.overlay.show(null, this.overlay.generateOverlay(text), null);
	};
	
	//***********************************
	// aggiungi amico
	//***********************************
	this.addFriend1 = function (profile_id, nick, element_id, aaa_fn) {
		this.overlay.scriptUrl = "http://" +  location.hostname + "/overlay_manager/aggiungi_amico";
		var  actionString = 't_nick=' + nick + '&userOverlay=no&t_profile_id=' + profile_id + '&element_id=' + element_id + '&aaa_fn=' + aaa_fn;	
		this.overlay.setContentByAjax(actionString, null, null, this);
	};
	
	this.addFriend2 = function (profile_id, element_id, nick) {
		this.overlay.scriptUrl = "http://" +  location.hostname + "/gui_manager/aggiungi_amico";
		var actionString = 't_profile_id=' + profile_id + '&element_id=' + element_id + '&t_nick=' + nick;
		this.overlay.getResultByAjax(actionString, this, this.addFriend3);
	};
	
	this.addFriend3 =  function (obj, data) {
		
		if(data['result'] == 'OK'){
			
			var html = '<a id="addFriendAction" href="javascript:OverlayConfiguration.useractions.removeFriend1(\'' + data['params'].profile_id + '\', \'' + data['params'].nick + '\', \'addFriendAction\');" class="elimina" title="rimuovi">' +
						'<span>rimuovi dalla buddy list</span></a>';
			
			document.getElementById(data['params'].element_id).className = 'elimina';
			document.getElementById(data['params'].element_id).title = 'rimuovi';
			document.getElementById(data['params'].element_id).href = "javascript:OverlayConfiguration.useractions.removeFriend1('" + data['params'].t_profile_id + "', '"  + data['params'].t_nick + "', '" + data['params'].element_id + "', 'buddy.add');";
			
		}
		
		// messaggio feedback
		var html = '<h3>' + data['header'] + '</h3>' +
					'<p>' + data['body'] + '</p>';
		obj.show(null, html, null);	
	}
	
	//***********************************
	// rimuovi amico
	//***********************************
	this.removeFriend1 = function (profile_id, nick, element_id, aaa_fn) {		
		this.overlay.scriptUrl = "http://" +  location.hostname + "/overlay_manager/rimuovi_amico";
		var  actionString = 't_nick=' + nick + '&userOverlay=no&profile_id=' + profile_id + '&element_id=' + element_id + '&aaa_fn=' + aaa_fn;
		this.overlay.setContentByAjax(actionString, null, null, this);
	};
	
	this.removeFriend2 = function (profile_id, element_id, nick) {
		this.overlay.scriptUrl = "http://" +  location.hostname + "/gui_manager/rimuovi_amico";
		var actionString = 't_profile_id=' + profile_id + '&element_id=' + element_id + '&t_nick=' + nick;
		this.overlay.getResultByAjax(actionString, this, this.removeFriend3);
	};
	
	this.removeFriend3 =  function (obj, data) {
		
		if(data['result'] == 'OK'){	
			//se sto visualizzando il formato maxi ricarico la pagina
			var maxi = YAHOO.util.Dom.getElementsByClassName('container maxi');
			var profileCounter = document.getElementById('profileCounter');
			if(maxi != ''){
			if(maxi[0].id == 'friends'){
				
				// se ci sono pił profili faccio reload ajax, altrimenti ricarico l'intera pagina
				if(Number(profileCounter) > 1){ 
					reload_ajax('http://' +conf.baseUrl +'/my_friends/all/1?method=friends','friends');
				}
				else{
					location.href = 'http://' + conf.baseUrl + '/my_friends/all/1';
				}
			}
			}
			document.getElementById(data['params'].element_id).className = 'aggiungi';
			document.getElementById(data['params'].element_id).title = 'aggiungi';
			document.getElementById(data['params'].element_id).href = "javascript:OverlayConfiguration.useractions.addFriend1('" + data['params'].profile_id + "', '"  + data['params'].t_nick + "', '" + data['params'].element_id + "', 'buddy.add');";
		}
		
		// messaggio feedback
		var html = '<h3>' + data['header'] + '</h3>' +
					'<p>' + data['body'] + '</p>';
		obj.show(null, html, null);	
	}
	
	//***********************************
	// aggiungi blacklist
	//***********************************
	this.addBlack1 = function (profile_id, nick, element_id, aaa_fn) {
		this.overlay.scriptUrl = "http://" +  location.hostname + "/overlay_manager/aggiungi_blacklist";
		var actionString = 't_nick=' + nick + '&userOverlay=no&t_profile_id=' + profile_id + '&element_id=' + element_id + '&aaa_fn=' + aaa_fn;	
		this.overlay.setContentByAjax(actionString, null, null, this);
	};
	
	this.addBlack2 = function (profile_id, element_id, nick) {
		this.overlay.scriptUrl = "http://" +  location.hostname + "/gui_manager/aggiungi_blacklist";
		var actionString = 't_profile_id=' + profile_id + '&element_id=' + element_id + '&t_nick=' + nick;
		this.overlay.getResultByAjax(actionString, this, this.addBlack3);
	};
	
	this.addBlack3 =  function (obj, data) {
		
		if(data['result'] == 'OK'){
			
			document.getElementById(data['params'].element_id).className = 'unlock';
			document.getElementById(data['params'].element_id).title = 'rimuovi';
			document.getElementById(data['params'].element_id).href = "javascript:OverlayConfiguration.useractions.removeBlack1('" + data['params'].t_profile_id + "', '"  + data['params'].t_nick + "', '" + data['params'].element_id + "', 'buddy.add');";
			
		}
		
		// messaggio feedback
		var html = '<h3>' + data['header'] + '</h3>' +
					'<p>' + data['body'] + '</p>';
		obj.show(null, html, null);	
	}
	
	//***********************************
	// rimuovi blacklist
	//***********************************
	this.removeBlack1 = function (profile_id, nick, element_id, aaa_fn) {
		this.overlay.scriptUrl = "http://" +  location.hostname + "/overlay_manager/rimuovi_blacklist";
		var  actionString = 't_nick=' + nick + '&userOverlay=no&profile_id=' + profile_id + '&element_id=' + element_id + '&aaa_fn=' + aaa_fn;
		this.overlay.setContentByAjax(actionString, null, null, this);
	};
	
	this.removeBlack2 = function (profile_id, element_id, nick) {
		this.overlay.scriptUrl = "http://" +  location.hostname + "/gui_manager/rimuovi_blacklist";
		var actionString = 't_profile_id=' + profile_id + '&element_id=' + element_id + '&t_nick=' + nick;
		this.overlay.getResultByAjax(actionString, this, this.removeBlack3);
	};
	
	this.removeBlack3 =  function (obj, data) {
		
		if(data['result'] == 'OK'){
			
			//se sto visualizzando il formato maxi ricarico la pagina
			var maxi = YAHOO.util.Dom.getElementsByClassName('container maxi');
			var profileCounter = document.getElementById('profileCounter');
			//id.friends
			if(maxi != ''){
			if(maxi[0].id == 'blacklist'){
				
				// se ci sono pił profili faccio reload ajax, altrimenti ricarico l'intera pagina
				if(Number(profileCounter) > 1){ 
					reload_ajax('http://' +conf.baseUrl +'/my_blacklist/all/1?method=blacklist','blacklist');
				}
				else{
					location.href = 'http://' + conf.baseUrl + '/my_blacklist/all/1';
				}
			}
			}
			
			document.getElementById(data['params'].element_id).className = 'lock';
			document.getElementById(data['params'].element_id).title = 'aggiungi';
			document.getElementById(data['params'].element_id).href = "javascript:OverlayConfiguration.useractions.addBlack1('" + data['params'].profile_id + "', '"  + data['params'].t_nick + "', '" + data['params'].element_id + "', 'buddy.add');";
		}
		
		// messaggio feedback
		var html = '<h3>' + data['header'] + '</h3>' +
					'<p>' + data['body'] + '</p>';
		obj.show(null, html, null);	
	}	
};

OverlayConfiguration.useractions = new UserActionsOverlay('textOverlay');
OverlayManager.useractions = function (args) {
	OverlayConfiguration.useractions.stepOne('useractions', args[0], args[1]);
};