var questions = new Array();
var currentQuestionIndex = 0;
var scriptURL = "http://" + conf.baseUrl + "/status_input/";

function getQuestions(myid) {
	var callback = {
	    success : function (o) {
			try {
	        	var rawQuestions = YAHOO.lang.JSON.parse(o.responseText);
	        	for(var index in rawQuestions)
	        		setQuestions(new Question(new WebProfile(rawQuestions[index].question.user.profile_id, rawQuestions[index].question.user.nick, rawQuestions[index].question.user.gender, rawQuestions[index].question.user.ageLocation, rawQuestions[index].question.user.avatar), rawQuestions[index].question.text, rawQuestions[index].question.id));	        
			}
	        catch(e) { 
	        	// alert(e);
	        }
	    },
	    failure : function (o) {
	    	// console.log('Caricamento domande fallito');
	    }
	};
	
	var actionString = scriptURL + 'get_questions';
	YAHOO.util.Connect.asyncRequest("POST", actionString, callback);
}

function setQuestions(question) {
	questions[questions.length] = question;
}

function setStatusInput(type) {
	
	// siamo già nella sezione answer e viene ripremuto il tasto answer
	var nextQ = false;
	if(type == 'answer' && document.getElementById("status_type").value == 'answer'){
		nextQ = true;
	}
	
	document.getElementById("status_type").value = type;
	// new YAHOO.util.Element("status_type").value = type;
	
	YAHOO.util.Dom.setAttribute(YAHOO.util.Dom.get("status_input"), "class", type + "_input");
	
	var label_text = YAHOO.util.Dom.get("label_text");
	var thumb = YAHOO.util.Dom.get("posterUserThumb");
	var nickname = YAHOO.util.Dom.get("posterUserNickname");
	var label = YAHOO.util.Dom.get("posterUserLabel");
	
	var set = {
		standard: function() {
			label.innerHTML = 'A cosa stai pensando in questo momento?';
		},
		shout: function() {
			label.innerHTML = 'Fatti notare dagli amici di The Club!';
		},
		media: function() {
			label.innerHTML = 'Scegli una foto da condividere';
		},
		answer: function () {
			
			if(nextQ == true){
				nextQuestion();
			}
			else{
				formatQuestion(questions[0]);
				currentQuestionIndex = 0;
			}
		}
	};
	
	set[type]();
	
	document.getElementById('text').value = '...dillo qui!';
}

function nextQuestion() {
	if(currentQuestionIndex == questions.length - 1) currentQuestionIndex = 0;
	else currentQuestionIndex++;
	formatQuestion(questions[currentQuestionIndex]);
}

function searchQuestion(questionId) {
	for(var index in questions)
		if(questions[index].questionId == questionId)
			return index;
	
	return false;
}

function formatQuestion(question) {
	
	var label_text = YAHOO.util.Dom.get("label_text");
	var thumb = YAHOO.util.Dom.get("posterUserThumb");
	//var thumb = document.getElementById("posterUserThumb");
	var nickname = YAHOO.util.Dom.get("posterUserNickname");
	var label = YAHOO.util.Dom.get("posterUserLabel");
	var questionId = YAHOO.util.Dom.get("questionId");
	
	// thumb.innerHTML = '<a class="' + (question.user.gender.toUpperCase() == 'M' ? 'male': 'female') + '" href=""><img src="' + Utilities.getUserAvatar(question.user.avatar, question.user.gender, 22) + '"/></a>';
	//var t = document.createElement('div');
	//t.innerHTML = RtfUtilities.createAvatar(question.user, 0);
	
	//thumb.appendChild(t);
	//thumb.innerHTML = RtfUtilities.createAvatar(question.user, 0);

	// nickname.innerHTML = '<a class="" href="">' + question.user.nick + '</a> <span>ti chiede:</span>';
	nickname.innerHTML = ''; //RtfUtilities.createNick(question.user.nick) + ' <span>ti chiede:</span>';
	label.innerHTML = question.text;
	questionId.value = question.id;
	thumb.innerHTML = RtfUtilities.createAvatar(question.user, 0);
	

}

function sendStatus(form) {
	var callback = {
	    success : function (o) {
			
			if(Utilities.needLP(o.responseText)) {
				Utilities.checkFeature(o.responseText);
				OverlayConfiguration.genericMessage.overlay.hide();
			}
			
			else {
				var notify = YAHOO.lang.JSON.parse(o.responseText); var notify = YAHOO.lang.JSON.parse(o.responseText); 
				
					if(notify.error != undefined){
						OverlayManager.show('genericMessage', 'Attenzione', notify.error, 'rosso');
					}
					else{
						OverlayConfiguration.genericMessage.overlay.hide();
						notify.creationDate = new Date();
						NotificationManager.loadNotifications(notify, rtf);
					}
					
					// set nuovo stato se siamo in pagina personale
					if(document.getElementById('currentStatus') && notify.subtype == 'standard' && notify.error == undefined){
						/*testo = notify.text;
						if(testo.length > 22){
							testo = testo.substring(0,25);
							testo = testo.concat("...");
						}*/					
						html = '<p>' +
								notify.text +
								'<span class="data">adesso</span>' + 
								'<span>' +
									'<a href="javascript:OverlayManager.show(\'status\', \'' + notify.id + '\');">cancella</a>' +
								'</span>' + 
								'</p>';
						document.getElementById('currentStatus').innerHTML = html;
					}
					
					// set valore default textarea
					document.getElementById('text').value = '...dillo qui!';
			}
		},
		failure : function (o) { 
			    	alert(o);
			    	},
	    upload: function (o) {
		
			if(Utilities.needLP(o.responseText)) {
				Utilities.checkFeature(o.responseText);
				OverlayConfiguration.genericMessage.overlay.hide();
			}
				
			else {
		    	var notify = YAHOO.lang.JSON.parse(o.responseText);
		    		
	    		if(notify.error != undefined){
	    			OverlayManager.show('genericMessage', 'Attenzione', notify.error, 'rosso');
	    		}
	    		else{
	    			OverlayConfiguration.genericMessage.overlay.hide();
	    			notify.creationDate = new Date();
	    			NotificationManager.loadNotifications(notify, rtf);
	    		}
	    		
	    		// set nuovo stato se siamo in pagina personale
				if(document.getElementById('currentStatus')  && notify.error == undefined){
					/*testo = notify.text; 
					if(testo.length > 22){
						testo = testo.substring(0,25);
						testo = testo.concat("...");
					}*/
					html = '<p>' +
							notify.text +
							'<span class="data">adesso</span>' + 
							'<span>' +
								'<a href="javascript:OverlayManager.show(\'status\', \'' + notify.id + '\');">cancella</a>' +
							'</span>' + 
							'</p>';
					document.getElementById('currentStatus').innerHTML = html;
				}
	    		
	    		document.getElementById('text').value = '...dillo qui!';
			}
		}
	};
	
	YAHOO.util.Connect.setForm(form, true);
	var conn = YAHOO.util.Connect.asyncRequest("POST", scriptURL + 'add', callback);
	OverlayConfiguration.genericMessage.show('sto inserendo il tuo status...', '<div class="loader"></div>', 'blu');
	
}

YAHOO.util.Event.onDOMReady(function () {
	getQuestions(1);
});
	
function getStatusHistory(page){
	
	var callback = {
	    success : function (o) {
			try {
				
				// eliminazione vecchio link di reload
				var parentElement = YAHOO.util.Dom.get('posts');
				var reloadElement = YAHOO.util.Dom.get('old_posts_loader');
				new YAHOO.util.Element(parentElement).removeChild(reloadElement);
				
				
				// inserimento vecchi status
				document.getElementById('posts').innerHTML += o.responseText;
				
				for(var k = 1; k <= page; k++){
					
					// profili status
					var el = YAHOO.util.Dom.get('jsProfiles_status_' + k);
					if(el){
						profiles = YAHOO.lang.JSON.parse(el.innerHTML);
						if(profiles){
							for(var i = 0; i < profiles.length; i++){
								YAHOO.neo.overlay.generateOverlay('status_' + k + '_' + Number(i + 1), profiles[i].features, {profile_id : profiles[i].profile_id, nick : profiles[i].nick}, true);
							}
						}
					}
					
					// profili question
					var el = YAHOO.util.Dom.get('jsProfiles_question_' + k);
					if(el){
						questionProfiles = YAHOO.lang.JSON.parse(el.innerHTML);
						if(questionProfiles){
							for(var i = 0; i < questionProfiles.length; i++){
								YAHOO.neo.overlay.generateOverlay('question_' + k + '_' + Number(i + 1), questionProfiles[i].features, {profile_id : questionProfiles[i].profile_id, nick : questionProfiles[i].nick}, true);
							}
						}
					}
					
					// profili comment
					var el = YAHOO.util.Dom.get('jsProfiles_comment_' + k);
					if(el){
						commentProfiles = YAHOO.lang.JSON.parse(el.innerHTML);
						if(commentProfiles){
							for(var i = 0; i < commentProfiles.length; i++){
								YAHOO.neo.overlay.generateOverlay('comment_' + k + '_' + Number(i + 1), commentProfiles[i].features, {profile_id : commentProfiles[i].profile_id, nick : commentProfiles[i].nick}, true);
							}
						}
					}
				}
			}
	        catch(e) { }
	    },
	    
	    failure : function (o) { 	
	    },
	    
	    upload: function (o) {
	    }
	};
	var nextPage = Number(page) + 1;	
	var conn = YAHOO.util.Connect.asyncRequest("POST", scriptURL + 'get_status_history/' + page, callback);
}