
$(document).ready(
	function() {
		ctv.setup.adjustMenuItemHeight();
		ctv.setup.init_PrimaryNav();
		ctv.setup.init_MenuNav();
		ctv.setup.init_Rollovers();
		
		ctv.setup.init_Collapsable();
		
	}
);


/*
	ctv: Cultivate
*/

var ctv = {};

/*
 * 	setup
 *  event attachment and dom prep routines
 */

 
ctv.setup = {

	// if any of the menu-nav items is more than one line, adjust the other items to reflect that height
	adjustMenuItemHeight: function() {
		var _menus = $('.menu-nav');
		_menus.each(
			function() {
				var _this = $(this);
				ctv.util.equalizeItemsHeight( _this.find('a'), 16 );
			}
		);
	},

	// attach mouse events to menu navigation
	init_MenuNav: function() {
		ctv.util.attachToggleFunctions( $('.menu-nav td'), 'hover',
			function() { 
				$(this).addClass('hover'); 
				$(this).find('ul').show();
			},
			function() { 
				$(this).removeClass('hover'); 
				$(this).find('ul').hide();
			}
		);
	},
	
	// attach mouse events to primary navigation
	init_PrimaryNav: function() {
		ctv.util.attachToggleFunctions( $('#primary-nav li'), 'hover');
	},
	
	// attach mouse events to images with a "rollover" class name
	init_Rollovers: function() {
		$('.rollover').hover(
			function() { ctv.util.mouseOver( $(this) ) },
			function() { ctv.util.mouseOut( $(this) ) }
		);
	},
	
	init_Collapsable: function() {
		
		$('.btn-close').click(
			function() {
				var _this = $(this);
				// if this is the first click, then store the current text value so it can be restored later
				if( !_this.attr('defaultText') ) {
					_this.attr('defaultText', _this.text() );
				}
				// determine whether to expand or collapse elements
				var bExpanded = _this.text() == _this.attr('defaultText');
				
				// locate the elements to be shown/hidden
				var jqRows = $('.' + _this.attr('collapseClass') );
				
				// show/hide elements, toggle icon and swap text
				if( bExpanded ) {
					jqRows.css('display', 'none');
					_this.text( _this.attr('altText') ).toggleClass('opened');
				} else {
					jqRows.css('display', '');
					_this.text( _this.attr('defaultText') ).toggleClass('opened');
				}
				return false;
			}
		);
	}
	
};




ctv.util = {
	
	// get the file extension from a file name
	getExt: function(src) {
		return src.match(/.[\w]{3,4}$/);
	},
	
	
	// attach toggle functions to elements in a collection
	attachToggleFunctions: function(jqCollection, sClassName, fnOver, fnOut) {
		var _fnOver = fnOver || function() { $(this).addClass('hover'); }
		var _fnOut = fnOut || function() { $(this).removeClass('hover'); }
		jqCollection.each(
			function() {
				var _this = $(this);
				// assuming this td isn't active by default, attach over/out events to toggle the 'active' class
				_this.hover( _fnOver, _fnOut );
			}
		);
	},
	
	// take a jquery collection of elements, determine the tallest one, and then stretch them all to match in height
	equalizeItemsHeight: function(jqItems, nOffset) {
		var nMaxMenuHeight = 0;
		jqItems.each(
			function() { if( this.offsetHeight > nMaxMenuHeight ) nMaxMenuHeight = this.offsetHeight; }
		);
		var sMenuHeight = (nMaxMenuHeight - nOffset) + "px";
		jqItems.each(
			function() { $(this).css( { height: sMenuHeight } );}
		);
	},
	
	mouseOver: function(img) {
		var sExt = ctv.util.getExt( img.attr('src') );
		var sNewSrc = img.attr('src').replace( sExt, '_on' + sExt);
		img.attr('src', sNewSrc);	
	},

	mouseOut: function(img) {
		var sExt = ctv.util.getExt( img.attr('src') );
		var sNewSrc = img.attr('src').replace( '_on' + sExt, sExt );
		img.attr('src', sNewSrc);
	}
	
	
	
} /*end: ctv.util */


