

// Login to CMS...
function loginCMS () {
  var history="ColbyPrevious=host&"+escape(window.location.host)+"&method&GET&protocol&"+escape(window.location.protocol.substring(0,window.location.protocol.length-1))+"&arguments&&local&0";

  var port=window.location.port;
  if ( ( window.location.protocol == 'https:' ) &&
       ( window.location.port == '443' ) ) { port=''; }

  if ( ( window.location.protocol == 'http:' ) &&
       ( window.location.port == '80' ) ) { port=''; }
  history=history+"&port&"+escape(port);
 
  var query_string='';
  if ( window.location.search.length > 0 ) {
	query_string=window.location.search.substring(1,window.location.search.length);
  }
  query_string=query_string + window.location.hash;
  history=history+"&query_string&"+escape(query_string);

  history=history+"&uri&"+escape(window.location.pathname);

  history=history+"; path=/; domain=colby.edu";
  document.cookie=history;
  window.location="https://www.colby.edu/ColbyMaster/login/";
  return;
}
// BRG REMOVED ON 11/11
//var autoexpand = 0;

// Accordion menu...
$(document).ready(function(){
	if($("#accordion").length){						   
	itemmoved=false;
	$("#tocarea").show();
	$("#accordion").show();					
	firstitem = true;	
	$("#tocarea div").each(function(i){	 
		
		switch($(this).attr("class")){
			case 'cs_control teamlist':
			case 'cs_control CS_Element_LinkBar':
			case 'cs_control CS_Element_PageIndex':	
					if($(this).children("table").length)
						$("#accordion").append('<h3 class="singleitem ui-accordion-header ui-helper-reset ui-state-default ui-corner-all">' +$(this).children("table").html() + '</h3><div></div>');
					else{
							$("#accordion").append($(this).html());

						}
				itemmoved=true;
				firstitem = false;
				break;
   			case 'cs_control TOClink':
			case 'cs_control CS_Element_Textblock':			
				if($(this).html().length > 3){	
					if($(this).html().replace( /^\s*/, "" ).substring(0,3) == "<h3"){/*alert($(this).html());*/$("#accordion").append($(this).html());}
					else{
						if($(this).children("a").length || $(this).children("div").children("a").length){
							// Only do this if there is a link...
							if($(this).children("div").length){
								$("#accordion").append('<h3 class="singleitem ui-accordion-header ui-helper-reset ui-state-default ui-corner-all">' + $(this).children("div").html() + '</h3><div></div>');											
							}
							else{
								$("#accordion").append('<h3 class="singleitem ui-accordion-header ui-helper-reset ui-state-default ui-corner-all">' + $(this).html() + '</h3><div></div>');											
							}	
						}
						else                  
							if($(this).attr("class")=='cs_control TOClink')
								$(this).remove();
							else{
	                            if($(this).text().length)
								    $("#accordion").parent().append(''+$(this).html()+'');
							}
							itemmoved = true;
						}}
				// itemmoved = true;
				firstitem = false;
				break;
			case 'cs_control':
			case 'cs_control CS_Element_CustomCF':
				if($(this).children("div").children("h3").length){
					//if(!itemmoved)
						$("#accordion").append($(this).html());
					itemmoved=1;
				}
				else
					if(($(this).children("div").children("div").children("h3").length)){
						$("#accordion").append($(this).children("div").html());
					}
					else{
						$("#tocarea").parent().append($(this).html());
						itemmoved = 1;
					}
				firstitem = false;
				break;
			case 'cs_control CS_Element_Image':		
				$("#tocarea").append($(this).html());
				firstitem = false;
				break;
			case 'cs_control CS_Element_Layout':
			case 'cs_control CS_Element_CustomCF':			
				break;
			case '':
			default:			
				if(!itemmoved && $(this).attr("id")!="accordion"){
					if($(this).children("h3").length)
						$("#accordion").prepend($(this).html());
					else
						$("#tocarea").prepend($(this).html());
					$(this).remove();
					}
				firstitem = false;					
				break;
		}
		if($(this).attr("id")!="accordion"){
			$(this).remove();}			
		});
						 

	$("#tocarea div.CS_Element_Textblock div").each(function(i){																			  
		if($(this).html().length > 0){
			$(this).remove();}});		
	
	$("#tocarea div.CS_Element_Textblock").each(function(i){																					  
		if($(this).html().length > 3){		
			$(this).remove();}});	
	
	$("#accordion h3.singleitem").hover(function(){$(this).addClass('ui-state-hover');}, function(){$(this).removeClass('ui-state-hover');});  // Non-expandable	
	$("#accordion .CS_Layout_SpacingHeight").remove();
	$("#tocarea .CS_Layout_SpacingHeight").remove();
	$("#accordion .CS_Textblock_Text").remove();	
	$("#tocarea").children("cs_control CS_Element_CustomCF").remove();
	
	$("#tocarea table").remove();	
	$("#accordion").accordion({ header: 'h3.expandable',
								 navigation: 'false',
								 collapsible: 'true',
								 autoHeight:'true',
								 clearStyle: 'true',
								 active: false,
								 animated:'slide'});
	
	curloc = document.location.href;curloc = curloc.replace('http://www.colby.edu','');curloc = curloc.replace('http://author.colby.edu','');curloc = curloc.replace('http://csdev.colby.edu','');	
	if(curloc.substring(curloc.length-1)=='/')					
		locwithpage = curloc + 'index.cfm';
	else
		locwithpage = curloc;
		
	if(typeof autoexpand == "undefined") 
		autoexpand=-1;
	$('#accordion h3 a').each(function(i){			
			// Setup the item location...
			itemlocation = $(this).attr("href");
			itemlocation = itemlocation.replace('http://www.colby.edu','');
			itemlocation = itemlocation.replace('http://author.colby.edu','');
			itemlocation = itemlocation.replace('http://csdev.colby.edu','');

			if(itemlocation=='index.cfm'){
				itemlocation = curloc.match( /.*\// ) + 'index.cfm';		
			}
			
			if(itemlocation ==""){
				itemlocation = curloc;
			}
			
			if($(this).text()=='Recently Added')
				autoexpand = $(this).parent();

			if(itemlocation==curloc || itemlocation==locwithpage || (itemlocation + '/') == curloc){
				if($(this).parent().next("div").children("ul").children("li").length || ($('#accordion h3 a').length-1) == i){
					// Check the ifrst item if there are thildren...
					if($(this).parent().next("div").children("ul").children("li").length){
						itemlocationli = $(this).parent().next("div").children("ul").children("li:first").children("a").attr("href");
						itemlocationli = itemlocationli.replace('http://www.colby.edu','');
						itemlocationli = itemlocationli.replace('http://author.colby.edu','');
						itemlocationli = itemlocationli.replace('http://csdev.colby.edu','');
						
						if(itemlocationli.substr(itemlocationli.length-1,1)=='/')
							itemlocationli = itemlocationli + 'index.cfm';

						if(itemlocationli==curloc || itemlocationli==locwithpage){											
								autoexpand = $(this).parent();
								$(this).parent().next("div").children("ul").children("li:first").children("a").addClass('selectedli');
								return false;												
						};
						
					}
					else{
						autoexpand = $(this).parent();

					}
				}
				else
				{
					
					if(autoexpand == -1)
						autoexpand = -2;
					else{
						if(autoexpand==0)
							autoexpand=-1;
					}
					$(this).addClass('ui-state-focus');
					$(this).addClass('ui-state-active');
					$(this).addClass('ui-state-hover');
				}								
				return false;
			}
			else{
				// Check the children...

				autoexpand_temp = $(this).parent();
				if($(this).parent().next().find('ul').length){
					// Subitems...
					$(this).parent().next().find('ul li').children('a').each(function(x){																				  	
						itemlocationli = $(this).attr("href");
						itemlocationli = itemlocationli.replace('http://www.colby.edu','');
						itemlocationli = itemlocationli.replace('http://author.colby.edu','');
						itemlocationli = itemlocationli.replace('http://csdev.colby.edu','');
						if(itemlocationli==curloc || itemlocationli==locwithpage){											
								autoexpand = autoexpand_temp;
								$(this).addClass('selectedli');
								return false;												
						}});
				}								
			}   
	});
/*	if(autoexpand==-1)
		autoexpand=0; */
	if(autoexpand != -1){	
		$('#accordion').accordion('activate',autoexpand);
	}
	
	$("#tocarea").show();
	$("#accordion").show();
	}
});