var pop_test=0;

temp_img("js/pop/pop_close_over.png");

$(document).ready(function(e) {
    $(".a_pop").a_pop();
});

// Ob
	jQuery.fn.pop_ob=function (ob)
	{
		var _pop=this;
		
		if (!$(_pop).is(".pop"))
		{
			_pop=$(_pop).closest(".pop");
		}
		
		if ($(this).is(".pop"))
		{
			var _div=$(_pop).find(".ajax");
			var _this=this;
			
			if (und(ob.width)) { $(_this).find(".pop_text_overflow").animate({ width:Number(ob.width) }, 1); $(_pop).attr("_width", ob.width); } // Ширина
			if (und(ob.height)) { $(_this).find(".pop_text_overflow").height(Number(ob.height)); } // Высота
			else
			{
				$(_this).pop_auto_resize();
			}
			if (und(ob.title))
			{
				$(_this).pop_title(ob.title);
			}
			// Html
				if (und(ob.html))
				{
					$(_this).pop_text(ob.html);
				}
			// Html End
			if (ob.reload)
			{
				window.location=window.location;
				return;
			}
			
			if (ob.close)
			{
				$(_this).fadeOut(400);
			}
		}
		
		return this;
	};
// Ob End

// Okno
	var pop_otstup_n=0;
	var pop_n = 0;
	
	function okno(params)
	{
		//params=$.extend({width:300}, params);
		pop_n ++;
		
		var _html="<div class=\"pop\">";
			_html+="	<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
			_html+="	  <tr>";
			_html+="	  	<td class=\"pop_t_l\"><img src=\"box/spacer.gif\" width=\"14\" height=\"1\" border=\"0\" /></td>";
			_html+="		<td class=\"pop_t pop_t_height\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"pop_t_height\">";
			_html+="		  <tr>";
			_html+="			<td class=\"pop_t_drag\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"pop_t_height\">";
			_html+="			  <tr>";
			_html+="				<td class=\"pop_t_text hid\" nowrap></td>";
			_html+="				<td class=\"pop_move\">&nbsp;</td>";
			_html+="			  </tr>";
			_html+="			</table></td>";
			_html+="			<td class=\"pop_close_container\"><div class=\"pop_close\"></div></td>";
			_html+="		  </tr>";
			_html+="		</table></td>";
			_html+="		<td class=\"pop_t_r\"><img src=\"box/spacer.gif\" width=\"14\" height=\"1\" border=\"0\" /></td>";
			_html+="	  </tr>";
			_html+="	  <tr>";
			_html+="	  	<td class=\"pop_l\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" border=\"0\" /></td>";
			_html+="		<td class=\"pop_c\" valign=\"top\"><div class=\"pop_resizable\">";
			_html+="		  <div class=\"pop_text_overflow\"><div class=\"pop_text ajax\"></div><div class=\"pop_text_bottom\">&nbsp;</div></div></div></td>";
			_html+="		<td class=\"pop_r\">&nbsp;</td>";
			_html+="	  </tr>";
			_html+="	  <tr>";
			_html+="	  	<td class=\"pop_b_l\"></td>";
			_html+="		<td class=\"pop_b\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" border=\"0\" /></td>";
			_html+="		<td class=\"pop_b_r\"><img src=\"box/spacer.gif\" width=\"1\" height=\"1\" border=\"0\" /></td>";
			_html+="	  </tr>";
			_html+="	</table>";
			_html+="	</div>";
			
			var _div=$(_html);
			var _pop_resize=$(_div).find(".pop_resizable");
			
			if (und(params.width)) { $(_pop_resize).width(params.width); } // Ширина
			if (und(params.height)) { $(_pop_resize).height(params.height); } // Высота
			$(_pop_resize).find(".pop_text_overflow").hide().show(1);
			
			if (params.title!=undefined)
			{
				$(_div).pop_title(params.title);
			}
			if (params.text!=undefined)
			{
				$(_div).pop_text(params.text);
			}
			
			$(_div).mousedown(function ()
			{
				$(this).css("z-index", z_index());
			});
			$(_div).mouseup(function ()
			{
				$("#datepicker_div").css("z-index", z_index()+10);
			});
			
			// Otstup
				var _left;
				var _top;
				
				if ($(window).width()>=1200)
				{
					_left=300+30*pop_otstup_n;
				}
				else
				{
					_left=60+30*pop_otstup_n;
				}
				
				if ($(window).height()<700)
				{
					_top=90+35*pop_otstup_n/*+$("body").scrollTop()*/;
				}
				else
				{
					_top=90+35*pop_otstup_n/*+$("body").scrollTop()*/;
				}
				pop_otstup_n++;
				if (pop_otstup_n>5) { pop_otstup_n=0; }
			// Otstup End
			
			$(_div).css("z-index", z_index()).css("left", _left).css("top", _top).draggable({
				handle:".pop_t_drag"
			});
			
			// Resize
				$(_pop_resize).resizable({
					resize:function ()
					{
						$(_pop_resize).pop_resize_max();
						$(_div).find(".pop_text_overflow").width('auto');
						
						//test($(_div).find(".pop_text").width());
					},
					//handles:'s, se',
					minWidth:195,
					minHeight:20
				});
			// Resize End
			
			// Close
				var _close=$(_div).find(".pop_close");
				$(_close).attr("id", "pop_close_"+pop_n).hoverClass("pop_close_over").click(function ()
				{
					$(_div).pop_close();
				});
				if (jQuery.fn.visitor_click)
				{
					$(_close).visitor_click();
				}
			// Close End
		
		var _container=$("<div class=\"pops_container\"></div>").append(_div);
		$("body").append(_container);
		
		return _div;
	}
	
	jQuery.fn.pop_resize_max = function ()
	{
		$(this).each(function ()
		{
			var _max=$(window).height()-250;
			if ($(this).height()>_max) { $(this).find(".pop_text_overflow").height(_max); }
			$(this).find(".pop_text_overflow").height($(this).height()-0);
		});
		
		return this;
	};
// Okno End


// Переход по адресу
	jQuery.fn.ajax_a=function (_http)
	{
		var _div=$(this).search_this(".ajax");
		var _pop=$(_div).closest(".pop");
		$(_div).load();
		
		if (pop_test)
		{
			$.get(_http, function (_html) { alert(_html); });
		}

		$.getJSON(_http, function (ob)
		{
			$(_div).attr("_http", _http);
			$(_pop).attr("_http", _http).pop_ob(ob);
		});
		
		return this;
	};
// Переход по адресу End

// Reload
	jQuery.fn.pop_reload=function ()
	{
		var _pop=this;
		
		if (!$(_pop).is(".pop"))
		{
			_pop=$(this).closest(".pop");
		}
		
		$(_pop).ajax_a($(_pop).attr("_http"));
		
		return this;
	};
// Reload End

// Close
	jQuery.fn.pop_close=function ()
	{
		var _pop=this;
		
		document_start_scroll();
		
		if (!$(this).is(".pop"))
		{
			_pop=$(this).closest(".pop");
		}
		var _absolute=$(_pop).parent();
		$(_pop).remove();
		$(_absolute).remove();
		delete _absolute;
		delete _pop;
	};
// Close End

// Scroll
	var _document_scroll_top=0;
	
	var _document_scroll_fn=function ()
	{
		$(document).scroll(function ()
		{
			$.scrollTo(_document_scroll_top);
		});
	};
	
	function document_stop_scroll ()
	{
		var _document_scroll_top=$(document).scrollTop();
		
		$(document).scroll(_document_scroll_fn);
		
		return this;
	};
	
	function document_start_scroll ()
	{
		$(document).unbind("scroll", _document_scroll_fn);
		return this;
	};
// Scroll End

// A Pop
	jQuery.fn.a_pop = function ()
	{
		$(this).each(function ()
		{
			$(this).click(function ()
			{
				var _http = $(this).attr("_http");
				
				if (und(_http))
				{
					pop(_http);
				}
			});
		});
		
		return this;
	};
// A Pop End

// Pop
	function pop(params)
	{
		if (!is_object(params))
		{
			params={http:params};
		}
		
		var _okno=okno(params);
		
		$(_okno).ajax_a(params.http);
		
		return _okno;
	}
// Pop End

// Title
	jQuery.fn.pop_title=function(_zag)
	{
		if (_zag!=undefined)
		{
			$(this).find(".pop_t_text").vis().html(_zag);
		}
		
		return this;
	};
// Title End

// Text
	jQuery.fn.pop_text=function(_text)
	{
		var _div=$(this).find(".pop_text");
		$(_div).html(_text);
		$(this).find(".pop_resizable").pop_resize_max();
			
			$(_div).find(".forma").forma().attr("_ajax", 1).attr("action", $(this).attr("_http"));
			
			// Tabs
				if (jQuery.fn.tabs)
				{
					$(_div).find(".tabsmy").tabsmy();
				}
			
			$(_div).find('.hover_src').hover_src();
			
			if (jQuery.fn.visitor_click)
			{
				$(_div).find(".visitor_click").visitor_click();
			}
		
		return this;
	};
// Text End

// Auto Resize
	jQuery.fn.pop_auto_resize=function ()
	{
		var _pop=this;
		
		if (!$(_pop).is(".pop"))
		{
			_pop=$(this).closest(".pop");
		}
		
		$(_pop).each(function ()
		{
			var _ov=$(this).find(".pop_text_overflow");
			$(_ov).height('auto');
			$(_pop).find(".pop_resizable").height('auto')/*.width('auto')*/;
			
			
			$(this).height('auto');
		});
		
		return this;
	};
// Auto Resize End

