$(document).ready(function() {

	$('#spotlights .items li:not(:eq(2))').hide();
	$('#spotlights .navs li:eq(2)').addClass('on');
	$('#spotlights .navs li').live('click', function() {
		// don't do anything if it's currently animating
		if ($('body').hasClass('animating')) { } else {
			clickedItem = $('.navs li').index(this);
			if(clickedItem == '0') {
				// add two above it
				lastItem = '<li>' + $('#spotlights .navs li:last-child').html() + '</li>';
				$('#spotlights .navs li:last-child').remove();
				lastItem = '<li>' + $('#spotlights .navs li:last-child').html() + '</li>' + lastItem;
				$('#spotlights .navs li:last-child').remove();
				// move it down 2 positions
				$('#spotlights .navs ul').prepend(lastItem).css({top: '-186px'});
				$('#spotlights .navs ul').animate({top: '0px'}, 'slow', function() {
					// allow click function again
					$('body').removeClass('animating');
				});
			} else if(clickedItem == '1') {
				// add one above it
				lastItem = '<li>' + $('#spotlights .navs li:last-child').html() + '</li>';
				$('#spotlights .navs li:last-child').remove();
				// move it down 1 position
				$('#spotlights .navs ul').prepend(lastItem).css({top: '-93px'});
				$('#spotlights .navs ul').animate({top: '0px'}, 'slow', function() {
					// allow click function again
					$('body').removeClass('animating');
				});
			}
			$('#spotlights .items li').hide();
			$('#spotlights .navs li').removeClass('on');
			selectedItem = $(this).find('a').attr('id').split('-').pop();
			selectedItem = parseInt(selectedItem,10);
			selectedItem = selectedItem - 1;
			$('#spotlights .items li:eq(' + selectedItem + ')').fadeIn('slow');
			$(this).addClass('on');
		}
		return false;
	});

	$('#spotlights ul.updown li.up a').live('click', function() {
		// don't do anything if it's currently animating
		if ($('body').hasClass('animating')) { } else {
			// get html from last item
			lastItem = $('#spotlights .navs ul li:last-child').html();
			lastItem = '<li>' + lastItem + '</li>';
			// put it at the end of the list, remove first item, reset margin
			$('#spotlights .navs ul').prepend(lastItem);
			$('#spotlights .navs ul li:last-child').remove();
			$('#spotlights .navs ul').css({top: '-93px'});
			// hide all items, fade in selected item, add on class to nav
			$('#spotlights .items li').hide();
			$('#spotlights .navs ul li').each(function(i) { if ($(this).hasClass('on')) {
					totalItems = $('#spotlights .navs ul li').size();
					selectedItem = $(this).find('a').attr('id').split('-').pop();
					selectedItem = parseInt(selectedItem,10);
					selectedItem = selectedItem - 2;
					if(selectedItem < 0) {
						selectedItem = totalItems;
						selectedItem = selectedItem - 1;
					}
				}
			});
			$('#spotlights .items li:eq(' + selectedItem + ')').fadeIn('slow');
			$('#spotlights .navs ul li.on').removeClass('on').prev().addClass('on');
			// stop click function
			$('body').addClass('animating');
			$('#spotlights .navs ul').animate({top: '0px'}, 'slow', function() {
				// allow click function again
				$('body').removeClass('animating');
			});
		}
		return false;
	});

	$('#spotlights ul.updown li.down a').live('click', function() {
		// don't do anything if it's currently animating
		if ($('body').hasClass('animating')) { } else {
			// hide all items, fade in selected item, add on class to nav
			$('#spotlights .items li').hide();
			$('#spotlights .navs ul li').each(function(i) { if ($(this).hasClass('on')) {
					totalItems = $('#spotlights .navs ul li').size();
					selectedItem = $(this).find('a').attr('id').split('-').pop();
					selectedItem = parseInt(selectedItem,10);
					if(selectedItem == totalItems) {
						selectedItem = 0;
					}
				}
			});
			$('#spotlights .items li:eq(' + selectedItem + ')').fadeIn('slow');
			$('#spotlights .navs li.on').removeClass('on').next().addClass('on');
			// stop click function
			$('body').addClass('animating');
			// move navs
			$('#spotlights .navs ul').animate({top: '-93px'}, 'slow', function() {
				// get html from first item
				firstItem = $('#spotlights .navs li:first-child').html();
				firstItem = '<li>' + firstItem + '</li>';
				// put it at the end of the list, remove first item, reset margin
				$('#spotlights .navs ul').append(firstItem);
				$('#spotlights .navs ul li:first-child').remove();
				$('#spotlights .navs ul').css({top: 0});
				// allow click function again
				$('body').removeClass('animating');
			});
		}
		return false;
	});

	$('#inpageTools ul').append('<li class="print"><a href="#">Print Friendly Version</a></li>');
	$('#inpageTools ul li.print a').click(function() {
		if($(this).hasClass('on')) {
			$(this).removeClass('on');
			$('head link:last-child').remove();
			$(this).text('Print Friendly Version');
			return false;
		} else {
			$(this).addClass('on');
			$('head').append('<link rel="stylesheet" type="text/css" media="all" href="/templates/default/css/system/print.css" />');
			$(this).text('Disable Print Friendly Version');
			window.print();
			return false;
		}
	});

});

$(window).load(function() {

	function balanceElements(element,toBalance){
		tallest = 0;
		$(element).each(function() {
			$(this).find(toBalance).each(function() {
				var elementHeight = parseInt($(this).height(), 10);
				if (elementHeight > tallest) {
					tallest = parseInt($(this).height(), 10);
				}
			});
			$(this).find(toBalance).css('height', tallest + "px");
			tallest = 0;
		});
	}

	balanceElements('#content .landingBoxes','.balance');
	balanceElements('#content .related','.balance');
	balanceElements('#content .landingBoxes','li:not(li li)');
	balanceElements('#content .landingAlt','li:not(li li)');
	balanceElements('#content .newsAndEvents','.balance:not(.filtered .balance)');
	balanceElements('#content .shopListing.viewAll','li');
	balanceElements('#content .join-boxes','.balance');

});
