$(document).ready(function() {
// MEGA Dropdown
function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).addClass('show');
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).removeClass('show') 
	  });
	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#navigation li .sub").css({'opacity':'0'});
	$("ul#navigation li").hoverIntent(config);
	


// NIVO Slider
	$('#slider').nivoSlider({
			effect:'boxRainReverse', // Specify sets like: 'fold,fade,sliceDown'
			animSpeed:1000, // Slide transition speed
			pauseTime:6000, // How long each slide will show
			startSlide:0, // Set starting Slide (0 index)
			directionNav:false, // Next & Prev navigation
			controlNav:false, // 1,2,3... navigation
      pauseOnHover: false, // Stop animation while hovering
	});
	$('#slider2').nivoSlider({
			effect:'boxRainReverse', // Specify sets like: 'fold,fade,sliceDown'
			animSpeed:1000, // Slide transition speed
			pauseTime:8000, // How long each slide will show
			startSlide:0, // Set starting Slide (0 index)
			directionNav:false, // Next & Prev navigation
			controlNav:false, // 1,2,3... navigation
      pauseOnHover: false, // Stop animation while hovering
	});	
		
// Accordion
$('div.accordion> div').hide();
$('div.accordion> div.CTA').show();  
  $('div.accordion> p.more').click(function() {
		//$(this).hide('slow');
    var $thisHeading = $(this);
    var $nextDiv = $(this).next();
    var $visibleSiblings = $nextDiv.siblings('div:visible');
    if ($visibleSiblings.length ) {
      $visibleSiblings.slideUp(1000, function() {
        $nextDiv.slideToggle(1000).removeClass("selected");
	    $.scrollTo($thisHeading,1000, {offset:-10} );
      });
    } else {
      $nextDiv.slideToggle(1000).addClass("selected");
	  $.scrollTo($thisHeading,1000, {offset:-10} );
    }
  });

// Small nudge for links with the .nudge class
	$('a.nudge').hover(function() { //mouse in
			$(this).animate({ paddingLeft: '10px' }, 400);
		}, function() { //mouse out
			$(this).animate({ paddingLeft: 0 }, 400);
		});

// Clears out input boxes that have values onclick
	$(".filled").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
		}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
		});
	
// Sends rel="external" to a new tab/window
	$('a[rel="external"]').click( function() {
			window.open( $(this).attr('href') );
			return false;
			});  

// Hover effects for image rollovers
  if ($.browser.msie && $.browser.version < 7) return;  
  $('#navigation li')
    .removeClass('highlight')
    .find('a')
    .append('<span class="hover" />').each(function () {
      var $span = $('> span.hover', this).css('opacity', 0);
      $(this).hover(function () {
        $span.stop().fadeTo(500, 1);
      }, function () {
        $span.stop().fadeTo(1000, 0);
      });
    });

});
