/* Menu Rollover Code */
$.fn.rolloverMenu = function(){
	return this.each(function(){
		var active = $('LI.active', this);
		active.addClass('over');
		$('LI', this).hover(
			function(){
				active.removeClass('over');
				$(this).addClass('over');
			},
			function(){
				$(this).removeClass('over');
				active.addClass('over');
			}
		);
	});
};

$.fn.dropdownMenu = function(){
	return this.each(function(){
		var active = $('LI.active', this);
		$('LI', this).hover(
			function(){
				active.removeClass('active');
				$(this).addClass('over');
			},
			function(){
				$(this).removeClass('over');
				active.addClass('active');
			}
		);
	});
};

jQuery.fn.inputFieldText = function(string, hintClass) {
	this.each(function() {
      $(this).filter(function(){
      	return ($(this).val() == '' || $(this).val() == string);
      }).addClass(hintClass).val(string);
      $(this).focus(function(){
        if ($(this).val() == string){
          $(this).removeClass(hintClass).val('');
        }
      });
      $(this).blur(function(){
        if ($(this).val() == ''){
          $(this).addClass(hintClass).val(string);
        }
      });
      var $this = $(this);
      $this.parents('FORM').submit(function(){
      	if ($this.val() == string) {
      		$this.val('');
      	}
      	return true;
      });
  });
  
  return this;
};

$(function(){
	$('#nav').dropdownMenu();
	$('#subnav').rolloverMenu();
	$('#nav LI:first').addClass('first-child');
	$('#footerNav LI:last').addClass('last-child');
	
	var totalWidth = $('#nav').width();
	var currentWidth = 0;

	$('#nav>li').each(function(){
		// we add a negative margin to the submenu if we think it's gonna go outside our bounds
		$ul = $(this).children('UL');
		var ulWidth = $ul.width() + 22; // 22 = padding we have on sub-elements
		if (currentWidth+ulWidth > totalWidth) {
			$ul.css({marginLeft:(totalWidth-(currentWidth+ulWidth))+'px'});
		}
		currentWidth += $(this).width();
	});
	
	$('#subnav a').each(function(){
		var height = $(this).height();
		if (height > 20) {
			$(this).addClass('twolines');
		}
	});
	
});

$(function(){
	$('.base TABLE').each(function(){
		var i = 0;
		$('TR', this).each(function(){
			if ($('TH',this).length == 0) {
				if (++i % 2 == 0) {
					$(this).addClass('even');
				}
			}
		});
	});
});

/*$(function(){
	$('a[rel^=fancybox]').fancybox({
		zoomSpeedIn:400,
		zoomSpeedOut:400,
		overlayShow: true,
		overlayColor: '#000',
		overlayOpacity: 0.8,
		hideOnContentClick:false
	});
});*/

$(function(){
	$('A, AREA').filter(function(){
		var href = $(this).attr('href');
		return href && !this.target && ((href.indexOf(window.location.hostname) == -1 && href.match(/^https?/i)) || href.match(/\.pdf$/i));
	}).attr('target', '_blank');
});


$(function(){
	$('#printLink A').click(function(){
		if (window.print) {
			window.print();
		} else {
			alert('Please use the print button in your browser.');
		}
		return false;
	});
});

$(function(){
	$('IMG[align=left]').addClass('alignLeft');
	$('IMG[align=right]').addClass('alignRight');
	
	$('HR').replaceWith('<div class="hr"></div>');
});


$(function(){
	$('#SearchControl LABEL').each(function(){
		$('#'+$(this).attr('for')).inputFieldText($(this).text(), 'hint');
	}).hide();
});

