/*
 * Copyright (c) 2009 David Johansson
 * Dual licensed under the MIT and GPL licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
(function($) {

/*
 * Creates a "select chain". On change of the first select the second is updated,
 * on the change the second the third is updated etc.
 * 
 */ 
$.fn.selectchain = function(options) {
        options = $.extend($.fn.selectchain.defaults,options);
        
        var selects = this;
        if (!this.is('select')) { //if this is not a list of selects find any under it (like if this is a form etc)
          selects = this.find('select');  
        } 
        
        if (selects.lenght < 2) { //bail out if less than two selects
            return this;
        }
        
        if (options.change) {
            selects.eq(selects.length-1).change(options.change);
        }
        
        selects.slice(0,selects.length-1).change(function(e){
            var url = $(this).val();
            
            if (url == 'NOCHOICE') {
                selects.slice(1).attr('disabled','true');
                return;
            }
            
            var next;
            for (var i=0; i<selects.length; i++) {
                if (selects.get(i) == this) {
                    next = selects.eq(i+1);
                    break;
                }
            }
            if (next) {
                if (options.load_start) { options.load_start.apply(this);  }
                $.get(url,callback=function(data){
                    if (options.data_func) {
                        data = options.data_func(data);
                    }
                    next.empty().append(data).removeAttr('disabled');
                    if (options.load_end) { options.load_end.apply(this);  }
                });
            }
        });
        selects.eq(0).change();
        
        return this;
};

$.fn.selectchain.defaults = { //all arguments are optional
        change: null,    //change event function for last select
        data_func: null,  //function applied to ajax loaded data, should return HTML option tags.
        load_start: null, //start stop functions on ajax loading, suitable for "loading" options
        load_end: null
};


// A data_func, takes json formatted data and makes <option>..</option> HTML out of it
// Use it as argument to 'data_func' option.
// json example, name is label of the option, value is the value attribute i.e. the url to load data from 
// for the next select in the chain (or a "real" value if this is the last select in the chain)
/*
    { "options": [  
                    { "name": 'Yes' , "value": 'yesoptions.json' },
                    { "name": 'Yes' , "value": 'nooptions.json' }
                    ]
    } 
*/
$.fn.selectchain.json_loader = function (data) {
    var options = '';
    data = eval('('+data+')');
    $.each(data,function(i){
        options += '<option class="'+(i%2==0?'even':'odd')+'" value="'+this.value+'">'+this.name+'</option>\n';
    });
    return options;
};


})(jQuery)
