//
//  jQuery IE Fade Fix
//
//  Adapted from code found at http://jquery.malsup.com/fadetest.html.
//
//  This is only needed for IE 7 and earlier, so this is best added to your page using IE's conditional comments
//  (http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx) as follows:
//      <!--[if lt IE 8]><script type="text/javascript" src="jquery-ie-fade-fix.js"></script><![endif]-->
//
(function($) {
    $.fn.fadeIn = function(speed, callback) {
        return this.animate({opacity: 'show'}, speed, function() {
                if ( $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }
                if ( $.isFunction(callback) )
                {
                        callback.call(this);
                }
        });
    };

    $.fn.fadeOut = function(speed, callback) {
        return this.animate({opacity: 'hide'}, speed, function() {
                if ( $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }
                if ( $.isFunction(callback) )
                {
                        callback.call(this);
                }
        });
    };

    $.fn.fadeTo = function(speed, to, callback) {
        return this.animate({opacity: to}, speed, function() {
                if ( to == 1 && $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }
                if ( $.isFunction(callback) )
                {
                        callback.call(this);
                }
        });
    };
})(jQuery);

// Document Ready
jQuery(document).ready(function() {

	// First thing we do is calculate the height of each sub menu and adjust the "top" value for 
	// that menu item by that amount, moving the menu out of sight.
	
	/*
	jQuery('ul.menu ul').each(function(index) {
		var curHeight = jQuery(this).height();
		var curPadding = parseInt(jQuery(this).css('padding-top').replace('px', '')) + parseInt(jQuery(this).css('padding-bottom').replace('px', ''));
		jQuery(this).css("top", 0-curHeight-curPadding);
	});
	*/


	// Code to handle drop down menu animations
	jQuery('li.expanded').hover(
		function(){

			$el = jQuery(this);
			$subNav = jQuery($el).children("ul.menu");
			var curHeight = jQuery($subNav).height();
			var bottomPadding = parseInt(jQuery($subNav).css('padding-bottom').replace('px', ''));
			var topPadding = parseInt(jQuery($subNav).css('padding-top').replace('px', ''));
			
			var curBottom = parseInt($subNav.css('bottom').replace('px', ''));
			var targetBottom = 85-curHeight-bottomPadding-topPadding;
			
			var newBottom = curBottom - targetBottom;

			jQuery($subNav).stop().animate({

				bottom: '-='+newBottom

			}, "slow");
		
		},
		function(){

			$el = jQuery(this);
			$subNav = jQuery($el).children("ul.menu");

			var curHeight = jQuery($subNav).height();
			var bottomPadding = parseInt(jQuery($subNav).css('padding-bottom').replace('px', ''));
			var topPadding = parseInt(jQuery($subNav).css('padding-top').replace('px', ''));
			
			var curBottom = parseInt($subNav.css('bottom').replace('px', ''));
			var targetBottom = 85;
			
			var newBottom = 0-(curBottom-targetBottom);
			
			jQuery($subNav).stop().animate({

				bottom: '+='+newBottom

			}, "slow");

		}
	);
	
	// Code to handle splitting out page titles. The word abelei will be wrapped in a special tag
	// and the last word of each title will also be wrapped in a special tag so we can style them
	// differently.
	$pageTitle = jQuery('h1#page-title');
	
	var titleArr = $pageTitle.text().split(' ');
	var arrLen = titleArr.length;
	var newTitle = "";
	
	for (word in titleArr)
	{
		if(titleArr[word].toLowerCase() == 'abelei') {
			newTitle = newTitle + '<span class="abelei">' + titleArr[word] + '</span> ';
		} else if (word == arrLen-1) {
			newTitle = newTitle + '<span class="last">' + titleArr[word] + '</span>';
		} else {
			newTitle = newTitle + titleArr[word] + ' ';
		}

	}
	
	$pageTitle.html(newTitle);

});;

