URL_PREFIX = location.pathname.indexOf('/admin/') == 0 ? '/admin' : ''
function Chooser(element, model, root_id) {
    this.dom_node = $(element)
    if (!this.dom_node.length) return;
    this.model = model
    this.root_id = root_id
		// make chooser work for edit_inline in admin panel
		if ($('label', this.dom_node.parent()).length > 0) {
			// take name from label if exist, slice id_
			this.name = $('label', this.dom_node.parent()).attr('for').slice(3)
		} else {
			this.name = $('*:input', this.dom_node).attr('name')
		}
    this.current_node = null
    this.nodes = []
    var _this = this
    $('.selected-level', this.dom_node).each(function(e) { _this.nodes.unshift($(this))} )
    $('.selected-level a', this.dom_node).click(function(e) {
        _this.pop_levels.call(_this, ($(this).parents('.selected-level')));
        return false;
    })
    this.guess_current_node()
}

Chooser.prototype = {
    change_root: function(new_root_id) {
        this.root_id = new_root_id
        this.dom_node.html('')
        this.load_children(null, new_root_id)
    },
    show_next_level: function(value) {
        this.load_children(this.current_node, value)
    },
    lock: function() {
        $('select', this.dom_node).attr('disabled', 'disabled')
        $('a', this.dom_node).hide()
    },
    unlock: function() {
        $('select', this.dom_node).removeAttr('disabled')
        $('a', this.dom_node).show()
        this.dom_node.change();
    },
    guess_current_node: function() {
        this.current_node = $('select', this.dom_node)
        var _this = this
        this.current_node.change( function (e) { _this.show_next_level(this.value) })
    },
    load_path: function(id) {
        $.get(URL_PREFIX + '/xhr/chooser/' + this.model + '/' + id + '/parents/?root_id='+this.root_id, function(data) { 
             path = eval('('+data+')')
             while(path.length > 0) {   
                _this.show_next_level(path.shift())
             }
        })
        return
    },
    pop_levels: function(node) {
        if(this.current_node)
            this.current_node.remove() 
        $(node).nextAll('.separator').remove()
        while( node.get(0) != this.nodes[0].get(0) ) {
            var n = this.nodes.shift()
            $(n).remove()
        }
        $(this.nodes.shift()).remove()
        this.current_node = null
        if(this.nodes.length)
            this.show_next_level($('input', $(this.nodes[0])).get(0).value)
        else
            this.show_next_level(this.root_id)
    },
    load_children: function(node, value) {
        var _this = this;
        _this.lock()
        $.get(URL_PREFIX + '/xhr/chooser/' + _this.model + '/' + value + '/select/', 
            function(data) {
                if(node) {
                    select_node = node.get(0)
                    selected_name = select_node.options[select_node.selectedIndex].text
                    var parent_id = $('input').get(0).value;
                    var div = $('<div class="level' + _this.nodes.length + ' selected-level"/>')
                    link = $('<small>Need to <a href="?' + _this.name + '=' + parent_id + '#' + _this.dom_node.attr('id') +'">change</a></small>')
                    input = $('<input type="hidden" disabled="disabled" name="' + _this.name + '" value="' + value + '"/>')
                    label = $('<span class="chooser_selected">' + selected_name + '</span>')
                    div.append(label)
                    div.append(link)
                    div.append(input)
                    link.click(function(e) { _this.pop_levels.call(_this, ($(this).parents('.selected-level'))); return false; })
                    $(node).replaceWith(div)
                    _this.nodes.unshift( div )
                }
                $(_this.dom_node).append(data)
                _this.guess_current_node()
                if(_this.current_node.get(0).options.length == 1) {
                    _this.current_node.remove()
                    _this.current_node = null
                    input.attr('disabled', false)
                } else {
                    if(typeof div != 'undefined')
                        $('<div class="separator">&raquo;</div>').insertAfter(div)
                    _this.current_node.attr('name', _this.name)
                    _this.current_node.focus()
                }
                _this.unlock()
            }
        )
        
    }
}

