goog.provide('campsh.nav');
goog.provide('campsh.nav.NavWindow');

goog.require('goog.dom');
goog.require('goog.fx.Dragger');
goog.require('goog.style');
goog.require('goog.History');
goog.require('goog.events');

campsh.nav.NavWindow = function(navDiv, frameDiv) {
    this.element = goog.dom.getElement(navDiv);
    this.frameDiv = goog.dom.getElement(frameDiv);
    this.items = [];
    this.h = new goog.History();
    
    goog.events.listen(window, 'unload', function(e) {
        this.dispose();
    });
    
    return this;
};
		
campsh.nav.NavWindow.prototype.navToItem = function(item) {
    if (this.currentItem) {
        goog.dom.removeNode(this.currentItem.div);
        this.currentItem.a.style.textDecoration = 'none';
            this.frameDiv.parentNode.style.display = 'none';
    }
    if (item) {
        this.currentItem = item;
        this.currentItem.a.style.textDecoration = 'underline';
        this.frameDiv.appendChild(this.currentItem.div);
        parent.location.hash = item.id;
        this.frameDiv.parentNode.style.display = 'block';
    } 
    return this;
};

campsh.nav.NavWindow.prototype.navToItemWithId = function(id) {
    var item = goog.array.find(this.items, function(el, i, arr) {
        return (el.id === id);
    });
    this.navToItem(item);
    return this;
};

campsh.nav.NavWindow.prototype.addItem = function(title, body, id) {
    var item = new campsh.nav.NavItem(title, body, this, id);
	this.items.push(item);
	return this;
};

campsh.nav.NavWindow.prototype.setupHistory = function() {
    // Listen to 'back'/'forward' events.
    var self = this;
    goog.events.listen(this.h, goog.history.EventType.NAVIGATE, function(e) {
        var id = parent.location.hash.substr(1);
        self.navToItemWithId(id);
    });
    this.h.setEnabled(true);
};

campsh.nav.NavWindow.prototype.show = function() {
    var l = this.element.parentNode.offsetWidth + this.element.parentNode.offsetLeft + 36 + 'px';
    this.frameDiv.parentNode.style.left = l;
    this.setupHistory();
}

// NAV ITEM
//==============

campsh.nav.NavItem = function(title, body, parent, id) {
    this.title = title;
    this.body = body;
    this.parent = parent;
    this.id = id;
    this.a = this.makeItemDom();
    this.div = this.makeDisplayDom();
    goog.dom.appendChild(this.parent.element, this.a);
	goog.dom.appendChild(this.parent.element, goog.dom.createDom('br'));
	goog.events.listen(this.a, goog.events.EventType.CLICK, this.navHere, true, this);
};

campsh.nav.NavItem.prototype.makeItemDom = function() {
	var newItem = goog.dom.createDom('a', {'class': 'navItem'}),
	htmlFragment = goog.dom.htmlToDocumentFragment(this.title);
	goog.dom.appendChild(newItem, htmlFragment);
	return newItem;
};

campsh.nav.NavItem.prototype.makeDisplayDom = function() {
    var div = goog.dom.createElement('div');
    goog.dom.appendChild(div, (goog.dom.htmlToDocumentFragment(this.body)));
    return div;
};

campsh.nav.NavItem.prototype.navHere = function(e) {
    this.parent.navToItem(this);
};
