window.checkedUsers = Object();

function checkUser(userid) {
	if ($('_inv_'+userid).checked==true) {
		delete window.checkedUsers[userid];
	} else window.checkedUsers[userid] = 1;
}

function makeIdents() {
	var idents = '';
	for (var k in window.checkedUsers) {
		if (idents=='') idents += k; else idents += ','+k;
	}
	$('_idents').value = idents;
}


function ratestar_over(num,id) {
	for(var i=1;i<=num;i++) {
		$('rs'+i+'_'+id).src="/img/star.gif";		
	}
	for(var i=5;i>num;i--) {
		$('rs'+i+'_'+id).src="/img/star_grey.gif";		
	}
}
function ratestar_out(num,id) {
	$('rs1_'+id).src='/img/star_grey.gif';
	$('rs2_'+id).src='/img/star_grey.gif';
	$('rs3_'+id).src='/img/star_grey.gif';
	$('rs4_'+id).src='/img/star_grey.gif';
	$('rs5_'+id).src='/img/star_grey.gif';
}

function RateCore() {
	this.currentPage = Object();
	this.ident ='';
	
	this.Rate = function(id,rating) {	
		 var data = new Object();	 
		 data['rating'] = rating;
		 data['userid'] = id;
		 data['contestid'] = window.contestid;
		 data['act'] = 'rating';
		 data['widget'] = 'userslist';

		 this.InitializeRequest('POST', '/ajax.php');
         this.Commit(postquery(data));	
	}
	
	this.OnSuccess = function()
      {
      	//alert(this.GetResponseText());
      	eval(this.GetResponseText());
      	// fade(0,"form_"+this.ident);
      	// $("loading_"+this.ident).style.display = 'none';
      }
}

RateCore.prototype = new ajax();
Rate = new RateCore();

function ListCore() {
	this.ident ='';
	this.currentPage = 1;
	this.perPage  = 20;	
	this.sort = 0;
	this.age_from = 0;
	this.age_till = 0;
	this.gender = 2;
	this.usergroupid=0;
  this.clubsid = 'all';
  
	this.bookmark = function(id) {
		var data = new Object();	
		data['userid'] = id;
		data['act'] = 'bookmark';
		data['widget'] = 'userslist';
		this.InitializeRequest('POST', '/ajax.php');
        this.Commit(postquery(data));
	}
	
	this.addFriend = function(id) {
		if (confirm('Добавить пользователя в друзья?') == true) {
			var data = new Object();	
			data['friendid'] = id;
			data['act'] = 'addfriend';
			data['widget'] = 'userslist';
			this.InitializeRequest('POST', '/ajax.php');
	        this.Commit(postquery(data));
		}
	}
	
	this.inviteUser = function(userid,groupid) {
		var data = new Object();	
		data['userid'] = userid;
		data['groupid'] = groupid;
		data['act'] = 'inviteuser';
		data['widget'] = 'userslist';
		this.InitializeRequest('POST', '/ajax.php');
        this.Commit(postquery(data));
	}
	
	this.inviteUserEvent = function(userid,eventid) {
		var data = new Object();	
		data['userid'] = userid;
		data['eventid'] = eventid;
		data['act'] = 'inviteuserevent';
		data['widget'] = 'userslist';
		this.InitializeRequest('POST', '/ajax.php');
        this.Commit(postquery(data));
	}
	
	this.displayMailblock = function(id) {
		if (!$('mailblock_'+id)) {
			var box = document.createElement('div');
			box.id = 'mailblock_'+id;
			//box.style.position = 'absolute';
			box.style.padding = '5px';
			box.style.border = '1px solid #000';
			box.style.background = '#e1e1e1';
			box.innerHTML = '<div><input type="text" style="width:300px;" id="subject_'+id+'"></div><div><textarea style="width:300px;height:50px;" id="message_'+id+'"></textarea></div><div style="text-align:center;"><span style="padding:2px;margin-right:5px;"><a style="cursor:pointer;" onclick="List.sendMail(\''+id+'\');">отправить</a></span><span style="padding:2px;margin-left:5px;"><a style="cursor:pointer;" onclick="List.cancelMail(\''+id+'\');">отменить</a></span></div>';
			$('useractions_'+id).appendChild(box);
		}
	}
	
	this.sendMail = function(id) {
		if ($('message_'+id) && $('message_'+id).value != '') {
			var data = new Object();	
			data['userid'] = id;
			data['subject'] = $('subject_'+id).value;
			data['message'] = $('message_'+id).value;
			data['act'] = 'sendmail';
			data['widget'] = 'userslist';
			this.InitializeRequest('POST', '/ajax.php');
	        this.Commit(postquery(data));
		} else alert('Вы пытаетесь отправить пустое собщение.');
	}
	
	this.cancelMail = function(id) {
		if ($('mailblock_'+id)) {
			$('useractions_'+id).removeChild($('mailblock_'+id));
		}
	}
	
	this.setcurrentPage = function(page) {
		if (page > 0) {
			this.currentPage = page;		
		} else {
			this.currentPage = 1;
		}
	}
	
	this.setParams = function() {
		this.age_from = $('age_from').options[$('age_from').selectedIndex].value;
		this.age_till = $('age_till').options[$('age_till').selectedIndex].value;
		this.usergroupid = $('usergroupid').options[$('usergroupid').selectedIndex].value;
		this.clubsid = $('clubsid').options[$('clubsid').selectedIndex].value;
		if ($('gender_0').checked == true) {
			this.gender = 0;
		} else if ($('gender_1').checked == true) {
			this.gender = 1;
		} else this.gender = 2;
	}
	
	this.setSort =function(sort) {
		this.sort = sort;
	}
	
	this.query = function() {
		var data = new Object();	
		data['page'] = this.currentPage;
		data['perpage'] = this.perPage;
		data['age_from'] = this.age_from;
		data['age_till'] = this.age_till;
		data['usergroupid'] = this.usergroupid;
		data['online'] = this.online;
		data['clubsid'] = this.clubsid;
		data['groupid'] = window.groupid;
		data['eventid'] = window.eventid;
		data['tourid'] = window.tourid;
		data['nickname'] = encodeURIComponent($('nickname').value);
		data['status'] = encodeURIComponent($('status').options[$('status').selectedIndex].value);
		data['contestid'] = window.contestid;
		data['gender'] = this.gender;
		data['invite_groupid'] = window.invite_groupid;
		data['invite_eventid'] = window.invite_eventid;
		data['invite_tourid'] = window.invite_tourid;
		data['checkedusers'] = Object();
		for (var key in window.checkedUsers) {
			data['checkedusers'][key] = window.checkedUsers[key];	
		}
		data['sort'] = this.sort;
		data['act'] = 'query';
		data['widget'] = 'userslist';
		this.InitializeRequest('POST', '/ajax.php');
        this.Commit(postquery(data));		
	}
	
	this.OnSuccess = function()
      {
      	// alert(this.GetResponseText());
      	 eval(this.GetResponseText());
      	// fade(0,"form_"+this.ident);
      	// $("loading_"+this.ident).style.display = 'none';
      }
}

ListCore.prototype = new ajax();
List = new ListCore();
