var ProfileReloader = {
	boxUsers: new Array(),
	flag: true,
	chosen: new Array(),
	minProfilesCount: 12,

	parseProfiles: function () {
		var p_div = YAHOO.util.Dom.getElementsByClassName('profilesData');
		
		for(i=0; i<p_div.length; i++)
			ProfileReloader.boxUsers[i] = { id: p_div[i].id,
											profiles : YAHOO.lang.JSON.parse(p_div[i].innerHTML)
										};
	},

	reload: function () {
		for(var i=0; i<ProfileReloader.boxUsers.length; i++) {
			try {
				if(ProfileReloader.boxUsers[i].profiles.length >= ProfileReloader.minProfilesCount)
					ProfileReloader.reloadBox(i);
				/*else
					console.log('Ci sono troppo pochi profili');*/
			} catch (e) {}
		}
	},
		
	reloadBox: function (boxType) {
		boxId = ProfileReloader.boxUsers[boxType].id;
		boxCount = ProfileReloader.getCount(boxId);		
		boxProfiles = ProfileReloader.boxUsers[boxType].profiles;
		
		if(ProfileReloader.flag) {
			var selectedProfileIndex = ProfileReloader.getRandomIndex(boxProfiles.length);
			var selectedBoxIndex = ProfileReloader.getRandomIndex(boxCount) + 1;
			
			// Seleziona un utente da mostrare e lo inserisce tra quelli visualizzati
			while(ProfileReloader.search(selectedProfileIndex, ProfileReloader.chosen[boxId]))
				selectedProfileIndex = ProfileReloader.getRandomIndex(boxProfiles.length);
						
			ProfileReloader.chosen[boxId][selectedBoxIndex - 1] = selectedProfileIndex;
			
			// Crea il nuovo utente e aggancia lo user overlay
			ProfileReloader.setNewUser(ProfileReloader.getUserBoxId(boxId, selectedBoxIndex), boxProfiles[selectedProfileIndex]);
		}
	},
	
	getUserBoxId: function (boxId, userBoxIndex) {
		return boxId.substr(11) + '_' + userBoxIndex;
	},
	
	chosenFlusher: function (chosen, pLength, pShown) {
		if(chosen.length == pLength)
			return chosen.slice(pLength - pShown);
		
		return chosen;
	},
	
	setNewUser: function (elId, user) {
		var el = YAHOO.util.Dom.get(elId);
		
		// Imposta il testo del div
		el.innerHTML = ProfileReloader.createPerson(user);
		
		// Aggancia lo user overlay
		YAHOO.neo.overlay.generateOverlay(elId, user.features, { profile_id: user.profile_id, nick: user.nick });
	},

	createPerson: function (person) {
		var affinity = (!person.features.test_affinity_result ? '' : ' ' + (person.features.test_affinity_result < 33 ? 'liv1' : (person.features.test_affinity_result < 66 ? 'liv2' : 'liv3')));
		
		if(person.features.test_affinity_result > 1) {
				return '<div class="thumb"><a class="' + (person.gender == 'M' ? 'male' : 'female') + '" href="http://' + person.nick + '.' + conf.domain + '"><span></span>' +
					'<img width="56" height="56" alt="' + person.nick + '" src="' + person.avatar + '"/>' + 
					'</a>' +
					'<span class="presence ' + (person.online == 'online' ? 'on' : 'off') + '"></span>' +
					'<span class="affine' + affinity + '"></span>' +
					'</div>';
		} else {
			return '<div class="thumb"><a class="' + (person.gender == 'M' ? 'male' : 'female') + '" href="http://' + person.nick + '.' + conf.domain + '"><span></span>' +
					'<img width="56" height="56" alt="' + person.nick + '" src="' + person.avatar + '"/>' + 
					'</a>' +
					'<span class="presence ' + (person.online == 'online' ? 'on' : 'off') + '"></span>' +
					'</div>';
		}	
		
	},
	
	getRandomIndex: function (mod) {
		return Math.floor((Math.random() * 100)) % mod;
	},
	
	search: function (needle, haystack) {
		for(var i=0; i<haystack.length; i++)
			if(haystack[i] == needle)
				return true;
		return false;
	},
	
	getCount: function (id) {
		return YAHOO.util.Dom.get(id + "_count").value;
	},
	
	initialize: function () {
		ProfileReloader.parseProfiles();
		
		for(var i=0; i<ProfileReloader.boxUsers.length; i++) {
			var boxId = ProfileReloader.boxUsers[i].id;
			ProfileReloader.chosen[boxId] = new Array();
			
			for(var j=0; j<ProfileReloader.getCount(boxId); j++)
				ProfileReloader.chosen[boxId][j] = j;
		}
	}
}

YAHOO.util.Event.onDOMReady(function () {
	ProfileReloader.initialize();
		
	window.setInterval(ProfileReloader.reload, 15 * 1000);
});