mingling = {
	mode : 'co',
	wide : true,

	memberlist : null,

	exclude_nick : '',
	school_name : '',
	city_name : '',
	school_id : 0,
	city_id : 0,
	count_total : 0,
	count_total2 : 0,
	count_total3 : 0,
	count_city : 0,
	count_chattotal : 0,
	count_chatcity : 0,
	threshold : 0,
	
	members_in_school : new Array(),
	members_in_city : new Array(),
	members_other : new Array(),

	members_output : new Array(),

	output_header : function() {
		var out = '';
		out += '<p class="info">';
		if (('co' == this.mode) && (0 == this.school_id)) {
			out += '<a href="'+HOME_URL+'search/online"><strong>';
			out += this.count_total+' Mitglieder online';
			out += '</strong></a>';
		} else if (('co' == this.mode) && (0 != this.school_id)) {
			out += '<a href="'+HOME_URL+'search/online"><strong>';
			out += this.count_total+' Mitglieder online';
			out += '</strong></a><span class="anker">';
			out += this.wide ? ' | ' : '<br />';
			out += '</span><a href="'+HOME_URL+'search/location/city/'+this.city_id+'" class="small">';
			out += this.count_city+' aus deiner Stadt';
			out += '</a>';
		} else if (('cc' == this.mode) && (0 != this.school_id)) {
			out += '<a href="'+HOME_URL+'search/chat"><strong>';
			out += this.count_chattotal+' Mitglieder im Chat';
			out += '</strong></a><span class="anker">';
			out += this.wide ? ' | ' : '<br />';
			out += '</span><a href="'+HOME_URL+'search/location/city/'+this.city_id+'" class="small">';
			out += this.count_chatcity+' aus deiner Stadt';
			out += '</a>';
		} else if (('cc' == this.mode) && (0 == this.school_id)) {
			out += '<a href="'+HOME_URL+'search/chat"><strong>';
			out += this.count_chattotal+' Mitglieder im Chat';
			out += '</strong></a>';
		} else if ('bday' == this.mode){
			out += '<a href="'+HOME_URL+'search/online"><strong>';
			out += this.count_total+' Mitglieder online';
			out += '</strong></a>';
			out += this.wide ? ' | ' : '<br />';
			out += 'Geburtstagskinder';
		} else if ('miss' == this.mode){
			out += '<a href="'+HOME_URL+'search/online"><strong>';
			out += this.count_total+' Mitglieder online';
			out += '</strong></a>';
			out += this.wide ? ' | ' : '<br />';
			out += 'Was macht eigentlich...';
		} else {
			out += '<a href="'+HOME_URL+'search/online"><strong>';
			out += this.count_total+' Mitglieder online';
			out += '</strong></a>';
			out += this.wide ? ' | ' : '<br />';
			out += 'Kennst Du schon ...?';
		} 
		out += '</p>';
		return out;
	},

	output_pic_url : function(pic_hash, pic_file, pic_offset, pic_size, compare) {
		pic_hash = pic_hash.substring(0,2)+'/'+pic_hash.substring(2,4)+'/'+pic_hash;
		pic_file = 'fotos/'+pic_file;
		pic_size = (60 == pic_size) ? '60,60,1,1,1,'+pic_offset+',-1,-1,-1,-1,-1,0,0,0' : '80,80,1,1,1,'+pic_offset+',-1,-1,-1,-1,-1,0,0,0';
		var out = PICTURE_URL+'images/ca/'+pic_hash+'.jpg?'+pic_file+','+pic_size;
		// if (out != compare) alert(out +'\r\n'+ compare);
		return out;
	},

	output_wide : function() {
		var out = '';
		out += '<div class="floatfix">';
		for (i = 0; i < this.members_output.length; i++) {
			var item = this.members_output[i];
			var c  = (item.c ) ? item.c  : this.city_id;
			var cn = (item.cn) ? item.cn : this.city_name;
			var s  = (item.s ) ? item.s  : this.school_id;
			var sn = (item.sn) ? item.sn : this.school_name;
			url_extend = '/typ/'+this.mode;
			if(item.hpic > 2){
				url_extend += '/picturewish/1';
			} 
			if (3 == i || 6 == i) out += '</div><div class="floatfix">';
			tooltip_title = sn + ' :: ' + cn;
			if(item.nam.length > 14){
				item.nam = item.nam.substr(0,11)+'...';
			}
			out += 	'<div class="membpic tooltip" title="' + tooltip_title + '">';
			out += 	'<a href="'+HOME_URL+'profile/'+item.nic + url_extend+'">';
			// out += 			'<img src="'+this.output_pic_url(item.p6, item.p, item.po, 60, item.p6u)+'" alt="'+item.nam+'">';
			out += 	'<img src="'+this.output_pic_url(item.p6, item.p, item.po, 60)+'" alt="'+item.nam+'" title="' + tooltip_title + '">';
			out += 	'</a>';
			out += 	'<p><a href="'+HOME_URL+'profile/'+item.nic + url_extend+'">'+item.nam+'</a></p>';
			out += 	'</div>';
		}
		out += '</div>';
		return out;
	},

	output_normal : function() {
		var out = '';
		for (i = 0; i < this.members_output.length; i++) {
			var item = this.members_output[i];
			var c  = (item.c ) ? item.c  : this.city_id;
			var cn = (item.cn) ? item.cn : this.city_name;
			var s  = (item.s ) ? item.s  : this.school_id;
			var sn = (item.sn) ? item.sn : this.school_name;
			url_extend = '/typ/'+this.mode;
			if(item.hpic > 2){
				url_extend += '/picturewish/1';
			}
			tooltip_title = sn + ' :: ' + cn;
			if(item.nam.length > 16){
				item.nam = item.nam.substr(0,13)+'...';
			}
			out += 	'<div class="membpic tooltip" title="' + tooltip_title + '" style="clear:left; padding-left:22px; width:96px;">';
			out += 		'<a href="'+HOME_URL+'profile/'+item.nic + url_extend+'">';
			// out += 			'<img src="'+this.output_pic_url(item.p8, item.p, item.po, 80, item.p8u)+'" alt="'+item.nam+'">';
			out += 			'<img src="'+this.output_pic_url(item.p8, item.p, item.po, 80)+'" alt="'+item.nam+'" title="' + tooltip_title + '">';
			out += 		'</a>';
			out += 		'<p><a href="'+HOME_URL+'profile/'+item.nic + url_extend+'">'+item.nam+'</a></p>';
			out += 	'</div>';
		}
		return out;
	},

	output : function() {
		var out = '';
		out += this.output_header();
		out += (this.wide) ? this.output_wide() : this.output_normal();
		document.getElementById('minglingdisplay').innerHTML = out;
	},

	set_exclude_nick : function(exclude_nick) {
		this.exclude_nick = exclude_nick;
	},

	set_wide : function(wide) {
		this.wide = wide;
	},

	set_total2 : function(total) {
		this.count_total2 = total;
	},

	set_total3 : function(total) {
		this.count_total3 = total;
	},

	set_threshold : function(threshold) {
		this.threshold = threshold;
	},
	
	prepare_total : function() {
		if (0 < this.count_total2) {
			this.count_total = this.count_total2;
			return;
		}
		if (0 < this.count_total3) {
			this.count_total = this.count_total3;
		}
		this.count_total += Math.round(Math.random()*40)-20;
		
	},

	extract_random : function(from, try_to_fill_to_this_number, type) {
		temp = 0;
		while(from.length == 0 && temp < 3){
			if(type == 'school'){
				from = this.members_in_city;
				type = 'city';
			} else if(type == 'city'){
				from = this.members_other;		
				type = 'other';
			} else if(type == 'other'){
				from = this.members_in_school;		
				type = 'school';
			}
			temp = temp + 1;
		}
		while ((this.members_output.length < try_to_fill_to_this_number) && (0 < from.length)) {
			// to_pick = Math.floor(Math.random()*(from.length));
			to_pick = Math.round(Math.random()*(from.length-1));
			// this.members_output.push(from[to_pick]);
			this.members_output.push(this.memberlist[from[to_pick]]);
			from.splice(to_pick,1); // remove picked item from list
		}
	},

	sort_list : function() {
		for (i = 0; i < this.memberlist.length; i++) {
			if (this.exclude_nick == this.memberlist[i].nic){
				continue;
			}
			if (typeof this.memberlist[i].s == 'undefined') {
				this.members_in_school.push(i);
			}else if (typeof this.memberlist[i].c == 'undefined'){
				this.members_in_city.push(i);
			}else {
				this.members_other.push(i);
			}
		}
	},

	process_data : function(data) {
		this.school_name = data.nr.sn;
		this.city_name = data.nr.cn;
		this.school_id = data.nr.s;
		this.city_id = data.nr.c;
		this.count_total = data.nr.total;
		this.count_city = data.nr.city;
		this.count_chattotal = data.nr.chattotal;
		this.count_chatcity = data.nr.chatcity;
		if (0 != this.school_id) {
			rnd_value = Math.random();
			if(rnd_value < 0.25){
				this.mode = 'dyk';
				this.memberlist = data.dyk;
			} else if (rnd_value < 0.60){
				this.mode = 'co';
				this.memberlist = data.co;
			} else if (rnd_value < 0.70){
				this.mode = 'cc';
				this.memberlist = data.cc;
			} else if (rnd_value < 0.80){
				this.mode = 'miss';
				this.memberlist = data.miss;
			} else {
				this.mode = 'bday';
				this.memberlist = data.bday;
			}
		} else {
			this.mode = 'co';
			this.memberlist = data.co;
		}
		this.sort_list();
		if (this.mode != 'bday' && this.mode != 'miss' && this.mode != 'dyk'){
			if (this.wide) {
				this.extract_random(this.members_in_school, 3, 'school');
				this.extract_random(this.members_in_city, 6, 'city');
				this.extract_random(this.members_other, 9, 'other');
			} else {
				this.extract_random(this.members_in_school, 2, 'school');
				this.extract_random(this.members_in_city, 4, 'city');
				this.extract_random(this.members_other, 6, 'other');
			}
		} else if (this.mode == 'bday'){
			if (this.wide) {
				this.extract_random(this.members_in_school, 1, 'school');
				this.extract_random(this.members_in_city, 3, 'city');
				this.extract_random(this.members_other, 9, 'other');
			} else {
				this.extract_random(this.members_in_school, 1, 'school');
				this.extract_random(this.members_in_city, 2, 'city');
				this.extract_random(this.members_other, 6, 'other');
			}
		} else if (this.mode == 'miss'){
			if (this.wide) {
				this.extract_random(this.members_in_school, 6, 'school');
				this.extract_random(this.members_in_city, 8, 'city');
				this.extract_random(this.members_other, 9, 'other');
			} else {
				this.extract_random(this.members_in_school, 4, 'school');
				this.extract_random(this.members_in_city, 5, 'city');
				this.extract_random(this.members_other, 6, 'other');
			}
		} else if (this.mode == 'dyk'){
			if (this.wide) {
				this.extract_random(this.members_in_school, 1, 'school');
				this.extract_random(this.members_in_city, 8, 'city');
				this.extract_random(this.members_other, 9, 'other');
			} else {
				this.extract_random(this.members_in_school, 1, 'school');
				this.extract_random(this.members_in_city, 5, 'city');
				this.extract_random(this.members_other, 6, 'other');
			}
		}
		this.prepare_total();
		this.output();
	}
}


function mingle_ondataready() {
	mingling.process_data(mingle_data);
}
