jQuery.noConflict();
jQuery(document).ready( function( $ ) {
function selectHasValue( select ) {
var hasValue = false;
select.find('option').each( function() {
if( '' != jQuery(this).val() && 0 != jQuery(this).val() ) {
hasValue = true;
}
});
return hasValue;
}
var decorateUnavailableSelections = function() {
var mode = '';
var select = jQuery('#modelSelect');
if( !selectHasValue( select ) ) {
switch( mode ) {
case 'hide':
select.hide();
select.prev('label').hide();
break;
case 'disable':
select.attr('disabled','disabled');
break;
}
}
var select = jQuery('#yearSelect');
if( !selectHasValue( select ) ) {
switch( mode ) {
case 'hide':
select.hide();
select.prev('label').hide();
break;
case 'disable':
select.attr('disabled','disabled');
break;
}
}
}
// @author Kevin van Zonneveld (http://kevin.vanzonneveld.net), Onno Marsman, Brett Zamir (http://brett-zamir.me)
function ucfirst (str) {
str += '';
var f = str.charAt(0).toUpperCase();
return f + str.substr(1);
}
decorateUnavailableSelections();
var chained = function() {
var showSelect = function( select ) {
jQuery(select)
.show()
.attr('disabled','')
.prev('label').show();
}
var loadNextSelectIfOneOption = function( loadedSelect, selectAfterLoaded ) {
if( jQuery('#' + loadedSelect + 'Select option' ).length == 1 ) {
var myFuncName = 'load' + ucfirst(selectAfterLoaded) + 's';
eval(myFuncName)();
}
}
function lastLevel() {
return 'year';
}
var getUrl = function( level ) {
var value = jQuery('#' + level + 'Select').val();
var product = jQuery('#vafProduct').val();
var url = 'http://www.theistore.com//vaf/ajax/process?';
url = url + '&product=' + product;
url = url + '&front=1';
url = url + '&' + level + '=' + value;
return url;
}
var loadModels = function() {
var callbackFunc = function(){
showSelect(this);
loadNextSelectIfOneOption( 'model', 'year' );
decorateUnavailableSelections();
jQuery(this).trigger('vafLevelLoaded');
};
decorateUnavailableSelections();
var url = getUrl( 'make' );
var loadingText = '';
jQuery('#modelSelect')
.html( loadingText )
.load( url, {}, callbackFunc );
jQuery('#yearSelect').html('');
}
var loadYears = function() {
var callbackFunc = function(){
showSelect(this);
autoSubmit();
decorateUnavailableSelections();
jQuery(this).trigger('vafLevelLoaded');
};
decorateUnavailableSelections();
var url = getUrl( 'model' );
var loadingText = '';
jQuery('#yearSelect')
.html( loadingText )
.load( url, {}, callbackFunc );
}
function hasOptions(){
return jQuery( '#' + lastLevel() + 'Select option' ).size() >= 1;
}
function hasSelection() {
return selectHasValue( jQuery( '#' + lastLevel() + 'Select' ) );
}
function selectionHasValue() {
var val = jQuery( '#' + lastLevel() + 'Select option:selected' ).val();
return '' != val && 0 != val;
}
function autoSubmit()
{
if( hasOptions() && hasSelection() && selectionHasValue() )
{
submitVafForm();
}
}
submitVafForm = function()
{
if( jQuery('#categorySelect').val() != '?' )
{
jQuery('#vafForm').attr( 'action', jQuery('#categorySelect').val() );
}
var chooser = jQuery('#categorySelect');
if( !chooser.is('input') ) {
chooser.html('');
}
jQuery().trigger("vafSubmit");
return false;
}
jQuery().bind("vafSubmit", function() {
document.vafForm.submit();
});
clearVafForm = function()
{
window.location = '?make=0&model=0&year=0';
}
jQuery( '#yearSelect').change( function() {
autoSubmit();
} );
jQuery('#vafSubmit').click( submitVafForm );
jQuery('#vafClear').click( clearVafForm );
jQuery( '.vafCheckAll' ).click( function() {
jQuery( '.vafcheck' ).attr( 'checked', jQuery(this).attr( 'checked' ) );
})
jQuery( '.vaf-cat-toggler' ).click( function() {
var icon = jQuery(this).children( 'div.vaf-toggle-icon ' );
icon.toggleClass( 'vaf-toggle-icon-minus' );
var toggleDiv = jQuery(this).nextAll( 'div.vaf-toggle' );
toggleDiv.toggle();
});
jQuery( 'div.vaf-toggle' ).hide();
var callback = loadModels;
jQuery('#makeSelect').change( callback );
var callback = loadYears;
jQuery('#modelSelect').change( callback );
}
jQuery.fn.chained = function() {
this.each( function() {
var mychained = new chained( this );
});
};
jQuery('#vafForm').chained();
});