$(document).ready(function() {

    // REMOVEME: Disable links to the Trimapee online store
    //$('a[href*=grandsocial]').attr({'href': '#','title':'Coming soon'}).click(function () {$(this).hide().text('COMING SOON!').fadeIn('slow');return false});

    // Restrict numeric fields to numeric input.
    $('input.numeric').numeric();

    // Generic AJAXy form submitter.
    $('table.form a#submit').click(function () {
	var formdata = {};

	$('table.form input, table.form textarea').each(function () {
	    formdata[$(this).attr('name')] = $(this).val();
	});

	// Make formdata an object acceptable to .post()
	formdata = $.extend(true, {}, formdata);
	$.post($('#enquiry_form').attr('action'), formdata);

	// Clear out form fields.
	$(this).parents('table').find('input').add('table textarea').val('');
	// Show appreciation.
	$(this).parents('td').find('p.thanks').fadeIn('slow');
	$(this).remove();
	return (false);
    });

    // Shrink sidebar to height of main image if present -- provide image with id="snuggleme" to activate.
    if ($('img#snuggleme').length) {
	var height = 279;
	if ($('img#snuggleme').height() > height)
	    height = $('img#snuggleme').height();
	$('div.sidebar-a').css({
	    'height': height,
	    'min-height': height
	});
    }
    // Dim header logo and fade in and out on hover.
    $('div#head-b img').css('opacity', 0.8).hover(function () {
						      $(this).fadeTo('fast',1);
						  },
						  function () {
						      $(this).fadeTo('fast',0.8);
						  });

    // Show/hide collections submenu in the sidebar navigation.
    $('ul li a.submenu').click(function () {
				  $(this).parent().toggleClass('deactive');
				  $(this).siblings('ul').toggle();
				  return(false);
    });

    /* BIO scroller */
    var interval = 200;		// Millisecond pause between scrolls.
		      var step = 40;		// Pixels to scroll by.
    var timer = null;		// Used to cancel scrolling.

    $('div#uparrow').hover(function () {
	function scrollup() {
	    if (parseInt($('div#bio').css('top')) < -step)
		$('div#bio').animate({top: '+=' + step},interval,'linear');
	}
	    scrollup();
	    timer = setInterval(scrollup,interval + 10);
	}, function () {
	    clearTimeout(timer);
    });;

    $('div#downarrow').hover(function () {
	function scrollup() {
	    $('div#bio').animate({top: '-=' + step},interval,'linear');
	}
	    scrollup();
	    timer = setInterval(scrollup,interval);
	}, function () {
	    clearTimeout(timer);
    });;

    /*** Collection carousel ***/
    if ($('ul#collectioncarousel').length) {
	// Append an empty list item so that the last image will be shown.
	$('ul#collectioncarousel').append('<li />');

	// Adjust to suit taste.
	var fadedOpacity = 0.1;

	// Fade out waiting images in collections.
	$('ul li.fade>img').css('opacity', fadedOpacity);

	function fadeIn (carousel, li, index, state) {
	    $(li).children().fadeTo('slow', 1);
	}

	function fadeOut (carousel, li, index, state) {
	    $(li).children().fadeTo('slow', fadedOpacity);
	}

	// Binds carousel controls to Up/Down arrows.
	function collectioncarouselSetup(carousel) {
	    $('#backarrow').bind('click', function () {
	        carousel.prev();
		return false;
	    });

	    $('#forwardarrow').bind('click', function () {
		carousel.next();
		return false;
	    });
	}

	// Activate carousel.
	$('ul#collectioncarousel').jcarousel({
	    initCallback: collectioncarouselSetup,
	    visibile: 1,
	    scroll: 1,
	    animation: 'slow',
	    wrap: 'both',
	    itemFirstInCallback: fadeIn,
	    itemFirstOutCallback: fadeOut
	});
    }
});
