var nav = {
	
	setupAccordion: function() {
		var thisParent = this;
		
		var theEvent = 'click';
		if (jQuery('#subnav').hasClass('topParent-4')) theEvent = '';
		
		var theActive = '.current_page_ancestor a';
		if (jQuery('#subnav .current_page_ancestor').length <= 0) theActive = '.current_page_item a';
		
		jQuery('#subnav > ul > li > ul, #subnav > ul > li > ol').each(function(){
			jQuery(this).parent().children('a:first').addClass('nestHeader');
		})

		jQuery.ui.accordion.animations.slowSlide = function(options) {
			this.slide(options, {duration: 350});
		};

		jQuery('#subnav_ul').accordion({
				event: theEvent,
				active: theActive,
				autoHeight: false,
				collapsible: true,
				navigation: false,
				animated: 'slowSlide',
				header: '.nestHeader',
				changestart: function(event, ui) {
					ui.oldContent.animate({opacity:0}, 400)
					ui.newContent.animate({opacity:1}, 400)
				},
				change: function() {
					thisParent.drawNavLines();
				}
		});
		
		this.drawNavLines()
	},
	
	navLinesDimensions: function() {
		var subnavBox, navBox,
			lineDims = {};
			
		subnavBox = jQuery('#subnav_ul');
		if (subnavBox.find('.current_page_item').length) {
			subnavBox = subnavBox.find('.current_page_item');
			if (subnavBox.parent().css('display') == 'none')
				subnavBox = jQuery('#subnav_ul').find('.current_page_parent');
		}
			
		navBox = jQuery('#nav');
		if (navBox.find('.current-page-ancestor').length)
			navBox = navBox.find('.current-page-ancestor');
		else if (navBox.find('.current-menu-item').length)
			navBox = navBox.find('.current-menu-item');
		
		lineDims.top = subnavBox.position().top + subnavBox.height()/2;
		lineDims.height = navBox.offset().top - subnavBox.offset().top + navBox.height()/2 - subnavBox.height()/2;
		lineDims.left = jQuery('#nav').width();

		return lineDims;
	},

	drawNavLines: function() {
		var navLines = jQuery('#navlines'),
			coords = this.navLinesDimensions();
			
		if (!navLines.length) { // navLines doesn't exist yet, create it and set the intial dimensions immediately
			navLines = jQuery('<div id="navlines" style="left:' + jQuery('#nav').width() + 'px"></div>').insertAfter('#nav');
			navLines.css(this.navLinesDimensions())
		}
		navLines.animate(coords, 50);
	},

	insertSWFObject: function(container, videoFile){

		var w = jQuery('#'+container).width(),
			h = jQuery('#'+container).height(),
		
			flashvars = {	file: videoFile, 
							overstretch: false,
							bufferlength: 10,
							autostart: true,
							usefullscreen: false,
							displayheight: h
						},
			params = {	allowfullscreen: false,
						allowscriptaccess: true
					 }
					
		if(videoFile.toLowerCase().substring(videoFile.length-3) == 'flv')
			videoFile = "/swf/flvplayer.swf";	// specify the flvplayer if there's an FLV file
							
		swfobject.embedSWF(videoFile, container, w, h, '9.0.0', '/swf/expressInstall.swf', flashvars, params, {});
	},


	resizeLocationSidebar: function(){
		var bbperson = jQuery('#bigbox .person'),
			blockHeight = 180;
		if (jQuery.support.boxModel) blockHeight += 10; // fudge the size a bit to prevent bad wraps in IE
		if (bbperson){
			jQuery('#locationSidebar').height( Math.ceil(jQuery('#locationSidebar').height() / bbperson.height() ) * 180 )
		}
	},

	toggleSection: function() {
		jQuery('.toggleSection .toggleThis').hide()
		jQuery('.toggleSection .toggleTrigger').toggle(
			function(){console.log('clicked on'); jQuery(this).parent().find('.toggleThis').slideDown('slow');},
			function(){console.log('clicked off'); jQuery(this).parent().find('.toggleThis').slideUp('slow');})
	},


	formatProductListings: function(){
		var products = jQuery('#resource-links img, #productpartners img');
		if ( products.length > 0 ) {
		products.removeClass('frameImageLeft').css('float', 'none').removeAttr('align');
		products.each( function() {
				if ( jQuery(this).width() > 180 || jQuery(this).height() > 180 ) {
					if ( jQuery(this).width() / jQuery(this).height() > 1 ) {
						jQuery(this).css( 'width', 180 );
					} else {
						jQuery(this).css( 'height', 180 );
					}
				}
			})
		jQuery('#resource-links p, #productpartners p').append('<br style="clear: both" />')
		products.wrap('<div class="thumb"/>');
		}
	},


	protectEmailLinks: function(){
		var d = document.links;
		for(i=0;i<d.length;i++){
			if(d[i].protocol == 'mailto:'){
				d[i].href = d[i].href.replace(/^(mailto:[^+]+)\+([^+]+)(?:\+|\.)?([^+?]+)?/,'$1@$2.$3').replace(/\.$/,'')
				if (d[i].childNodes.length==0) // add email address to no-text links
					d[i].appendChild(document.createTextNode(d[i].href.substring(7)));
			}
		};
	},

	initialize: function(){
		jQuery('.js').removeClass('js'); // remove the js class now that we're loaded		
		if ( jQuery('#subnav').length ) this.setupAccordion();

		this.protectEmailLinks();

		this.formatProductListings();
		
		if (jQuery('.toggleSection').length ) this.toggleSection();
	}
},

ImageSwap = function(imgElement, options){
	this.img = jQuery(imgElement);
	this.wrapper = ( this.img.parent().is('a') ) ? this.img.parent() : this.img;
	this.wrapper = this.wrapper.wrap('<div style="position: relative;" />');
	if ( this.wrapper.is('img') ) this.wrapper = this.wrapper.parent();	// if it's an image return the parent, if it's whatever is containing the img, leave it
	this.options = { duration: 1500 };	// default options
	jQuery.extend( this.options, options ); // overwrite default options with input options

	this.swap = function(newImageUrl) {
		var thisParent = this,
		newImage = this.img.clone(true);

		newImage.css({'top': 0, 'left': 0, 'position': 'absolute', 'display': 'none' });	// float the replacement over the top of the old image;

		newImage.hide().load( function() {
									thisParent.wrapper.append(newImage);
									newImage.fadeIn(thisParent.options.duration, function() {
												/*
												thisParent.img.attr( 'src', newImage.attr('src') );
												newImage.remove();
												*/
												newImage.prev().remove();
											});
									});
		newImage.attr('src', ( ( typeof( newImageUrl ) == 'string') ? newImageUrl : newImageUrl.src ) );	// check replacement for element or string, match src attribute
	}
}


jQuery(function(){nav.initialize()})
