$(document).ready(function(){

    // First and last classes
    $('.pre-nav li:last, .tFooter li:last').addClass('last');
    $('table tbody tr td .cell:first-child').addClass('first');
    $('table tbody tr td .cell:last-child').addClass('last');
    $('table tbody tr:last td').addClass('bottom');
    $('.product_categories .row:last').addClass('last');
    $('.row .product_category:nth-child(4n)').addClass('last');

    // Main navigation
    $('#nav li:first a').css({ 'margin-left': '15px' });
    $('#nav li:last a').css({ 'margin-right': 0 });
    
	// Product finder 
	
	$('#product_finder').change(function() {
		$('#product_finder_form').attr('action',$(this).val()).submit();
	});
	
	// Main product page
	
	$(function() {
		$('.rollover').hover(function() {
			var currentImg = $(this).attr('src');
			$(this).attr('src', $(this).attr('longdesc'));
			$(this).attr('longdesc', currentImg);
		}, function() {
			var currentImg = $(this).attr('src');
			$(this).attr('src', $(this).attr('longdesc'));
			$(this).attr('longdesc', currentImg);
		});
	});
	
    // Homepage scrollable
		$('#scrollable').scrollable({ circular: true }).navigator({
			navi: '#scrollable-nav',
			naviItem: 'a',
			activeClass: 'current',
			history: true
		}).autoscroll({ autoplay: true, interval: 4000 });
		$('#scrollable-nav a:first').addClass('current');
    
    // Search placeholder
    $('#query').each(function(){
        var placeholder = 'Search';
        $(this).attr('value', placeholder);
        $(this).addClass('blurred');
        $(this).focus(function(){
            if ($(this).attr('value') == placeholder) {
                $(this).removeClass('blurred').attr('value', '');
            }
        });
        $(this).blur(function(){
            if ($(this).attr('value') == '') {
                $(this).addClass('blurred').attr('value', placeholder);
            }
        });
    });
    
    $('td').each(function(){
        $(this).wrapInner('<div/>').children('div').addClass('cell');
    });
    
    // Equal heights...zzzz...
    // Uhh...not working...?
    function equalize(parent, offset) {
        parent.each(function(offset){
            var tallest = 0;
            $(this).children().each(function(){
                thisHeight = $(this).height();
                if (thisHeight > tallest) {
                    tallest = thisHeight;
                }
            });
            $(this).children().each(function(){
                $(this).height(tallest);
            });
        });
    }
    // equalize($('#main .equal'), 17);

    
    // Product detail tabs
    $('.tabs-nav').tabs('.tabs-panel');

    // Fadey fade + arrows on category homepage
    $('.category p a').append('<span class="enhanced"/>').hover(function(){
        $(this).find('.enhanced').stop(true, true).fadeIn('fast');
    }, function(){
        $(this).find('.enhanced').stop(true, true).fadeOut('fast');
    });
    
    // Vertical centering
//     $('.category p a span').not('.enhanced, .arrow').each(function(){
//         var move = ($(this).parent().height() - $(this).height()) / 2;
//         $(this).css({ 'top': move + 'px' });
//     });
            
//     $('.category').each(function(){
//         var total = $(this).find('.thumb > a').height();
//         var image_width;
//         var image_height;
//         $(this).find('img').load(function() {
//             image_width = this.width;
//             image_height = this.height;
//             var move = Math.round((total - image_height) / 2);
//             $(this).css({ 'top': move + 'px' });   
//         });
//     });
});
