$(function(){
	// Диалог "сделать ставку" над блоком выше
	$("#newrate").hide();
	if($("#newrate").length>0){
		newrate_a = function(){
			$("#newrate").dialog("open");
			return false;
		}
		
		$("#newrate form").submit(function(){
			$(".ui-dialog button").attr("disabled","disabled").addClass("ui-state-disabled");
			$(".ui-dialog-title .ui-icon").addClass("wait");
			
			$("#rate").val($("#slider").slider('value'));
			$.post(location.href,$("#newrate").children("form").serialize(),function(data){
				if(data=="ok"){
					$(".youshans").html($("#rateshans span").html()).effect("highlight");
					$("#newrate").dialog("close");
				} else {
					if($("#newrate_msg").length==0){
						$("#newrate").prepend("<div id='newrate_msg' class='ui-state-error p4 m10 hide'></div>");
					}
					$("#newrate_msg").html(data).slideDown().wait(5000).slideUp();
				}
				$(".ui-dialog button").attr("disabled","").removeClass("ui-state-disabled");
				$(".ui-dialog-title .ui-icon").removeClass("wait");
			});
			$("#newrate_a").attr("href",location.href+"?#newrate").unbind('click',newrate_a).click(function(){window.location.reload()});
			return false;
		});
		$("#newrate")
			.dialog({
				title: "<span class='l ui-icon ui-icon-cart'></span> Сделать ставку",
				autoOpen:false,bgiframe:true,
				width:420,height:350,resizable:true,
				modal: true,
				buttons: {
					"Отмена": function() {
						$(this).dialog("close");
					},
					"Сделать ставку": function(){$("#newrate form").submit()}
				},
				close:function(){
					$("#newrate_msg").remove();
				}
			});
		
		$("#newrate_a").click(newrate_a);
		
		$(window).load(function(){
			var curl = location.href;
			curl = curl.substr(curl.indexOf('#') + 1);
			if(curl=="newrate") $("#newrate_a").click();
		});
		
	} else {
		$("#newrate_a").hide();
	}
	
	
	// comments
	$("form#comment").submit(function(){
		if($("form#comment textarea").val().length>2&&$("form#comment textarea").val().length<=1000){
			$.post(location.href,$("form#comment").serialize(),function(data){
				if(data=="ok"){
					$(".mes").prepend("<div class='hide'><div class='mes-h'><span class='l ui-icon ui-icon-comment'></span><b>"+$("#p_nick").html()+"</b></div><div class='mes-t'>"+$("#comment textarea").val()+"</div></div>");
					$(".mes>.hide").slideDown(function(){$(this).effect("highlight")}).removeClass("hide");
					$("form#comment textarea").val("");
				} else alert(data);
			})
		} else alert("Комментарий должен быть от 3 до 1000 символов!");
		return false;
	});
	$("form#comment textarea").focus(function(){
		$("form#comment .ui-state-highlight").slideDown();
	});
	$("form#comment textarea").blur(function(){
		$("form#comment .ui-state-highlight").slideUp();
	});
	$("form#comment textarea").keyup(function(){
		var d = $(this).val();d = d.length;
		$("form#comment .ui-state-highlight span").html("(осталось <b>"+(1000-d)+"</b>)");
	});
	
	
	$("form#comment .ui-icon-cancel").click(function(){
		$("form#comment textarea").val("");
		return false;
	});
	$("form#comment .ui-icon-check").click(function(){
		$("form#comment").submit();
		return false;
	});
	
	
	/// list
	
	$("a.remind").click(function(){
		ths = $(this);
		if(ths.hasClass("pp")){
			ths.animate({width:"16px","margin-left":"148px"}).children(".ui-icon").addClass("wait");
		} else {
			ths.animate({width:"16px","margin-left":"100px"}).children(".ui-icon").addClass("wait");
		}
		var id = parseInt(ths.attr("href"));
		$.post($(this).attr("href"),"remind="+id,function(data){
			if(data=="ok") ths.fadeOut();
		});
		return false;
	});
	
	if($(".nullballans").length>0){
		$(".nullballans").dialog({
			title: "<span class='l ui-icon ui-icon-alert'></span> Недостаточно средств для ставки",
			autoOpen:true,bgiframe:true,modal:true,
			width:400,height:120
		});
	}
});