

(function ($) {
  
  
  Drupal.behaviors.triggerTrimSelector = {
    attach: function(context) {
      $('.trim-select-list-item-drop-down:not(triggerTrimSelector-processed)', context).click(function () {
        $(this).addClass('triggerTrimSelector-processed');
        if ($('.trim-select-list-options', $(this)).hasClass('expanded')) {
          $('.trim-select-list-options', $(this)).slideUp().removeClass('expanded');
          $('.trim-select-list-options', $(this)).parent().removeClass('expanded');
        }
        else {
          $('.trim-select-list-options', $(this)).slideDown().addClass('expanded');
          $('.trim-select-list-options', $(this)).parent().addClass('expanded');
        }
      });
      $('.price-info-trim-select-drop-down:not(triggerTrimSelector-processed)', context).click(function () {
        $(this).addClass('triggerTrimSelector-processed');
        if ($(this).hasClass('expanded')) {
          $('.price-info-trim-value ul.trim-select-list-options').slideUp();
          $(this).removeClass('expanded');
        }
        else {
          $('.price-info-trim-value ul.trim-select-list-options').slideDown();
          $(this).addClass('expanded');
        }
      });
    }
  };
  
  Drupal.behaviors.openRoadVehiclesCompareModal = {
    attach: function(context, settings) {
      // Instantiate the dialog object
      $( "#compare-vehicles-wrapper", context).dialog({
        autoOpen: false,
        width: 900,
        modal: true,
        position: ['center', 60],
        title: 'Compare Vehicles'
      });
      $('a.trim-select-compare-link', context).click(function (event) {
        
        event.preventDefault();
    		var redirect_url = this.href;
    		
    		$('#compare-vehicles-wrapper').html('<div class="ajax-loader-white"></div>');
        $('#compare-vehicles-wrapper').dialog('option', 'height', 150);
        $('#compare-vehicles-wrapper').load(redirect_url + ' #compare-vehicles-wrapper-inner', function() {
          $('#compare-vehicles-wrapper').dialog('option', 'height', 'auto');
          Drupal.attachBehaviors($('#compare-vehicles-wrapper'));
        });
        $('#compare-vehicles-wrapper').dialog('open');
    		$('#compare-vehicles-wrapper').closest('.ui-dialog').addClass('ui-dialog-compare-vehicles');
    		return false;
      });
    }
  };
  
  Drupal.behaviors.openroadVehiclesContactForm = {
    attach: function (context, settings) {
      $( "#contact-form-modal-wrapper", context).dialog({
        autoOpen: false,
        width: 400,
        modal: true,
        position: ['center', 60],
        title: 'Contact Us'
      });
      $('#edit-contact-submit', context).click(function (event) {
        event.preventDefault();
    		//var redirect_url = this.href;
    		
    		$('#contact-form-modal-wrapper').html('<div class="ajax-loader-white"></div>');
        $('#contact-form-modal-wrapper').load('/contact-us?contact_manufacturer='+$('input[name="contact_manufacturer"]').val() + ' #block-system-main', function() {
          //$('#contact-form-modal-wrapper').dialog('option', 'height', 'auto');
          Drupal.attachBehaviors($('#contact-form-modal-wrapper'));
          // Pre-populate the fields on the form
      		// Get and set the email address
      		var email = $('#edit-contact-email').val();
      		$('#webform-client-form-603 input#edit-submitted-email').val(email);
      		// Get and set the department based on the type of contact
      		var subject = $('#openroad-vehicle-price-info-form input[name=contact_type]:checked').val();
      		$('#webform-client-form-603 input#edit-submitted-subject').val(subject);
      		$('#edit-submitted-department').val('sales');
      		var title = $('#content-header .title-wrapper-vehicle').html();
      		$('input[name="submitted[vehicle_information]"]').val(title);
      		$("#webform-component-department").hide();
        });
        $('#contact-form-modal-wrapper').dialog('option', 'height', 'auto');
        $('#contact-form-modal-wrapper').dialog('open');
    		$('#contact-form-modal-wrapper').closest('.ui-dialog').addClass('ui-dialog-contact-form');
    		return false;
      });
    }
  }
  
  Drupal.behaviors.openroadVehiclesCalculatorDialog = {
    attach: function(context, settings) {
      // Instantiate the dialog object
      $( "#pricing-calculator-wrapper" ).dialog({
        autoOpen: false,
        width: 550,
        modal: true,
        position: ["center", 60],
        title: "Pricing Calculator"
      });
      $('a.price-info-pricing-calculator-link').click(function(event) {
        event.preventDefault();
    		var redirect_url = this.href;
        $('#pricing-calculator-wrapper').html('<div class="ajax-loader-white"></div>');
        $('#pricing-calculator-wrapper').dialog('option', 'height', 200);
        $('#pricing-calculator-wrapper').load(redirect_url + ' #pricing-calculator-dialog-wrapper', function() {
          $('#pricing-calculator-wrapper').dialog('option', 'height', 'auto');
          Drupal.attachBehaviors($('#pricing-calculator-wrapper'));
        });
        $('#pricing-calculator-wrapper').dialog('open');
        $('#pricing-calculator-wrapper').closest('.ui-dialog').addClass('ui-dialog-pricing-calculator');
        return false;
      });
    }
  }
  
})(jQuery);;

