$(document).ready(function() {

	$('.topnav').each(function(index, element) {
		$(this).tooltip({
			relative: true,
			position: 'bottom right',
			effect: 'toggle',
			onBeforeShow: function() {
				$(this).addClass('active');
				// recalculate our offset since the widths aren't exactly correct until after the
				// tooltip might be created
				var trigger = this.getTrigger();
				var w = $(trigger).width();
				var pl = parseInt($(trigger).css('padding-left').replace('px', ''));
				var pr = parseInt($(trigger).css('padding-right').replace('px', ''));
				var offset = -1 * eval(w + pl + pr);

				if (index == ($('.topnav').length - 1)) {
					// this is the last one.  So set the offset to be right-aligned instead.
					var tip = this.getTip();
					var tipWidth = $(tip).width();

					// if the tooltip itself is wider than the triggering element, move it over
					// an additional amount of the difference between widths
					if (tipWidth > Math.abs(offset)) {
						offset -= eval(tipWidth - Math.abs(offset));
					}
				}
				// get the tooltip configuration
				var conf = this.getConf();
				conf.offset = [1, offset];
			},
			onHide: function() {
				$(this).removeClass('active');
			}
		});
	});

	$('#btnMore').click(function() {
		$('#boxMoreInfoHome').slideToggle('slow', function() {
			// Animation complete.
			$('#btnMore').toggleClass('active');
		});
	});

	/*
	$(".scrollable").scrollable({
	mousewheel: false,
	circular: false,
	speed: 300,
	onSeek: function(evt, idx) {
	var parentContainer = this.getItemWrap().parent().parent();
	parentContainer.find('.index').html(idx + 1);
	}
	});
	*/

	$('.scrollableContainer a.next').click(function() {
		updateScrollable($(this), 'next')
	});
	$('.scrollableContainer a.prev').click(function() {
		updateScrollable($(this), 'prev')
	});
	setScrollableHeight();

	$('.plusMinus').click(function() {
		var pm = $(this);
		var quadNav = $(this).parent().find('ul.quadNav');
		if ($(quadNav).hasClass('active')) {
			$(quadNav).slideUp(300, function() {
				$(this).removeClass('active');
				$(pm).html('+');
			});
		}
		else {
			$(quadNav).slideDown(300, function() {
				$(this).addClass('active');
				$(pm).html('-');
			});
		}
	});

	$('#q').keypress(function(e) {
		if (window.event) {
			key = window.event.keyCode;
		}
		else {
			key = e.which;
		}
		if (key == 13) {
			e.preventDefault();
			$('#searchSubmit').trigger('click');
		}
	});

	// weather
	$('.forecastDayContainer').mouseover(function() {
		$('.forecastDetailContainer').hide();
		$(this).find('.forecastPlus').hide();
		$(this).find('.forecastDetailContainer').show();
	});
	$('.forecastDayContainer').mouseout(function() {
		$(this).find('.forecastDetailContainer').hide();
		$(this).find('.forecastPlus').show();
	});
});
function setScrollableHeight() {
	var intervalIdx = 0;

	var scrollableInterval = setInterval(function() {
		if (intervalIdx < 5) {
			$('.scrollableItem.active').each(function() {
				var myHeight = $(this).height() + 20;
				var container = $(this).parents('.scrollableContainer');
				$(container).animate({
					height: myHeight
				}, 'fast');
			});
			intervalIdx++;
		}
		else {
			clearInterval(scrollableInterval);
		}
	}, 500);
}
function updateScrollable(navItem, direction) {
	var container = $(navItem).parents('.scrollableContainer');
	var allItems = $(container).find('.scrollableItem');
	var itemCount = $(allItems).length;
	var visibleItem = $(container).find('.scrollableItem.active');
	var visibleItemIndex = $(visibleItem).index();
	var navContainer = $(navItem).parents('.scrollableNav');
	var navPrev = $(navContainer).find('.prev');
	var navNext = $(navContainer).find('.next');
	var nextItemIndex = 0;

	if (direction == 'next') {
		nextItemIndex = (itemCount - 1 == visibleItemIndex) ? 0 : (visibleItemIndex + 1);
	}
	else {
		nextItemIndex = (visibleItemIndex == 0) ? (itemCount - 1) : (visibleItemIndex - 1) ;
	}
	var nextItem = allItems[nextItemIndex];

	$(visibleItem).fadeOut('fast', function() {
		$(this).removeClass('active');
		var thisHeight = $(nextItem).height();
		var newContainerHeight = thisHeight;

		$(container).animate({
			height: newContainerHeight
		}, 'fast', function() {
			$(nextItem).fadeIn('fast', function() {
				$(this).addClass('active');
				$(navContainer).find('.index').html(nextItemIndex + 1);
				if (nextItemIndex == 0) {
					//$(navPrev).hide();
					$(navPrev).addClass('disabled');
				}
				else {
					//$(navPrev).show();
					$(navPrev).removeClass('disabled');
				}
				if (nextItemIndex == itemCount - 1) {
					//$(navNext).hide();
					$(navNext).addClass('disabled');
				}
				else {
					//$(navNext).show();
					$(navNext).removeClass('disabled');
				}
			});
		});
	});
}
