/*
 * jQuery selectRelationAjax plugin
 * Version: 0.1.0
 *
 * Copyright (c) 2008 westsite nv
 * author : Alexander Smalle
 *
 * only for internal use
 *
 *
 * Changelog: 
 * v 0.1.01 - 20009-06-18 - don't empty child ....
 * v 0.1.0 - 2008-09-17
 *
 * Todo:
 * - bugs ?
 */
 

(function($) {
  jQuery.fn.selectRelationAjax = function(settings) {
    settings = jQuery.extend({
    version: "0.1.0"
    }, settings);
    var selectRelationContainterList=this;
    return selectRelationContainterList.each(function(){
      var selectRelationContainterDomElem = this;
      jQuery('.parent').each(function() {
        var parent=jQuery(this).attr('id');
        var parentAjaxUrl = jQuery(this).attr('selectRelationAjaxOnchangeUrl');
        var parentValue = jQuery('#'+parent).attr('value');
        var childDomElem = jQuery('.child_' + parent, selectRelationContainterDomElem);
        var child=childDomElem.attr('id');
        //var childSelectedValue=jQuery('#'+child).attr('value');
        //childSelectedValue = (typeof childSelectedValue == "undefined")? "" : childSelectedValue;
        //jQuery('#'+child).html(''); // do not empty
        //var childValue = jQuery( '#'+child).attr('value');
        jQuery('#'+parent).change(
          function() {
            var parentValue = jQuery(this).attr('value');
            jQuery('#'+child).html(''); //empty
            var data = {};
            data.id=parentValue;
            jQuery.get( parentAjaxUrl, data,
              function(data){ jQuery('#'+child).append(data); //alert("Data Loaded: " + data); 
              });
          jQuery('#'+child).trigger("change");
          jQuery('#'+child).focus();
        });
      });
    });
  };
})(jQuery);

