jQuery(function($)
{
	if(getCookie('usr_mail') != null)
	{
		updateMsgUnread();
	}
	
	// смена статуса
	$("#user-status-change").click(function()
	{
		$("#user-status .val").hide();
		$(this).hide();
		$("#user-status-save, #user-status .mod").show();
		return false;	
	});

	$("#user-status-save").click(function()
	{
		$("#user-status-mod").attr('disabled', 'disabled');
		$.getJSON("/code/ajax/user.php", { 'cmd': 'status.set', 'val': $("#user-status-mod").val() }, function(e)
		{
			if (e.success)
			{
				//console.log(empty(e.val));
				$("#user-status .val").html(e.val != '' ? e.val : $("#user-status .def").html()).show();
				user_status_short = $("#user-status .val").html();
				if (e.val != '')
				{
					$("#user-status .val-full").html(e.val_full);
					$("#user-status-mod").val(e.val_full);
				}
				else
				{
					$("#user-status .val-full").html($("#user-status .def").html());
					$("#user-status-mod").val('');
				}
				$("#user-status-save, #user-status .mod").hide();
				$("#user-status-change").show();
			}
			else
			{
				// обработка ошибки
			}
			$("#user-status-mod").removeAttr('disabled');
		});
		return false;	
	});
	
	// показать весь статус при наведении
	var user_status_short = $("#user-status .val").html();
	$("#user-status").hover(function()
	{
		$("#user-status .val").html($("#user-status .val-full").html());
	},
	function()
	{
		$("#user-status .val").html(user_status_short);
	});
	
	Shadowbox.init(
	{
		handleOversize: 'none',
	}
	);
});

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function retrievePassword() {
	$("a#remindA").blur();
	$("small#remindLink").hide();
	$("small#remindAnswer").hide();
	$("small#remindLoading").show();
	$.getJSON("/code/ajax/sendPassword.php",{
		loginEMail: $("input#loginEMail").val()
	}, function(data){
		$("small#remindLoading").hide();
		if( data.ok ){
			$("small#remindAnswer").html("<br /><br />" + data.answer);
			$("small#remindAnswer").show();
		} else {
			$("small#remindAnswer").html("<br /><br />" + data.answer);
			$("small#remindLink").show();
			$("small#remindAnswer").show();
		}
		
	});
}

function updateMsgUnread() {
	$.get("/code/ajax/getMessagesUnread.php", function(html){
		if( html == "" ) {
			$("img#icoMsg").attr("src", "/images/icoMsgClosed.gif");
			$("#msgUnreadCount").html("Мои сообщения").parent("b").parent("u").parent("a").css("color", "#4B83DC");
		} else {
			$("img#icoMsg").attr("src", "/images/icoMsgAni.gif");
			$("#msgUnreadCount").html("<b>Новые сообщения " + html + "</b>").parent("b").parent("u").parent("a").css("color", "red");
			
		}
	});
	window.setTimeout('updateMsgUnread()', 180000);
}

function TrimString( sInString ){
	sInString = sInString.replace(/ /g,' ');
	return sInString.replace(/(^\s+)|(\s+$)/g, "");
}

function confirmation(sConfirmation) { 
	var ans = window.confirm(sConfirmation);
	return ans; 
}

function disable_messagesWriteSubmit() {
	$("#messagesWriteSubmit").attr('disabled',true);
	//$("form#messagesWrite").submit();
}

function userRate( destID, type ) {
	if ( type == 0 ) {
		rate = -1;
	} else {
		rate = 1;
	}
	$("#userRating").css("color", "#ccc");
	$.post("/code/ajax/addRateUser.php",{
		idMesta: destID,
		iBall: rate
	},function(){
		$("#userRating").load("/code/ajax/getRateUser.php?id="+destID, function(){
			$("a.rate").hide();
			$("#userRating").css("color", "#000");
		});
	});
	$("a.rate").blur();
}

function addPhotoSend() {
	$("form#addPhoto").ajaxSubmit(function(answer){
		alert(answer);
	});
	return false;
}

function addPhotoFileAddField() {
	$("#addPhotoFileFields").append("<input type=\"file\" name=\"addPhotoFile[]\" id=\"addPhotoFile[]\" size=\"40\" /><br /><br />");
}

function messagesWriteSubmitTest(sender)
{
	sender.attr('disabled', 'disabled');
	if ($("#captcha").length)
	{
		$.getJSON("/code/ajax/security.php", { 'cmd': 'check', 'val': $("#captcha").val() }, function(e)
		{
			sender.removeAttr('disabled');
			if (e.success)
			{
				$(".red").html('');
				$("#messagesWrite").submit();
			}
			else
			{
				$(".red").html('<div>'+e.error+'</div>');
				$("#captcha-img").attr('src', '/captcha.php?r=' + Math.random());
				$("#captcha").val('');
			}
		});
	}
	else
	{
		$("#messagesWrite").submit();
	}
	return false;
}
