﻿function populateWithThis($_this){
	shipping.newAddress(true);
	var $ = jQuery;
	// jQuery time!!
	$this = $($_this);
	
	$('.lcb_company').val($this.find('.company').text());
	$('.lcb_add1').val($this.find('.add1').text());
	$('.lcb_add2').val($this.find('.add2').text());
	$('.lcb_postcode').val($this.find('.postcode').text());
	$('.lcb_city').val('London');
	$('.lcb_region select').removeClass('validation-failed').hide().val('');
	$('.lcb_region .input-text').addClass('validation-passed').show().val('');
	$('.lcb_country select').val('GB');
	
	
}

(function($){

    $.fn.dropDown = function() {
        var $this = $(this);
        var $selected = $(this).find('> .selected');
        $selected.click(function(){
            $(this).siblings('.options').slideDown('fast').show();
            $(this).parent().hover(function() {
            }, function(){
                $(this).find(".options").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
            });
        });
    }

    $(document).ready(function($){

        $('.footer-right-links .newsletter a').click(function(){
            var $ndt = $('#newsletter-dropdown-trigger');
            $('html, body').animate({
                scrollTop: $ndt.offset().top
                }, 1500);
            $ndt.addClass('selected').siblings('#newsletter-dropdown').addClass('displayed');
            return false;
        });


        $($('.stock_type select').change(function(){
            var val = $(this).val();
            $('.product-wrap > .content .case-wine, .product-wrap > .content .case_discount').show();
            $('.product-wrap > .content > .available-message').hide();
            if(val != 0){
              $('.'+val+' > .content .case-wine, .'+val+' > .content .case_discount').hide();
              $('.'+val+' > .content > .available-message').text($('#'+val+'-error-message').text()).show();
	            // show under bond label against title
	            
	            if(val == 'no-bond') {
	            	$('.item').each(function(){
	            		if(!$(this).hasClass('no-bond')){
		            			$(this).find('h2').append('<span class="ub_label">(Under Bond)</span>');
		            			var $a = $(this).find('a.card-small');
		            			$a.attr('href', $a.attr('href')+'ub/1' );
	            		}
	            	});
	            } else {
	            	$('.item h2 .ub_label').remove();
	            	$('.item a.card-small').each(function(){
	            		var $a = $(this);
	            		$a.attr('href', $a.attr('href').replace(/ub\/1$/, ''));
	            	});
	            }
            } else {
            	console.log(val);
            }
            
            $('.stock_type select').val(val);
        }).get(0)).change();
        
        if($('.category-en-primeur').size()){
        	$($('.stock_type select').val('no-bond').get(0)).change();
        }
        
        $('.search-filters').each(function(){
            var $this = $(this);
            var max_items = 8;
            $this.find('#narrow-by-list dd').each(function(){
                var $this = $(this);
                var $items = $this.find(' > ul > li');
                if($items.size() > max_items){
                    var height = 0;

                    $items.each(function(n){
                        if(n < 8){
                            height += $(this).height();
                        }
                    });
                    
                    var $ul = $this.find('> ul');
                    $ul.get(0).min_height = height;
                    $ul.get(0).max_height = $ul.height();
                    $this.find('> ul').height(height);
                    var $more = $('<a href="#" class="faux-button-b more-button">more</a>');
                    $this.append($more);
                }
            });

            $this.find('#narrow-by-list > dd .more-button').click(function(){
                var $this = $(this);
                var $ul = $this.prev();
                if($this.hasClass('.less-button')){
                    $this.text('more');
                    $this.removeClass('.less-button');
                    $ul.animate({height: $ul.get(0).min_height}, 500);
                } else {
                    $(this).text('less');
                    $this.addClass('.less-button');
                    $ul.animate({height: $ul.get(0).max_height}, 500);
                }
                return false;
            });
        });

        if($('#home-login-form .login-form').size()){
            var dataForm = new VarienForm('login-form', true);
        }
    });

})(jQuery);
