var changeProfileTime = 1000;

var KissOverlay = function (id) {	
	this.overlay = new Overlay(id, {	width: '300px',
										draggable: true,
										visible: false,
										zindex: 1000,
										fixedcenter: true,
										constraintoviewport: true
	});

	this.overlay.scriptUrl = 'http://' + conf.baseUrl + '/gui_manager/kiss/';
	
	this.sendKiss = function (t_pid, t_nick, aaa_fn) {		
		
		this.overlay.color = 'blu';
		// Chiamata Ajax
		this.overlay.scriptUrl = 'http://' + conf.baseUrl + '/gui_manager/kiss/';
		var actionString = 't_pid=' + t_pid + '&t_nick=' + t_nick;
		
		this.overlay.setContentByAjax(actionString, null, null, this);
	};
	
	this.show = function (anchorShow, text, anchorHide) {
		this.overlay.show(null, this.overlay.generateOverlay(text), null);
	};
	
	this.sendKiss1 = function (profile_id) {
		
		var actionString = 't_pid=' + profile_id + '&from=romance';
		var conn = YAHOO.util.Connect.asyncRequest("POST", 'http://' + conf.dynamicUrl + '/gui_manager/kiss/' + profile_id, this.sendKiss2, actionString);
	};
	
	this.sendKiss2 = {
		
		success: function(o){	
			var data = YAHOO.lang.JSON.parse(o.responseText);
			
			// kiss OK
			if(String(data['result']) == "OK"){
				
				//document.getElementById('currentThumb').innerHTML = '<img src="http://" + conf.staticUrl + "/gfx/bacio.gif"/>';
				document.getElementById('currentLink').innerHTML += '<span id="currentAction" style="width : 32px; height : 32px; left : 155px; top : 10px; background : transparent url(http://' + conf.staticUrl + '/gfx/32x32/bacia.png) no-repeat scroll right center"></span>';
				//obj.overlay.hide();
				save = 'yes';
			}
			// kiss KO
			else{
				
				//obj.overlay.color = 'rosso';
				obj.show(null, '<h3>Attenzione</h3><br>' + data['message'], null);
				//obj.overlay.color = 'blu';
				save = 'no';
			}
			
			var t=setTimeout(function(){OverlayConfiguration.kiss.changeProfile1(document.getElementById('currentIndex').value, document.getElementById('currentPage').value, 'kiss', save);}, changeProfileTime);
		},
		failure: function(o){
			
		}
	};
	
	this.changeProfile1 = function(index, page, type, save){
		
		// Chiamata Ajax
		//this.overlay.scriptUrl = 'http://' + location.hostname + '/gui_manager/changeKissProfile/' + index + '/' + page + '/' + type + '/' + save;
		//this.overlay.getResultByAjax(null, this, this.changeProfile2);
		var conn = YAHOO.util.Connect.asyncRequest("POST", 'http://' + location.hostname + '/gui_manager/changeKissProfile/' + index + '/' + page + '/' + type + '/' + save, this.changeProfile2);
	};
	
	
	this.changeProfile2 = {
		
		success : function(o){
				var data = YAHOO.lang.JSON.parse(o.responseText);
				
				//obj.overlay.hide();
				//***************************************************
				// setting profilo corrente
				//***************************************************
				
				// elimina azione overlay precedente
				if(String(data['params']['save']) == "yes"){
					var aux = document.getElementById('currentLink');
					aux.removeChild(aux.lastChild);
				}
				
				// thumb utente corrente
				document.getElementById('currentLink').href = 'http://' + data['params']['currentProfile']['nick'] + '.' + conf.domain;
				document.getElementById('currentThumb').src = data['params']['currentProfile']['crypted_profile_content'][593];
				
				// status utente corrente
				html = '<div id="currentStatus" class="status">' +
						'<p>' +
							'<span class="nickname"><a href="http://' + data['params']['currentProfile']['nick'] + '.' + conf.domain + '">' + data['params']['currentProfile']['nick'] + '</a></span>';
				/*if(data['params']['currentProfile']['last_status'] != null){
					html += ' ' + data['params']['currentProfile']['last_status']['text'] +
					'<span class="data">' + Utilities.prettyDate(data['params']['currentProfile']['last_status']['creation_date']) + '</span>';		
				}
				else{*/
					html += ' ' + data['params']['currentProfile']['description'];
				//}
				html += '</p></div>';
				document.getElementById('currentStatus').innerHTML = html;
				
				// data nascita utente corrente
				document.getElementById('currentBirthdate').innerHTML = data['params']['currentProfile']['birthdate'];
				
				// località utente corrente
				document.getElementById('currentLocation').innerHTML = data['params']['currentProfile']['location'];
					
				// setting kiss utente corrente
				document.getElementById('currentKiss').href = "javascript:OverlayConfiguration.kiss.sendKiss1('" + data['params']['currentProfile']['profile_id'] + "')";
				
				// setting form messaggio utente corrente
				document.getElementById('currentProfileId').value = data['params']['currentProfile']['profile_id'];
				document.getElementById('currentProfileNick').value = data['params']['currentProfile']['nick'];
				
				// aggiornamento indici dei profili
				document.getElementById('currentPage').value = data['params']['currentPage'];
				document.getElementById('currentIndex').value = data['params']['nextIndex'];
				
				// setting prossimo profilo
				//document.getElementById('nextThumb').href = 'http://' + data['params']['nextProfile']['nick'] + '.' + conf.domain;
				document.getElementById('nextThumb').href = "javascript:OverlayConfiguration.kiss.changeProfile1(document.getElementById('currentIndex').value, document.getElementById('currentPage').value, '', 'no');"
				document.getElementById('nextThumb').innerHTML = '<img widht="56" height="56" src="' + data['params']['nextProfile']['crypted_profile_content'][497] + '">';
				
				// pulizia textarea messaggio
				document.getElementById('messageArea').value = 'ti piace? mandagli un messaggio!';
				
				var animation = new YAHOO.util.Anim('profileBox' , { opacity: {from: 0, to: 1 } }, 1);
				animation.animate();
				
				// aggiornamento storico
				if(String(data['params']['save']) == "yes"){
					var el = document.getElementById('romance_list');
					
					if(document.getElementById('romance_people_7')){
						var elToRemove = document.getElementById('romance_people_7');
						new YAHOO.util.Element(el).removeChild(elToRemove);
					}
					
					for(var i = 6; i > 0; i--){
						if(document.getElementById('romance_people_' + i)){
							document.getElementById('romance_people_' + i).id = 'romance_people_' + (i + 1);
						}
					}
					
					elToInsert = document.createElement("div");
					elToInsert.setAttribute('id', 'romance_people_1');
					elToInsert.setAttribute('style', 'filter: alpha(opacity=0); opacity: 0.0; -moz-opacity: 0.0;');
					YAHOO.util.Dom.addClass(elToInsert, 'box');
					
					
					if(data['params']['previousProfile']['gender'] == 'F'){
						genderClass = 'female';
					}
					else if(data['params']['previousProfile']['gender'] == 'M'){
						genderClass = 'male';
					}
					html = '<div class="thumb">' +
							'<a class="' + genderClass + '" id="nextThumb" href="http://' +  data['params']['previousProfile']['nick'] + '.' + conf.domain + '" title="' + data['params']['previousProfile']['nick'] + '"><span></span><img width="56" height="56" src="' + data['params']['previousProfile']['crypted_profile_content'][497] + '" alt="' + data['params']['previousProfile']['nick'] + '"/>';
					if(data['params']['previousProfile']['online']){	
						html += '<span class="presence on"></span>';
					}
					else{
						html += '<span class="presence off"></span>';
					}
					
					if(data['params']['type'] == 'kiss'){
						html += '<span style="width : 16px; height : 16px; left : 38px; background : transparent url(http://' + conf.staticUrl + '/gfx/16x16/bacia.png) no-repeat scroll right center"></span>';
					}
					else if(data['params']['type'] == 'message'){
						html += '<span style="width : 16px; height : 16px; left : 38px; background : transparent url(http://' + conf.staticUrl + '/gfx/16x16/scrivi_msg.png) no-repeat scroll right center"></span>';
					}
					
					html += '</a></div><div class="separator"></div>';
					elToInsert.innerHTML = html;
					new YAHOO.util.Element(el).insertBefore(elToInsert, document.getElementById('romance_people_2'));
					
					var animation = new YAHOO.util.Anim('romance_people_1' , { opacity: {from: 0, to: 1 } }, 1);
					animation.animate();
				}
		},
		failure : function(o){	
		}
	};
	
	this.sendMessage1 = function(form){
		this.overlay.color = 'blu';
		var callback = {
		    success : function (o) {
			},
		    failure : function (o) { 
		    	alert(o);
		    },
		    upload: function (o) {
		    	
		    	if(Utilities.needLP(o.responseText)) {
		    		Utilities.checkFeature(o.responseText);
		    		return;
		    	}
		    		
			    	var data = YAHOO.lang.JSON.parse(o.responseText);
			    	
			    	if(data['result'] == 'OK'){
				    	OverlayConfiguration.genericMessage.overlay.hide();
				    	document.getElementById('currentLink').innerHTML += '<span id="currentAction" style="width : 32px; height : 32px; left : 155px; top : 10px; background : transparent url(http://' + conf.staticUrl + '/gfx/32x32/scrivi_msg.png) no-repeat scroll right center"></span>';
				    	save = 'yes';
			    	}
			    	// kiss KO
					else{
						
						//obj.overlay.color = 'rosso';
						obj.show(null, '<h3>Attenzione</h3><br>' + data['message'], null);
						//obj.overlay.color = 'blu';
						save = 'no';
					}
			    	
			    	// cambio utente
			    	var t=setTimeout(function(){OverlayConfiguration.kiss.changeProfile1(document.getElementById('currentIndex').value, document.getElementById('currentPage').value, 'message', save);}, changeProfileTime);
		    	
		    }
		};
		
		YAHOO.util.Connect.setForm(form, true);
		var conn = YAHOO.util.Connect.asyncRequest("POST", 'http://' + conf.baseUrl + '/gui_manager/send_sms', callback);
		//OverlayConfiguration.genericMessage.show('loading...', '<div class="loader"></div>', 'blu');
	};
	
	this.setTipMessage = function(){
		
		var messages = YAHOO.lang.JSON.parse(document.getElementById('tipMessages').innerHTML);
		var index = Math.floor(Math.random()* messages.length);
		document.getElementById('messageArea').value = messages[index];
	}
};