// зебра
function odd(){
	$("table.odd tr:nth-child(odd), div.odd>div:nth-child(odd)").addClass("odd");
	$("table.odd tr:nth-child(even), div.odd>div:nth-child(even)").addClass("even");
}
function stake(id,bar,html,max){
	$.get("/ajax.php?p=stake&id="+id,function(data){
		data = parseInt(data);
		var per = data*100/max;
		
		if( per==100 && parseInt($(html).html())<data ){
			$("body").append("<div id='lotend'>Поздравляем! Ваша ставка стала последней. Порог активации розыгрыша пройден. Розыгрыш состоится через 3 часа.</div>");
			$("#lotend").dialog({
				title: "<span class='l ui-icon ui-icon-alert'></span> Поздравляем!",
				autoOpen:true,bgiframe:true,modal:true,resizable:false,
				buttons: {
					"OK": function() {
						$(this).dialog("close");
					}
				},
				close:function(){
					location.reload();
				}
			});
		}
		
		// у слайдера убавляем максимум, если есть новые ставки (не тестировал)
		if( parseInt($(html).html())<data ){
			$('#slider.slider_'+id).slider('option','max',$('#slider.slider_'+id).slider('option','max')-(data-parseInt($(html).html())) );
		}
		
		$(bar).progressbar("value",	per);
		
		$(html).html(data);
		setTimeout(function(){stake(id,bar,html,max)},5000);
	});
}

$(function(){
	// rub
	$(".rub>div>div").hide();
	$(".rub h2").click(function(){
		$(this).parent().toggleClass("open");
		$(this).children("span").toggle();
		$(this).parent().children("div").children("div").toggle("blind",250);
	});
	
	// lots
	$("#llots .lot:not(:last-child)")
		.hover(function(){
			if($(this).attr("animate")!=1){
				$(this)
					.addClass("hover")
					.attr("zindex",$(this).css("z-index"))
					.attr("animate",1);
				$(this).animate({"left": "-=133px"}, 250, function(){
					if($(this).hasClass("hover")) $(this).css("z-index",10)
					$(this).animate({"left": "+=133px"}, 250, function(){
							if(!$(this).hasClass("hover")) $(this).css("z-index",$(this).attr("zindex"))
							$(this).attr("animate",0);
						})
				});
			}	
		},function(){
			$(this).removeClass("hover")
			if($(this).attr("animate")!=1){
				$(this)
					.attr("animate",1)
					.animate({"left": "-=133px"}, 250, function(){
						$(this)
							.css("z-index",$(this).attr("zindex"))
							.animate({"left": "+=133px"}, 250, function(){
								$(this).attr("animate",0);
							})
					})
			}
		});
	
	/// style
	$("table.tech tr td:first").css("font-weight","bold");
	odd();
	$(".ui-state-default").hover(
		function(){$(this).addClass("ui-state-hover")},
		function(){$(this).removeClass("ui-state-hover").removeClass("ui-state-active")}
	)
	$("#cntrs").fadeTo(0,0.3).hover(function(){
		$(this).stop().fadeTo(300,1);
	},function(){
		$(this).stop().fadeTo(1000,0.3);
	});
	
	
	
	$("#myraffles").dialog({
		autoOpen:false,bgiframe:false,modal:false,resizable:false,width:480,title:unescape("%u041C%u043E%u0438%20%u043D%u043E%u0432%u044B%u0435%20%u0440%u043E%u0437%u044B%u0433%u0440%u044B%u0448%u0438")
	});
		
	$("#account a[href$='/myraffles/']").click(function(){
		var ico = $(this).children(".ui-icon");
		ico.addClass("waitblack");
		$.get("/profile/myraffles/",function(data){
			$("#myraffles").html(
				"<table width='100%' class='odd'>"+
				//"<tr><th></th><th>Лот</th><th>Ставок</th><th>Всего вложено</th><th>Шанс выигрыша</th></tr>"+
				data+
				"</table>"
			).dialog("open");
			if($("#myraffles tr").length-1<parseInt($("#myrafcnt").html())){
				$("#myraffles").append("<div align='center'><a href='/profile/myraffles/'>"+unescape("%u043F%u043E%u043A%u0430%u0437%u0430%u0442%u044C%20%u0432%u0441%u0435%20%u043C%u043E%u0438%20%u0440%u043E%u0437%u044B%u0433%u0440%u044B%u0448%u0438")+"</a></div>");
			}
			odd();
			ico.removeClass("waitblack");
		});
		
		return false;
	});
	

	var freg = function(){
		$(".ui-dialog button").attr("disabled","disabled").addClass("ui-state-disabled");
		$(".ui-dialog-title .ui-icon").addClass("wait");
			
		$.post("/reg/", $("#regdiv form").serialize(), function(data){
			if(data=='ok'){
				$("#regdiv").dialog("close");
				$("#regdiv").html("Вы успешно зарегистрировались!<br/> Зайдите на Ваш E-mail для его подтверждения!").dialog({
					bgiframe:true,modal:true,resize:false,title:"Успешная регистрация",
					buttons: {
						"OK": function() {
							location.href = "/entry/";
							$(this).dialog("close");
						}
					}
				});
			} else {
				$("#regdiv").html(data);
				$("#regdiv form").submit(freg);
				$("#regdiv form input:submit").hide();
			}
			
			$(".ui-dialog button").attr("disabled","").removeClass("ui-state-disabled");
			$(".ui-dialog-title .ui-icon").removeClass("wait");
		});
		return false;
	};
	function regshow(){
		$("#regdiv form").submit(freg);
		$("#regdiv input:submit").hide();
		
		$("#regdiv").dialog({
			bgiframe:true,modal:true,resizable:false,width:480,title:"<span class='l ui-icon ui-icon-contact'></span> Регистрация",
			buttons: {
				"Отмена": function() {
					$(this).dialog("close");
				},
				"Регистрация": freg
			},
			close:function(){$(this).dialog("destroy");}
		});
	}
	$("a[href$='/reg/']").click(function(){
		if($("#regdiv").size()==0){
			$("body").append("<div id='regdiv' class='hide'></div>");
			$.get("/reg/?jscss",function(data){
				$("#regdiv").html(data);
				regshow();
			});
		} else {
			regshow();
		}
		return false;
	});
	$("#body a[href$='/entry/']").click(function(){
		$("#account").effect("pulsate",1000);
		$("#lgn").focus();
		return false;
	});
});