function enter(){
	var login = $("#login").val();
	var pass = $("#password").val();
	
	if (login && pass)
	{
		$.post('/user/auth', {login:login, pass:pass}, function(e){
			if (e){
				window.location.reload()
			} else {
				alert("Неверный логин или пароль!");
			}
		},'json');
	}
}

function remind_password()
{
	var email = $("#remind_email").val();
	
	if (email)
	{
		$.post("/user/remind_email", {email:email}, function(e){
			if (e.result)
			{
				alert("На вашу почту был выслан пароль!");
			} else {
				alert("Неверный email");
			}
			
		}, 'json');
	}
}


function init_paginator(target_class, return_result_div)
{
	
	
	$(document).ready(function(){
		
		
		$(target_class).find(".paginator").each(function(){
			
			$(this).find("a").each(function(){
				
				$(this).click(function(){
					var href = $(this).attr("href");
					$.get(href,"", function(e){
				
						$(return_result_div).html(e);
						
					});
					return false;
				});
			});	
		
		});
	});
}


$(document).ready(function(){

	window.setInterval(function(){
		
		var current_object = $(".promoblock").find(".shown");
		current_object.fadeOut(function(){
			$(this).addClass("hidden").removeClass("shown");
		})
		
		var next = current_object.index() + 1;
		
		
		if (current_object.index() == $(".promoblock").children().length - 1){
			$(".promoblock .promowrap").eq(0).show(function(){
				$(this).addClass("shown").removeClass("hidden");
			})
		} else {		
			
			current_object.next().show(function(){
				$(this).addClass("shown").removeClass("hidden");
			
			})
			
		}
		
		
		
		
	}, 4000);
	
});

function createMiniBubble()
{
	
	return $(document.createElement("div")).addClass("grayminibubble");
}

intervals = {};
function doOracle(object, event_id){
	var o = $(object);
	var bplace = o.parent().find(".bplace");
	clearInterval(intervals[event_id]);
	intervals[event_id] = window.setTimeout(function(){
		var team_a = o.parent().find(".team_a").val();
		var team_b = o.parent().find(".team_b").val();
			if ( (team_a.match(/\d/) && team_b.match(/\d/)) || (team_a.match(/^$/) && team_b.match(/^$/)) ){
				$.post("/oracle/do_oracle/"+event_id, {a : team_a, b: team_b}, function(e){
					
					var bubble = createMiniBubble();
					if (e.updated)				
						bubble.html("Обновлено " + team_a+ " : " + team_b );
					if (e.added)					
						bubble.html("Добавлено " + team_a+ " : " + team_b );	
					if (e.removed) {
						bubble.html("Oracle removed");
						bubble.css({ color: "orange"});
					}
					if (e.error) {
						bubble.html("Server err " + e.error);
						bubble.css({ color: "red"});
					}	
					bubble.appendTo( bplace );
					window.setTimeout(function(){
						bubble.fadeOut(function(){
							$(this).remove();
						})
					}, 1000);
					
				},'json');
		} 
	},700);
}


function doRegister()
{
	var email = $("#email").val();
	var display_name = $("#display_name").val();
	var pass = $("#pass").val();
	var repeat_pass = $("#repeat_pass").val();
	$(".err").html("");
	var err = false;
	if (!email.match(/^[-+.\w]{1,64}@[-.\w]{1,64}\.[-.\w]{2,6}$/)) {
		$(".email").html("Неверный e-mail");
		err = true;
	}
		
	if (!display_name) {
		$(".display_name").html("Ваше имя?");
		err = true;
	}
		
	if (!pass) {
		$(".pass").html("Введите пароль");
		err = true;
	} else {
		if (!repeat_pass) {
			$(".repeat_pass").html("Повторите ваш пароль");
			err = true;
		} else {
			if (pass != repeat_pass)
			{
				$(".repeat_pass").html("Введенные пароли не совпадают!");
				err = true;
			}
			
		}
	}
	if (!err)
	{
		$.post("/user/do_register",{
			email: email,
			display_name:display_name,
			pass:pass			
		}, function(e){
			if (e.error)
			{
				if (e.msg == "email")
					$(".email").html("E-mail уже есть в базе");
				if (e.msg == "display_name")
					$(".display_name").html("Такое имя уже есть в базе");
				
			}
			else {
				alert("Успешно! Следуйте инструкциям, указанным в письме!");
			}
		}, 'json');
	}
}

function upload_avatar()
{
	
	$('#avatar_form').ajaxSubmit({
		dataType : "json",
		success: function(e) { 
		  if (e.result == true) {
			  alert("Аватар успешно загружен!");
			  window.location.reload();
		  }
		  else
			  alert("Произошла непредвиденная ошибка!");
		}
	}); 

}

function remove_friend( id )
{
	if (window.confirm("Удалить из друзей?"))
	{
		$.get("/friendship/remove_friend/"+id, {}, function(){
			$(".remove_friend_link").hide();
			$(".add_friend_link").show();
			$("#this_is_my_friend").hide();
		})
	}
	
}


function reject_friendship( id )
{
	if (window.confirm("Отклонить дружбу?"))
	{
		$.get("/friendship/remove_friend/"+id, {}, function(){
			window.location.reload();
		})
	}
	
}

function confirm_friendship( id )
{
	if (window.confirm("Подтвердить дружбу?"))
	{
		$.get("/friendship/add_friend/"+id + "?confirm=1", {}, function(){
			alert("Пользователь добавлен в друзья!");
		})
	}
	
}

function add_friend( id )
{
	if (window.confirm("Добавить этого пользователя в друзья?"))
	{
		$.get("/friendship/add_friend/"+id, {}, function(){
			$(".add_friend_link").hide();
			$(".remove_friend_link").show();
			$("#this_is_my_friend").show();
		})
	}
}

function send_comment(object, comment_type, object_id )
{
	var text = $(object).parents(".comment_send").find("textarea").val();
	if (!text)
	{
		alert("Введите текст комментария");
	}
	$.post("/comment/add",{
		comment_type : comment_type,
		object_id : object_id,
		text: text
	}, function(e){
		$(object).parents(".comment_block").find(".comment_list .comment_listing").prepend(e);
		$(object).parents(".comment_send").find("textarea").val("");
	});
}


function iLikeDislikeState(obj)
{
	var o = $(obj);
	var active = false;
	
	var pid = o.parent().find('input[name="pid"]').val();
	var object_type = o.parent().find('input[name="type"]').val();
	
	
	
	var class_split = o.attr("class").split(" ");
	var type = class_split[0];
	if (class_split.length == 2)
		active = true;
	
	if (active)
	{
		o.removeClass("active");
		$.post("/attitude/igiveashit/"+pid+"/type/"+object_type, {}, function(){
			
		});
		return;
	}
	
	switch(type)
	{
		case "ilikeit":
			$.post("/attitude/ilikeit/"+pid+"/type/"+object_type, {}, function(){
				o.parent().find("div").removeClass("active");
				o.addClass("active");
			});
			
		break;
		case "idislikeit":
			$.post("/attitude/idislikeit/"+pid+"/type/"+object_type, {}, function(){
				o.parent().find("div").removeClass("active");
				o.addClass("active");
			});
		break;
		case "igiveashit":
			$.post("/attitude/igiveashit/"+pid+"/type/"+object_type, {}, function(){
				o.parent().find("div").removeClass("active");
				o.addClass("active");
			});
		break;
		
		
	}
}


