/* Southwestern University Effects (requires jQuery) */
/* by White Whale Web Services */

var subnavs,hoverTimer,hideTimer;
var img_offset=0;

$(document).ready(function() {
	equalColumns();
	
	// NAV DEBUG:
	/*$('#subnavigation:not(.horizontal) a').each(function() {
		var href=$(this).attr('href'),
			id=$(this).parent().attr('id'),
			parts = href.split('/');
		if(!id) {
			$(this).css('border','2px dashed #00c').attr('title','nav_'+href);
			return;
		}
		if(parts[1]=='departments'||parts[1]=='offices'||parts[2]=='sira'||parts[2]=='llc'||parts[2]=='registrar') parts = parts.slice(3);
		else parts = parts.slice(2);
		href = parts.join('_');
		if(href.indexOf('.php')!=-1||href.indexOf('.html')!=-1) { href = href.replace('.php','').replace('.html',''); }
		else href+=(href.lastIndexOf('_')==href.length-1 ? 'index' : '_index');
		if('nav_'+href!=id) $(this).css('border','2px dashed #c00').attr('title','nav_'+href).show().parents().show();
	});*/
	// END NAV DEBUG
	
	var classes = $('body').attr('class').split(' '), // get all of the body classes
		id = $('body').attr('id');

	// HANDLE SUBNAVS
	$('#subnavigation:not(.horizontal)').find('.active') // find the active class (we don't apply this in .horizontal navs)
		.parents('ul') // find all of its parent lists
			.siblings() // grab their siblings
			.andSelf() // and themselves
				.show(); // and make sure they're all showing
	$('#subnavigation li:not(:has(>a))') // in addition, find any li that doesn't have a link
		.children() // find its children (<ul>s)
			.show(); // and show them

	// Quick Access for Search
	var query = $('#search_query');
	if(!query.val()) {
		query.attr('autocomplete','off').val('Search').one('focus',function() {
			$(this).val('');
			$('#search').append('<div id="qa_results"/>');
			$.get('/includes/quickaccess.html',function(data) {
				$('body').append(data);
				$('#search_query').quickaccess({selector:'#qa_links a',results:'#qa_results',focus:false,forceSelect:false});
				$('#qa_results').removeClass('qa_blur');
			});
		});
	}
		
	// Quick access for gateways
	try {
		$('#quickquery').quickaccess({selector:'#qa_links a,.qa'});
	} catch(e) {}
		
	if($('body').hasClass('departments')) {
		$.each(classes,function() { $('#departmentlist .nav_'+this).addClass('active'); });
	}
	
// Mottoes
	var mottoes = new Array(
		{img:'engagingmindstransforminglives',alt:'Engaging Minds, Transforming Lives'},
		{img:'georgetowntexas',alt:'Georgetown, Texas'},
		{img:'nonquissedquid',alt:'Non Quis Sed Quid'},
		{img:'since1840',alt:'Since 1840'},
		{img:'texasfirstuniversity',alt:'Texas\' First University'},
		{img:'tosurviveandexcel',alt:'To Survive and Excel'}
	);	
	var motto = mottoes[Math.floor(Math.random()*mottoes.length)];
	$('#motto img').attr('src','/images/motto/'+motto.img+'.gif').attr('alt',motto.alt).css('cursor','pointer');
	$('#motto img').click(function() {
		var blackout = $('<div class="blackout"/>').prependTo('body').hide().fadeTo(1,0,function() {
			$(this).show().fadeTo(500,0.9).click(function() { $('.blackout,#motto_description').remove(); });
		});
		var spinner = $('<div class="spinner"/>').height($(window).height()).appendTo(blackout);
		var motto_description = $('<div id="motto_description"/>').prependTo('body');
		$.get('/includes/motto/'+motto.img+'.html',function(data) {
			var src = $(data).find('img').eq(0).attr('src');
			$('<img src="'+src+'"/>').load(function() { // load the image first
				spinner.fadeOut(function() {
					$(this).remove();
					motto_description.html(data).fadeIn(1000);
					$('.motto_close').click(function() { $('.blackout,#motto_description').remove(); return false; });
				});								
			});
		});
	});

	// Departments popup
	$('#show_departments').click(function() {
		var departments = $('<div id="all_departments"/>').prependTo('body').fadeIn(300);
		var spinner = $('<div class="spinner"/>').height($(window).height()).appendTo(departments);
		$.get('/includes/departmentlist.html',function(data) {
			var categories = new Array();
			$.each($('<div>'+data+'</div>').find('ul'),function() {
				var list = new Array();
				$.each($(this).find('li'),function() { list.push($(this).html()); });
				//list[list.length-2]=list[list.length-2]+ ' and ' + list.pop();
				categories.push(list.join(', '));
			});
			spinner.fadeOut(function() {
				$(this).remove();
				departments.html('<h3>Majors</h3>' + categories[0] + '<br/><h3>Other Programs of Study</h3>' + categories[1] + '<br/><a class="departments_close" href="#">Close</a>').fadeIn(1000);
				$('.departments_close').click(function() { $('#all_departments').remove(); return false; });
			});
		});
		return false;
	});
	
//Inside drop-down scripts
	$('ul.dropdown li:even').addClass('odd_row');//zebra striping the li's
	
	dropdowns = $('#subnavigation.horizontal>li'); // grab all dropdowns
	dropdowns.hover( // the dumb flyout behavior (for instant satisfaction)
		function() { $(this).addClass('selected').find('ul.dropdown').show(); },
  		function() { $(this).removeClass('selected').find('ul.dropdown').hide(); }
	);


//Inside drop-down scripts	(Sarofim)
	dropdowns = $('#subnavigation_sarofim>li'); // grab all dropdowns
	dropdowns.hover( // the dumb flyout behavior (for instant satisfaction)
		function() { $(this).addClass('selected').find('ul.dropdown').show(); },
  		function() { $(this).removeClass('selected').find('ul.dropdown').hide(); }
	);

//zebra striping the li's in calendar
	$('.calendarlist ul li:even').addClass('odd_row');	

// Scrolling image feature
	$('#images').children().clone().appendTo('#images');//appends a copy of all lis after the last li
	$('#images').children().clone().appendTo('#images');//appends a second copy
	$('#up').attr('disabled', 'disabled').fadeTo(300, .5);//disable and fade #up on load
	
	$('#down').click(function(){ 
		img_offset+=$('#images li').height()*3;
		if(img_offset>=$('#images').height()/3) $('#down').attr('disabled', 'disabled').fadeTo(300, .5);
		$('#images').animate({top:'-'+img_offset+'px'},400);
		$('#up').removeAttr('disabled').fadeTo(300, 1);
	});
	$('#up').click(function(){ 
		img_offset-=$('#images li').height()*3;
		if(img_offset<=0) $('#up').attr('disabled', 'disabled').fadeTo(300, .5);
		$('#images').animate({top:'-'+img_offset+'px'},400);
		$('#down').removeAttr('disabled').fadeTo(300, 1);
	});

	$(window).load(function() { setTimeout(equalColumns,250); }); // after _everything_ has loaded
});

function equalColumns() { // Equal height for subpage columns
	var content_height=$('div#content').height('auto').height();
	var sidebar_height=$('div#resource_sidebar,div#departmentlist,div#imgsidebar').height('auto').height(); 
	if(sidebar_height>content_height) $('div#content,div#imgsidebar').height(sidebar_height+'px'); 
	else $('div#resource_sidebar,div#departmentlist,div#imgsidebar').height(content_height+'px');
};