YAHOO.namespace('tunelocker');

YAHOO.tunelocker.LineDisplay = function(element_id, title_id) {
    var element_id = typeof(element_id) != 'undefined' ? element_id : 'lineDisplayWidget';
    var title_id = typeof(title_id) != 'undefined' ? title_id : 'lineDisplayHeader';
    this.element_id = element_id;
    this.title_id = title_id;
    this.element = document.getElementById(this.element_id);
    this.title_element = document.getElementById(this.title_id);
    this.cur_display_list = null;
    this.up_kl = new YAHOO.util.KeyListener(document, {keys: 38},
        {fn: this.navUp, 
         scope: this,
         correctScope: true});
    this.down_kl = new YAHOO.util.KeyListener(document, {keys: 40}, 
        {fn: this.navDown, 
         scope: this,
         correctScope: true});
    this.left_kl = new YAHOO.util.KeyListener(document, {keys: 37}, 
        {fn: this.navLeft, 
         scope: this,
         correctScope: true});
    this.right_kl = new YAHOO.util.KeyListener(document, {keys: 39}, 
        {fn: this.navRight, 
         scope: this,
         correctScope: true});
    this.enter_kl = new YAHOO.util.KeyListener(document, {keys: 13}, 
        {fn: this.navPlay, 
         scope: this,
         correctScope: true});
    this.space_kl = new YAHOO.util.KeyListener(document, {keys: 32}, 
        {fn: this.navAppend, 
         scope: this,
         correctScope: true});
    this.enableKeyListeners();
}

YAHOO.tunelocker.LineDisplay.prototype.enableKeyListeners = function() {
    this.up_kl.enable();
    this.down_kl.enable();
    this.left_kl.enable();
    this.right_kl.enable();
    this.enter_kl.enable();
    this.space_kl.enable();
}

YAHOO.tunelocker.LineDisplay.prototype.display = function(display_list) {
    if (display_list.prev_display_list == null) {
        display_list.prev_display_list = this.cur_display_list;
    }
    while (this.element.childNodes.length >= 1) {
        this.element.removeChild(this.element.firstChild);
    }
    this.cur_display_list = display_list;
    this.cur_display_list.display = this;
    this.element.appendChild(this.cur_display_list.element);
    this.cur_display_list.displayItem();
    return this.cur_display_list;
}

YAHOO.tunelocker.LineDisplay.prototype.updateTitle = function() {
    if (this.cur_display_list != null) {
        while (this.title_element.childNodes.length >= 1) {
            this.title_element.removeChild(this.title_element.firstChild);
        }
        var text_node = this.cur_display_list.getTitle();
        this.title_element.appendChild(text_node);
        //text_node.scrollIntoView();
    }
}

YAHOO.tunelocker.LineDisplay.prototype.navUp = function() {
    if (this.cur_display_list != null) {
        var result = this.cur_display_list.displayPrev();
        return result;
    } else {
        return null;
    }
}

YAHOO.tunelocker.LineDisplay.prototype.navDown = function() {
    if (this.cur_display_list != null) {
        var result = this.cur_display_list.displayNext();
        this.updateTitle();
        return result;
    } else {
        return null;
    }
}

YAHOO.tunelocker.LineDisplay.prototype.navLeft = function() {
    if (    this.cur_display_list != null && 
            this.cur_display_list.prev_display_list != null ) {
        this.display(this.cur_display_list.prev_display_list);
    }
}

YAHOO.tunelocker.LineDisplay.prototype.navRight = function() {
    if (this.cur_display_list != null) {
        var next_list = this.cur_display_list.activateCurrent(this);
        if (next_list != null) {
            this.display(next_list);
        }
    }
}

YAHOO.tunelocker.LineDisplay.prototype.navPlay = function() {
    if (this.cur_display_list != null) {
        return this.cur_display_list.playCurrent(this);
    }
}

YAHOO.tunelocker.LineDisplay.prototype.navAppend = function() {
    if (this.cur_display_list != null) {
        return this.cur_display_list.appendCurrent(this);
    }
}

YAHOO.tunelocker.LineDisplayList = function(title) {
    this.element = document.createElement('ul');
    YAHOO.util.Dom.addClass(this.element, 'lineDisplayList');
    this.display = null;
    this.title = title;
    this.item_list = [];
    this.prev_display_list = null;
    this.cur_idx = 0;
}

YAHOO.tunelocker.LineDisplayList.prototype.getTitle = function() {
    var title_span = document.createElement('span');
    YAHOO.util.Dom.addClass(title_span, 'header');
    var title_text = this.title;
    title_text += " (" + (this.cur_idx + 1) + " of " + this.item_list.length + ")";
    title_span.appendChild(document.createTextNode(title_text));
    return title_span;
}

YAHOO.tunelocker.LineDisplayList.prototype.currentItem = function() {
    if (this.cur_idx >= 0 && this.cur_idx < this.item_list.length) {
        return this.item_list[this.cur_idx];
    } else {
        return null;
    }
}

YAHOO.tunelocker.LineDisplayList.prototype.displayItem = function(idx) {
    idx = typeof(idx) != 'undefined' ? idx : this.cur_idx;
    this.cur_idx = idx;
    var cur_item = this.currentItem();
    if (cur_item != null && this.display != null) {
        var scroll = new YAHOO.util.Scroll(this.display.element, {
                scroll: {
                    to: [0, this.cur_idx * 25]
                }
        }, 0.25);
        scroll.animate();
        //cur_item.element.scrollIntoView();
    }
    if (this.display != null) {
        this.display.updateTitle();
    }
    return cur_item;
}

YAHOO.tunelocker.LineDisplayList.prototype.activateCurrent = function(display) {
    var cur_item = this.currentItem();
    if (cur_item == null) {
        return null;
    } else {
        return cur_item.activate(display)
    }
}

YAHOO.tunelocker.LineDisplayList.prototype.playCurrent = function(display) {
    var cur_item = this.currentItem();
    if (cur_item == null) {
        return null;
    } else {
        return cur_item.play(display)
    }
}

YAHOO.tunelocker.LineDisplayList.prototype.appendCurrent = function(display) {
    var cur_item = this.currentItem();
    if (cur_item == null) {
        return null;
    } else {
        return cur_item.append(display)
    }
}

YAHOO.tunelocker.LineDisplayList.prototype.displayNext = function() {
    if (this.cur_idx >= this.item_list.length - 1) {
        this.cur_idx = 0;
    } else {
        this.cur_idx += 1;
    }
    return this.displayItem();
}

YAHOO.tunelocker.LineDisplayList.prototype.displayPrev = function() {
    if (this.cur_idx <= 0) {
        this.cur_idx = this.item_list.length - 1;
    } else {
        this.cur_idx -= 1;
    }
    return this.displayItem()
}

YAHOO.tunelocker.LineDisplayList.prototype.insertItem = function(item, index) {
    index = typeof(index) != 'undefined' ? index : this.item_list.length;
    if (index == this.item_list.length) {
        this.item_list[index] = item;
    } else {
        this.item_list.splice(index, 0, item);
    }

    var new_node = item.buildElement();
    this.element.insertBefore(new_node, this.element.childNodes[index] || null);
    return item;
}

YAHOO.tunelocker.LineDisplayList.prototype.removeItem = function(index) {
    this.item_list.splice(index, 1);
    this.element.removeChild(this.element.childNodes[index]);
    this.displayItem();
}

YAHOO.tunelocker.LineDisplayList.prototype.addItem = function(item) {
    this.insertItem(item);
}

YAHOO.tunelocker.LineDisplayItem = function(title, sub_list) {
    sub_list = typeof(sub_list) != 'undefined' ? sub_list : null;
    this.title = title;
    this.element = null;
    this.sub_list = sub_list;
}

YAHOO.tunelocker.LineDisplayItem.prototype.buildElement = function() {
    this.element = document.createElement('li');
    var title_node = document.createTextNode(this.title);
    this.element.appendChild(title_node);
    return this.element;
}

YAHOO.tunelocker.LineDisplayItem.prototype.activate = function(display) {
    return this.sub_list;
}

YAHOO.tunelocker.LineDisplayItem.prototype.play = function(display) {
    display.navRight();
}

YAHOO.tunelocker.LineDisplayItem.prototype.append = function(display) {
}


