var tides = {
	content: {
		init: function() {
			jQuery(".tabs").tabs(".panes > div.tab-panel");
			jQuery("#hero #items").tabs(".panes > div", {
				effect: 'fade',
				fadeOutSpeed: "slow",
				rotate: true
			}).slideshow({autoplay:true, interval:9000, clickable: false});
			jQuery(".accordion").tabs(".accordion div.pane", {tabs: 'h3', effect: 'slide'});
			jQuery(".scrollable").scrollable();

			// add span tags inside of ordered lists in main content columns.
			jQuery("ol li").each(function() {
				if(this.childNodes[0] && this.childNodes[0].nodeType == 3){
					var data = $.trim(this.childNodes[0].data);
					if(data){
						this.childNodes[0].data = data;
						$(this.childNodes[0]).wrap("<span></span>");
					}
				}
			});

			// So... some js somewhere is preventing ordered list ordinals from displaying, dependably in IE. This seems to help.
			jQuery('ol li').each(function() {
				jQuery(this).hide();
				jQuery(this).show();
			});

			// Make the tab text clickable
			jQuery('#hero #items li a span').click(function() {
				clickable = jQuery(this);
				heroUrl = clickable.parent('a').attr('href');
				window.location = heroUrl;
			});
			//And make it look like a link when hovering over it.
			jQuery('#hero #items li a span').hover(
				function() {
					jQuery(this).addClass('hover');
				},
				function() {
					jQuery(this).removeClass('hover');
				}
			);
		}
	},
	nav: {
		init: function() {
			var height = 0
			var currentActive = jQuery('#nav li.active');

			// set styles to determine height
			jQuery('#pushdown').css({display: 'block', visibility:'hidden', position:'absolute'});

			// set equal H4 heights
			jQuery('#pushdown h4').each(function() {
				h = jQuery(this).height();
				if(h > height) height = h;
			})
			jQuery('#pushdown h4').height(height);

			// set equal UL heights
			jQuery('#pushdown ul').each(function() {
				h = jQuery(this).height();
				if(h > height) height = h;
			})
			jQuery('#pushdown ul').height(height)

			// revert styles
			jQuery('#pushdown').css({display: 'none', visibility:'visible', position:'relative'});

			// add click event
			jQuery('#want').click(function() {
				jQuery('#pushdown').slideToggle('fast');
				if(currentActive) {
					if(currentActive.hasClass('pushHide')) {
						currentActive.removeClass('pushHide');
						jQuery('#want').removeClass('active');
					} else {
						currentActive.addClass('pushHide');
						jQuery('#want').addClass('active');
					}
				};

				return false;
			});

			// Try to fix odd IE bug, where opened nav item doesn't close
			// jQuery('ul#nav > li').mouseleave(function() {
			// 	$(this).mouseleave();
			// });
		}
	},
	news: {
		init: function() {
			addThisAnchors = $('a.addthis');

			if(addThisAnchors) {
				addThisAnchors.each(function(index, value) {
					config = {
						ui_click : true
					};
					share = {
						url : $(this).attr('addthis:url')
					};

					addthis.button(this, config, share);
				});
			}
		}
	}
}

var textFields = {
	/**
	 * This function grabs labels for text fields and places the label text in the field.
	 * When the field gains focus, the label text is removed. On blur, if the text field is empty,
	 * the label text is placed back in the field. When the lable text is inserted into the field,
	 * the input is given the class 'hint'.
	 *
	 *  @param string hintClass The label class to use for hinting.
	 *  @param boolean useAll Determines wheter or not all lables wiill be used for hinting.
	 */
	init : function(hintClass, useAll) {
		if(hintClass) {
			labels = $('label.'+hintClass);
		} else if(useAll) {
			labels = $('label');
		}
		if(labels) {
			labels.each(function() {
				forInput = $(this).attr('for');
				var i = $('#'+forInput);
				var l = $(this).text();
				if(i.is('input:text')) {
					// hide the label
					$(this).hide();
					// put it in the field
					i.addClass('hint')
					i.attr('value', l);
					i.focus(function() {
						i.removeClass('hint')
						if(i.attr('value') == l) {
							i.attr('value', '');
						}
					}).blur(function() {
						if(i.attr('value') == '') {
							i.addClass('hint')
							i.attr('value', l);
						}
					});
				}
			});
			textFields.initHeaderForm();
		}
	},
	initHeaderForm: function() {
		$('.header-form .redesign input[type="text"],#header .search-wrapper .redesign input[type="text"]').each(function() {
			if(!$(this).val()) $('label[for="'+$(this).attr('id')+'"]').show();
			$(this).focus(function() {
				$('label[for="'+$(this).attr('id')+'"]').hide();
			}).blur(function() {
				if(!$(this).val()) $('label[for="'+$(this).attr('id')+'"]').show();
			});
		});
	}
}

var ie6Help = {
	init: function() {
		jQuery('.filter-resultsPerPage').bgiframe();
		DD_belatedPNG.fix('.pngFix, #content ul.tabs a.current span, .addthis_button img, .addthis img, .hero-corner, #hero .message, h4 span, .tout-content, table.resultsTable .list-callout .inner, .list-callout .top, .list-callout .bottom, h3.block.green-arrow, .tidings');
	}
}

var ieHelp = {
	fixBrowseLinks: function() {
		$('ul.browse li span').css('top',function(){
			return ($(this).parents('li:eq(0)').height() - $(this).height()) / 2;
		});
	}
}

var homeSlider = {
	init : function() {
		$('.flexslider').flexslider({
			controlsContainer: ".slider-controls"
		});
	}
}

jQuery(document).ready(function() {
	tides.content.init();
	
	tides.nav.init();
	tides.news.init();
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		ie6Help.init();
	}
	if($.browser.msie) {
		ieHelp.fixBrowseLinks();
	}
	textFields.init('hintable', false);
	
	homeSlider.init();
});
