/**
 * gc_phil.js
 */

/**
 * Custom jQuery extentions
 */
(function($) {

$.fn.makeMenu = function(steadySubmenu) {
	$(this).parent('div')																			// get the parent div of the element on which makeMenu is called (e.g. #mainMenu)
		.nextAll('.hdBar').remove().end()															// remove any .hdBar divs after it
		.after('<div class="hdBar gray"><\/div>')													// place exactly one gray .hdBar after it
		.add($(this).parent().next())																// include the newly created gray .hdBar in the jQuery stack
		.wrapAll(( $(this).parents('.menuWrapper').length == 0 ) ? '<div class="menuWrapper"><\/div>' : '');		// wrap both of those divs in either a .menuWrapper div (if they are not already contained in one) or nothing (if they are contained in a .menuWrapper div)
	
	$(this).children('li').hoverIntent(
		 function() {
			$(this).parents('.hdBar').next().empty().html($(this).children('ul').clone(true)); /* $(this) is a list item in the main menu. This command tells jQuery to put the contents of the <ul> within $(this) in the submenu. */
		},
		function() {}
	);
	$(this).parents('.menuWrapper').hoverIntent(	function() {},
													function() { 
														// If the list in the submenu div is the same as the list in the steadySubmenu...
														if ( $('.menuWrapper > div:eq(1) > ul').html() != $('#mainMenu > li[@title="'+steadySubmenu+'"] > ul').html() ) {
															// ...then fade the list in the submenu div and replace it with the steadySubmenu.
															$('.menuWrapper > div:eq(1)').children().fadeOut('normal',showSteadySubmenu);
														}
													}
													);
	function showSteadySubmenu() { $('.menuWrapper').children('div:eq(1)').empty().html($('#mainMenu').children('li[@title="'+steadySubmenu+'"]').clone(true).children('ul').clone(true)); }

	/*--- HIGHLIGHT STEADY SUBMENU ---*/
	showSteadySubmenu();
	$('#mainMenu').children('li[@title="'+steadySubmenu+'"]').children('a').css('color','#009');

}

/**
 * deobfuscate() deobfuscates email addresses contained in elements of class antispider.
 * If it seems overly complicated, it is. It's an attempt to confuse even the clever spiders.
 * See the Philosophy Program webmaster's manual for more information.
 */
$.fn.deobfuscate = function(prefix) {

  //TODO: Pass prefix into the function called below so that it can be written into the page.
  
  /**
   * For each element of class antispider, do the following:
   *  1. Set a new attribute, called prefix, using the prefix variable.
   *     This allows prefix to be accessed from inside the function that's about to be called.
   *  2. Call a function to turn each antispider element into a mailto link.
   */
  $(this).find('.antispider').each(function() {
    
    /**
     * Set some variables to call later. By keeping @ and . out of the replace commands, I hope to confuse some spiders.
     */
    var squiggle = '@'; 
    var drop = '.';
    
    /**
     * Replace [at-sign-here] and [put-a-dot-here] with the real symbols in $(this).text()
     */
    
    $(this).text($(this).text()
	    			.replace(/ \[at-sign-here\] /,squiggle)
	    			.replace(/ \[put-a-dot-here\] /g,drop)); 

	var content = 'foobar';
	
	/**
	 * Set the new content for the antispider span.
	 * If the email address is surrounded by double brackets (e.g., [[foo@bar.edu]]), replace it with a mailto: link wrapped around whatever other text is in the antispider span.
	 * Otherwise, just wrap the email address in a mailto: link.
	 * See the main site homepage for an example of the first usage.
	 */
	var regExp = /\[\[\s*?\S+?@\S+?\s*?\]\]/;
	if ($(this).text().match(regExp)!=null) {
	  var innerText = $(this).text().split(/\s*\[\[\s*/);
      $(this).html(innerText[0]);

	  var content = '<a href="mailto:'+ innerText[1].replace(/\s*\]\].*$/,'') + '">'+ innerText[0] +'</a>';
	}
	else {
	  var content = '<a href="mailto:'+ $(this).text() +'">'+ $(this).text() +'</a>';
	} 


	/**
	 * Rewrite the HTML for $(this), using content.
	 */
    $(this).html(content);
  });
  
}

})(jQuery); 


/**
 * Code to execute as soon as the document is ready.
 */
	var pageArea;

	$(document).ready(function() {

		// set pageArea to the area of the web site, using the bread crumb trail in the page title
		var breadCrumbTrail = document.title.split(/\s*:\s*/);						// split the page title at the ":" divider, returning an array
		if (breadCrumbTrail.length>1) { pageArea = breadCrumbTrail[1]; }		// if the length of the array is greater than one, take breadCrumbTrail[1] and extract the text, making it lower case
		else { pageArea = 'Home'; }

		// set the sidebar image based on pageArea
		$('#sidebar').css('background','#000099 url("/philosophy/images/sidebars/'+pageArea.replace(/ /g,'').toLowerCase()+'.jpg")  no-repeat');

		/*---- ON HOME PAGE, activate main menu ----*/
		if ($(document).attr('location')=='http://web.gc.cuny.edu/philosophy/') {
			$('#mainMenu').makeMenu(pageArea);	// on the home page, activate the menu immediately
		}
		else {
		  /**
		   * GET MENU
		   * The following command calls the complete menu from the server. 
		   * This allows us to keep the menu in only one place. 
		   * Because the page gets cached, this should be nearly instantaneous. 
		   * N.B. though, that you should keep the full menu on th e home page, too.
		   * That makes TWO COPIES of the menu to keep up-to-date.
		   */
		  $.get('http://web.gc.cuny.edu/philosophy/menu.html', 									// get the menu from this URL; this URL should contain nothing but an <ul> with the id mainMenu 
			  function(menu) { 	// once the server gets the menu from the server...
					  $('#mainMenu')																	// ...go to the #mainMenu <ul> currently on the page...
						  .parent().empty()															// ...get rid of it by emptying its parent div...
						  .nextAll('div.hdBar').remove()												// ...remove any .hdBar divs after it (in case makeMenu has already been called)...
						  .end()																		// ...then go back to the .hdBar that contains #mainMenu
						  .html(menu)																	// ...write the new menu to that div...
						  .children('#mainMenu').makeMenu(pageArea);									// ...and then call makeMenu on the list that was just written to the div
			  });
		}

	
		/*---- STRIPE TABLES AND LISTS ------*/
		$('.stripeMe').find('tr:has(td):even').css('backgroundColor','#ccc');										// add the class 'stripe' to every other child of each element with class 'stripeMe', starting with the 0th
	
		/*---- INITIALIZE TOGGLING DIVS -----*/	
		$('.toggled').hide().each(function() {
			$(this).before('<a class="toggleLink">Show '+$(this).attr('title')+'</a>')
				.prev('a.toggleLink').click(function() {
												$(this).next().slideToggle('fast',setSidebarHeight);			// shows the .toggled element immediately after the link
												// swaps 'Show' and 'Hide' as necessary in the toggling link
												if ($(this).html().substr(0,4)=='Show') { $(this).html($(this).html().replace(/Show/,'Hide')); }
												else { $(this).html($(this).html().replace(/Hide/,'Show')); }
											});
		});
			
		setSidebarHeight();
	});
	
	// set the height of the sidebar to the height of the content div and reposition the GC logo 33 pixels from the bottom left of the sidebar
	function setSidebarHeight() { 
		$('#sidebar').height($('#mainContent').outerHeight()-1);
		$('#gcLogo').css('marginTop', ($('#sidebar').height()-130) ); 
	}
	
	function showSubmenu(submenu) {
		if (submenu) $('#subMenuBar').empty().html('<ol class="subMenuList">'+submenu.html()+'</ol>');
//		else if (pageArea=='main') $('#subMenuBar').empty();
		else {
//			$('#subMenuBar').empty().html('foo'); // html($('#mainMenuList>li[@title="'+pageArea+'"]').children('ol').html());
		}
	}
	
	
/*		$('.toggleLinkInline')													// the following code is for inline toggling links 
			.html('...read more >>')
			.after('&nbsp;')
			.next('.toggled').css('border','none').append('&nbsp;&nbsp;<a class="toggleLinkInline"><<<\/a>');
		$('.toggleLinkInline').css('display','inline');
		$('.toggleLinkInline')
			.click(function() {
				if ($(this).next('.toggled').css('display')=='none') { 
					$('.toggleLinkInline').next('.toggled:hidden').css('display','inline'); 
					$(this).hide();	
				} else if ($(this).parent().prev().css('display')=='none') {
					$(this).parent().hide();
					$(this).parent().prev().css('display','inline');
				}
				setSidebarHeight();
			}); */


