$(document).ready(function () {
    // product list "Add to Basket" buttons
    $('.product-list form.add').show();
    $('.product-list a.add').click(function (e) {
        e.preventDefault();
        $(this).closest('form').submit();
    });
    
    // splash "Add to Basket" buttons
    $('.splash form.add input.button').hide();
    $('.splash form.add>div').append(
        '<a class="add" href="/shop/cart">Add to Basket</a>');
    $('.splash a.add').click(function (e) {
        e.preventDefault();
        $(this).closest('form').submit();
    });
    
    // product details "Add to Basket" button
    $('.app\\/product-details .info-bar input.button').hide();
    $('.app\\/product-details .info-bar form>div').append(
            '<a class="add" href="/shop/cart">Add to Basket</a>');
    $('.app\\/product-details .info-bar a.add').click(function (e) {
        e.preventDefault();
        $(this).closest('form').submit();
    });
    
    // sidebar "checkout" button
    $('.box-cart input.button').hide();
    $('.box-cart form>div').append(
            '<input type="hidden" name="operation" value="Checkout" />'+
            '<a class="checkout" href="/shop/basket">Checkout</a>');
    $('.box-cart form a.checkout').click(function (e) {
        e.preventDefault();
        $(this).closest('form').submit();
    });
    
    // reveal long product names
    var products = $('.product-list li');
    products.each(function () {
        var outer = $(this).find('.product-name');
        var inner = outer.children('h4');
        var o_w = outer.width();
        var i_w = inner.width();
        if (o_w - i_w < -2)
        {
            $(this).addClass('reveal-title');
        }
    });
    
    products.filter('.reveal-title').hover(
        function (e) {
            var outer = $(this).find('.product-name');
            var inner = outer.children('h4');
            var o_w = outer.width();
            var i_w = inner.width();
            inner.animate({"margin-left":(o_w-i_w)+'px'}, (i_w-o_w)*17, 'linear');
        },
        function (e) {
            $(this).find('.product-name>h4').stop().css({"margin-left":"0px"});
        }
    );
    
    // on shipping form, hide "other" field unless selected
    var date_select = $('#ship_date_field');
    if (date_select.size())
    {
        var other_field = date_select.parent().children('span');
        other_field.css({'visibility':'visible'});
        var other_input = other_field.children('input');
        other_input.attr('value', 'To arrive by what date?');
        function show_other_date()
        {
            var sel = date_select.find(':selected').attr('value');
            if (sel == 'Other')
            {
                if (other_field.css('visibility') == 'hidden')
                {
                    other_field.css({'visibility':'visible'});
                    other_input.focus().select();
                }
            }
            else
            {
                if (other_field.css('visibility') == 'visible')
                {
                    other_field.css({'visibility':'hidden'});
                }
            }
        }
        other_input.bind('click', function () { $(this).select(); });
        date_select.bind('click keyup change', show_other_date);
        show_other_date();
    }
});
