// Fire up the JS
var base = function($){

	$(function(){

		// Rotate the banner
		$('#slideshow').cycle({speed: 3000, timeout: 4000});

		// Pretty file inputs
		$('input[type=file]').each(function(){
			var $input = $(this);
			var $related = $('<input id="browseFileName" name="browseFileName" type="text" readonly class="text"/>');
			var $container = $('<span class="file"><img src="/themes/default/images/button-browse.gif" alt="browse"/></span>');

			$input.change(function() {
				var filestring = $(this).val();
				if (filestring.indexOf('\\') > 0) {
					// Trim it down if we have a path.
					filestring = filestring.substr(filestring.lastIndexOf('\\') + 1);
				}

				$related.val(filestring);
			});

			$container.prepend($related).insertAfter($input);
			$input.css({'opacity':0, cursor: 'pointer'});
		});

		// Menu

		var speed = 200;

		$('body>nav>ul>li').hover(function(e){
			$('a', this).stop().animate({color:'#000'}, speed);
		}, function(e){
			$('a', this).stop().animate({color: '#77746B'}, speed);
		})
		
		$('body>nav ul ul a').hover(function(e){
			$(this).stop().animate({color: '#00B0D8'}, speed);
		}, function(e){
			$(this).stop().animate({color: '#000'}, speed);
		});

		var sub_height = 0;
		$('body>nav ul ul').each(function(){
			sub_height = Math.max(sub_height, $(this).height());
		});
		sub_height = 300-10-sub_height-$('body>nav').height();
		//$('body>nav').css('background-position', '0px -' + sub_height + 'px');
		$('body>nav').hoverIntent(
			function(e){
				var $this = $(this);
				$('ul ul', $this).css({opacity:1, background: 'transparent'}).hoverFlow(e.type, {height:'toggle'}, 300);
				$this.hoverFlow(e.type, {backgroundPosition:'0px -' + sub_height + 'px'}, 300, function(){
					$this.addClass('open');
				});
				$('a', this).hoverFlow(e.type, {color: '#77746B'}, 300);
			},
			function(e) {

				var $this = $(this);
				$('ul ul', this).css('background', '#fff').css({opacity:0}).hoverFlow(e.type, {height:'toggle'}, 500);
				$this.hoverFlow(e.type, {backgroundPosition:'0px -300px'}, 500, function(){
					$this.removeClass('open');
				});
				$('a', this).hoverFlow(e.type, {color: '#77746B'}, 500);
			}
		);

		// Search input
		$('#SearchForm_SearchForm').css('opacity', .6).hover(
			function(e) {
				$(this).fadeTo(100,1);
			},
			function(e) {
				if ( $('#SearchForm_SearchForm :focus').length < 1 ) {
					$(this).fadeTo(100,.6);
				}
			}
		);

		$('#SearchForm_SearchForm_action_results').val('');
		$('#SearchForm_SearchForm').labelToPlaceholder();
	});


	// Some public functions that are used site wide.
	return {
		invalidHandler : function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {

				if ( !$('form p.error').length ) {
					$('<p class="message error">').hide().prependTo(validator.currentForm);
				}

				var message = errors == 1
						? "You missed 1 field. It has been highlighted below"
						: "You missed " + errors + " fields.  They have been highlighted below.";
				$("form p.error").html(message).show();
			} else {
				$("form p.error").remove();
			}
		}
	}

}(jQuery);

/**
 * A simple jQuery plugin to convert labels into placeholder text.
 */
(function($) {
	$.fn.labelToPlaceholder = function(settings) {

		if ( this.length == 0 ) return this;

		settings = $.extend({
			overflow : 'hidden',
			opacity : 0.6
		}, settings);

		var $set = this.add('input,textarea',this);

		$set.each(function(){

			var $this = $(this);
			var $label = $('label[for="' + $this.attr('id') + '"]');

			if ( $label.length == 0 ) return;

			$label.css({
				position: 'absolute',
				overflow: settings.overflow,
				margin: 0,
				padding: 0,
				display: 'block',
				width: $this.width(),
				height: $this.height(),
				lineHeight: $this.height() + 'px',
				fontSize: $this.css('fontSize'),
				opacity: settings.opacity,
				'float': 'none'
			}).addClass('placeholder');

			var input_position = $this.position();
			var inner_offset = $this.innerOffset();
			
			$label.css({
				top: input_position.top + inner_offset.top,
				left: input_position.left + inner_offset.left
			});
			if ($this.val() != '') $label.hide();

			$this.focus(function(){
				$label.hide();
			});

			$this.blur(function(){
				if ($($this).val() == '') $label.show();
			});
		});
		return this;
	};
})(jQuery);

/**
 *  A jQuery plugin that returns the 'innerOffset' of an element. That is,
 *  the difference between the outside and the inside of a container for
 *  each of the sides (i.e. the sum of the border width and padding).
 */
(function($) {
	$.fn.innerOffset = function(){
		if (this.length == 0) return null;
		var $first = $(this.get(0));
		
		// TODO: Deal with IE returning text values like 'medium' for border width.
		
		return {
			top : parseFloat($first.css('paddingTop').replace(/px/, '')) + parseFloat($first.css('borderTopWidth').replace(/px/, '')),
			right : parseFloat($first.css('paddingRight').replace(/px/, '')) + parseFloat($first.css('borderRightWidth').replace(/px/, '')),
			bottom : parseFloat($first.css('paddingBottom').replace(/px/, '')) + parseFloat($first.css('borderBottomWidth').replace(/px/, '')),
			left : parseFloat($first.css('paddingLeft').replace(/px/, '')) + parseFloat($first.css('borderLeftWidth').replace(/px/, ''))
		}
	}
})(jQuery);
