/*
 * fhDynamicSelect - jQuery Plugin
 * simple dynamic selector plugin
 *
 * Copyright (c) 2009 Gijs van Zon MA
 *
 * Version: 0.1.0 (25/12/2009)
 * Requires: jQuery v1.3+
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

(function($){

  $.fn.fhDynamicSelect = function(options){

    var defaults = $.extend({
      onComplete: function(){},
      id: 'id'
    }, options);

    return this.each(function(){

      var $select = $(this);

      if(!$select.attr('title') && (!$select.attr('route') && !$select.attr('form'))){
        throw('The select should have a title attribute which contains: route, form OR trigger. Or a route or a form attribute');
        return false;
      }

      var attributes = $select.attr('title').replace(/^\s+|\s+$/g,"").split(',');
      for(var i = 0; i < attributes.length; i++){
        var value = attributes[i].split(':');
        if(value[0] == 'route') $select.data('route', value[1]);
        if(value[0] == 'target') $select.data('target', value[1]);
        if(value[0] == 'id') $select.data('id', value[1]);
        if(value[0] == 'trigger') $select.data('trigger', value[1]);
        if(value[0] == 'form') $select.data('form', value[1]);
      }
      
      if($select.attr('route')) $select.data('route', $select.attr('route'));
      if($select.attr('target')) $select.data('target', $select.attr('target'));
      if($select.attr('variable')) $select.data('id', $select.attr('variable'));
      if($select.attr('trigger')) $select.data('trigger', $select.attr('trigger'));
      if($select.attr('targetForm')) $select.data('form', $select.attr('targetForm'));

      if(!$select.data('target')){
        if($select.data('form')){
          $select.data('form', $($select.data('form')));
        }else{
          $select.data('target', $select.parent());
        }
      }else{
        $select.data('target', $($select.data('target')));
        if($select.data('target').length < 1) {
          throw('The target attribute doesn\'t exist');
          return false;
        }
      }

      if($select.data('trigger')){
        $select.data('target').hide();
      }

      if(!$select.data('id')){
        $select.data('id', defaults.id);
      }

      $select.bind('change', function(){

        var $this = $(this);

        if($this.data('trigger')){
          if($this.val() == $this.data('trigger')){
            $this.data('target').show();
          }else{
            $this.data('target').hide();
          }
        }

        if($this.data('form')){
          $this.data('form').trigger('submit');
        }else{
          if($this.data('route')){
            
            var pos = $this.data('route').indexOf('?');
            
            $this.data('target').load($this.data('route') + (pos == -1 ? '?' : '&') + $this.data('id') + '=' + $this.val(), function(data){
              $(this).find('select.fhDynamic').fhDynamicSelect({
                onComplete: defaults.onComplete
              });

              defaults.onComplete($(this));
            });
          }
        }

      });

    });
  };

})(jQuery);
