$(document).ready(function() {
	$('.search_select').sb({
		fixedWidth : true,
		arrowMarkup : ""
	});

	$("ul.topnav li").each(checkOverlap);

	function checkOverlap() {
		
	}

	// On Hover Over
	function megaHoverOver() {
		$(this).find("a").addClass("active");
		$(this).find("div.menu_left").addClass("menu_left_active");
		$(this).find("div.menu_right").addClass("menu_right_active");
		if($(this).find(".sub").length == 0)
		{
			$(this).addClass("totalround");
		}
		$(this).find(".sub").show(); // Find sub
		// and
		/*
		var height = 0;
		var fold = 0;
		$(this).find(".sub li").each(function() {
			height += $(this).height();
			if (height > 170) {
				fold ++;
				height = 0;
				$(this).parent().parent().css({'width': (255 * (fold+1)) + 10});
				if($(this).parent().parent().has("ul#target"+(fold-1)).length == 0)
				{
					$(this).parent().parent().append("<ul class='target' id='target"+(fold-1)+"'></ul>");
				}
			}
			if (fold > 0) {
				var newul = $(this).parent().parent().find("ul#target"+(fold-1));
				newul.append($(this));
			}
		});
		*/
		
		if ($(this).position().left + $(this).find(".sub").width() > 850) {
			var width = $(this).position().left + $(this).find(".sub").width();
			width = 850 - parseInt(width);
			$(this).find(".sub").css({
				'left' : width + "px"
			});
		}
		// fade it in
		(function($) {
			// Function to calculate total width of all ul's
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				// Calculate row
				$(this).find("ul").each(function() { // for each
					// ul...
					rowWidth += $(this).width(); // Add each ul's
					// width
					// together
				});
			};
		})(jQuery);

		if ($(this).find(".row").length > 0) { // If row exists...

			var biggestRow = 0;

			$(this).find(".row").each(function() { // for each row...
				$(this).calcSubWidth(); // Call function to calculate
				// width of
				// all ul's
				// Find biggest row
				if (rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});

			$(this).find(".sub").css({
				'width' : biggestRow
			}); // Set width
			$(this).find(".row:last").css({
				'margin' : '0'
			}); // Kill last row's margin

		} else { // If row does not exist...

			/*
			 * $(this).calcSubWidth(); // Call function to calculate width of
			 * all // ul's $(this).find(".sub").css({ 'width' : rowWidth }); //
			 * Set Width
			 */

		}
	}
	// On Hover Out
	function megaHoverOut() {
		$(this).find(".sub").hide();
		$(this).find("a").removeClass("active");
		$(this).find("div.menu_left").removeClass("menu_left_active");
		$(this).find("div.menu_right").removeClass("menu_right_active");
	}
	
	function subOver()
	{
		//console.log($(this).find(".subsub").width() )
		if ($(this).parent().parent().parent().position().left + + $(this).width() + $(this).find(".subsub").width() > 850) {
			$(this).find(".subsub").css({
				'left' : "-264px"
			});
			$(this).find(".subsub").addClass("subright");
		}
		$(this).find(".subsub").show();
	}
	function subOut()
	{
		$(this).find(".subsub").hide();
	}

	var config = {
		sensitivity : 4, // number = sensitivity threshold (must be 1
		// or
		// higher)
		interval : 50, // number = milliseconds for onMouseOver polling
		// interval
		over : megaHoverOver, // function = onMouseOver callback
		// (REQUIRED)
		timeout : 100, // number = milliseconds delay before onMouseOut
		out : megaHoverOut
	// function = onMouseOut callback (REQUIRED)
	};
	var config2 = {
			sensitivity : 4, // number = sensitivity threshold (must be 1
			// or
			// higher)
			interval : 50, // number = milliseconds for onMouseOver polling
			// interval
			over : subOver, // function = onMouseOver callback
			// (REQUIRED)
			timeout : 100, // number = milliseconds delay before onMouseOut
			out : subOut
			// function = onMouseOut callback (REQUIRED)
	};

	$("ul.topnav > li").hoverIntent(config); // Trigger Hover intent with
	$("ul.topnav .sub li").hoverIntent(config2); // Trigger Hover intent with
	// custom
	// configurations

});

