var Team = new function() {
    this.fullpath = '/';
    this.items = new Object();
    this.vacancy = false;
    
    this.switchPhoto = function() {
        var args = arguments;
        var i,yes = false;
        var photo = args[0].parentNode.parentNode.getElementsByTagName('IMG')[1];
        if(args.length > 2) {
            for(i=1;i<args.length;i++) {
                if(photo.src.indexOf(args[i]) >= 0) {
                    if(i + 1 < args.length) {
                        photo.src = args[i + 1];
                        return false;
                    }
                }
            }
            photo.src = args[1];
        }
        
        return false;
    }
    
    this.showPerson = function(id) {
        if (this.items[id] == null) {
            this.showPreload();
            
            JsHttpRequest.query(
                this.fullpath+'?action=ajaxGetItem&cache='+Utils.getCacheValue(),
                {'id': id},
                function(result, error) {
                    if (error) alert(error);
                    if (result['error'].length) {
                        for(var i in result['error']) {
                            alert(result['error'][i]);
                        }
                    } else {
                        Team.items[id] = result['body'];
                        Team._showPerson(id);
                    }
                }
            );
        } else {
            this._showPerson(id);
        }
        
        return false;
    }
    
    this._showPerson = function(id) {
        if (this.items[id] == null) return;
        
        $('#popup-body').empty().append(this.items[id]);
        this.showPopup();
    }
    
    this.showPreload = function() {
        $('#popup-body').empty().append('<div class="loader"></div>');
        this.showPopup();
    }
    
    this.showPopup = function() {
        var cover = document.getElementById('cover');
        var popup = document.getElementById('popup');
        cover.style.display = 'block';
        popup.style.display = 'block';
        popup.style.left = ((cover.offsetWidth - popup.offsetWidth) / 2) + 'px';
        popup.style.top = ((cover.offsetHeight - popup.offsetHeight) / 2) + 'px';
        return false;
    }
    
    this.showVacancy = function() {
        if (this.vacancy == false) {
            this.showPreload();
            
            JsHttpRequest.query(
                this.fullpath+'?action=ajaxGetVacancy&cache='+Utils.getCacheValue(),
                {},
                function(result, error) {
                    if (error) alert(error);
                    if (result['error'].length) {
                        for(var i in result['error']) {
                            alert(result['error'][i]);
                        }
                    } else {
                        Team.vacancy = result['body'];
                        Team._showVacancy();
                    }
                }
            );
        } else {
            this._showVacancy();
        }
    }        
    
    this._showVacancy = function() {
        if (this.vacancy == false) return;
        
        $('#popup-body').empty().append(this.vacancy);
        this.showPopup();
    }
    
    return this;
} ();