var person_id;
var input_work=false;

$(document).ready(function()
{
	session_ping();

        $(".furrghtd").live("mouseup", function () { MyOtziv.mo_show_box(); });
	
	/*if ($.browser.mozilla)
	{
		document.addEventListener('mousemove', function(event)
			{
				if (moved)
				{
					//alert(event.target.nodeName.toLowerCase());
				}
				if ((event.target.nodeName.toLowerCase() == 'img')||(event.target.nodeName.toLowerCase() == 'a')||(event.target.nodeName.toLowerCase() == 'div'))
				{
					event.preventDefault();
					return false;
					//event.stopPropagation(); return false;
				}
			}, true);
	}*/
	
	$("._a").live("click", function ()
	{
		_do($(this).html());
		window.location=$(this).attr("href");
	});
	
	$(window).unload(function ()
	{
		_do("<font color=\"#000000\">close</font>");
	});
	
	$(".gb_a").click(function ()
	{
		page_pop('gb.php');
	});
	
	$(".person_mes_a").live('click', function ()
	{
		page_pop('person_mes.php');
	});
	
	$(".person_dann_a").live('click', function ()
	{
		page_pop('person_data.php');
	});
	
	$(".about_a").live('click', function ()
	{
		page_pop('about.php');
	});
	
	$(".contacts_a").live('click', function ()
	{
		page_pop('contacts.php');
	});
	
	$(".person_exit_a").live('click', function ()
	{
		window.location='index.php?exit=1';
	});
	//$(".click_select").live("click", function () { $(this).select()=true; });
	
	// vals
		$(".vals").change(function ()
		{
			var _val;
			if ($(this).is("[type=checkbox]"))
			{
				if ($(this).is(":checked"))
				{
					_val=$(this).val();
				}
				else
				{
					_val='';
				}
			}
			else
			{
				_val=$(this).val();
			}
			
			$.get("vals.php", {name:$(this).attr("name"), val:_val}/*, function (text) { alert(text); }*/);
		});
	// vals End
	
	// Start
		if ((0)&&(person_id==0)&&(stat_user_new==1))
		{
			var _start_text=$("<div style=\"padding:25px;\"><h1>Что это такое?</h1><div class=\"div\" style=\"font-size:13px;\">Под этим сообщением находится бесконечная плоскость, которую Вы можете перемещать мышкой и рисовать в любом ее свободном месте.</div><div class=\"div\"><strong>Вы можете делать свои рисунки ссылками.</strong></div><div class=\"div\">&nbsp;</div><a href=\"javascript: close_start();\">Закрыть это сообщение</a></div>").click(function () { $(_start).remove(); });
			var _start=$("<div class=\"start\"></div>").prepend(_start_text);
			$("body").append(_start).click(function () { $(".start").remove(); });
			//$(_start).find(".start_a").click(function () { $(_start).remove(); });
		}
	// Start End
	
	// Panel
		$("#info_pred").live("click", function ()
		{
			var _div=$(this).closest(".left_panel_info").load_100();
			$.get("left_info.php?n="+(Number($(_div).attr("_n"))-1), function (ret)
			{
				$(_div).html(ret);
			});
		});
		$("#info_next").live("click", function ()
		{
			var _div=$(this).closest(".left_panel_info").load_100();
			$.get("left_info.php?n="+(Number($(_div).attr("_n"))+1), function (ret)
			{
				$(_div).html(ret);
			});
		});
	// Panel End
	
	// input drag
		$(".input").live("mousedown", function ()
		{
			input_work=true;
		}).live("mouseup", function ()
		{
			input_work=false;
		});
	// input drag End
	
	// Page Close
		$(".page_close").live("click", function () { page_close(); });
	// Page Close End
	
	// Coord
		$("#coord_x").add("#coord_y").keyup(function (event)
		{
			if (event.keyCode==13)
			{
				$("#coord_form").submit();
			}
		});
	// Coord End
	
	// Auth
		$("#auth_form").auth_form();
	//Auth End
	
	// Register
		$("#register_a").click( function ()
		{
			page_pop("register.php", function ()
			{
				$(_page).find("form").submit(function ()
				{
					if ($("#register_pass").val()!=$("#register_pass2").val())
					{
						$("#form_register_pass").attr("class", "forma_err");
						$("#form_register_pass2").attr("class", "forma_err");
						alert("Пароли не совпадают");
						return false;
					}
				});
			});
			return false;
		});
	// Register
	
	// Panel
		$("#panel").panel();
	// Panel End
});

function close_start()
{
	$(".start").remove();
}


// Auth Form
	jQuery.fn.auth_form=function ()
	{
		$(this).each(function ()
		{
			$(this).submit(function ()
			{
				if (($("#auth_user_mail").val()=='')||($("#auth_user_pass").val()==''))
				{
					alert(lan("auth_form_err"));
				}
				else
				{
					//$("#panel_person").load_100();
					$.post("auth.php", $(this).serialize(), function (ob)
					{
						$("#panel_person").html(ob.html).find("#auth_form").auth_form();
						if (ob.sc!=undefined)
						{
							eval(ob.sc);
						}
					}, "json");
				}
				return false;
			});
		});
		
		return this;
	};
// Auth Form End

function session_ping()
{
	if (person_id!=0)
	{
		$.getJSON("session_ping.php", function (ob)
		{
			if (ob.person_mes_num!=undefined)
			{
				$("#person_mes_num").html(ob.person_mes_num);
			}
		});
		setTimeout("session_ping()", 1000*60);
	}
}

function _do(str)
{
	$.get("_do.php?str="+escape(str));
}

function _alert(params)
{
	params=jQuery.extend({title:'', text:''}, params);
	
	var win=$("<div class=\"foto_drag_div\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">      <tr>        <td class=\"foto_t_l\"><img src=\"box/spacer.gif\" width=\"3\" height=\"1\" /></td>        <td class=\"foto_t\"><div class=\"foto_drag_zag\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr><td class=\"foto_drag_zag_td\" nowrap=\"nowrap\">"+params.title+"</td><td class=\"foto_drag_zag_td\" nowrap=\"nowrap\" align=\"right\"><img src=\"img/foto_close.jpg\" border=0 width=16 height=16 class=\"foto_close\" align=\"absmiddle\" /></td></table></div></td>        <td class=\"foto_t_r\"><img src=\"box/spacer.gif\" width=\"4\" height=\"1\" /></td>      </tr>      <tr>        <td class=\"foto_l\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>        <td class=\"page_pop_center\">"+params.text+"</td>        <td class=\"foto_r\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>      </tr>      <tr>        <td><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>        <td><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">  <tr>    <td class=\"foto_b_l\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>    <td class=\"foto_b\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>  </tr></table></td>        <td class=\"foto_b_r\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>      </tr>    </table></div>");
	$(win).find("img.foto_close").click(function()
		{
			$(win).remove();
		}
	);
	$(win).find("img.foto_close").hover(
		function()
		{
			$(this).attr('src', 'img/foto_close1.jpg');
		},
		function()
		{
			$(this).attr('src', 'img/foto_close.jpg');
		}
	);
	page_i++;
	if (page_i>10) { page_i=1; }
	$(win).css('top', getBodyScrollTop()-30*page_i+body_h()/2);
	$(win).css('left', 30*page_i+body_w()/2-30);
	page_z_index++;
	$(win).css('z-index', page_z_index);
	$(win).bind('mousedown', function()
		{
			page_z_index++;
			$(win).css('z-index', page_z_index);
		}
	);
	$("body").prepend(win);
	$(win).draggable(/*{handle:".foto_t"}*/);
}

jQuery.fn.hide_show=function ()
{
	$(this).each(function ()
	{
		if ($(this).is(".hid")) { $(this).hide().removeClass("hid"); }
		if ($(this).is(":hidden"))
		{
			$(this).show();
		}
		else
		{
			$(this).hide();
		}
	});
	return this;
};

function get_menu(params)
{
	params=$.extend({hidden:false, text:"" }, params);
	
	var ret=$("<div class=\"menu_div\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">  <tr>    <td class=\"menu_t_l\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>    <td class=\"menu_t\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>    <td class=\"menu_t_r\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>  </tr>  <tr>    <td class=\"menu_l\">&nbsp;</td>    <td class=\"menu_c\"></td>    <td class=\"menu_r\">&nbsp;</td>  </tr>  <tr>    <td class=\"menu_b_l\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>    <td class=\"menu_b\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>    <td class=\"menu_b_r\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" /></td>  </tr></table></div>");
	$(ret).find(".menu_c").append(params.text);
	if (params.hidden)
	{
		$(ret).hide();
	}
	return ret;
}