function Contact(){

}

Contact.prototype={
    age:function(){
        var d=new Date();
        var bd=new Date();
        bd.setDate(this.birthdate.getDate());
        bd.setMonth(this.birthdate.getMonth());
        bd.setFullYear(d.getFullYear());

        var age=d.getFullYear()-this.birthdate.getFullYear();

        if(d<bd){
            age=age-1
        }
        return age;
    }
}
function Contacts(location_id){
    this.contacts={};
    this.interval=5;
    this.location=location_id;
    this.filter='';
};

Contacts.prototype={
    get:function(id){
      id=parseInt(id);
      return this.contacts[id];
    },
    init:function(interval){
        this.interval=interval
    },
    start:function(){
        var self=this;
        self._update();
        this.pe=new PeriodicalExecuter(function(){
            self._update();
        },this.interval)
    },
    stop:function(){
        this.pe.stop();
    },
    
    refilter:function(str){
        this.filter=str;
        this.redraw();
    },
    redraw:function(){
        var __dfl_sort_func=function(o1,o2){
            var s1=o1.nick.toLowerCase().replace(/[^а-яa-z0-9 ]/g,'');
            var s2=o2.nick.toLowerCase().replace(/[^а-яa-z0-9 ]/g,'');
            return  ( ( s1 == s2 ) ? 0 : ( ( s1 > s2 ) ? 1 : -1 ) );
        }
        var d=$('userlist_wrapper');
        d.innerHTML='';
        var clist=[];
        var total_count=0;
        for (var c in this.contacts){
            if(
                (this.contacts[c].status !=2)
                && (this.contacts[c].status !=99)
            ){
		total_count++;
                if (this.contacts[c].nick.toLowerCase().indexOf(this.filter.toLowerCase()) >= 0){
                    clist.push(this.contacts[c])
		}
            }
        }
            
        clist.sort(__dfl_sort_func);
	try{
        for (var i =0; i<clist.length;i++){
            var c=clist[i];
            var li=document.createElement('li');
            var span=document.createElement('span');
            var a= document.createElement('a');a.href='/'+c.id+'/';a.target="_blank";
            a.className=((c.sex=='M')?'male':'female')
            var img=new Image();
	    // console.log(c.photo);
            img.className=(c.photo?'':'noPhoto')
            img.src=_MEDIA_URL+'i/s.gif';
            a.appendChild(img);
            span.appendChild(a);
            
            a= document.createElement('a');a.href='/'+c.id+'/';a.target="_blank";
	    if(c.status == 3){
		a.className='goAway';
	    }
            a.appendChild(document.createTextNode(c.nick));
            span.appendChild(a);
            
            var img=new Image();img.src=_MEDIA_URL+'i/index/etc/_clan_logo.gif';
            img.className='clanLogo';
            span.appendChild(img);
            
            li.appendChild(span);
            d.appendChild(li);
        }
	}catch(e){alert(e);}
        $('userlist_count').innerHTML=total_count.toString();
    },
    __updating:false,
    _update:function(syncronous){
        var self=this
        if(this.__updating){
            return;
        }
        this.__updating=true;
        
        var url='/ajax/contacts-p/?random='+Math.round(Math.random()*100000);

        var params={
            'last':0,
            'ids':'',
            'loc':this.location
        };

        this.contactRequest= new Ajax.Request(
            url, {
                method: 'post',
                parameters:params,
                asynchronous:true,
                onSuccess:function(transport){
		    try{
                    if(!transport.responseXML){
                        self.__updating=false;
                        return;
                    }

                    var root=transport.responseXML.getElementsByTagName('contacts').item(0);
                    var last=parseInt(root.getAttribute('last'));
                    self.contacts={};
                    for (var i = 0; i < root.childNodes.length; i++) {
                        var contact = root.childNodes.item(i);
                        if (contact.tagName != 'contact'){
                            continue;
                        }
                        var c=new Contact();
                        c.id=parseInt(contact.getAttribute('id'));
                        c.sex=contact.getAttribute('sex');
                        c.photo=(contact.getAttribute('photo') !='');
                        c.birthdate=contact.getAttribute('birthdate');
                        c.birthdate=new Date(
                            c.birthdate.substring(0,4),
                            parseInt(c.birthdate.substring(5,7))-1,
                            c.birthdate.substring(8,10)
                        );
                        var f=function(name){
                            if(contact.getElementsByTagName(name).item(0).childNodes[0])
                            {
                                return contact.getElementsByTagName(name).item(0).childNodes[0].nodeValue;
                            }else{
                                return '';
                            }
                        }
                        c.status=parseInt(f('status'),10);
                        c.nick=f('nick');
                        c.name=f('name');
                        c.statusline=f('statusline')
                        self.contacts[c.id]=c
                    }
		    }catch(e){console.log(e);}
                    self.__updating=false;
                    self.redraw();
                },
                onFailure: function(transport){
                    self.__updating=false;
                }
            }
        );
    }
}
