$(document).ready(function() {

	function loginShowHide() {

		var login = $('#login');

		// create links
		var loginToggle = $("<a href='#' class='loginToggle'>Login</a>");
		var closeToggle = $("<a href='#' class='loginToggle'>Close</a>");


		loginToggle.click(function() {
			login.show();
			$(this).hide();
			login.find('input[@type=text]:first').focus();
		});
		closeToggle.click(function() {
			login.hide();
			loginToggle.show();
		});

		// inject links
		$('#login form').append(closeToggle);
		$('#utilities').prepend(loginToggle);
		login.hide();
	};

	function subNavAnimation() {

		var subnav = $('#subnav');
		var childLinks = $('#subnav .parent > a');


		$('#subnav li ul').each(function() {
			// set the height first otherwise you get a jump in the animation because jQuery can't work out height with margins and padding - doh!
			$(this).css('height', $(this).height())

			// find all lists that don't have a child with the 'selected' class and hide them
			$(this).filter(function(index) {
				return $(this).find('.selected').length < 1;
			}).hide();
		});

		// onclick show the ul but not if this one is already open
		childLinks.click(function() {
			$('#subnav ul:visible').not($(this).siblings('ul')).slideUp();
			$(this).siblings('ul').slideDown();
			return false;
		});


	};

	function productImageSwap() {
		$('.product-view-thumbs a').click(function() {

			// swap img src to link href
			var imageSrc = $(this).attr('href');
			$('.product-view img')
            .not('.product-view-thumbs img')
            .attr('src', imageSrc);

			// change product caption to link title
			if ($(this).attr('title') != '') {
				$('#product-image p').text($(this).attr('title')).show();
			} else {
				$('#product-image p').hide();
			}
			return false;
		});
	};

	function focusOnSearch() {
		$('#search input:first').focus();
	};

	function panelRollOver() {
		var infoHeight = $('.panel a em').outerHeight();

		$('.panel a em').css({
			'top': infoHeight * -1,
			'display': 'block'
		});

		$('.panel a')
        .mouseenter(function() {
        	var info = $(this).find('em');
        	info.stop().animate({
        		'top': 2
        	}, 300);
        })
        .mouseleave(function() {
        	var info = $(this).find('em');
        	info.stop().animate({
        		'top': infoHeight * -1
        	});

        });
	};

	//loginShowHide();
	subNavAnimation();
	productImageSwap();
	//focusOnSearch();
	panelRollOver();
});

