var banned = {

    buffer: { },
    
	reasonHTML: function(uniqId, buttonName) {
		return '<div style="width: 100%; margin-top: 10px">\
			<b>Причина:</b>\
			<br />\
			<div><textarea id="bfrm_'+uniqId+'" name="bfrm_'+uniqId+'" style="width: 100%; height: 40px"></textarea></div>\
			<br />\
			<div style="text-align: right">\
                <input type="button" value="Отмена" onclick="banned.commit(\''+uniqId+'\', (banned.buffer[\''+uniqId+'\'].action = \'close\'))">\
                <input type="button" value="'+buttonName+'" onclick="banned.commit(\''+uniqId+'\', document.getElementById(\'bfrm_'+uniqId+'\').value)">\
			</div>\
			</div>\
		';
	},
	
	warnUser: function(userId, uniqId, draw_func) {
        var div = document.getElementById('warnreason-'+uniqId);
        div.style.display = '';
        if (typeof this.buffer[uniqId] != 'undefined' && this.buffer[uniqId] != null) {
            this.commit(uniqId, (this.buffer[uniqId].action = 'close'));
            return;
        }
        this.buffer[uniqId] = { 
            action: 'warnUser', 
            divObj: div, 
            divHTML: div.innerHTML,
            userId: userId, 
            draw_func: draw_func 
        };
        div.innerHTML = this.reasonHTML(uniqId, 'Сделать предупреждение');
        div.style.display = 'block';
    },
	
	blockedThread: function(topicId) {
        var uniqId = "thread_"+topicId;
        var div = document.getElementById('thread-reason-'+topicId);
        if (typeof this.buffer[uniqId] != 'undefined' && this.buffer[uniqId] != null) {
            this.commit(uniqId, (this.buffer[uniqId].action = 'close'));
            return;
        }
        this.buffer[uniqId] = { 
            action: 'blockedThread', 
            divObj: div, 
            divHTML: div.innerHTML,
            topicId: topicId
        };
        div.innerHTML = this.reasonHTML(uniqId, 'Заблокировать топик');
        div.style.display = 'block';
	},
	
	unblockedThread: function(topicId) {
		if (confirm('Уверены, что хотите разблокировать топик?')) {
            var uniqId = "thread_"+topicId;
            var div = document.getElementById('thread-reason-'+topicId);
            this.buffer[uniqId] = { 
                action: 'blockedThread', 
                divObj: div, 
                divHTML: div.innerHTML,
                topicId: topicId
            };
            this.commit(uniqId, 'unban');
            return true;
		} else {
			return false;
		}
	},

	blockedProject: function(projectId) {
        var uniqId = "project_"+projectId;
        var div = document.getElementById('project-reason-'+projectId);
        if (typeof this.buffer[uniqId] != 'undefined' && this.buffer[uniqId] != null) {
            this.commit(uniqId, (this.buffer[uniqId].action = 'close'));
            return;
        }
        this.buffer[uniqId] = { 
            action: 'blockedProject', 
            divObj: div, 
            divHTML: div.innerHTML,
            projectId: projectId
        };
        div.innerHTML = this.reasonHTML(uniqId, 'Заблокировать проект');
        div.style.display = 'block';
	},

	unblockedProject: function(projectId) {
		if (confirm('Уверены, что хотите разблокировать проект?')) {
            var uniqId = "project_"+projectId;
            var div = document.getElementById('project-reason-'+projectId);
            this.buffer[uniqId] = {
                action: 'blockedProject',
                divObj: div, 
                divHTML: div.innerHTML,
                projectId: projectId
            };
            this.commit(uniqId, 'unblocked');
            return true;
		} else {
			return false;
		}
	},

	blockedCommune: function(communeId) {
        var uniqId = "project_"+communeId;
        var div = document.getElementById('project-reason-'+projectId);
        if (typeof this.buffer[uniqId] != 'undefined' && this.buffer[uniqId] != null) {
            this.commit(uniqId, (this.buffer[uniqId].action = 'close'));
            return;
        }
        this.buffer[uniqId] = { 
            action: 'blockedCommune', 
            divObj: div, 
            divHTML: div.innerHTML,
            projectId: projectId
        };
        //div.innerHTML = this.reasonHTML(uniqId, 'Заблокировать проект');
        //div.style.display = 'block';
	},
    
	commit: function(uniqId, reason) {
        if (reason) {
            
            if (typeof(reason) == "string") {
                reason = reason.replace(/&/g, "&amp;");
                reason = reason.replace(/"/g, "&quot;");
                reason = reason.replace(/'/g, "&#039;");
                reason = reason.replace(/</g, "&lt;");
                reason = reason.replace(/>/g, "&gt;");
            }

            if (this.buffer[uniqId].divHTML) {
                if (this.buffer[uniqId].action == 'close') {
                    this.buffer[uniqId].divObj.innerHTML = this.buffer[uniqId].divHTML;
                    this.buffer[uniqId].divObj.style.display = 'none';
                } else {
                    this.buffer[uniqId].divObj.innerHTML = '<div style="width: 100%; text-align: center"><img src="/images/load_fav_btn.gif" width="24" height="24" border="0"></div>';
                }
            }
			switch (this.buffer[uniqId].action) {
                case 'blockedThread': xajax_BlockedThread(this.buffer[uniqId].topicId, reason); break;
                case 'blockedProject': xajax_BlockedProject(this.buffer[uniqId].projectId, reason); break;
                case 'warnUser' :
                    if (document.getElementById('message' + uniqId)) {reason = reason + '\n\nhttp://' + document.location.host + '/blogs/view.php?tr=' + uniqId + '\n' + document.getElementById('message' + uniqId).innerHTML;}	//blogs
                    if (document.getElementById('project' + uniqId)) {reason = reason + '\n\n' + document.location.href + '\n' + document.getElementById('project' + uniqId).innerHTML;}	//projects
                    xajax_WarnUser(this.buffer[uniqId].userId, reason, this.buffer[uniqId].draw_func, uniqId);
                    break;
            }
            delete this.buffer[uniqId];
            } else {
                alert('Необходимо указать причину!');
                return false;
           }
    }

}