$(document).ready(function () {

    /*
     * Search box magic.
     */
    var searchfield = $('input[name=q]');
    var searchprompt = ' Find it here';
    if (searchfield.length) {
	/* Prefill with search prompt. */
	if (searchfield.val() == '')
	    searchfield.val(searchprompt);

	/* Clear search prompt on first keypress. */
        searchfield.keydown(function () {
	    $(this).unbind('keydown');
	    if ($(this).val() == searchprompt)
	        $(this).val('');
	});

	/* Clear search prompt on click. */
        searchfield.click(function () {
	    /* Clear search prompt. */
	    if ($(this).val() == searchprompt)
	        $(this).val('');
	});

	/* Give the search box focus and move the cursor to the start. */
	sf = searchfield.get(0);
	sf.focus();
	if (sf.setSelectionRange) /* Firefox style. */
	    sf.setSelectionRange(0,0);
	else if (sf.createTextRange) { /* IE style. */
	    var range=sf.createTextRange();
	    range.collapse(true);
	    range.moveEnd('character', 0);
	    range.moveStart('character', 0);
	    range.select();
	}
   }

    /* Cross fade from B+W to colour on hover in the gallery. */
    if ($('ul#gallery').length) {
        $('ul#gallery li').hover(function () {
	    $('p#title').html('<span class="triangle">&#9650; </span> '+$(this).find('img:first').attr('title'));
            $(this).find('img').toggleClass('invisible');
        },
        function () {
	    $('p#title').text('');
            $(this).find('img').toggleClass('invisible');
        });
    }

    /* Cross fade from B+W to colour on hover on the results page. */
    if ($('div.result').length) {
	$('div.result').hover(function () {
            $(this).find('img').toggleClass('invisible');
	    $(this).find('h3.resulttitle').toggleClass('underline');
	}, function () {
            $(this).find('img').toggleClass('invisible');	
	    $(this).find('h3.resulttitle').toggleClass('underline');
	});
    }

    /* Rollover for the BUY button. */
    if ($('div.buy').length) {
        $('div.buy').hover(function () {
	    $(this).toggleClass('buyhover');
	}, function () {
	    $(this).toggleClass('buyhover');
        });
    }

    /* Rollover for the pagination controls. */
    if ($('ul.pagenav li').length) {
        $('ul.pagenav li').not('.active').hover(function () {
	    $(this).toggleClass('active');
	}, function () {
	    $(this).toggleClass('active');
	});
    }

});