(function($) {
	$.game = {
		delay: 2000,	
		_window_is_open:0,
		current_answer:0,
		wr_timer: 0,
		game_finish_try:0,
		game_finish_exit:0,
		game_was_started:0,
		game_begin_try: 0,
		game_begin_exit: 0,
		is_begin:0,
		reload_glist_time: 10,
		waiting_game_time: 10,
		counter_time: 1,
		fast_enter_class:"",
		pl_text:"",
		newWin:null,
		q_data:null,
		stack:null,
		qiterator: 0,
		open_window: function() {

			if($.game._window_is_open!=1)
			{
				$.game._window_is_open = 1;
//				$.refresher.destroy({copy:2});
//				$.refresher.destroy({copy:1});

				$('#basicModalContent').modal(
				{
					onOpen: function (dialog) {
						dialog.overlay.show();
						dialog.container.show();
						dialog.data.show();
//						dialog.overlay.slideDown('fast', function () {
//							dialog.container.slideDown('fast', function () {
//								dialog.data.fadeIn('fast'); // See Other Notes below regarding
//							});
//						});
					},

					onClose: $.game.close_modal,
					onRoll: $.game.roll_modal
				});
				//$('.modalCloseImg').hide();
				$('.modalRollImg').hide();
				
				}
		},
		close_modal: function (dialog) {
			dialog.overlay.hide();
			dialog.container.hide();
			dialog.data.hide();

	$.game._window_is_open = 0;
						$.modal.close();
						
//			dialog.data.fadeOut('slow', function () {
//
//				dialog.container.slideUp('slow', function () {
//					dialog.overlay.slideUp('slow', function () {
//
//						$.game._window_is_open = 0;
//						$.modal.close();
//
//					});
//				});
//			});
		},
		init_game: function()
		{
			$.game.open_window();
			$.game.notify("Добро пожаловать на демо версию игры! <br><br>Ваша ставка 300 руб.");
			setTimeout('$.game.notify("1 уровень вопросов.")',$.game.delay*2);
			setTimeout('$.game.create_game()',$.game.delay*3);
		},
		create_game: function() {
			$.game.open_window();
			$.getJSON(base_url+'spguestgame/create/',function(data){
				$.game.log('Соединился с сервером.');
				
				//$('#basicModalContent').append('<h3>'+data.s+" "+lang.jstat[data.s]+'</h3>');
				$.game.q_data = data;
				$.game.handle_status(data);
			});
		},
		handle_status: function (param)
		{
			$.game.play_game();
		},
		show_result: function()
		{
			$.game.log('show_result');
			$('#basicModalContent').html('');
			clone0 = $('#sr_blank').clone();

			clone0.find("#rtable").attr('id','res_table');
			clone0.find("#winner_blank").attr('id','winner_login');
			clone0.appendTo('#basicModalContent');


			clone0.show();

				

		},
		play_game:function()
		{


				//			game_form_blank
				clone = $('#game_form_blank').clone();
				clone.attr('id','gwindow');
				clone.find("#quest_blank").attr('id','question');
				clone.find("#active_answer_r").attr('id','active_answer');
				clone.find("#game_timer_blank").attr('id','game_timer');
				clone.find("#counter").attr('id','game_notify');
				clone.find("#prev_blank").attr('id','prev');
				clone.find("#next_blank").attr('id','next');
				clone.find("#qnum_blank").attr('id','qnum').addClass("qnumber");
				clone.find("#ma_blank").attr('id','make_answer');
				


				$('#basicModalContent').html('');
				clone.appendTo('#basicModalContent');
				clone.show();

				
				$.game._q_filling();
				//$("#question").bind('click',$.game.disactivate_input());
				//$("#active_answer").bind('click',$.game.activate_input());
				setTimeout("$.game.activate_input()",100);
			
		},
		activate_input: function()
		{
		
				inpelem = $("#active_answer");
				inpelem.blur(function(){inpelem.focus()});
			 	inpelem.focus();
			 	
			 	
			 	inpelem.keyup(function(event) {
			 		
			 	if (event.keyCode == 13){
			 		$.game.send_answer();
			 		}
			 	}).click(function() {inpelem.focus()});
		},
		fill_partial_result: function (data)
		{
			$('.atx').html(data.atx);
		},
		fill_question: function(data)
		{
			$.game.qiterator++;
			$.game.fill_partial_result(data);
			$.game.animate_atx();
			
			if (data.finish==1)
			{
				setTimeout('$.game.notify("Итого выиграно: <b>+1800 руб.</b>")',$.game.delay);
				
			}else{
				
			
//			
//			if(data.end == 1)
//			{
//				$.game.show_result();
//				return 0;
//			}
				$.game.q_data = data;
				if ($.game.qiterator == 3)
				{
					setTimeout('$.game.notify("2 уровень вопросов.")',$.game.delay);
					setTimeout("$.game.play_game()",$.game.delay*2);
				}else if ($.game.qiterator == 6)
				{
					setTimeout('$.game.notify("3 уровень вопросов.")',$.game.delay);
					setTimeout("$.game.play_game()",$.game.delay*2);
				}else
				{
					setTimeout("$.game._q_filling()",$.game.delay);
				}
			}
		},
		_q_filling: function()
		{
			$("#active_answer").val("");
			data = $.game.q_data;
			$('#question').html(data.q);
			$('#qnum').html(data.o+".");
			$('.atx').html(data.atx);
			$.game.act_but();

		},
		disact_but: function()
		{
			$('#make_answer').unbind('click').css({'opacity':0});
			
		},
		act_but: function()
		{

			$('#make_answer').bind('click', function() {
				$.game.send_answer();
			}).css({'opacity':1});
			$("#active_answer").focus();
		},
		unique_number: function ()
		{
			return (new Date()).getTime();
		},
		send_answer: function()
		{
			if (!$('#active_answer').val())
			{
				alert('Ошибка! Введите ответ!');
			}else
			{
				$.game.disact_but();
				$.post(base_url+'spguestgame/answer/',{answer:$('#active_answer').val()},function(data)
				{
					if (data.q!=null || data.finish==1)
					{
						$.game.current_answer = 0;
						
						$.game.fill_question(data);
						
					}
					else
					{
						$.game.act_but();
						$.game.animate_wrong_answer();	
					}
					
					

				},"json");
			}

		},
		animate_wrong_answer: function ()
		{
			//alert("Wrong answer");
			
			$.game.highlight('active_answer',"#ff6666", 'div');
			//$("#active_answer").css({"background-color": "#ff0000"});
		},
		animate_atx: function ()
		{
			$.game.highlight('game_notify',"#ff6666", 'span');
			//$(".atx").animate({"color":"red"});
			//alert($(".atx").text());
			//$("#active_answer").css({"background-color": "#ff0000"});
		},
		highlight: function(id,color,conttype)
		{
			var new_id = "hl_"+id;

			if (!$("#"+new_id).is(conttype))
			{
				$("#"+id).wrap('<'+conttype+' id="'+new_id+'"></'+conttype+'>');
				$("#"+new_id).css({'background': color, width:$("#"+id).width(), height: $("#"+id).height()});
				$("#"+new_id).addClass("ansinputfix");
			}else
			{
				$("#"+new_id).css('background', color);
			}
				

			$("#"+id).animate({opacity:"0.1"},"fast")
			.animate({opacity:"1"},"fast")
			.animate({opacity:"0.1"},"fast")
			.animate({opacity:"1"},"fast",function(){$("#"+new_id).css('background','transparent')});
			//$("#"+new_id).css({'background': color, width:$("#"+id).width(), height: $("#"+id).height()});
		},			
		notify:function(message)
		{
			$('#basicModalContent').html("<h2 class='notify'>"+message+"</h2>");
		},
		log: function(data)
		{
			$(".console").append("<li>"+data+"</li>");
			return this;
		}

	};

})(jQuery);
