jQuery().ready(
    function() {
      /* tab scheda */
      $('.scheda-CNT .tabs a').click(function() {
        if ($(this).hasClass('active')) {
          return false;
        } else {
       /*   if (!$(this).hasClass('noajax')) {*/
            $('.scheda-CNT .tabs a').removeClass('active');
            var lnkTAB = $(this).attr('id').replace('-lnk', '');
            $('.scheda-CNT .content-TAB:visible').hide(0, function() {
              $('.scheda-CNT .tab-CNT #' + lnkTAB).show();
            });
            $(this).addClass('active');
            return false;
          /*}
          else
          {
          //  $(this).attr('href', $(this).attr('href').replace('.tab', '.html'));
          }*/
        }
      });

      /* tab mediagallery */
      $('.gallery-TAB a').click(function() {
        if ($(this).hasClass('active')) {
          return false;
        } else {
          $('.gallery-TAB a').removeClass('active');
          var lnkTAB = $(this).attr('href').replace('#', '');
          $('.media-gallery-TAB .gallery-TAB-CNT:visible').hide(0, function() {
            $('.media-gallery-TAB #' + lnkTAB).show();
          });
          $(this).addClass('active');
          return false;
        }
      });

 
      /* elimina disclaimer se trova cookie */
      if (true) {
        //$('#fitment-disclaimer').remove();
        $('#fitment-find-button').css('visibility', 'visible');
       // $('#fitment-find-button').addClass('not-active');
      }

      /* size-range fitment form */
      $.getJSON(ctxPage + '/tyre/brands.htm', {
        pageHandle : handle,
        vehicleType : 'CAR'
      }, function(data) {
    		
        $('select#manufacturer option').remove();
       if (typeof fitManufactures != "undefined") {
	        $('select#manufacturer').append(
	            '<option value="">'+fitManufactures+': </option>');
        }
        $.each(data, function(i, item) {
          $('select#manufacturer').append(
              '<option value="' + item.id + '">' + item.label + '</option>');
          if ($.cookie('car_manufacturer') == item.id) 
        	  $('select#manufacturer option').attr("selected", "selected");
        });

        if ($.cookie('car_model'))
        {
        	$("select#manufacturer").change();
            if ($.cookie('car_version'))
            {
            	$("select#model").change();
            }
            else
            {
                $('select#version option').remove();
                if (typeof fitVersion != "undefined") {
                $('select#version').append('<option value="">'+fitVersion+': </option>');
                }
                $('select#version').attr("disabled", "disabled");
            }          	
        }
        else
        {
	        $('select#model option').remove();
	        if (typeof fitModel != "undefined") {
	        $('select#model').append('<option value="">'+fitModel+': </option>');
	        }
	        $('select#model').attr("disabled", "disabled");

            $('select#version option').remove();
            if (typeof fitVersion != "undefined") {
            $('select#version').append('<option value="">'+fitVersion+': </option>');
            }
            $('select#version').attr("disabled", "disabled");
        }

//        if (($.cookie('car_manufacturer')) && ($.cookie('car_model')) && ($.cookie('car_version')))
//        	$('#sizeRange .find-button').click();
        $('#fitment-disclaimer').show();
      });

      
      $("select#manufacturer").change(
          function() {
          	var mValue = $(this).val();
          	var isManufClick=false;
        	if (!mValue){
        		mValue = $.cookie('car_manufacturer');  
        		isManufClick=true;
        	 }       	  
              $.ajax({
				  url:ctxPage + '/tyre/models.htm',
				  dataType: 'json',
				  data: {
            	  	pageHandle : handle,
				  	brandId : $(this).val()
				  	},                 	
            	  async: false,
            	  success: function (data){
                  $('select#model option').remove();
                  $('select#model').append(
                      '<option value="">'+fitModel+': </option>');
                  $.each(data, function(i, item) {
                    $('select#model').append(
                        '<option value="' + item.id + '">' + item.label
                            + '</option>');
                    if ($.cookie('car_model') == item.id && isManufClick==false ) 
                  	  $('select#model option').attr("selected", "selected");                        
                  });
                  if (isManufClick==false)
                  	  $('select#model').removeAttr("disabled");
                     else
                   	  $('select#model').attr("disabled", "disabled");

                  $('select#version option').remove();
                  $('select#version').append(
                      '<option value="">'+fitVersion+': </option>');
                  $('select#version').attr("disabled", "disabled");            	  
              }
			})
			checkFindButton();
          });

      $("select#model").change(
              function() {
            	var mValue = $(this).val();
            	var isModelClick=false;
            	if (!mValue){
            		isModelClick=true;
            		mValue = $.cookie('car_model');    
            	  }          	  
                  $.ajax({
    				  url:ctxPage + '/tyre/versions.htm',
    				  dataType: 'json',
    				  data: {
                	  	pageHandle : handle,
                	  	modelId : mValue
    				  	},                 	
                	  async: false,
                	  success: function(data) {
                          $('select#version option').remove();
                          $('select#version').append(
                              '<option value="">'+fitVersion+': </option>');
                          $.each(data, function(i, item) {
                            $('select#version').append(
                                '<option value="' + item.id + '">' + item.label
                                    + '</option>');
                            if ($.cookie('car_version') == item.id && isModelClick==false) 
                            	  $('select#version option').attr("selected", "selected");                         
                          });
                          if (isModelClick==false)
                            	 $('select#version').removeAttr("disabled");
                          else
                            	 $('select#version').attr("disabled", "disabled");
                        }
    			})
    			checkFindButton();
              });
      
      
      $("select#version").change(
    		  function() {
    			  checkFindButton();
    		  });


      $('#sizeRange .find-button').click(function() {
    	if($('select#manufacturer').val() && $('select#model').val() && $('select#version').val()){
        	$('#results-CNT').html("").show('blind', 223);
        	$('#results-CNT-related').html("").show('blind', 223);
        	$('#fitment-find-button').addClass('not-active'); //blocco il tasto 
        	$('div#wait').html(ajaxLoader); // metto il wait
          var manufacturer = $('#manufacturer option:selected').val();
          var model = $('#model option:selected').val();
          var version = $('#version option:selected').val();

	      if (!version)
	    		version = $.cookie('car_version');   
          $.cookie('car_manufacturer', manufacturer , {path: '/', expires : 365 });
          $.cookie('car_model', model , {path: '/', expires : 365 });
          $.cookie('car_version', version, {path: '/', expires : 365 });
          
          $.ajax( {
            url : ctxPage + '/tyre/fitResult.htm',
            data : {
              pageHandle : handle,
              locale : homeStateLocale,
              manufacturerId : manufacturer,
              modelId : model,
              versionId : version,
              productId : productId
            },
            async: false,
            success : function(html) {
              $('#results-CNT').html(html).show('blind', 223);
            }
          });
          
          $.ajax( {
              url : ctxPage + '/tyre/fitResultRelated.htm',
              data : {
                pageHandle : handle,
                locale : homeStateLocale,
                manufacturerId : manufacturer,
                modelId : model,
                versionId : version,
                productId : productId
              },
              async: false,
              success : function(html) {
                $('#results-CNT-related').html(html).show('blind', 223);
              }
            });          
          $('#fitment-find-button').removeClass('not-active');//sblocco il tasto
          $('div#wait').html(''); // tolgo il wait
        }
        return false;
      });

      /* accordion */
      $(".accordion-CNT").accordion( {
        collapsible : true,
        autoHeight : false,
        navigation : true,
        active : false
      });
      
      checkFindButton();
    });

function checkFindButton(){
	if($('select#manufacturer').val() && $('select#model').val() && $('select#version').val()){
		$('#fitment-find-button').removeClass('not-active');
	}else{
		$('#fitment-find-button').addClass('not-active');
	}
}
