/**
 * uid - ID юзера, которого мы хотим зафрендить/расфрендить
 * act: 1 - добавить в друзья, 2 - удалить
 * callback - коллбэк AJAX-запроса к /ajax/friend
 */
function Friend(uid, act, callback) {
	var msg;
	if(act == 1) msg = "Добавить пользователя в друзья?";
	else if(act == 2) msg = "Удалить пользователя из списка друзей?";
	else return false;

	if(!confirm(msg)) return false;

	$.getJSON("/ajax/friend/", {uid: uid, act: act}, function(data) {
		if(data.code >= 100) {
			alert(data.msg);
			return false;
		}
		(callback)(data);
		return true;
	});

	return true;
}

function CreateGIcon() {
	Icon = new GIcon(G_DEFAULT_ICON);
	Icon.image = "/i/gico.png";
	Icon.iconSize = new GSize(25, 36);
	Icon.shadow = "/i/gico_shadow.png";
	Icon.shadowSize = new GSize(35, 36);
	Icon.iconAnchor = new GPoint(11, 34);

	return Icon;
}

/*
	Хайлайтнутый маркер
*/
function CreateGIcon_em() {
	Icon = new GIcon(G_DEFAULT_ICON);
	Icon.image = "/i/gico_em.png";
	Icon.iconSize = new GSize(25, 36);
	Icon.shadow = "/i/gico_shadow.png";
	Icon.shadowSize = new GSize(35, 36);
	Icon.iconAnchor = new GPoint(11, 34);

	return Icon;
}


function GotoCity(city_id) {
	city_id = parseInt(city_id);
	if(city_id > 0) location.href = "http://vdolevke.ru/cities/" + city_id + "/";
	return false;
}

function chCity() {
	GotoCity($("#CitySelect").val());
}

function Attract(obj) {
	obj.addClass("attract")
	setTimeout(function() {
		obj.focus();
		setTimeout(function(){obj.removeClass("attract");}, 200);
	}, 100);

	return false;
}

function Abuse(elem, area, id) {
	var requestText = "Укажите, пожалуйста, причину жалобы.";
	var $elem = $(elem);
	var $abuseForm = $('#abuse_form');
	var $abuseError = $abuseForm.find('#abuse_error');
	
	if($abuseForm.length == 0) {
		$('body').append('<div id="abuse_form"><div id="abuse_error">sdf</div><input type="hidden" name="area" value="'+area+'" /><input type="hidden" name="id" value="'+id+'" /><textarea>'+requestText+'</textarea><div><a href="#">Отменить</a><input type="button" value="Отправить" /></div><div id="abuse_note">Жалобы без указания причины не рассматриваются администрацией сайта.</div></div>');
		$abuseForm = $('#abuse_form');
		$abuseError = $abuseForm.find('#abuse_error');
		
		$abuseForm.find("input[type='button']").click(function() {
			var reason = $abuseForm.find('textarea').val();
			var area = $abuseForm.find("input[name='area']").val();
			var id = $abuseForm.find("input[name='id']").val();			
			
			if(reason == requestText || reason == '')
				$abuseError.html(requestText).show();
			else {
				$.post("/ajax/abuse/", {area: area, id: id, reason: reason}, function(data) {
					if(data == "OK") {
						alert("Модератору отправлено сообщение.");
					} else {
						alert(data);
					}
				});
				
				$abuseForm.hide();
				$abuseError.html('').hide();
				$abuseForm.find('textarea').css({color:'#999'}).val(requestText);
			}
		});
		
		$abuseForm.find("a").click(function() {
			$abuseForm.hide();
			$abuseError.html('').hide();
			return false;
		});
		
		var $abuseReason = $abuseForm.find('textarea');
		
		$abuseReason.focus(function() {
			if($(this).val() == requestText)
				$(this).css({color:'#000'}).val('');
		});
		
		$abuseReason.blur(function() {
			if($(this).val() == '')
				$(this).css({color:'#999'}).val(requestText);
		});				
	}
	
	var coords = $elem.offset();
	$abuseForm.find("input[name='area']").val(area);
	$abuseForm.find("input[name='id']").val(id);
	$abuseForm.show();
	$abuseError.html('').hide();
	$abuseForm.css({top:coords.top+$elem.height()+5, left:coords.left-($abuseForm.width()-$elem.width())-10});
	
	return false;
}

function Favore(area, id, act) {
	$.post(
		"/ajax/favore/",
		{area: area, id: id, act: act},
		function(data) {
			data = eval ("(" + data + ")");
			alert(data.msg);
			if(data.code==1 && act==2){
				document.getElementById("FF_Post_"+id).style.display="none";
			}
		}
	);
	return false;
}
function Law_Post(id, c){
	if(c == 3)	if(!confirm('Вы не будете участвовать в этом обсуждении?'))	return false;
	if(c == 2)	if(!confirm('Вы хотите покинуть это обсуждение?'))	return false;
	$.post('/ajax/law_to_post/', {id : id, confirm: c}, function(data){if(data.ok == 1){alert(data.txt);window.location.reload();}}, "json");
	return false;
}

function prr(x) { if (typeof(x) == 'object') {	var str = "Object {\n"; for (var i in x) str = str + i + ' => ' + prr(x[i]) + "\n"; str = str + "}\n"; return str; } else return x; }

$(function() {
    $("#city_").autocomplete("/ajax/cities/", {
        formatItem: function (row, i, n, needle) {
            return row[0] + "<br /><small>" + row[1] + "</small>";
        }
    }).keypress(function(e) {
        if(e.which == 13) {
            $("#CityOther_").submit();
            return false;
        }
        else return true;
    }).result(function(event, data, formatted) {
        $("#city_id_").val(data[2]);
    });
    
    $("a.blocked_cmt_more").click(function(evnt){
		evnt.preventDefault();
		$this = $(this);
		$this.hide(); 
		$this.parent().find('.blocked_cmt').addClass('blocked_cmd_show');
	});
	
    //$('#panasonic_link').click(function(){ $.post('/ajax/aad/panasonic/');	})
	//$('#IntroLinks .adsber a').click(function() {$.post('/ajax/sber_click/');});
	//$('.SBIntroLinks a').click(function() {$.post('/ajax/sber_click/');});
});
