/// <reference path="jquery-vsdoc.js" /> 

(function($)
{
	// jQuery extensions:
	
	$.fn.fixOverflow = function()
	{
		/// <summary>
		/// Fixes the Internet Explorer overflow problem:
		/// http://remysharp.com/2008/01/21/fixing-ie-overflow-problem/
		/// </summary>
		
		if (/*@cc_on!@*/0)
		{
			return this.each(function()
			{
				if (this.scrollWidth > this.offsetWidth)
				{
					$(this).css({ "padding-bottom": "20px", "overflow-y": "hidden" });
				}
			});
		}
		
		return this;
	};
	
	$.fn.findAdjacentControl = function(id)
	{
		/// <summary>
		/// Finds an ASP.NET control with the specified ID
		/// in the same naming container as the current element.
		/// </summary>
		/// <param name="id" type="String">
		/// The server-side ID of the control.
		/// </param>
		/// <returns type="jQuery" />
		
		var idList = this
			.map(function()
			{
				var prefix = this.id;
				if ("string" !== typeof(prefix) || 0 === prefix.length) { return ""; }
				
				var index = prefix.lastIndexOf("_");
				prefix = (-1 == index) ? "" : prefix.substr(0, 1 + index);
				return "#" + prefix + id;
			})
			.filter(function()
			{
				return 0 !== this.length;
			});
		
		idList = jQuery.unique(idList);
		return jQuery(idList.join(", "));
	};
	
	$.fn.makeAbsolute = function(rebase)
	{
		/// <summary>
		/// Converts an element to an absolutely positioned element
		/// at the same position on the screen.
		/// </summary>
		/// <param name="rebase" type="Boolean">
		/// If true, forces the element to be removed from the document flow
		/// and attached to the body element to ensure proper absolute positioning. 
		/// Be aware that this may cause ID hierachy for CSS styles to be affected.
		/// </param>
		/// <returns type="jQuery" />
		
		return this.each(function()
		{
			var el = $(this);
			var pos = !!rebase ? el.offset() : el.position();
			
			el.css(
			{
				position: "absolute",
				marginLeft: 0,
				marginTop: 0,
				top: pos.top,
				left: pos.left
			});
			
			if (!!rebase)
			{
				el.remove().appendTo("body");
			}
		});
	};
	
	if (window.getSelection)
	{
		$.fn.selectAll = function()
		{
			/// <summary>
			/// Selects the contents of the first node.
			/// </summary>
			/// <returns type="jQuery" />
			
			if (0 !== this.length)
			{
				window.getSelection().selectAllChildren(this.get(0));
			}
			
			return this;
		};
	}
	else if (document.selection)
	{
		$.fn.selectAll = function()
		{
			/// <summary>
			/// Selects the contents of the first node.
			/// </summary>
			/// <returns type="jQuery" />
			
			if (0 !== this.length)
			{
				var range = document.selection.createRange();
				range.moveToElementText(this.get(0));
				range.select();
			}
			
			return this;
		};
	}
	else
	{
		$.fn.selectAll = function()
		{
			/// <summary>
			/// Selects the contents of the first node.
			/// </summary>
			/// <returns type="jQuery" />
			
			return this;
		};
	}
	
})(jQuery);

jQuery(function()
{
	// Menu:
	jQuery("ul.sf-menu > li.sf-menu-item, ul.sf-menu > li.sf-menu-item selected").hoverIntent(
	{
		over: function() { $(this).addClass("menu-hover"); },
		out: function() { $(this).removeClass("menu-hover"); },
		timeout: 500,
		interval: 200
	});
	
	jQuery("ul.sf-menu").addClass("sf-js-enabled");
	
	/*
	jQuery("ul.sf-menu").superfish(
	{
		delay: 400,
		pathClass: "current"
	});	
	*/
	
	// Expando:
	/*
	jQuery("div.expando").each(function()
	{
		this.isExpanded = !jQuery(this).hasClass("collapsed");
		
		jQuery(this).find("div.expandoTitle").click(function()
		{
			var div = jQuery(this).parent("div.expando")[0];
			var isExpanded = div.isExpanded;
			div.isExpanded = !isExpanded;
			
			if (isExpanded)
			{
				jQuery(div).addClass("collapsed");
			}
			else
			{
				jQuery(div).removeClass("collapsed");
			}
			
			var action = isExpanded ? "hide" : "show";
			jQuery(this).next("div.expandoContent").animate(
			{
				height: action, 
				opacity: action
			}, "normal");
		});
		
		if (!this.isExpanded)
		{
			jQuery(this).find("div.expandoContent").hide();
		}
	});
	*/
	
	// Collapsing sections:
	jQuery("a[rel*=expand], a[rel*=collapse]").click(function()
	{
		var rel = this.getAttribute("rel");
		if ("string" === typeof(rel) && 0 !== rel.length)
		{
			var id = rel.replace(/(expand|collapse)\[([^\]]*)\]/i, "$2");
			if (0 !== id.length)
			{
				var me = jQuery(this);
				var other = me.siblings("a[rel*=expand], a[rel*=collapse]");
				if (1 === other.length)
				{
					me.hide();
					other.show();
				}
				
				var el = jQuery("#" + id);
				if (0 !== el.length)
				{
					var isExpand = (/expand\[([^\]]*)\]/i).test(rel);
					var action = isExpand ? "show" : "hide";
					el.animate(
					{
						height: action, 
						opacity: action
					}, "fast");
				}
				
				return false;
			}
		}
	});
	
	// Collapsing sections 2:
	jQuery("a[rel*=show-hide]").click(function()
	{
		var rel = this.getAttribute("rel");
		if ("string" === typeof(rel) && 0 !== rel.length)
		{
			var id = rel.replace(/show-hide\[([^\]]*)\]/i, "$1");
			if (0 !== id.length)
			{
				jQuery(this).findAdjacentControl(id).toggle();
				return false;
			}
		}
	});
	
	// Select all:
	jQuery("a[rel*=select-all]").click(function()
	{
		var rel = this.getAttribute("rel");
		if ("string" === typeof(rel) && 0 !== rel.length)
		{
			var id = rel.replace(/select-all\[([^\]]*)\]/i, "$1");
			if (0 !== id.length)
			{
				jQuery(this).findAdjacentControl(id).selectAll();
				return false;
			}
		}
	});
	
	// Top of page:
	/*
	var el = jQuery("div.topOfPage");
	if (el)
	{
		el.click(function(evt)
		{
			if ("undefined" !== typeof(window.scrollTo))
			{
				window.scrollTo(0, 0);
			}
			else
			{
				jQuery(window).scrollTop(0).scrollLeft(0);
			}
			
			evt.stopPropagation();
			evt.preventDefault();
			evt.returnValue = false;
		});
		
		updatePosition = function()
		{
			var top = jQuery(window).scrollTop();
			if (0 === top)
			{
				jQuery("div.topOfPage").hide();
			}
			else
			{
				jQuery("div.topOfPage").show();
			}
		};
		
		jQuery(window).scroll(updatePosition);
		updatePosition();
	}
	*/
	
	
	// Menu Gripper:
	/*
	el = jQuery("div.menuGripper");
	if (el)
	{
		var toggleMenu = function()
		{
			var el = jQuery(document.body);
			if (el.hasClass("noMenu"))
			{
				el.removeClass("noMenu");
				jQuery("div.menuGripper a img").attr("alt", "Hide the menu");
				if ("undefined" !== typeof(document.cookie)) { document.cookie = "nomenu=0"; }
			}
			else
			{
				el.addClass("noMenu");
				jQuery("div.menuGripper a img").attr("alt", "Show the menu");
				if ("undefined" !== typeof(document.cookie)) { document.cookie = "nomenu=1"; }
			}
		};
		
		el.click(function(evt)
		{
			toggleMenu();
			evt.stopPropagation();
			evt.preventDefault();
			evt.returnValue = false;
		});
		
		if (document.cookie)
		{
			var cookieValue = null;
			var values = document.cookie.split("; ");
			for (var i = 0; i < values.length; i++)
			{
				var value = values[i].split("=");
				if ("nomenu" === value[0].toLowerCase())
				{
					switch(value[1])
					{
						case "1":
							cookieValue = true;
							break;
						case "0":
							cookieValue = false;
							break;
						default:
							cookieValue = null;
							break;
					}
					break;
				}
			}

			if (null !== cookieValue && cookieValue !== jQuery(document.body).hasClass("noMenu"))
			{
				toggleMenu();
			}
		}
	}
	*/
	
	// Formatted dates: (particularly from XSL)
	jQuery("a[rel*=date-format]").each(function()
	{
		var fmt = this.getAttribute("rel");
		if ("string" === typeof(fmt) && 0 !== fmt.length)
		{
			fmt = fmt.replace(/date-format\[([^\]]*)\]/i, "$1");
			if (0 !== fmt.length)
			{
				var sp = jQuery(this);
				var d = new Date(sp.text());
				if (!isNaN(d)) { sp.text(d.format(fmt)); }
			}
		}
	});
	
	// Fix overflow for IE:
	if (/*@cc_on!@*/0)
	{
		var fixup = function() { jQuery("td.request > div").fixOverflow(); };
		jQuery(window).resize(fixup);
		fixup();
	}
});

scrollToElement = function(id)
{
	var el = jQuery.$(id);
	if (!el || "undefined" === typeof(el.scrollIntoView)) { return false; }
	el.scrollIntoView(true);
	return true;
};