(function($){

    var source = '/master-standard-advanced-search.html';
    
    function putData(category,depth,container)
    {
        $.ajax({
            url: source + '?cat=' + category,
            dataType: "html",
            success: function(response)
            {
                regexp = /__CategoryTitle__/gi;
                if (response.match(regexp) == null ){
                    if (depth==1){
                        option = $('<option></option>').attr('value','').html('Select Car Model');
                        $('.model',container).html(option);
                        $('.model',container).append(response);
                        
                        $('.model option',container).each(function(){
                            var value = $(this).html();
                            value = value.replace(' Accessories','');
                            $(this).html(value);
                        });
                    }
                    if (depth==2){
                        option = $('<option></option>').attr('value','').html('Select Product Type');
                        $('.product-type',container).html(option);
                        $('.product-type',container).append(response);
                        
                        $('.product-type option',container).each(function(){
                            var value = $(this).html();
                            value = value.replace(' Accessories','');
                            $(this).html(value);
                        });
                    }
                }
            }
        });
    };
    
    function clearData(depth)
    {
        if (depth==2){
            option = $('<option></option>').attr('value','').html('Select Product Type');
            $('#product-type').html(option);
        }
    }
    
    function stripCat(category)
    {
        regexp=/\//gi;
        var result = category.replace(regexp,'');
        result = result.replace('categorylist','');
        return result;
    }
    
    $(document).ready(function()
    {
        $('#make option').each(function(){
        	var value = $(this).html();
        	value = value.replace(' Accessories','');
        	$(this).html(value);
        });
    
        $('#make').change(function()
        {
            if ($(this).val() != ''){
                putData(stripCat($(this).val()),1,'#Vehicle-Form');
            }
            clearData(2);
        });
        
        $('#model').change(function()
        {
            if ($(this).val() != ''){
                putData(stripCat($(this).val()),2,'#Vehicle-Form');
            }
        });
        
        $('.Search-Menu-Button', '#Vehicle-Form').click(function()
        {
            if ( $('#make').val() != '' )
            {
                if ( $('#product-type').val() != '' ) document.location.href = $('#product-type').val();
                else if ( $('#model').val() != '' ) document.location.href = $('#model').val();
                else document.location.href = $('#make').val();
            }
        });
        
        $('#make2 option').each(function(){
        	var value = $(this).html();
        	value = value.replace(' Accessories','');
        	$(this).html(value);
        });
        
        $('#make2').change(function()
        {
            if ($(this).val() != ''){
                putData(stripCat($(this).val()),1,'#Vehicle-Form2');
            }
            clearData(2);
        });
        
        $('#model2').change(function()
        {
            if ($(this).val() != ''){
                putData(stripCat($(this).val()),2,'#Vehicle-Form2');
            }
        });
        
        $('.Search-Menu-Button', '#Vehicle-Form2').click(function()
        {
            if ( $('#make2').val() != '' )
            {
                if ( $('#product-type2').val() != '' ) document.location.href = $('#product-type2').val();
                else if ( $('#model2').val() != '' ) document.location.href = $('#model2').val();
                else document.location.href = $('#make2').val();
            }
        });
        
    });
    
})(jQuery);
