$(document).ready(
	function() {
		function callBackBefore(formData, jqForm, options) {
			var queryString = $.param(formData); 			
			if($('#title').val()=='') {
				$("#message").html("<span class=\"error\">Заполните поле имя!</span><br/>").fadeIn();
				return false;
			}
			$("#fileForm").hide();
			files = $("#protguFile :file").fieldValue();
			code = '';
			//for(i=0;i<=5;i++) {
				if(files[0] != '') {
					code += '<p>'+files[0]+'</p>';
				} else {
					$("#fileForm").show();
					$("#message").html("<span class=\"error\">Укажите файл!</span><br/>"+code).fadeIn();
					return false;
				}
			//}			
			$("#message").html("<span class=\"ok\">Загрузка файлов...</span><br/>"+code).fadeIn();
		}
			
		function callBackAfter() {
			$("#message").append('<br/><p><a href="javascript:void(0)" onclick="againLoad()">Загрузить еще</a></p>');	
			return false;
		}		
		
		function msgSend() {
			$("#pmsg").hide();
			$("#msgsend").fadeIn();
		}
		
		function msgDel() {
			reloadList();
		}
		
		$("#commentForm").submit(function() {
			var options = {
				target:			'#message',
				success:		sendBlogComment,
				resetForm:		true
			}
			
			if($("#txt_con").val()=="") {
				$("#txt_error").html("Пустое сообщение!");
			} else if($("#captcha").val()=="") {
				$("#captcha_error").html("Введите антиспам код!");
			} else {
				$(this).ajaxSubmit(options);
			}
			return false;
		});
		
		// ---
		$("#protguFile").submit(function() {
			var options = {
				target:			'#message',
				beforeSubmit:	callBackBefore,
				success:		callBackAfter			
			}
			$(this).ajaxSubmit(options);
			return false;
		});
		// ---
		
		$("#delMsgForm").submit(function() {
			var options = {
				target:			'#message',
				success:		msgDel,
				resetForm:		true
			}
			if(confirm("Вы действительно хотите удалить выделенные сообщения?")) {
				$(this).ajaxSubmit(options);
			}
			return false;
		});
		
		$("#pmsgForm").submit(function() {
			var options = {
				target:			'#message',
				success:		msgSend,
				resetForm:		true
			}
			$(this).ajaxSubmit(options);
			return false;
		});
		
		$("#loading").ajaxStart(function(){$(this).show().html("Подождите...");});
		$("#loading").ajaxStop(function(){$(this).hide().html("&nbsp;");});
		
		$("#login").focus(function() {
			if($(this).val()=='email') $(this).val("");
		});
		$("#login").blur(function() {
			if($(this).val()=='') if($(this).val()=='') $(this).val("email");
		});
	}
);	


function sendBlogComment() {
	if($("#message").text()!='error') {
		$.post("/blogs/comments/"+blog_id+"/"+cpage,
			function(data) {
				if(data!='error') {
					$("#comment_list").html(data);
					$("#addcomment_form").hide();
					$("#txt_con").val("");
					$("#txt_error").html("");
					$("#captcha").val("");
					$("#captcha_error").html("");
					$("#addcomment").show();
					$("p.success").html("Сообщение добавлено!").fadeIn();
				}
			}
		);
	} else {
		$("#captcha_error").html("Неверный антиспам код!");
	}
}
		
function againLoad() {
	$("#message").fadeOut();
	$("#fileForm").show();
}

function tagWork() {
	delim = $("#tag").val()==""?'':', ';
	if($("#tagSelect").val()!=0) $("#tag").val($("#tag").val()+delim+$("#tagSelect").find("option[@value="+$("#tagSelect").val()+"]").html());
}

function vote(type, id) {
	$.get('/files/vote/'+type+'/'+id+'/',
		function(response) {
			handleVote(response, id);
		});
}

function handleVote(response, id) {
	if(response=='ERROR1') return $('#vote').html()+'<br/>Ошибка голосования! Попробуйте еще раз!<br/>';
	if(response=='ERROR2') return $('#vote').html('Вы уже голосовали за этот файл!');
	if(response!='ERROR1' && response!='ERROR2') {
		$('#vote').fadeOut();
		totalFileRating(id);
	}
}

function totalFileRating(id) {
	$.get('/files/rating/'+id+'/',
		function(response) {
			handleTotalFileRating(response);
		});
}

function handleTotalFileRating(response) {
	return $('#total').html('Всего: '+response);
}

function fileComment(uid, fid) {
	var cmmnt = $('#uCmmnt').val();
	if(cmmnt=='') return false;
	$.get('/files/comment/add/'+fid+'/'+uid+'/', {'comment':cmmnt},
		function(response) {			
			handleFileComment(fid, response);
		});
}

function handleFileComment(fid, response) {		
	$('#uCmmnt').val('');
	$('#status').fadeIn();	
	$.post('/files/comment/show/'+fid+'/', function(data) { $("#requestComment").html(data);});
	setTimeout('statusClean()', 1000*2);	
}

function statusClean() {
	$('#status').fadeOut();
}

function reloadList() {
	$.post("/xReloadList/"+inout+"/"+cpage+"/",
		function(data) {
			$("#delMsgForm").html(data);
		}
	);
}

function pmsgShow() {
	visible = $("#pmsg").attr("style")=="display: none;" || $("#pmsg").attr("style")=="DISPLAY: none"?'n':'y';
	if(visible == 'y') {
		$("#pmsg").hide();
	}
	else {
		$("#pmsg").show();
	}	
}

function readMsg(id) {
	$.post("/xReadMsg/"+id,
		function(data) {
			$("#pmsgForm").html(data);
			$("#readMsg").show();
			$("#msgList").hide();			
		}
	);
}

function getBrunch(id) {
	$.post("/xGetBrunch/"+id,
		function(data) {
			$("#pmsg").hide();
			$("#brunch").html(data);
			$("#readMsg").show();
			$("#msgList").hide();
		}
	);
}

function setSearch() {
	var type = 0;
	$("input[id^=search_type]").each(function() {
		if($(this).attr('checked')) {
			type = $(this).attr('id').replace('search_type_', '');
		}
	});
	switch(type) {
		case '1':action='/search';break;
		case '2':action='/blogs/search';break;
		case '3':action='/files/search';break;
	}
	$("#searchForm").attr('action', action).submit();
	return false;
}
