/***************************************
   =hoverIntent r5 // 2007.03.27 // jQuery 1.1.2
-------------------------------------- */
/**
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @return    The object (aka "this") that called hoverIntent, and the event object
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

/***************************************
   =Superfish v1.3.1 - jQuery menu widget
-------------------------------------- */
/*
 * Copyright (c) 2007 Joel Birch
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
*/

(function($){
	$.fn.superfish = function(o){
		var $sf = this,
			defaults = {
			hoverClass	: 'sfHover',
			pathClass	: 'overideThisToUse',
			delay		: 800,
			animation	: {opacity:'show'},
			speed		: 'normal'
		},
			over = function(){
				clearTimeout(this.sfTimer);
				clearTimeout($sf[0].sfTimer);
				$(this)
				.showSuperfishUl()
				.siblings()
				.hideSuperfishUl();				
			},
			out = function(){
				var $$ = $(this);
				if ( !$$.is('.'+o.bcClass) ) {
					this.sfTimer=setTimeout(function(){
						$$.hideSuperfishUl();
						if (!$('.'+o.hoverClass,$sf).length) { 
							over.call($currents.hideSuperfishUl());
						}
					},o.delay);
				}		
			};
		$.fn.extend({
			hideSuperfishUl : function(){
				return this
					.removeClass(o.hoverClass)
					.find('ul:visible')
						.hide()
					.end();
			},
			showSuperfishUl : function(){
				return this
					.addClass(o.hoverClass)
					.find('>ul:hidden')
						.animate(o.animation,o.speed)
					.end();
			},
			applySuperfishHovers : function(){
        return this[($.fn.hoverIntent) ? 'hoverIntent' : 'hover'](over,out);
           // return this['hoverIntent'];
			}
		});
		o = $.extend({bcClass:'sfbreadcrumb'},defaults,o || {});
		var $currents = $('li:has(ul)',this).filter('.'+o.pathClass);
		if ($currents.length) {
			$currents.each(function(){
				$(this).removeClass(o.pathClass).addClass(o.hoverClass+' '+o.bcClass);
			});
		}
		var $sfHovAr=$('li:has(ul)',this)
			.applySuperfishHovers(over,out)
			.find('a').each(function(){
				var $a = $(this), $li = $a.parents('li');
				$a.focus(function(){
					over.call($li);
					return false;
				}).blur(function(){
					$li.removeClass(o.hoverClass);
				});
			})
			.end()
			.not('.'+o.bcClass)
				.hideSuperfishUl()
			.end();
		$(window).unload(function(){
			$sfHovAr.unbind('mouseover').unbind('mouseout');
		});
		return this.addClass('superfish').blur(function(){
			out.call(this);
		});
	};
})(jQuery);


/***************************************
   =drop-down menu
-------------------------------------- */
$(document).ready(function() {
  $('ul.topnav').superfish({
    animation: {height: 'show'},
    delay: 600,
    speed: 1
  });
});

/***************************************
   =print stylesheet mod for Shelly
-------------------------------------- */
$(document).ready(function() {
  $('#footer h5:eq(1)').click(function() {
    $('link[media=print]').remove();
    $('link[media=screen]').attr('media','all');
  });  
});

/***************************************
   =add class to current-page link
-------------------------------------- */

$(document).ready(function() {
  var thisSection = location.pathname.replace(/^\//,'').split('/').slice(-2,-1);
  thisSection += 'yes';
  function filterPath(pathString) {
    return pathString.replace(/^\//,'').replace(/\/(index.php)?$/,'').replace(/english\//,'engl/');
  }
  var thisPage = filterPath(location.pathname);
  $('#left li a').each(function() {
    var thisLink = filterPath(this.pathname);
    if (thisPage == thisLink && location.hostname == this.hostname) {
      $(this).addClass('current-page'); 
    }
  });
  $('#right a').each(function() {
    var thisLink = this.pathname.replace(/^\//,'').replace(/\/(index.php)?$/,'');
    if (thisPage == thisLink && location.hostname == this.hostname) {
      $(this).addClass('current-page'); 
      $('#left li a').each(function() {
        var linkSection = this.pathname.replace(/^\//,'').split('/').slice(-2,-1);
        linkSection += 'yes';
        if (thisSection == linkSection) {
          $(this).addClass('current-page');
        }
      });
    }
  });  
});


/** =expandable textareas
************************************************************/
$(document).ready(function() {
  if ($('textarea').length) {
    $.getScript('/academic/engl/festival/scripts/jquery.expandable.js', function() {
      $('textarea').expandable({init: true});
    });
  }
});

/***************************************
   =cosmetic touch-ups
-------------------------------------- */

$(document).ready(function() {
  // table striping
  
  $('table:not(.schedule) tr:has(td):nth-child(odd)').addClass('alt');
  
  //cool indian star thing for current section
  var bodyClass = $('body')[0].className.replace(/\s*sidebar\s*/,'');
  var $topnav = $('#topnav > ul');
  $topnav.find('> li > a').each(function() {
    var topnavClass = this.className.replace(/ffw-/,'');
    if (bodyClass != '' && bodyClass == topnavClass) {
      $(this).parent('li').addClass('active');
    }
  });
  
  // add "last" class for last child top-menu item
  $topnav.find('li li:last-child').addClass('last');
  // add decorative item to right sidebar nav
  
  $('#right li:last-child').addClass('last');
  
  // add bottom bar to footer
  $('<img id="bottom-bar" src="/academic/engl/festival/styles/images/bg-bottom-bar.png" alt="" />').appendTo('#footer');
  
  
});

$(window).bind('load', function(event) {
  // balance height of left column and main content
  var leftHeight = $('#left').height(),
      contentHeight = $('#content').height();
  $('#content, #left, #right').height(Math.max(leftHeight, contentHeight));
});
/***************************************
   =convert legend to h3
-------------------------------------- */
$(document).ready(function() {
  // $('legend').each(function() { 
  //   var $this = $(this);
  //   var legendText = $this.text(); 
  //   $this.before('<h4>' + legendText + '</h4>').remove();
  // });
  
});

