﻿$.fn.watchForFlyout = function(flyoutsel)
{
	var sel = flyoutsel;
	var irows = 0;
	var offset = 0;
	return this.each(function()
	{
		if($(this).offset().top != offset)
		{
			irows++;
			offset = $(this).offset().top;
		}
		$(this).mouseover(function(e)
		{
			var offset = $(this).offset();
			var pos = $(this).position();
			$(sel + $(this).attr("id")).css("top", pos.top - 32 + "px");
			if($(this.parentNode).width() < (pos.left + $(this).width() - 20))
			{
				$(sel + $(this).attr("id")).addClass("right");
				$(sel + $(this).attr("id")).css("left", ((pos.left - $(sel + $(this).attr("id")).width()) + 20) + "px");
			}
			else
			{
				$(sel + $(this).attr("id")).removeClass("right");
				$(sel + $(this).attr("id")).css("left", ((pos.left + $(this).width()) - (Math.ceil($(this).width()/2))) + "px");
			}
			$(sel + $(this).attr("id")).removeClass("hide");
			var aobj = $(this);
			$(sel + $(this).attr("id") + " .flyoutAnchor").click(function(e)
			{
				$(this).attr("href", aobj.attr("href"));
			});
		});
		$(this).mouseout(function(e)
		{
			$(sel + $(this).attr("id")).addClass("hide");
		});
		if(irows > 2)
		{
			$(".availableStylesList a.showallLink").removeClass("hide");
			var trmsCntr = $(this).parent();
			$(".availableStylesList a.showallLink").click(function(e)
			{
				e.preventDefault();
				trmsCntr.css("height","auto");
				$(this).addClass("hide");
				$(".availableStylesList a.hideOverFlowLink").removeClass("hide");
			});
			$(".availableStylesList a.hideOverFlowLink").click(function(e)
			{
				e.preventDefault();
				trmsCntr.css("height","30px");
				$(".availableStylesList a.showallLink").removeClass("hide");
				$(".availableStylesList a.hideOverFlowLink").addClass("hide");
			});
		}
		else
		{
			$(".availableStylesList a.showallLink").addClass("hide");
		}
	});
};
