/*

Javascript for Northwestern University (Kellogg). Copyright (c) 2009 Northwestern University

*/

$(document).ready(function(){

	$(document).pngFix();
	
	/*
	
	Search Form Value Swap / Search Options Box
	
	*/
	
	$(".search input.field").focus(function() {
		
		if($(this).val() == $(this).attr("alt")) {
		
			$(this).val("");
		
		}
		
		$(".search .search_options").css("display", "block");
		
	});
	
	$(".search input.field").blur(function() {
		
		//If search options are open, we don't want to replace this value on blur
		
		if($(".search .search_options").css("display") == "none") {
		
			if($(this).val().length == 0) {
			
				$(this).val($(this).attr("alt"));
			
			}
			
		}
		
	});
	
	$(".search .search_options .close").click(function() {
		
		if($(".search input.field").val().length == 0) {
		
			$(".search input.field").val($(".search input.field").attr("alt"));
		
		}
		
		$(this).parent().css("display", "none");
	
	});
	
	/*
	
	Expandable Guides
	
	*/
	
	$('.expandable_guides .inner_content').each(function(i) {
		
		if($(this).hasClass("expanded")) {
		    
		    $(this).parent().find('.header .open_close').removeClass('open');
		    
		    $(this).parent().find('.header .open_close').addClass('close');
		    
		} else {
		    
		    //It has the class of close, so its already open they now want to close it
		    
		}
		
	});
	
	$(".open_close").click(function(){
	
		if($(this).hasClass("open")) {
			
			//It has a class of open, so they are going to want to open it
			
			$(this).removeClass('open');
			
			$(this).addClass('close');
			
			$(this).parent().next().slideToggle("slow", function() {
				
				$(this).addClass('expanded');
				
			});
			
		} else {
			
			//It has the class of close, so its already open they now want to close it
			
			$(this).removeClass('close');
			
			$(this).addClass('open');
			
			$(this).parent().next().slideToggle("slow", function() {
				
				$(this).removeClass('expanded');
				
			});
			
		}
	
	});
	
	//Primary Tabs
	
	$('.tabs_box_primary').each(function(i) {
		
		$(this).find('.header').css('display', 'block');
	
		$('.tabs_box_primary .header ul li a').each(function(i) {
			
			var name = $(this).attr('name');
			
			$('.tabs_box_primary .inner_content .inner_content_middle#' + name).addClass('hide');
			
			currentHash=document.location.hash.substring(2,document.location.hash.length);

            if ((currentHash=="" && i==0)   |   currentHash==name   |      name.indexOf("_0")>0            ){

                                                            $(this).addClass("active");

                                                            $('.tabs_box_primary .inner_content .inner_content_middle#' + name).removeClass('hide');

                                                            $('.tabs_box_primary .inner_content .inner_content_middle#' + name).addClass('show');

            }

            if (      currentHash.lastIndexOf("_0",0)==-1  && currentHash!=""   && currentHash!=name &&   currentHash.substring(0,currentHash.lastIndexOf("_"))==name.substring(0,name.lastIndexOf("_"))   ){

                                                            $(this).removeClass("active");

                                                            $('.tabs_box_primary .inner_content .inner_content_middle#' + name).addClass('hide');

                                                            $('.tabs_box_primary .inner_content .inner_content_middle#' + name).removeClass('show');

            }
           
			
		});
	
	});
		
	$('.tabs_box_primary .header ul li a').click(function(){
		
		$(this).parent().parent().parent().parent().find('.header ul li a').each(function(i) {
			
			var name = $(this).attr('name');
			
			$(this).parent().parent().parent().parent().find('.header ul li a').removeClass('active');
			
			$(this).parent().parent().parent().parent().find('.inner_content .inner_content_middle#' + name).removeClass('show');
			
			$(this).parent().parent().parent().parent().find('.inner_content .inner_content_middle#' + name).addClass('hide');
			
		});
		
		var name2 = $(this).attr('name');
		
		$(this).addClass('active');
		
		$(this).parent().parent().parent().parent().find('.inner_content .inner_content_middle#' + name2).removeClass('hide');
		
		$(this).parent().parent().parent().parent().find('.inner_content .inner_content_middle#' + name2).addClass('show');
		
	});
	
	//Secondary Tabs
	
	$('.tabs_box_secondary').each(function(i) {
		
		$(this).find('.header').css('display', 'block');
	
		$('.tabs_box_secondary .header ul li a').each(function(i) {
			
			var name = $(this).attr('name');
			
			$('.tabs_box_secondary .inner_content .inner_content_middle#' + name).addClass('hide');
			
			if($(this).hasClass("active")) {
				
				$('.tabs_box_secondary .inner_content .inner_content_middle#' + name).removeClass('hide');
				
				$('.tabs_box_secondary .inner_content .inner_content_middle#' + name).addClass('show');
				
			}
			
		});
	
	});
		
	$('.tabs_box_secondary .header ul li a').click(function(){
		
		$(this).parent().parent().parent().parent().find('.header ul li a').each(function(i) {
			
			var name = $(this).attr('name');
			
			$(this).parent().parent().parent().parent().find('.header ul li a').removeClass('active');
			
			$(this).parent().parent().parent().parent().find('.inner_content .inner_content_middle#' + name).removeClass('show');
			
			$(this).parent().parent().parent().parent().find('.inner_content .inner_content_middle#' + name).addClass('hide');
			
		});
		
		var name2 = $(this).attr('name');
		
		$(this).addClass('active');
		
		$(this).parent().parent().parent().parent().find('.inner_content .inner_content_middle#' + name2).removeClass('hide');
		
		$(this).parent().parent().parent().parent().find('.inner_content .inner_content_middle#' + name2).addClass('show');
		
	});
	
});

/*
	
	I Banner
	
	*/
	function setMBox(iDuration, blnOnOff)
	{
	//Set this to true to have the banner rotate, if false banner won't rotate but clickable links will still work
	var onOff = blnOnOff;
	
	//How long should the animation pause before it switches slides?
	var duration = iDuration;
	
	//How long should we wait after the person's mouse moves off of the banner?
	var hoverOutDuration = 5000;
	
	//Speed of the fade in
	var fadeSpeed = 0;
	
	var currentBanner = 0;
	
	var nextBanner = 0;
	
	var numBanners = $('.i_banner .i_banner_bottom ul li').length;
	
	var Busy = false;
	
	//By default the banner is set to display none. so lets set the i banner class to block as well as the content and image that go along with the active button.
	$('.i_banner').css('display', 'block').each(function(i) {
		
		//This will generate a random number bettween 1 and 4
		currentBanner = Math.floor(Math.random()*4) + 1;
		
		$(this).find('.i_banner_bottom ul li a.active').removeClass('active');
		
		$('.i_banner .i_banner_bottom ul li a[name=\'' + currentBanner + '_i_banner\']').addClass('active');
		
		//Uncomment this if we want to allow them to assign the default banner (be sure to comment out the line about which selects one at random
		
		//currentBanner = parseInt($(this).find('.i_banner_bottom ul li a.active').attr('name'));
		
		$(this).find('.i_banner_top .overlay .content div#i_banner_content_' + currentBanner).css('display', 'block');
		
		$(this).find('.i_banner_top .images div#i_banner_image_' + currentBanner).css('display', 'block');
		
	});
	
	if(onOff) {
	
		var bannerRotate = function() {
			
			if(!Busy) {
				
				Busy = true;
				
				bannerTimeout = false;
				
				currentBanner = parseInt($('.i_banner .i_banner_bottom ul li a.active').attr('name'));
				
				//We are assuming the banners start with 1, not 0.
				if(currentBanner == numBanners) {
					
					nextBanner = 1;
					
				} else {
				
					nextBanner = currentBanner + 1;
					
				}
				
				$('.i_banner .i_banner_bottom ul li a[name=\'' + currentBanner + '_i_banner\']').removeClass('active');
				
				$('.i_banner_top .images div#i_banner_image_' + currentBanner).css('display', 'none');
				
				$('.i_banner_top .images div#i_banner_image_' + + nextBanner).fadeIn(fadeSpeed);
				
				$('.i_banner_top .overlay .content div#i_banner_content_' + currentBanner).css('display', 'none');
				
				$('.i_banner_top .overlay .content div#i_banner_content_' + nextBanner).fadeIn(fadeSpeed);
				
				$('.i_banner .i_banner_bottom ul li a[name=\'' + nextBanner + '_i_banner\']').addClass('active');
				
				Busy = false;
				
				if(!bannerTimeout) {
							
					bannerTimeout = setTimeout(bannerRotate, duration);
					
				}
				
			}
			
		};
		
		var bannerTimeout = setTimeout(bannerRotate, duration);
		
		$('.i_banner').hover(function() {
		    
		    clearTimeout(bannerTimeout);
			
			bannerTimeout = false;
		    
		}, function() {
		    
		    if(!bannerTimeout) {
		    
		    	bannerTimeout = setTimeout(bannerRotate, hoverOutDuration);
		    	
		    }
		
		});
	
	}
	
	$('.i_banner .i_banner_bottom ul li a').click(function() {
		
		currentBanner = parseInt($(this).attr('name'));
		
		$(this).parent().parent().parent().parent().find('.i_banner_top .overlay .content div').each(function(i) {
			
			$(this).css('display', 'none');
			
		});
		
		$(this).parent().parent().parent().parent().find('.i_banner_top .images div').each(function(i) {
			
			$(this).css('display', 'none');
			
		});
		
		$(this).parent().parent().find('a.active').removeClass('active');
		
		$(this).addClass('active');
		
		$(this).parent().parent().parent().parent().find('.i_banner_top .images div#i_banner_image_' + currentBanner).fadeIn(fadeSpeed);
		
		$(this).parent().parent().parent().parent().find('.i_banner_top .overlay .content div#i_banner_content_' + currentBanner).fadeIn(fadeSpeed);

		clearTimeout(bannerTimeout);
			
		bannerTimeout = true;
		
	});
}