$(document).ready(function(){

	// Galleries
	$('.gallery').galleria({
	  width: 485,
	  height: 370, 
	  showCounter: false
	});
	
	// MEGA NAV
	$("ul#mega-nav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#mega-nav li").hoverIntent({
    sensitivity: 2,   
    interval: 300,  
    timeout: 200, 
    over: (function() {
	  $(this).find(".sub").stop().fadeTo('fast', 1).show();
		//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).outerWidth(true); //Add each ul's width together
		            });
		        };
		    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

				    }
    }), 
    out: (function() {
		  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
		    $(this).hide();  //after fading, hide it
		  });
    })
  });

	$("#quick-links").selectbox({
	  onChange: function (val, inst) {
	    window.location = val;
	  }
	});

  $('.sbSelector').html('Quick Links');

	//search default text 
	// Form input hide/display default text on focus/blur
 	$('.default-value').each(function() {
	  var default_value = this.value;
	  $(this).focus(function() {
	    if(this.value == default_value) {
	      this.value = '';
	    }
	  });
	  $(this).blur(function() {
	    if(this.value == '') {
	      this.value = default_value;
	    }
	  });
	});

	// Banner rotation
	$('#banner .banners').cycle({
	  timeout: 5000,
	  pager: '#banner-nav'
	});

  $('#banner .message').css('background-color', 'transparent')

	// News Flashes
	$('#news-flashes').cycle({
	  fit: 1,
	  width: '690px'
	});


	// ACCORDIAN LIKE TOGGLES
	$('dl.toggle dd').hide();
	$('dl.toggle dt').click(function(event){
	  $(this).next().toggle(300, function() {
	    $('#main-content').resetHeight();
	  });
	})
	
	$('dl.toggle dt').css({cursor: 'pointer'});

	$('table.toggle .open').click(function(event){
	  $(this).next().toggle(300, function() {
	    $('#main-content').resetHeight();
	  });
	})
	$('table.toggle .open').css({cursor: 'pointer'});

	$('select.goto').change(function(){
    window.location.hash = $(this).val();
  });

  $('.listing li:last-child').addClass('last');
  
  // rollover images
  $(".rolloverImageLink img").hover(
    function() {
      this.src = this.src.replace("-off","-roll");
    },
    function() {
      this.src = this.src.replace("-roll","-off");
    }
  );
	
  
  $('.tabs').tabs();
  
  $('form.redirect').submit(function(event){
    event.preventDefault();
    var url = $(this).find('select').first().val();
    var target = $(this).attr('target');
    window.open(url, target);
  });
  
  $('form.redirect select').change(function(event){
    $(this).closest('form').submit();
  })
  
  $('.load-more').click(function(event){
  	event.preventDefault();
  	
  	var more = $(this).prev().find('.hidden').first();
  	if (more) {
	  	more.slideDown(2000);
	  	more.removeClass('hidden');
  	}
  	
  	more = $(this).prev().find('.hidden').first();
  	if ( ! more.is('ul') ) {
  		$(this).slideUp(2000);
  	}
  })
  
  
});

$(function(){
  $('#main-content').resetHeight();
})


$.fn.resetHeight = function() {
  if ($('body').hasClass('embed')) {
    return;
  }
  
  //console.log($(this).outerHeight());
  
  $(this).css('height', 'auto');
  
  if ( $(this).height() < $('#menu').height() ) {
    $(this).height($('#menu').height());
  }
  
  if ( $(this).height() < $('#sidebar').height() ) {
    $(this).height($('#sidebar').height());
  }

  
}






















