function Moderation(){
	this.currentTaskId=0;
	this.currentTaskRes=0;
	this.intervalTestTask=5000;
	this.intervalobj;
	this.initEvents=function(){
		$('#moder_alert_get').click(function(){moder.getTask()});
	//	$('#moder_send_res').click(function(){});
		$("#moder_check_rights").click(function(){moder.checkrights()});
		$("#moder_iwantM").click(function(){moder.iwantBeModer()});
		$("#moder_idntwantM").click(function(){moder.idntwantBeModer()});
		$("#moder_check_setting_1").click(function(){moder.setSetting(1)});
		$("#moder_check_setting_2").click(function(){moder.setSetting(2)});
		$("#moder_check_setting_3").click(function(){moder.setSetting(3)});
	};
	this.setSetting=function(n){
		var ok=1;
		if($("#moder_check_setting_"+n+":checked").length==1) ok=1;
		else ok=0;
		$.ajax({
   			url: '/business/setSetting',
		   	dataType : "text",
		   	data:'n='+n+'&ok='+ok,
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
		   		
		   	}
		});	
	}
	this.updavatar=function(){
		var path=$('#div_avatar img').attr('src');
		$('#div_avatar img').attr('src',path+'?d='+Math.floor(Math.random()*1000))
	//	var img = document.getElementById('banner');
		//img.src = "img.php?id=X"
	}
	this.idntwantBeModer=function(){
		$.ajax({
   			url: '/business/idntwantbemoder',
		   	dataType : "text",
		   	data:'',
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
		   		$('#moder_iwantmoderblock').show('slow');
				$('#moder_stat').hide('slow');
				$('#moder_idntwantmoderblock').hide('slow');
				window.clearInterval(this.intervalobj);
		   	}
		   	});	
	};
	this.iwantBeModer=function(){
		$.ajax({
   			url: '/business/iwantbemoder',
		   	dataType : "text",
		   	data:'',
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
		   		$('#moder_iwantmoderblock').hide('slow');
				$('#moder_idntwantmoderblock').show('slow');
				$('#moder_stat').show('slow');
				this.intervalobj=setInterval("moder.sendTest()",this.intervalTestTask);
		   	}
		   	});	
	};
	this.checkrights=function(){
		if ($("#moder_check_rights:checked").length==1)$("#moder_iwantM").removeAttr('disabled');
		else $("#moder_iwantM").attr('disabled','disabled');
	};
	this.sendTest=function (){
		$.ajax({
   			url: '/business/test',
		   	dataType : "json",
		   	data:'',
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
		   		if (data.status=="1"){
		   			$('#moder_btn').show();
		   		}else{
		   			$('#moder_btn').hide();
		   		}
		   	}
		   	});	
	};
	
	this.getTask=function (){
		var d_width=400;
		var c_task_id = -1;
		var title="";
		$.ajax({
   			url: '/business/gettask',
		   	dataType : "json",
		   	data:'',
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
		   		if (data.id==0){
					title=$('#moder_notask_name').attr('value');
			   		$( "#moder_desc" ).html($('#moder_notask_desc').attr('value'));
			   		$( "#moder_errors").hide();
			   		$( "#moder_content" ).html('');
					$('#moder_btn').hide();
					this.currentTaskId=-1;
		   		}else{
			   		c_task_id = data.id;
					$('#moder_uid').html(data.id);
			   		this.currentTaskId=data.id;
			   		currentTaskRes=0;
			   		$( "#moder_errors").show();
			   		
					
					switch(data.type){
						case '1':
							title=$('#moder_ava_name').attr('value');
							$( "#moder_desc" ).html($('#moder_ava_desc').attr('value'));
							$( "#moder_content" ).html('<center><img width="150" src="'+data.content+'" /></center>');
							d_width=400;
					    break;
						case '2':
							title=$('#moder_video_name').attr('value');
							$( "#moder_desc" ).html($('#moder_video_desc').attr('value'));
							$( "#moder_content" ).html('<center><iframe width="560" height="315" src="http://www.youtube.com/embed/'+data.content+'" frameborder="0" allowfullscreen></iframe></center>');
							d_width=582;
						break;
						case '3':
							title=$('#moder_photo_name').attr('value');
							$( "#moder_desc" ).html($('#moder_photo_desc').attr('value'));
							$( "#moder_content" ).html('<center><img width="300" src="'+data.content+'" /></center>');
							d_width=800;
					    break;
					}
			   		
		   		}
		   		$( "#moder_dialog" ).dialog({
		   			modal: true,
					resizable: false,
					title:title,
		   			width:d_width,
					buttons: { "OK": function() { moder.sendRes(); } },
		   			close:function(){
		   				if (c_task_id!='-1'){
			   				$.ajax({
			   		   			url: '/business/deltask',
			   				   	dataType : "text",
			   				   	data:'task_id='+c_task_id,
			   				   	context: this,
			   				   	type:'POST',
			   				   	success: function(data) {
			   				   		
			   				   	}
			   				   	});
			   			}
		   			}
		   			});
		   	}
		   	});
	};

	
	
		
	this.sendRes=function(){
		if(this.currentTaskId==0||this.currentTaskId==-1){
			$( "#moder_dialog" ).dialog( "destroy" );
		}else{
			this.currentTaskRes=$('input:radio[name=moder_errors]:checked').val();
			$( "#moder_dialog" ).dialog( "destroy" );
			$.ajax({
				url: '/business/sendres',
				dataType : "text",
				data:'task_id='+this.currentTaskId+'&res='+this.currentTaskRes,
				context: this,
				type:'POST',
				success: function(data) {
					this.getTask();
					}
				});
			}

	};
	this.ismoder=function(){
	
		$.ajax({
   			url: '/business/isUserAModer',
		   	dataType : "text",
		   	data:'',
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
			
		   		if (data=='1')this.intervalobj=setInterval("moder.sendTest()",this.intervalTestTask);
		   	}
		   	});
	};
}


var moder=new Moderation();
function mcache_test(){
	$.ajax({
   			url: '/business/mctest',
		   	dataType : "text",
		   	data:'',
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
				
		   	}
	});
}
$(document).ready(function(){
	moder.initEvents();
	moder.ismoder();
	moder.updavatar();
	
	
});
