/* Use when several pulldowns need to be filtered.
 * Requires jQuery v1.3.2+
 * All the dropdown form elements should have "filterable" class and "level" attribute with number (0,1,2,3...)
 * Options should have "id_parent" attribute which contains value of the parent option value for filter.
 * Usage example: $("#container").DropDownFilter();
 */
(function ($) {
	var DropDownFilter = function () {		
		return {
			init : function(show){
				if (show == null) show = true;
				var original = [];
				var original_selected = [];
				var parent = this;				
				$("select.filterable", parent).each(function(){
					original[$(this).attr("level")] = $(this).html();
					var tmp = []
					$(this).find(":selected").each(function(){
						tmp.push(this.value);
					});					
					original_selected[$(this).attr("level")] = tmp;
				});				
							
				$("select.filterable", parent).bind('change', function(e){
					var childLevel = parseInt($(this).attr("level"))+1;
					var child = $("select[level="+childLevel+"].filterable", parent);
					if (child.length){
						child.html( original[childLevel] );

						if (!show && $(this).val()==''){
							$('option', child).each(function(){
								if ($(this).val() != ""){
									$(this).remove();
								}
							})
							child.trigger($.Event("change"));
							return;
						}

						var values_array = [];
						$(this).find(":selected").each(function(){
							values_array.push($(this).val());
						});
						if ($.inArray( "", values_array) != -1){
							values_array = [];
							$(this).find("option").each(function(){
								values_array.push($(this).val());
							});
						}
                                                var is_in_arr = [];
						$('option', child).each(function(){
							var arr = $(this).attr("id_parent");
                                                        if (arr){
								arr = arr.split(',');
								var found = false;
                                                                for (var i=0; i<arr.length; i++){
									if ( $.inArray( $.trim(arr[i]), values_array ) != -1 ){
										found = true;
                                                                                if( $.inArray( $(this).val(), is_in_arr ) != -1){
                                                                                    $(this).remove();
                                                                                }
                                                                                else{
                                                                                    is_in_arr.push($(this).val());
                                                                                }
                                                                                
									}

								}
								if ( !found && ($(this).val() != "" || !show)){
									$(this).remove();
								}
							}
                                                        else {
								$(this).remove();
							}
						});

						$('option', child).removeAttr("selected");
						//if (show) $('option:first', child).attr("selected", "true");
						if ($.browser.msie) child.hide().show();
						child.trigger($.Event("change"));
					}
				});	
				
				$(original_selected).each(function(level,values){
                                        if (values){
						$(values).each(function(){
							$("select[level="+level+"].filterable option[value="+this+"]").attr("selected", "true");
						});
						$("select[level="+level+"].filterable").trigger($.Event("change"));
					}
				})
			}
		}
	}()
	$.fn.extend({
		DropDownFilter: DropDownFilter.init		
	});
})(jQuery)
