//////////////////////////////////////////////////////////////////////////////////
// Cascading Select Boxes V1.0.0
// (c) 2011 by Vasja Laharnar thanks to the article by Jeremy Satterfield
// http://jsatt.blogspot.com/2010/01/cascading-select-boxes-using-jquery.html
// MIT License
//
// Please retain this copyright header in all versions of the software
//////////////////////////////////////////////////////////////////////////////////
(function( $ ) {
    $(document).ready(function () {
        $('form.cascading').CascadingSelectBoxes()
    });

    function CascadingSelectBoxes(f, opts) {
        var s = $('select.cascading', f);
		
		this.cascadeSelect = function (parent, child){
			var childOptions = child.find('option:not(.static)');
			child.data('options', childOptions);
 
			parent.change(function(){
				childOptions.remove();
				child.append(child.data('options').filter('.sub_' + this.value)).change();
 			})
 
			childOptions.not('.static, .sub_' + parent.val()).remove();
		}
        
        this.init = function () {
			if ( s.length > 1 ) {
				for (i=0; i < s.length-1; i++) {
					this.cascadeSelect($(s[i]), $(s[(i+1)]));
				}
			}
        };
		
		this.init();
    }
    $.fn.CascadingSelectBoxes = function (options) {
        this.each(function () {
			opts = $.extend({}, $.fn.CascadingSelectBoxes.defaults, options);
			$(this).data('cascade', new CascadingSelectBoxes($(this), opts))
        });
        return this
    };
    $.fn.CascadingSelectBoxes.defaults = {

    }
})(jQuery);
