

$(function()
{
	init_overton_window("#overton_window_education", 6, 9);
	init_overton_window("#overton_window_energy", 3, 5);
	init_overton_window("#overton_window_weapons", 3, 5);
	init_overton_window("#overton_window_welfare", 3, 5);
	
	$("#overton_window_education").css("display", "block");
	
	
	$(".overton_window_tab").each(function(i)
	{
		var tab = $(this);
		
		$(this).click(
			function(e)
			{
				$(".overton_window").each(function(i)
				{
					$(this).css("display", "none");
					$(tab.attr("href")).css("display", "block");
					
					$(".overton_window_tab").each(function(i)
					{
						$(this).removeClass("selected");
					});
					tab.addClass("selected");
				});
				
				return false;
			}
		);
	});
		
	
	function init_overton_window(id, ia, ib)
	{
		var MCPP_OW_begin = null;
		var MCPP_OW_end = null;
		var MCPP_OW_selection = null;
		var MCPP_OW_isBegin = false;
		var MCPP_OW_isEnd = false;
		var MCPP_OW_isEnclosed = false;
		var MCPP_OW_isMouseDown = false;
		
		$(id + " ol li").each(function(i)
		{
			$(this).mousedown(
				function(e)
				{
					MCPP_OW_isMouseDown = true;
					
					
					if ($(this).offset().top > MCPP_OW_end.offset().top)
					{
						window_move(MCPP_OW_end, $(this));
						MCPP_OW_selection = MCPP_OW_end;
						MCPP_OW_isEnclosed = true;
						MCPP_OW_isBegin = false;
						MCPP_OW_isEnd = false;
					}
					else if ($(this).offset().top < MCPP_OW_begin.offset().top)
					{
						window_move(MCPP_OW_begin, $(this));
						MCPP_OW_selection = MCPP_OW_begin;
						MCPP_OW_isEnclosed = true;
						MCPP_OW_isBegin = false;
						MCPP_OW_isEnd = false;
					}
					else
					{
						MCPP_OW_isBegin = (MCPP_OW_begin[0] == this);
						MCPP_OW_isEnd = (MCPP_OW_end[0] == this);
						
						var ithis = item_index($(this));
						
						MCPP_OW_isEnclosed = (item_index(MCPP_OW_begin) < ithis &&
																	item_index(MCPP_OW_end) > ithis)
						
						MCPP_OW_selection = $(this)
					}
					
					return false;
				}
			);
			
			$(this).mouseover(
				function(e)
				{
					if (MCPP_OW_isMouseDown)
					{
						if (MCPP_OW_isBegin && (item_index($(this)) < item_index(MCPP_OW_end)))
							window_resize($(this), MCPP_OW_end);
						else if (MCPP_OW_isEnd && (item_index($(this)) > item_index(MCPP_OW_begin)))
							window_resize(MCPP_OW_begin, $(this));
						else if (MCPP_OW_isEnclosed)
						{
							window_move(MCPP_OW_selection, $(this));
							MCPP_OW_selection = $(this);
						}
					}
					
					return false;
				}
			);
			
			$(this).mouseup(
				function(e)
				{
					MCPP_OW_isMouseDown = false;
					
					return false;
				}
			);
		});
		
		
		$(id + " .overton_window_up").click(function(i)
		{
			var items = $(id + " ol li");
			var index = item_index(MCPP_OW_begin);
			
			if (index > 0)
				window_move(MCPP_OW_begin, $(items[index - 1]));
		});
		
		$(id + " .overton_window_down").click(function(i)
		{
			var items = $(id + " ol li");
			var index = item_index(MCPP_OW_end);
			
			if (index < (items.length - 1))
				window_move(MCPP_OW_end, $(items[index + 1]));
		});
		
		
		var items = $(id + " ol li");
		
		window_resize($(items[ia]), $(items[ib]));
		
		$(id).css("display", "none");
		
		
		function item_index(item)
		{
			var items = $(id + " ol li");
			var i = 0, count = items.length;
			
			for (; i < count; ++i)
			{
				if (item[0] == items[i])
					return i;
			}
			
			return -1;
		}
		
		
		function window_move(handle, destination)
		{
			var items = $(id + " ol li");
			var count = items.length;
			
			var ibegin = item_index(MCPP_OW_begin);
			var iend = item_index(MCPP_OW_end);
			var ihandle = item_index(handle);
			var idestination = item_index(destination);
			
			ibegin += idestination - ihandle;
			iend += idestination - ihandle;
			
			if (ibegin < 0) { iend -= ibegin; ibegin = 0; }
			else if (ibegin > count) ibegin = count;
			
			if (iend < 0) iend = 0;
			else if (iend > count) iend = count;
			
			window_resize($(items[ibegin]), $(items[iend]));
		}
		
		
		function window_resize(begin, end)
		{
			var top = begin.offset().top;
			var bottom = end.offset().top;
			
			if (top > bottom)
			{
				var temp = top;
				top = bottom;
				bottom = temp;
				
				temp = begin;
				begin = end;
				end = temp;
			}
			
			bottom += end.height() + 12;
			
			$(id + " .window")
				.animate({"top" : (top - $($(id + " .window")[0].offsetParent).offset().top) + "px", "height" : (bottom - top) + "px"}, 50);
			
			var items = $(id + " ol li"), is_center = -1;
			var i = 0, count = items.length;
			
			for (; i < count; ++i)
			{
				if (items[i] == begin[0])
				{
					begin.css("cursor", "n-resize");
					begin.attr("title", "Drag to resize window");
					is_center = 0;
				}
				else if (items[i] == end[0])
				{
					end.css("cursor", "url(s-resize");
					//end.css("cursor", "url(/media/images/common/TOW_CursorDown.png), s-resize");
					end.attr("title", "Drag to resize window");
					is_center = 1;
				}
				else if (is_center == -1)
				{
					$(items[i]).css("cursor", "n-resize");
					$(items[i]).attr("title", "Click to move window");
				}
				else if (is_center == 0)
				{
					$(items[i]).css("cursor", "move");
					$(items[i]).attr("title", "Drag to move window");
				}
				else if (is_center == 1)
				{
					$(items[i]).css("cursor", "s-resize");
					$(items[i]).attr("title", "Click to move window");
				}
			}
			
			MCPP_OW_begin = begin;
			MCPP_OW_end = end;
		}
	}
});


