var banned = {

    buffer: { },
    
    reasons: {},
    
	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>\
		';
	},
	
	selectReasonHTML: function( uniqId ) {
        return '<div class="form fs-p mab">\
        	<b class="b1"></b>\
        	<b class="b2"></b>\
        	<div class="form-in">\
        		<strong>Причина блокировки проекта</strong>\
        		<div id="bfrm_div_sel_'+uniqId+'" class="mab-sel">\
        			<select id="bfrm_sel_'+uniqId+'" name="bfrm_sel_'+uniqId+'" disabled>\
        				<option>Подождите...</option>\
        			</select>\
        		</div>\
        		<div class="mab-txt">\
        			<textarea id="bfrm_'+uniqId+'" name="bfrm_'+uniqId+'" rows="5" cols="20" disabled></textarea>\
        		</div>\
        		<div class="mab-btns">\
        			<input type="button" id="bfrm_btn_'+uniqId+'" disabled onclick="banned.commit(\''+uniqId+'\', document.getElementById(\'bfrm_'+uniqId+'\').value)" value="Заблокировать" class="i-btn">&nbsp;\
        			<a href="#" onclick="banned.commit(\''+uniqId+'\', (banned.buffer[\''+uniqId+'\'].action = \'close\')); return false;" class="lnk-dot-666">Отменить</a>\
        		</div>\
        	</div>\
        	<b class="b2"></b>\
        	<b class="b1"></b>\
        </div>\
        ';
	},
	
	adjustReasonHTML: function( uniqId ) {
        var field        = $('bfrm_sel_' + uniqId);
        if (field) {
            var dim = field.getParent().getParent().getSize();
            var borderLeft   = parseInt(field.getStyle('border-left-width'));
            var borderRight  = parseInt(field.getStyle('border-right-width'));
            var paddingLeft  = parseInt(field.getStyle('padding-left'));
            var paddingRight = parseInt(field.getStyle('padding-right'));
            var marginLeft   = parseInt(field.getStyle('margin-left'));
            var marginRight  = parseInt(field.getStyle('margin-right'));
            var styleWidth   = dim.x - borderLeft - borderRight - paddingLeft - paddingRight - marginLeft - marginRight - 20;

            field.setStyle('width', styleWidth );
        }
        
        field        = $('bfrm_'+uniqId);
        dim          = field.getParent().getParent().getSize();
        borderLeft   = parseInt(field.getStyle('border-left-width'));
        borderRight  = parseInt(field.getStyle('border-right-width'));
        paddingLeft  = parseInt(field.getStyle('padding-left'));
        paddingRight = parseInt(field.getStyle('padding-right'));
        marginLeft   = parseInt(field.getStyle('margin-left'));
        marginRight  = parseInt(field.getStyle('margin-right'));
        styleWidth   = dim.x - borderLeft - borderRight - paddingLeft - paddingRight - marginLeft - marginRight - 20;
        
        field.setStyle('width', styleWidth );
	},
	
	setReason: function( uniqId ) {
	    var parts      = uniqId.split('_');
	    var reasonType = ( typeof parts[1] != 'undefined' ) ? parts[0] : '';
	    var reasonId   = $('bfrm_sel_'+uniqId).get('value');
	    
	    var uniqReason = reasonType + '_' + reasonId;
	    
	    if ( reasonId != '' ) {
            if ( this.buffer[uniqId].reasonId == '' ) {
                this.buffer[uniqId].customReason = $('bfrm_' + uniqId ).get( 'value' );
            }
	        
            $('bfrm_' + uniqId).set( 'readonly', true );
            
            if ( typeof this.reasons[uniqReason] != 'undefined' ) {
                $('bfrm_' + uniqId ).set( 'value', this.reasons[uniqReason] );
            }
            else {
                var sFuncName = 'xajax_' + reasonType + 'GetBlockedReason';
                window[sFuncName].apply( null, Array(uniqId, reasonId) );
            }
	    }
	    else {
	        $('bfrm_' + uniqId ).set( 'value', this.buffer[uniqId].customReason );
	        $('bfrm_' + uniqId).set( 'readonly', false );
	    }
	    
	    this.buffer[uniqId].reasonId = reasonId;
	},
	
	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';
        this.adjustReasonHTML( uniqId );
    },
	
	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';
        this.adjustReasonHTML( uniqId );
	},
	
	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,
            customReason: '',
            reasonId: ''
        };
        div.innerHTML = this.selectReasonHTML( uniqId );
        div.style.display = 'block';
        this.adjustReasonHTML( uniqId );
        xajax_BlockedProjectReasons( uniqId );
	},

	blockedProjectWithComplains: 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: 'blockedProjectWithComplains', 
            divObj: div, 
            divHTML: div.innerHTML,
            projectId: projectId,
            customReason: '',
            reasonId: ''
        };
        div.innerHTML = this.selectReasonHTML( uniqId );
        div.style.display = 'block';
        this.adjustReasonHTML( uniqId );
        xajax_BlockedProjectReasons( uniqId );
	},

	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 'userBan':
                    $('ban_btn').set( 'disabled', true );
                    $('ban_btn').set( 'value', 'Подождите' );
			        if ( this.buffer[uniqId].act_id > 1 && reason.replace(/(^\s+)|(\s+$)/g, "") == '' ) {
			            alert('Необходимо указать причину!');
                        $('ban_btn').set( 'disabled', false );
                        $('ban_btn').set( 'value', 'Сохранить' );
                        return false;
			        }
                    var date = '';
                    if ( $('ban_to_date').get('checked') ) {
                        date = $('ban_year').get('value') + '-' + $('ban_month').get('value') + '-' + $('ban_day').get('value');
                    }
                    xajax_updateUserBan( this.buffer[uniqId].userId, this.buffer[uniqId].act_id, reason, this.buffer[uniqId].reasonId[this.buffer[uniqId].act_id], date, this.banNoSend, JSON.encode(this.buffer[uniqId].context) );
                    break;
                case 'blockedThread': 
                    xajax_BlockedThread(this.buffer[uniqId].topicId, reason, this.buffer[uniqId].reasonId, this.buffer[uniqId].reasonName);
                    break;
                case 'blockedProject': 
                    xajax_BlockedProject( this.buffer[uniqId].projectId, reason, this.buffer[uniqId].reasonId ); 
                    break;
                case 'blockedProjectWithComplains': 
                    xajax_BlockedProjectWithComplain(this.buffer[uniqId].projectId, reason, this.buffer[uniqId].reasonId ); 
                    break;
                case 'warnUser' :
                    if (document.getElementById('message' + uniqId)) {reason = reason + '\n\nhttp://' + document.location.host + '/blogs/view.php?tr=' + ((typeof blog_thread_id != 'undefined')? blog_thread_id+'&openlevel='+uniqId+'#o'+uniqId : 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

                    // Defile
                    if (this.buffer[uniqId].draw_func=='defile') {reason = reason + '\n\nhttp://' + document.location.host+'/defile/view/'+defile_work_id;}


					// Comments in admin panel
					if (this.buffer[uniqId].draw_func=='comments') {
						var m = uniqId.split('-');
						switch (m[0]) {
							case '1':
								reason = reason + '\n\nhttp://'+document.location.host+'/blogs/view.php?tr='+m[2];
								break;
							case '2':
								reason = reason + '\n\nhttp://'+document.location.host+'/articles/?id='+m[2];
								break;
							case '3':
								reason = reason + '\n\nhttp://'+document.location.host+'/defile/view/'+m[2];
								break;
						}
					}

                    xajax_WarnUser(this.buffer[uniqId].userId, reason, this.buffer[uniqId].draw_func, uniqId);
                    break;
            }
            delete this.buffer[uniqId];
            } else {
                alert('Необходимо указать причину!');
                return false;
           }
    }

}

