//On Hover Over

jQuery.fn.outerHTML = function(s) {
return (s)
? this.before(s).remove()
: jQuery("<li>").append(this.eq(0).clone()).html();
}

var BLOCK_OPEN_ACTIVE = false;
var BLOCK_OPEN_ACTIVE_TIMEOUT;

function megaHoverOver(){
	BLOCK_OPEN_ACTIVE = true;
	
	if(init == false) {
		$("ul#topnav li.active_top").removeClass('active_top');
		$(this).addClass('active_top');
	} else {
		init = false;
	}

	$(this).parent().find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
	      $(this).hide();  //after fading, hide it
	 });
	
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    
    var links = $(this).find("li");
    var items = 3;
    var offset_div = 0;
    var offset_ul = 5;
    var offset_li = 10;
    
    if ( links.length > 0  &&  $(this).find(".sub").attr('modified') != 'true') { //If row exists...
    	// split the li's into groups
    	var counter = 0;
    	var iteration = 0;
    	var row = '';
    	links.each(function(){
    		if(counter == 0) {
    			row += '<ul class="row">';
    		}
    		
    		row += $(this).outerHTML();
    		iteration ++;
    		counter ++;
    		
    		if(counter == items) {
    			row += '</ul>';
    			counter = 0;
    		}
    	});
    	
    	//close last ul if not closed automatically
    	if(iteration == links.length && counter != 0) {
    		row += '</ul>';
    	}

    	//set new ordered list
    	$(this).find(".sub").html(row);
    	
    	//set the ul's width
    	$(this).find("ul").each(function() {	//for each link...
    		var biggestLink = 0;
        	$(this).find('li').each(function() {
	        	var width = $(this).width() + offset_li;
	        	//Find biggest Link
	            if(width > biggestLink) {
	            	biggestLink = width;
	            }
        	});
        	
        	$(this).css({'width' :biggestLink});
        	$(this).find('li').css({'width' :biggestLink});
        });
    	
    	//Function to calculate total width of all ul's
    	(function($) {
            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); 

    	//Get width of all Row's
        if ( $(this).find(".row").length > 0 ) { //If row exists...
        	rowWidth = 0;
            $(this).find(".row").each(function() {	//for each row...
            	rowWidth += $(this).width() + offset_ul;
            });
            rowWidth - offset_ul;

            $(this).find('.sub').css({'width' :rowWidth + offset_div}); //Set width
            $(this).find(".sub").attr('modified','true');
        } 
        
        
        var direction = 450 <= ($(this).position().left - $('#menu').position().left) ? 'left' : 'right';
        
        if(direction == 'left') {
	      	var li_width = $(this).outerWidth();
			var div_sub = $(this).find('div.sub');
			var div_width = div_sub.outerWidth();
			var offset_left = div_width - li_width;
			div_sub.css('left',-offset_left);
			div_sub.css('border-left','0');
			div_sub.css('border-right','5px solid #F4B945');
		}
        
        initNavigation($(this).find('a'));
        
    } 
}
//On Hover Out
function megaHoverOut(){
  var is_open = $(this).find("li.active").length;
	
  if(!is_open) {
	BLOCK_OPEN_ACTIVE = false;
	window.clearTimeout(BLOCK_OPEN_ACTIVE_TIMEOUT);
	BLOCK_OPEN_ACTIVE_TIMEOUT = window.setTimeout("openActiveMenuItem()",1000);
  }
//  return true;
  
  /*
  */
}

function openActiveMenuItem() {
	if(location.hash.length != 0 && location.hash != "#" && !BLOCK_OPEN_ACTIVE) {
		var hash = location.hash.split('#')[1].replace('ajax.html','');
			
		var active_li = $('ul#topnav a[href$='+hash+']');
		var active_top;
		
		//determine if 1st or 2nd level
		if(active_li.parent().parent().is('ul#topnav')) {
			active_li.parent().find('li').removeClass('active');
			active_top = active_li.parent();
		} else {
			active_top = active_li.parent().parent().parent().parent();
			active_li.parent().parent().parent().find('li').removeClass('active');
			active_li.parent().addClass('active');
		}
		megaHoverOver.call(active_top);
	}
}
