(function($){
    $.fn.clearSelect = function() {
        return this.each(function(){
            if(this.tagName=='SELECT') {
                this.options.length = 0;
                $(this).attr('disabled','disabled');
            }
        });
    }

    $.fn.clearField = function(selector) {
        this.nextAll(selector).remove();
        return this;
    }

    $.fn.fillSelect = function(dataArray) {
        return this.clearSelect().each(function(){
            if(this.tagName=='SELECT') {
                var currentSelect = this;
//                var start = new Option('-', '-1');
//                if($.support.cssFloat) {
//                    currentSelect.add(start, null);
//                } else {
//                    currentSelect.add(start);
//                }
                $.each(dataArray,function(index,data){
                    if(data.name) {
                        var option = new Option(data.name,data.id);
                        if($.support.cssFloat) {
                            currentSelect.add(option,null);
                        } else {
                            currentSelect.add(option);
                        }
                    }
                });
                $(this).removeAttr('disabled').find('option:first').attr('selected', 'selected');
            }
        });
    }
})(jQuery);

function getCategory(url, pcategory, level, select) {
    $.ajax({
        url: url,
        type: 'GET',
        data: {"pid": pcategory, "act":"getCategory", "level" : level, "isAjax":"1", "select":select},
        dataType: 'JSON',
        beforeSend: function(){
            $('select[name^='+select+'_]').attr('disabled', 'disabled');
        },
        complete: function(){
            $('select[name^='+select+'_]').removeAttr('disabled');
        },
        success: function(response){
            var data = eval('('+ response +')');
			
			if (data.count === 'undefined' || data.count == 0) {
				$('select[name='+select+'_' + (data.level - 1) + ']').clearField('select[name^='+select+']').clearField('div');
				return false;
			}

			if ($('select[name='+select+'_' + data.level + ']').length) {
				$('select[name='+select+'_' + data.level + ']').clearField('select[name^='+select+']').clearField('div').fillSelect(data.item);
			} else {
				$('#'+select+' div:last').after('<div><select name="'+select+'_' + data.level + '"></select></div>');
				$('select[name='+select+'_' + data.level + ']').fillSelect(data.item);
			}
				
			$('select[name='+select+'_' + data.level + ']').parent().nextAll('div').remove();
			$('select[name='+select+'_' + data.level + ']').unbind('change');
			
			$('select[name='+select+'_' + data.level + ']').change(function(){
				return clickEvent(url, $(this), select);
			});
			
			return false;
        }
    });
}

function clickEvent(url, select, objSelected)
{
    var id = select.find('option:selected').attr('value');
	
	$(select).parent().nextAll('div').remove();
	
	if (id == '-1') {
		select.clearField('select[name^='+objSelected+']').clearField('div');
		return false;
	}
		
	var level = parseInt(select.attr('name').replace(objSelected+'_', '')) + 1;

    return getCategory(url, id, level , objSelected);
}
