var SCPD = function() {
	return {
		mystanfordconnection : function() {
			// the objects we'll be working with:
			$container = $('#mystanfordconnection');
			$hideable = $('.hideable', $container);
			$trigger = $('#header li.mystanford a');	
			// give parents of links marked as 'selected' the same class (LI and A both yellow)
			$('body.logged-in #mystanfordconnection .content>ul li a.selected').parent('li').addClass('selected');
			// close anything that's doesn't have a child marked as selected
			$('body.logged-in #mystanfordconnection .content>ul li:not(:has(.selected))').addClass('closed');
			// init the tree
			$('body.logged-in #mystanfordconnection .content>ul').treeview({animated: 'fast'});
			
			if( $('a.selected', $hideable).length == false && $container.hasClass('open') == false ) { SCPD.hide() }
			$('#header li.mystanford a').click(function(){SCPD.toggle(); return false });
		},
		toggle : function() {
			if($trigger.hasClass('open')) {
				$trigger.removeClass('open');
				$("#exceptionWindow:visible").slideUp("slow");	
				$hideable.slideUp(null,function(){ $container.addClass('closed'); });
			} else {
				$trigger.addClass('open');
				
				$container.removeClass('closed');
				$hideable.slideDown(null,function(){ /*$("#username",$hideable).focus();*/ });
				
			}
		},
		hide : function() {
			$hideable.hide();
			$trigger.removeClass('open');
		},
		show : function() {
			$container = $('#mystanfordconnection');
			$hideable = $('.hideable', $container);
			$trigger = $('#header li.mystanford a');	
			//alert($hideable);
			$hideable.show();
			$trigger.addClass('open');
			$container.removeClass('closed');
		},
		nav : function(){
			// give parents of links marked as 'selected' the same class (LI and A both yellow)
			$('#navigation li a.selected').parent('li').addClass('selected');
			// close anything that's doesn't have a child marked as selected
			$('#navigation li:not(:has(.selected))').addClass('closed');
			// only toggle before-collapsable if the element is a top-level item
			$('#navigation>ul>li:has(ul li.selected)').prev().addClass('before-collapsable');
			// init the tree
			$('#navigation>ul').treeview({animated: 'fast'});
		},
		popup : function(trigger) {
		   popupwindow = window.open(trigger.href,'popupwindow','resizable,scrollbars,height=500,width=600');
			if (window.focus) {popupwindow.focus()}
		},
		popupec : function(trigger) {
		   popupwindow = window.open(trigger.href,'popupwindow','resizable,scrollbars,width=460,height=550');
			if (window.focus) {popupwindow.focus();}
		},
		collapsing_tables : function(){
			/*
				Apply "alt" class for striped rows
				For any tables that have more than 3 rows (SHOW_ROWS)...
				* Hide the extra rows
				* Add a trigger icon to the thead that toggles the hidden rows
				* Add a tfoot with the "more" icon that shows how many more rows are hidden
			*/
			var SHOW_ROWS=new Array();
			$('table.collapsable').each(function(i){
				SHOW_ROWS[i] = $('tbody tr td span',this).length;
				ROW_COUNT = $('tbody tr',this).length;
				$trigger = $('<a href="#" class="toggle closed">#</a>').click(function(){
					$table = $(this).parents('table');
					$('a.toggle',$table).toggleClass('closed').toggleClass('opened');
					$('tr:gt('+ SHOW_ROWS[i] +')',$table).toggle();
					return false
				});
				$tfoot = $('<tfoot><tr><td colspan=' + $('tbody tr:first td',this).length + '><a href="#"><strong>View more applicable courses...</strong></a></td></tr></tfoot>');
				$('tr:odd',this).addClass('alt');
				
				if (ROW_COUNT > SHOW_ROWS[i]) {
					$('tr:gt('+ SHOW_ROWS[i] +')',this).hide();
					$trigger.attr('title',ROW_COUNT + ' ' + $('thead th:first',this).text());
					$('thead th:first',this).prepend($trigger);
					$('td a strong',$tfoot).click(function(){
						$table = $(this).parents('table');
						$('a.toggle',$table).toggleClass('closed').toggleClass('opened');
						$('tr:gt('+ SHOW_ROWS[i] +')',$table).toggle();
						return false
					});
					$(this).append($tfoot);
				}
			});
		}
	};
}();




/* flickering fix for IE6 */
try { document.execCommand('BackgroundImageCache', false, true);} catch(err) {};

/* DEBUGGING - supress Firebug console outside of FF */
if(!("console"in window)||!("firebug"in console)){var names=["log","debug","info","warn","error","assert","dir","dirxml","group","groupEnd","time","timeEnd","count","trace","profile","profileEnd"];window.console={};for(var i=0;i<names.length;++i)window.console[names[i]]=function(){}};

$().ready(function(){ 
	/* clear the seach box when clicked on*/
	$('#search form input.text').focus(function(){
		if( $(this).attr("value") == "Search:") $(this).attr("value","");
	}).blur(function(){
		if( !$(this).attr("value")) $(this).attr("value","Search:")
	});
	/* initialize popups with rel="popup" */
	
	$('a[rel=popup]').click(function(){
			SCPD.popup(this);	
			return false;
	});
	$('a[rel=popupec]').click(function(){
			SCPD.popupec(this);	
			return false;
	});
	
});




/*
 * Treeview 1.4 - jQuery plugin to hide and show branches of a tree
 * 
 * http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
 * http://docs.jquery.com/Plugins/Treeview
 *
 * Copyright (c) 2007 Jörn Zaefferer
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
(function(A){A.extend(A.fn,{swapClass:function(E,D){var C=this.filter("."+E);this.filter("."+D).removeClass(D).addClass(E);C.removeClass(E).addClass(D);return this},replaceClass:function(D,C){return this.filter("."+D).removeClass(D).addClass(C).end()},hoverClass:function(C){C=C||"hover";return this.hover(function(){A(this).addClass(C)},function(){A(this).removeClass(C)})},heightToggle:function(C,D){C?this.animate({height:"toggle"},C,D):this.each(function(){jQuery(this)[jQuery(this).is(":hidden")?"show":"hide"]();if(D){D.apply(this,arguments)}})},heightHide:function(C,D){if(C){this.animate({height:"hide"},C,D)}else{this.hide();if(D){this.each(D)}}},prepareBranches:function(C){if(!C.prerendered){A(".treeview>li:first-child").addClass("first");A(".treeview li li").addClass(B.nested);this.filter(":last-child:not(ul)").addClass(B.last);this.filter((C.collapsed?"":"."+B.closed)+":not(."+B.open+")").find(">ul").hide()}return this.filter(":has(>ul)")},applyClasses:function(C,D){this.filter(":has(>ul):not(:has(>a))").find(">span").click(function(E){D.apply(A(this).next())}).add(A("a",this)).hoverClass();if(!C.prerendered){this.filter(":has(>ul:hidden)").addClass(B.expandable).replaceClass(B.last,B.lastExpandable);this.not(":has(>ul:hidden)").addClass(B.collapsable).replaceClass(B.last,B.lastCollapsable);this.prepend('<div class="'+B.hitarea+'"/>').find("div."+B.hitarea).each(function(){var E="";A.each(A(this).parent().attr("class").split(" "),function(){E+=this+"-hitarea "});A(this).addClass(E)})}this.find("div."+B.hitarea).click(D)},treeview:function(D){D=A.extend({cookieId:"treeview"},D);if(D.add){return this.trigger("add",[D.add])}if(D.toggle){var I=D.toggle;D.toggle=function(){return I.apply(A(this).parent()[0],arguments)}}function C(L,N){function M(O){return function(){F.apply(A("div."+B.hitarea,L).filter(function(){return O?A(this).parent("."+O).length:true}));return false}}A("a:eq(0)",N).click(M(B.collapsable));A("a:eq(1)",N).click(M(B.expandable));A("a:eq(2)",N).click(M())}function F(){if(A(this).hasClass("selected-hitarea")){return false}A(this).parent().find(">.hitarea").swapClass(B.collapsableHitarea,B.expandableHitarea).swapClass(B.lastCollapsableHitarea,B.lastExpandableHitarea).end().swapClass(B.collapsable,B.expandable).swapClass(B.lastCollapsable,B.lastExpandable).find(">ul").heightToggle(D.animated,D.toggle);if(!A(this).parent().hasClass("nested")){A(this).parents("li:not(.nested)").prev().toggleClass("before-collapsable")}if(D.unique){A(this).parent().siblings().find(">.hitarea").replaceClass(B.collapsableHitarea,B.expandableHitarea).replaceClass(B.lastCollapsableHitarea,B.lastExpandableHitarea).end().replaceClass(B.collapsable,B.expandable).replaceClass(B.lastCollapsable,B.lastExpandable).find(">ul").heightHide(D.animated,D.toggle)}}function K(){function M(N){return N?1:0}var L=[];J.each(function(N,O){L[N]=A(O).is(":has(>ul:visible)")?1:0});A.cookie(D.cookieId,L.join(""))}function E(){var L=A.cookie(D.cookieId);if(L){var M=L.split("");J.each(function(N,O){A(O).find(">ul")[parseInt(M[N])?"show":"hide"]()})}}this.addClass("treeview");var J=this.find("li").prepareBranches(D);switch(D.persist){case"cookie":var H=D.toggle;D.toggle=function(){K();if(H){H.apply(this,arguments)}};E();break;case"location":var G=this.find("a").filter(function(){return this.href.toLowerCase()==location.href.toLowerCase()});if(G.length){G.addClass("selected").parent().addClass("selected").parents("ul, li").add(G.next()).show();A(G).parents("li:not(.nested)").prev().addClass("before-collapsable")}break}J.applyClasses(D,F);if(D.control){C(this,D.control);A(D.control).show()}return this.bind("add",function(M,L){A(L).prev().removeClass(B.last).removeClass(B.lastCollapsable).removeClass(B.lastExpandable).find(">.hitarea").removeClass(B.lastCollapsableHitarea).removeClass(B.lastExpandableHitarea);A(L).find("li").andSelf().prepareBranches(D).applyClasses(D,F)})}});var B=A.fn.treeview.classes={open:"open",closed:"closed",expandable:"expandable",expandableHitarea:"expandable-hitarea",lastExpandableHitarea:"lastExpandable-hitarea",collapsable:"collapsable",collapsableHitarea:"collapsable-hitarea",lastCollapsableHitarea:"lastCollapsable-hitarea",lastCollapsable:"lastCollapsable",lastExpandable:"lastExpandable",last:"last",nested:"nested",hitarea:"hitarea"};A.fn.Treeview=A.fn.treeview})(jQuery);