

$(document).ready(function() {
		
	var maxColHeight = 200;
	var heightCounter = 0;
	var columnCounter = 1;
	var maxHeight = 0;
	
	/**
	 * Calculates the position for the drop-down ('segment-containers')
	 * menus in the 'topmenu' section, and the width as the 'segments'
	 * within the 'segments-container' are positioned absolutely)
	 */
	$(".topmenu").find(".segments-container").each( function (i) {

		heightCounter = 0;
		columnCounter = 1;
		maxHeight = 0;

		$( this ).find(".segment" ).each( function (j) {
			
			if (heightCounter > maxColHeight) {

			    columnCounter += 1;
			  
			    $(this).addClass("column_" + columnCounter);
			    $(this).css("position", "absolute");
			    heightCounter = 0;
			    $(this).css("top", heightCounter + "px");
			    heightCounter = $(this).outerHeight();			    
			}	
			else {
			    $(this).addClass("column_" + columnCounter);
			    $(this).css("position", "absolute");
			    $(this).css("top", heightCounter + "px");
			    heightCounter += $(this).outerHeight();

			    if(heightCounter > maxHeight) {
				maxHeight = heightCounter;
			    }
			}					   
		    })
		
		myWidth = (columnCounter * 180);
		myHeight = maxHeight + 100;
		$(this).css("width", myWidth + "px");
		$(this).css("height", maxHeight + "px");
		$(this).addClass("hide-menu");
	    });
	
	/**
	 * Adds the 'hovering' class to the selected list element (button) and
	 * calculates the position for the drop-down box (segment-container)
	 */
	function addMega(){
	    $(this).addClass("hovering");
	    $(this).children(":first").find("a").addClass("active"); // used for hover button-down picture

	    // Get the width of the segment container
	    segmentWidth = $(this).children(".segments-container").outerWidth();

	    // Get the widths and X positions of the main container
	    containerX1Pos = Math.floor( $(this).parent().offset().left );
	    containerWidth = $(this).parent().outerWidth();
	    containerX2Pos = containerX1Pos + containerWidth;

	    // Get the X1 (left) position of the parent button
	    buttonX1Pos = Math.floor( $(this).offset().left );

	    // Calculate the X1 + 2 positions of the segment container
	    segmentX1Pos = buttonX1Pos - containerX1Pos; 
	    segmentX2Pos = buttonX1Pos + segmentWidth;
	
	    // If the segment container's left edge is past the main container
	    if (segmentX2Pos > containerX2Pos) {

		// move it back towards the left so that it fits in
		segmentX1Pos = containerX2Pos - segmentWidth - containerX1Pos;

		// If the segment container is only slightly left of the button 
		// move it further to the left so the two respective vertical
		// lines don't appear too close together (looks bad)
		if (buttonX1Pos - containerX1Pos - segmentX1Pos < 20) {
		    segmentX1Pos -= 20;
		}
		$(this).children(".segments-container").css("left", segmentX1Pos + "px");
	    }
	    else {
		$(this).children(".segments-container").css("left", segmentX1Pos + "px");
	    }	    
        }

	function removeMega(){
	    $(this).removeClass("hovering");
	    $(this).children(":first").find("a").removeClass("active");
        }

	var megaConfig = {
	    interval: 150,
	    sensitivity: 4,
	    over: addMega,
	    timeout: 150,
	    out: removeMega
	};

	$("li.mega-dropdown").hoverIntent(megaConfig);
	
    });    

