$(document).ready(function(){    
	
	// Back to Top
	var scroll_timer;
	var displayed = false;
	var $message = $('a.back_to_top');
	var $window = $(window);
	var top = $(document.body).children(0).position().top;
 
	$window.scroll(function () {
		window.clearTimeout(scroll_timer);
		scroll_timer = window.setTimeout(function () { 
			if($window.scrollTop() <= top) 
			{
				displayed = false;
				$message.fadeOut(500);
			}
			else if(displayed == false) 
			{
				displayed = true;
				$message.stop(true, true).show().click(function () { $message.fadeOut(500); });
				$('a.back_to_top').click(
					function() { 
						$("html, body").animate({ scrollTop: 0 }, 500);
					}
				);
			}
		}, 100);
	});
	
	

    var currentSubmenu = '';
    var submenuTimeout = '';

    function showSubMenu(){
	$('#sub-navigation > ul > li > ul').not('.frontload').addClass('frontload');
        currentSubmenu.children('ul').removeClass('frontload');
    }

    /*** subnav interactivity (hovers) ***/
    $('#sub-navigation > ul > li.active > ul').removeClass('frontload');
    $('#sub-navigation > ul > li').hover(function(){
	currentSubmenu = $(this);
        submenuTimeout = window.setTimeout(showSubMenu, 250);
    });

    $('#sub-navigation > ul').mouseleave(function(){
	$('#sub-navigation > ul > li > ul').addClass('frontload');
	$('li.active', this).children('ul').removeClass('frontload');
	currentSubmenu = $('li.active', this);
        submenuTimeout = window.setTimeout(showSubMenu, 250);
    });

    $('#sub-navigation > ul > li').each(function(){
	if($(this).children().size() > 1){
	    $('> a', this).append('&nbsp;&raquo;');
	}
    });

        
    /* search box stuff */
    $('#search-website').bind('submit', function(){
	$('#search-website').attr('action','/search/' + $('#searchInput').val());
    });
    
    $('#searchInput').bind({
	focus: function() {
	    $(this).val("");
	},
	blur: function() {
	    var val = jQuery.trim($(this).val());
	    if (val == ''){
		$(this).val("Search cornish.edu");
	    }
	}
    });
    
    
    /***
     * Quicklinks code
     * **/
    var navVisible = false;
    var navTimer = null;
    var highlightCounter = 0;
	
/*
    function highlightQuickLinks(flashes) {	
	$('#page-head p.quick-links a').animate({ backgroundColor: '#d3007f' , color: '#ffffff'}, 400, function(){
	    $(this).animate({ backgroundColor: '#f0f0f0', color: '#d3007f' }, 400, function(){
		$(this).css({ 'background-color': 'transparent' });
		if(highlightCounter < flashes){
		    highlightCounter++;
		    highlightQuickLinks(flashes);
		}
	    });	
	});
    }
*/
    
    //highlightQuickLinks(1);
    
    function hideQuickLinks() {
	$('#page-head nav.quick-links').fadeOut();
/* 
	$('#page-head p.quick-links').css({
	    'background-color' : 'transparent',
	    'border' : 'none'
	});
*/
	navVisible = false;
    }
    
    $('#page-head p.quick-links').bind({
	mouseenter: function(){
	    clearTimeout(navTimer);
/* 
	    $(this).css({
		'background-color' : '#000000',
		'height' : '20px'
	    });
*/
	    
	    if(!navVisible){
		navVisible = true;
		$(this).next().hide();
		$(this).next().removeClass('frontload');
		$(this).next().fadeIn();
	    }
	},
	mouseleave: function(){
	    navTimer = setTimeout(hideQuickLinks, 200);
	}
    });
    
    $('#page-head nav.quick-links').bind({
	mouseover: function(){
	    clearTimeout(navTimer);
	},
	mouseleave: function(){
	    navTimer = setTimeout(hideQuickLinks, 75); 
	}
    });
    
    
	// Pumpkins
	$('nav.auxiliary-content').css({'display':'none'});
	$('nav.auxiliary-content').fadeIn(2000);
	
	$('div.auxiliary-content').css({'display':'block'}).css({'height':'01px'});
	$('a.search, a.evening_edition').click(
		function() {
			if($('nav.auxiliary-content').hasClass('active')) {
				$('#page').animate({'margin-top':'00'}, 350 );
				$('nav.auxiliary-content').animate({'top':'00'}, 350 );
				$('div.auxiliary-content').animate({'height':'01px'}, 350);
				$('nav.auxiliary-content').removeClass('active');
			} else {
				$('#page').animate({'margin-top':'196px'}, 350 );
				$('nav.auxiliary-content').animate({'top':'196px'}, 350 );
				$('div.auxiliary-content').animate({'height':'198px'}, 350);
				$('nav.auxiliary-content').addClass('active');
			}
		}
	);
	
	$('a.email').click(
		function() {
			if($(this).hasClass('active')) {
				$(this).next().animate({'width':'toggle'});
				$(this).css({'background':'url(http://www.cornish.edu/content/images/renovate/_nav_auxiliary_content/letter_closed.png) no-repeat'});				
				$(this).removeClass('active');
			} else {
				$(this).next().animate({'width':'toggle'});
				$(this).css({'background':'url(http://www.cornish.edu/content/images/renovate/_nav_auxiliary_content/letter_open.png) no-repeat'});
				$(this).addClass('active');				
			}
			
			
		}
	);
	
	
	
	
	

    /** featured sub nav (ie, admission) **/
    $('nav#featured-sub-navigation > ul > li').bind({
	click: function(){
	    if($('nav#featured-sub-navigation').hasClass($(this).attr('id')+ '-open')){
		$('nav#featured-sub-navigation').toggleClass($(this).attr('id') + '-open');
		$(this).children('ul').toggle();
		$(this).children().children('a').removeClass('current');
	    }
	    else {
		$('nav#featured-sub-navigation ul li ul').hide();
		$('nav#featured-sub-navigation li a').removeClass('current');
		$('nav#featured-sub-navigation').removeAttr('class').attr('class', 'width4 first leading ' + $(this).attr('id') + '-open');
		$(this).children('ul').show();
		$(this).children().children('a').addClass('current');
	    }
	}
	
    });
    $('nav#featured-sub-navigation ul li ul').hide();    

    $('#toggle-grid').click(function(){
	$('.layout-grid').toggle();
    });
   
   
   
        
});
