/* input skinner plugin */
jQuery.fn.customInput = function(){
	$(this).each(function(i){	
		if($(this).is('[type=checkbox],[type=radio]')){
			var input = $(this);
			
			// get the associated label using the input's id
			var label = $('label[for='+input.attr('id')+']');
			
			//get type, for classname suffix 
			var inputType = (input.is('[type=checkbox]')) ? 'checkbox' : 'radio';
			
			// wrap the input + label in a div 
			$('<div class="custom-'+ inputType +'"></div>').insertBefore(input).append(input, label);
			
			// find all inputs in this set using the shared name attribute
			var allInputs = $('input[name='+input.attr('name')+']');
			
			// necessary for browsers that don't support the :hover pseudo class on labels
			label.hover(
				function(){ 
					$(this).addClass('hover'); 
					if(inputType == 'checkbox' && input.is(':checked')){ 
						$(this).addClass('checkedHover'); 
					} 
				},
				function(){ $(this).removeClass('hover checkedHover'); }
			);
			
			//bind custom event, trigger it, bind click,focus,blur events					
			input.bind('updateState', function(){	
				if (input.is(':checked')) {
					if (input.is(':radio')) {				
						allInputs.each(function(){
							$('label[for='+$(this).attr('id')+']').removeClass('checked');
						});		
					};
					label.addClass('checked');
				}
				else { label.removeClass('checked checkedHover checkedFocus'); }
										
			})
			.trigger('updateState')
			.click(function(){ 
				$(this).trigger('updateState'); 
			})
			.focus(function(){ 
				label.addClass('focus'); 
				if(inputType == 'checkbox' && input.is(':checked')){ 
					$(this).addClass('checkedFocus'); 
				} 
			})
			.blur(function(){ label.removeClass('focus checkedFocus'); });
		}
	});
};
//end plugin


if (typeof(jQuery) != 0) {
    $(function() {
        var colour_chosen = null;
        $('body').addClass('hasJs'); /* Add class hasJs to xxx to target js centric styles - use to show-hide relevant parts in accessible pages */
        $('#wine_chooser input, #wine_chooser.colour_type input').customInput(); // call the radio skinning plugin 
        $('#wine_chooser fieldset#wine_price input[name="price"]').attr('disabled', true); // disable 2nd radios
        $('#wine_chooser fieldset#wine_price label').hover(function() { $(this).removeClass('hover').css('cursor', 'default'); }); // disable 2nd radios look

        //on colour page 
        $('#wine_chooser #advanced_search').click(function() {//add click handler
            $(this).html() == 'Advanced search' ? $(this).html('Hide options') : $(this).html('Advanced search');
            $('#advanced_options').slideToggle(300);
            this.blur();
            return false;
        });

        //advanced options on quick wine chooser 
        $('#qwc_advanced_search').click(function() {//add click handler
            $(this).html() == 'Advanced search' ? $(this).html('Hide options') : $(this).html('Advanced search');
            $('#qwc_advanced_options').slideToggle(300);
            this.blur();
            return false;
        });

        //enabling price radio rows
        $("input[name='colour']").click(function() {//add click handler
            colour_chosen = this.value;
        });

        //dropdown state change styling 
        $('#advanced_options select').change(function() {//add changehandler
            $(this).addClass('dd_selected');
        });

        //change wine links href's to not link to wine id
        //$('h2 a.wine-header').click(function() {
        //  $(this).attr('href', $(this).attr('rel'));
        //});
    }
)};
