$(document).ready(function() {
	var hideTimeout = false;
	var search = $('#search');
	var login = $('#log-in');
	var registration = $('#registration-form');
	var ie6 = ($.browser.msie && $.browser.version == '6.0');
	
	//
	// Setup click and mouseout events for the login and search buttons
	//
	$('#masthead a.button').click(function() {
		var target_id = $(this).attr('href');	
		var target = $(target_id);
		
		closePopups();
		target.toggle();
		return false;	
	});
	
	$('#log-in, #search')
		.mouseout(function() { hideTimeout = setTimeout(closePopups, 1000);})
		.mouseover(function() {if (hideTimeout) clearTimeout(hideTimeout);});
	
	function closePopups() {
    clearTimeout(hideTimeout);
		search.hide();
		login.hide();
  }

	//
	// Setup jquery.example on the login and registration forms.
	//
	$("#log-in, #registration-form").find('input').example(function() {
		return $(this).attr('title');
	}, { className: 'hint' });
	
	//
	// Setup hover tooltips on member photos
	//
	var badges = $('.wld_badge_item');
	
	badges.each(function() {
		var badge = $(this);	

		badge
			.find('.wld_badge_item_detail')
			.wrapInner('<div class="wrapper"></div>')
			.end()
			.mouseover(function() {
				var pos  = $(this).offset();
				var ww   = $(window).width(); // window width
				var tip  = $(this).find('.wld_badge_item_detail');
				var top  = pos.top - 30;
				var left = pos.left + ($(this).width() / 2);
				
				if ((ww - 10) < (left + tip.width())) {
					tip.addClass('reverse');
					left = left - tip.width();
				}
				
				$(this).addClass('hover')
				tip.show().css({
					'top'  :  top + 'px',
					'left' :  left + 'px' 
				});
			})
			.mouseout(function() {
				badge
					.removeClass('hover')
					.find('.wld_badge_item_detail')
					.removeClass('reverse')
					.hide();
			});
	});
	
	//
	// Setup carousel
	//
	if (!ie6) {
		$('#carousel').jcarousel({
			auto: 6,
			wrap: 'last',
			size: 4,
			scroll: 1
		});
	}
});