$(function(){

	$('.emptytic').click(function(){
		if($('#urur').attr("value")) {
			if($('#urur').attr("value") < 1){
				alert('Недостаточно средств на балансе аккаунта.');
				return false;
			}
			
			qwest=window.confirm('Вы действительно хотите купить билет Клевер Клуба? ');
			if(qwest){
				$.ajax({
					url: 'index.php',
					type: 'POST',
					data: 'module=game&action=btick&t='+$(this).text(),

					success: function(result) {
						
					}
				});
				$(this).toggleClass("redtic");
				$(this).toggleClass("emptytic");
			}
		}else{
			alert('Для того чтобы играть нужно зарегистрироваться.');
		}
	});
	
	$("#reg_form").dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,
		width: 454,
		height: 550,
		modal: true
	});
	
	$("#windows").dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,
		position: "top",
		width: 525,
		modal: true
	});
	
	$('#regist').click(function(){
		$("#reg_form").dialog("open");
		$('#reg_form').load('index.php?module=login&action=register&pop=1&cache='+Math.random());
	});
	
	$('#loadgrul').click(function(){
		$("#windows").dialog("open");
		$('#windows').load('index.php?module=game&action=rules&pop=1&cache='+Math.random());
	});
	
	$('#loadgstat').click(function(){
		$("#windows").dialog("open");
		$('#windows').load('index.php?module=game&action=gamestat&pop=1&cache='+Math.random());
	});
	
	$('#loadgset').click(function(){
		$("#windows").dialog("open");
		$('#windows').load('index.php?module=login&action=profile&pop=1&cache='+Math.random());
	});
	

	$('#but_next').click(function(){      
		
		var nextid = parseInt($('#race').attr("value"));
		if(nextid > (parseInt($('#mrace').attr("value"))-1)) {
			nextid = 0;
		}
		$('#race').val(nextid+1);
		
		$.ajax({
			url: 'index.php',
			type: 'GET',
			data: 'module=shadows&action=nextrace&sub=1&pop=1&race='+$('#race').attr("value"),

			success: function(result) {
				$('#bookpic').replaceWith(result);
			}
		});	
		
		$.ajax({
			url: 'index.php',
			type: 'GET',
			data: 'module=shadows&action=nextrace&sub=2&pop=1&race='+$('#race').attr("value"),

			success: function(result) {
				$('#book1zag').replaceWith(result);
			}
		});
			
		$.ajax({
			url: 'index.php',
			type: 'GET',
			data: 'module=shadows&action=nextrace&sub=3&pop=1&race='+$('#race').attr("value"),

			success: function(result) {
				$('#book1_text').replaceWith(result);
			}
		});
		
	});

	$('#but_beck').click(function(){    

		var nextid = parseInt($('#race').attr("value"));
		if(nextid < 2) {
			nextid = parseInt($('#mrace').attr("value"))+1;
		}
		$('#race').val(nextid-1);	
		
		$.ajax({
			url: 'index.php',
			type: 'GET',
			data: 'module=shadows&action=nextrace&sub=1&pop=1&race='+$('#race').attr("value"),

			success: function(result) {
				$('#bookpic').replaceWith(result);
			}
		});	
		
		$.ajax({
			url: 'index.php',
			type: 'GET',
			data: 'module=shadows&action=nextrace&sub=2&pop=1&race='+$('#race').attr("value"),

			success: function(result) {
				$('#book1zag').replaceWith(result);
			}
		});
			
		$.ajax({
			url: 'index.php',
			type: 'GET',
			data: 'module=shadows&action=nextrace&sub=3&pop=1&race='+$('#race').attr("value"),

			success: function(result) {
				$('#book1_text').replaceWith(result);
			}
		});
		
	});
	
	
	$("#but_cancel").bind("mouseenter",function(){
		$(this).toggleClass("but_cancel_in");
	}).bind("mouseleave",function(){
		$(this).toggleClass("but_cancel_in");
	});

	$("#but_ok").bind("mouseenter",function(){
		$(this).toggleClass("but_ok_in");
	}).bind("mouseleave",function(){
		$(this).toggleClass("but_ok_in");
	});
	
	$("#but_beck").bind("mouseenter",function(){
		$(this).toggleClass("but_beck_in");
	}).bind("mouseleave",function(){
		$(this).toggleClass("but_beck_in");
	});
	
	$("#but_next").bind("mouseenter",function(){
		$(this).toggleClass("but_next_in");
	}).bind("mouseleave",function(){
		$(this).toggleClass("but_next_in");
	});
	
	
	var mousein = 0;
	var toptext = 1;
	var step = 5;
	$("#begunok_top").bind("mouseenter",function(){
		mousein = 1;
	}).bind("mouseleave",function(){
		mousein = 0;
	});
	
	$("#begunok_buttom").bind("mouseenter",function(){
		mousein = 2;
	}).bind("mouseleave",function(){
		mousein = 0;
	});
	
	function OnTimer() {
		if(mousein==0) return;
		if(mousein==1) toptext=toptext-step;
		if(mousein==2) toptext=toptext+step;
		if(toptext<1) toptext = 1;
		
		$("#book1_text").scrollTop(toptext);
	
	}
	
	OnTimer();
	window.setInterval(OnTimer, 50);

	
	$('#but_ok').click(function(){   
		$.ajax({
			url: 'index.php',
			type: 'POST',
			async: false,
			data: 'module=shadows&action=checkerr&pop=1&name='+$('#o_pass').attr("value"),

			success: function(result) {
				if(result!='GOOOOD') {
					alert(result);
				} else {
					$("#createform").submit();
				}
			}
		});	
	});
	
	
	$('#taskdel1').click(function(){ 
		if($('#taskdel1').attr("title") != 'deleted') {
			qwest=window.confirm('Вы действительно хотите пометить на удаление тень с именем '+$('#taskdel1').attr("title")+' ?\nПосле этого тень будет удалена через 3 дня. ');
			if(qwest){
				$.ajax({
					url: 'index.php',
					type: 'POST',
					data: 'module=shadows&action=delshadow&pop=1&name='+$('#taskdel1').attr("title"),

					success: function(result) {
						$('#pics1').append('<img class="krest" src="images/gamegui/krest.png" alt="Удален" border="0"/>');
						$('#taskdel1').attr("title",'deleted');
					}
				});			
			}
		}
	});
	
	$('#taskdel2').click(function(){ 
		if($('#taskdel2').attr("title") != 'deleted') {
			qwest=window.confirm('Вы действительно хотите пометить на удаление тень с именем '+$('#taskdel2').attr("title")+' ?\nПосле этого тень будет удалена через 3 дня. ');
			if(qwest){
				$.ajax({
					url: 'index.php',
					type: 'POST',
					data: 'module=shadows&action=delshadow&pop=1&name='+$('#taskdel2').attr("title"),

					success: function(result) {
						$('#pics2').append('<img class="krest" src="images/gamegui/krest.png" alt="Удален" border="0"/>');
						$('#taskdel2').attr("title",'deleted');
					}
				});			
			}
		}
	});
	
	$('#taskdel3').click(function(){ 
		if($('#taskdel3').attr("title") != 'deleted') {
			qwest=window.confirm('Вы действительно хотите пометить на удаление тень с именем '+$('#taskdel3').attr("title")+' ?\nПосле этого тень будет удалена через 3 дня. ');
			if(qwest){
				$.ajax({
					url: 'index.php',
					type: 'POST',
					data: 'module=shadows&action=delshadow&pop=1&name='+$('#taskdel3').attr("title"),

					success: function(result) {
						$('#pics3').append('<img class="krest" src="images/gamegui/krest.png" alt="Удален" border="0"/>');
						$('#taskdel3').attr("title",'deleted');
					}
				});			
			}
		}
	});
	
	$('#taskdel4').click(function(){ 
		if($('#taskdel4').attr("title") != 'deleted') {
			qwest=window.confirm('Вы действительно хотите пометить на удаление тень с именем '+$('#taskdel4').attr("title")+' ?\nПосле этого тень будет удалена через 3 дня. ');
			if(qwest){
				$.ajax({
					url: 'index.php',
					type: 'POST',
					data: 'module=shadows&action=delshadow&pop=1&name='+$('#taskdel4').attr("title"),

					success: function(result) {
						$('#pics4').append('<img class="krest" src="images/gamegui/krest.png" alt="Удален" border="0"/>');
						$('#taskdel4').attr("title",'deleted');
					}
				});			
			}
		}
	});
	
	
	
});
