jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

$(function(){
	$('#featured li').first().addClass('first');
	$('#featured li').last().addClass('last');
	$('#global-nav li').last().addClass('last');
	
	$('.slide:random').addClass('active');
	
	$('.slide .description').each(function(){
		var $this = $(this),
			marginTop = $this.outerHeight()/2;
		$(this).css({top:'50%',marginTop:'-'+marginTop+'px'});
	});
	
	$('.slide').css('opacity',0);
	slideSwitch();
	
	setInterval( "slideSwitch()", 7500 );
	
	//Set custom configurations
	var config = {
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		 interval: 0, // number = milliseconds for onMouseOver polling interval
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		 timeout: 0, // number = milliseconds delay before onMouseOut
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	
	
	
	$("#global-nav li .submenu").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("#global-nav li").hoverIntent(config); //Trigger Hover intent with custom configurations

	
});

function slideSwitch() {
	var $active = $('.slide.active');

	if ( $active.length == 0 ) $active = $('.slide').last();

    var $next =  $active.next().length ? $active.next()
        : $('.slide').first();

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active');
		});
}

	//On Hover Over
function megaHoverOver(){
	
	$(this).find('a').first().addClass('hovered');
	
    $(this).find(".submenu").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 1;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).outerWidth(); //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(".submenu").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(".submenu").css({'width' : rowWidth}); //Set Width
	    var position = $(this).find(".submenu").position();
		console.log(position);
		/*
		if( !$.browser.msie && offset.left > '590' )
		{
			$(this).find(".submenu").css({left:'inherit',right:'-1px'});
		}
		*/
    }
}
//On Hover Out
function megaHoverOut(){

	$(this).find('a').first().removeClass('hovered');
	
  $(this).find(".submenu").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}