jQuery.fn.linkselects = function() {
    return this.each(function() {


        //remove all options from the link targets
        $("select.link-" + this.id).each(function() {
            var options= Array();
            $(this).children().each(function() {options.push($(this).clone());});
            $(this).children().remove();
            this.optiontags=options;
        });

        //create a change function to replace the selected options
        $(this).change(function() {
            var v=$(this).attr("value");

            $("select.link-" + this.id).each(function() {
                var select=$(this);
                $(this).children().remove();
                $(this.optiontags).each(function() {
                    if($(this).hasClass("link-" +v)) {
	                    select.append(this);
                    }
                });

                this.selectedIndex=-1;
                //Invoke change on the target in case it is also a source
                //in order to cascade updates
                $(this).change();
            });


        });

        //Raise a change event to initialise the content
        $(this).change();

    });
};
$(document).ready(function(){
$('select.linksource').linkselects();
});