jQuery.fn.forma=function ()
{
	$(this).each(function ()
	{
		var _form=this;
		
		$(this).find(".input").focus(function ()
		{
			$(this).addClass("input_focus");
		}).blur(function ()
		{
			$(this).removeClass("input_focus");
		});
		
		// Kod2
			var _kod2=$(this).find(".kod2");
			if ($(_kod2).length)
			{
				$(_kod2).val($(_kod2).attr('_kod'));
			}
		// Kod 2 End
		
		// Input Max
			$(this).find(".input_max").input_max();
		// Input Max End
		
		// Input Vipad
			$(this).find(".input_vipad").input_vipad();
		
		// Data
			if ($.fn.attachDatepicker)
			{
				$(this).find(".input[tip=data]").attachDatepicker();
			}
		// Data End
		
		// File
			if ($.fn.input_file)
			{
				$(this).find(".input_file").input_file();
			}
		// File End
		
		// Tini
			if ($.fn.input_tini)
			{
				$(this).find(".tini").input_tini();
			}
		// Tini End
		
		// Submit button
			$(this).find(".submit_hover").hoverClass("submit_over");
			$(this).find(".submit").hoverClass("submit_over").each(function ()
			{
				if (($.browser.msie)&&($(this).width()<150))
				{
					$(this).width(150);
				}
			});
		// Submit button End
		
		// Cancel button
			$(this).find(".cancel").hoverClass("cancel_over").each(function ()
			{
				if ($(this).width()<150)
				{
					$(this).width(150);
				}
			});
		// Cancel button End
		
		// Submit
			$(this).submit(function ()
			{
				// Bloked
					if ($(this).attr("_bloked") == 1)
					{
						return;
					}
				
				// File Upload
					var _upload_file=$(this).find(".gotov_k_zagruzke:first");
					if ((1)&&($(_upload_file).length))
					{
						// Когда скрыт, надо показать, а то возникает ошибка
							var _vis_hid=$(_upload_file).closest(".dop_pola_vis");
							if ($(_vis_hid).length)
							{
								$(_vis_hid).removeClass('hid').show().css('position', 'fixed').css('opacity', 0).fadeOut(110).oneTime(100, function ()
								{
									$(this)/*.css('left', screen.width)*/;
									input_file_upload_start($(_upload_file).attr("_id"));
								});
							}
						// Когда скрыт, надо показать, а то возникает ошибка End
						
						// Не скрыт, все в порядке
							else
							{
								input_file_upload_start($(_upload_file).attr("_id"));
							}
						// Не скрыт, все в порядке
						return false;
					}
				// File Upload End
				
				// Nado
					var form_ok=true;
					$(this).find(".input[nado], .check[nado]").each(function ()
					{
						// Скрытый элемент
							if ($(this).closest(":hidden").length || ($(this).closest(".hid").length))
							{
								return;
							}
						
							var input_ok=true;
							
							if ($(this).attr("nado")==1)
							{
								if ($(this).val()=='') { input_ok=false; }
							}
							
							if ($(this).attr("nado")=='check')
							{
								if (!$(this).is(":checked")) { input_ok=false; }
							}
							
							// Ok
								if (input_ok)
								{
									$(this).closest(".forma_blok").removeClass("form_no");
								}
							// Ok End
							
							// No
								else
								{
									$(this).closest(".forma_blok").addClass("form_no");
									form_ok=false;
								}
							// No End
					});
					
					if (!form_ok)
					{
						alert($(_form).attr("_alert"));
					}
					
					// Ajax
						if ((form_ok)&&($(this).attr("_ajax")==1))
						{
							var _action=$(this).attr("action");
							var _serialize=$(this).serialize();
							var _div=$(this).closest(".ajax");
							//$(this).load();
							if ((jQuery.fn.submit_load != undefined) && (submit_load_swf!=undefined))
							{
								$(this).find(".submit").submit_load();
							}
							else
							{
								$(_div).load();
							}
							
							if (pop_test)
							{
								$.post(_action, _serialize, function (ob)
								{
									alert(ob);
								});
							}
							
							$.post(_action, _serialize, function (ob)
							{
								if ((jQuery.fn.submit_load != undefined) && (submit_load_swf!=undefined))
								{
									$(this).find(".submit").submit_unload();
								}
								if (jQuery.fn.ajax_ob)
								{
									$(_div).ajax_ob(ob);
								}
								else
								{
									$(_div).closest(".pop").pop_ob(ob);
								}
							}, "json");
							
							return false;
						}
					// Ajax End
					
					return form_ok;
				// Nado End
			});
		// Sumbit End
	});
	
	return this;
};


// Input Max
	jQuery.fn.input_max = function ()
	{
		$(this).each(function ()
		{
			var _blok = $(this).closest(".forma_blok");
			var _info = $(_blok).find(".input_max_div");
			var _this = this;
			var _max = Number($(this).attr("_max"));
			
			
			// Max
				/*$(_this).keypress(function ()
				{
					var _val = $(_this).val();
					if (_val.length >= _max)
					{
						return false;
					}
				});*/
			
			// Update
				var _update = function ()
				{
					var _val = $(_this).val();
					
					$(_info).html(_val.length + ' / '+_max);
				};
				
				$(_this).keyup(function ()
				{
					_update();
				});
				
				_update();
		});
		
		return this;
	};
// Input Max End


// Forma Blok
	jQuery.fn.forma_blok = function ()
	{
		$(this).each(function ()
		{
			$(this).attr("_bloked", 1);
			
			$(this).find(".submit").attr("disabled", "disabled");
		});
		
		return this;
	};
// Forma Blok End

// Forma Unblok
	jQuery.fn.forma_unblok = function ()
	{
		$(this).each(function ()
		{
			$(this).attr("_bloked", 0);
			
			$(this).find(".submit").removeAttr("disabled");
		});
		
		return this;
	};
// Forma Unblok End


// Input Load Vkl
	jQuery.fn.input_load_vkl = function ()
	{
		$(this).each(function ()
		{
			var _blok = $(this).closest(".forma_blok");
			var _div = $(_blok).find(".forma_input_div");
			
			var _load = $("<div class=\"input_load\"></div>").load();
			
			$(_div).hide().after(_load);
		});
		
		return this;
	};


// Input Load Vikl
	jQuery.fn.input_load_vikl = function ()
	{
		$(this).each(function ()
		{
			var _blok = $(this).closest(".forma_blok");
			var _div = $(_blok).find(".forma_input_div");
			
			var _load = $(_blok).find(".input_load");
			
			$(_div).show();
			$(_load).remove();
		});
	};


// Input Vipad
	jQuery.fn.input_vipad = function ()
	{
		$(this).each(function ()
		{
			var _input = this;
			var _blok = $(this).closest(".forma_blok");
			var _input_hid = $(_blok).find('.input_vipad_hidden');
			var _options = $(_blok).find(".input_vipad_options");
			var _options_over = 0;
			var _focus = 0;
			var _last_id = $(_options).find(".input_vipad_row_selected").attr("_id");
			
			
			$(_input).unbind();
			
			
			// Options Selected
				var _options_selected = function ()
				{
					$(_options).find(".input_vipad_row").each(function ()
					{
						if ($(this).attr("_id") == $(_input_hid).val())
						{
							$(this).addClass("input_vipad_row_selected");
						}
						else
						{
							$(this).removeClass("input_vipad_row_selected");
						}
					});
				};
			
			
			/*// Scroll To
				var _scroll_to_over = function ()
				{
					
				};*/
			
			
			// Open
				var _open = function ()
				{
					_options_selected();
					$(_options).removeClass('hid').show();
					var _scroll_to_row = $(_options).find(".input_vipad_row_selected");
					if ($(_scroll_to_row).length)
					{
						$(_options).scrollTo(_scroll_to_row, { offset: {top:-($(_options).height() - 25)/2}});
					}
					$("body").click(_close);
				};
			
			
			// Mouse Down
				//$(_input).click(_open);
			
			
			// Close
				var _close = function ()
				{
					if (_focus == 0)
					{
						$(_options).hide();
						$("body").unbind("click", _close);
					}
				};
				
			
			// Focus
				$(_input).focus(function ()
				{
					_focus = 1;
					_open();
				});
				$(_input).blur(function ()
				{
					_focus = 0;
					if (_options_over == 0)
					{
						_close();
					}
				});
			
			
			// Trigger
				var _last_val = $(_input).val();
				var _trigger = function ()
				{
					if ($(_input).val() != _last_val)
					{
						$(_input_hid).trigger('change');
						$(_input).trigger('change');
					}
					_last_val = $(_input).val();
				};
				$(_input).blur(_trigger);
				$(_input).keyup(function (e)
				{
					if (e.keyCode == 13)
					{
						_trigger();
					}
				});
			
			
			// Set Val 
				var _set_val = function (_text, _id)
				{
					$(_input).val(_text);
					if (und(_id))
					{
						$(_input_hid).val(_id);
					}
					else
					{
						$(_input_hid).val(_text);
						
						$(_options).find(".input_vipad_row").each(function ()
						{
							if ($(this).html() == _text)
							{
								$(_input_hid).val($(this).attr("_id"));
							}
						});
					}
					
					// Отправляем событие Click
					
				};
			
			
			// Keyboard
				$(_input).keydown(function (e)
				{
					// Up Down
						if ((e.keyCode == 38) || (e.keyCode == 40))
						{
							// Открыть список
								if (($(_options).is(".hid")) || ($(_options).is(":hidden")))
								{
									_open();
								}
								
								
							// Текущая позиция
								var _current = $(_options).find(".input_vipad_row_over");
								if ($(_current).length == 0)
								{
									_current = $(_options).find(".input_vipad_row_selected");
								}
								
								
							// Вверх
								if (e.keyCode == 38)
								{
									var _to = $(_current).prev();
									if ($(_to).length == 0)
									{
										_to = $(_options).find(".input_vipad_row:last");
									}
								}
								
								
							// Вних
								if (e.keyCode == 40)
								{
									var _to = $(_current).next();
									if ($(_to).length == 0)
									{
										_to = $(_options).find(".input_vipad_row:first");
									}
								}
								
							
							// Переключение
								if ($(_to).length)
								{
									$(_options).find(".input_vipad_row_over").removeClass("input_vipad_row_over");
									$(_to).addClass("input_vipad_row_over");
								}
							
							
							// Scroll To
								var _scroll_to_row = $(_options).find(".input_vipad_row_over");
								if ($(_scroll_to_row).length)
								{
									$(_options).scrollTo(_scroll_to_row, { offset: {top:-($(_options).height() - 25)/2}});
								}
						}
				}).keydown(function (e)
				{
					// Enter Down
						if (e.keyCode == 13)
						{
							return false;
						}
				}).keyup(function (e)
				{
					// Enter Up
						if (e.keyCode == 13)
						{
							var _over = $(_options).find(".input_vipad_row_over");
							if ($(_over).length)
							{
								$(_over).removeClass('input_vipad_row_over');
								_set_val($(_over).html(), $(_over).attr("_id"));
							}
							_trigger();
							_focus = 0;
							_close();
							
							return false;
						}
					
					// Обновить Выделение
						else
						{
							_set_val($(this).val());
							_options_selected();
						}
				});
			
			
			// Select Click
				$(_options).find('.input_vipad_row').click(function ()
				{
					_set_val($(this).html(), $(this).attr("_id"));
					_trigger();
				});
			
			
			// Options Hover
				$(_options).hover(function ()
				{
					_options_over = 1;
				},
				function ()
				{
					_options_over = 0;
				});
				
			
			// Row Hover
				$(_options).find('.input_vipad_row').hover(function ()
				{
					$(this).addClass('input_vipad_row_over');
				},
				function ()
				{
					$(this).removeClass('input_vipad_row_over');
				});
		});
		
		return this;
	};

















