function Quatrains()
{
}

Quatrains.TEXT_NODE = 3;
Quatrains.ELEMENT_NODE = 1;

Quatrains.prototype = {
    _q_doc: null,
    _q_index: [],
	_q_current_idx: 0,
    
    _quatrainTextId: "quatrainText",
    _searchButtonId: "search",
    _searchResultsId: "searchresults",
    _previousButtonId: "prevBtn",
    _nextButtonId: "nextBtn",
        
    loadQuatrains: function()
    {
        dojo.xhrGet({
            url: "q_en_all.xml",
            load: function(response, ioArgs)
            {
                this.quatrains._onload(response, ioArgs);
            },
            error: function(err)
            {
                this.quatrains._onerror(err);
            },
            handleAs: "xml",
            quatrains: this
        });
    },
    
    onQuatrainClick: function(e)
    {
        if (!e) var e = window.event;
        var id = e.target.getAttribute("id");
        if (id != null && id.substr(0, 4) == "qidx")
            this.loadQuatrain(parseInt(id.substring(4)));
    },
    
    onNextBtnClick: function()
    {
        this._loadNextQuatrain();
    },
    
    onPreviousBtnClick: function()
    {
        this._loadPreviousQuatrain();
    },
    
    onRandomBtnClick: function()
    {
        this._loadRandomQuatrain();
    },
    
    _onload: function(response, ioArgs)
    {
        this._q_doc = response;
        this._fillSections();
    },

    _onerror: function(err)
    {
        var pane = dijit.byId(this._quatrainTextId);
        pane.setContent("<h3>There was an error reading the quatrain data: " + err + "</h3>");
    },
    
    _fillSections: function()
    {
        var centuries = this._q_doc.getElementsByTagName("century");
        for (var i = 0; i < centuries.length; i++)
            this._fillSection(centuries[i]);
    },
    
    _fillSection: function(century)
    {
        if (century == null)
            return;
        var c_id = century.getAttribute("cid");
        var pane = dijit.byId(c_id);
        
        var html = "<p id='" + c_id + "_content'>";
        for (var q = century.firstChild; q != null; q = q.nextSibling)
        {
            if (q.nodeType != Quatrains.ELEMENT_NODE)
                continue;
            var q_id = q.getAttribute("qid");
            var q_title = q.getAttribute("title");
            this._q_index.push({c_id: c_id, q_id: q_id});
            html += "<a class='qlink' id='qidx" + (this._q_index.length - 1) + "'>" + q_title + "</a> ";
        }
        html += "</p>";
        pane.setContent(html);
    },
    
    _loadRandomQuatrain: function()
    {
        if (this._q_index.length <= 0)
            return;

        var i = Math.floor(Math.random() * this._q_index.length);
        this.loadQuatrain(i);
    },

    _loadPreviousQuatrain: function()
    {
        if (this._q_current_idx > 0)
            this.loadQuatrain(this._q_current_idx - 1);
    },
    
    _loadNextQuatrain: function()
    {
        if (this._q_current_idx < this._q_index.length - 1)
            this.loadQuatrain(this._q_current_idx + 1);
    },
    
    loadQuatrain: function(idx)
    {
        var c_id = this._q_index[idx].c_id;
        var q_id = this._q_index[idx].q_id;
        
        this._q_current_idx = idx;
        
        var century = null;
        var centuries = this._q_doc.getElementsByTagName("century");
        for (var i = 0; i < centuries.length; i++)
        {
            if (centuries[i].getAttribute("cid") != c_id)
                continue;
            century = centuries[i];
            break;
        }    
        if (century == null)
            return;

        var quatrain = null;
        for (var q = century.firstChild; q != null; q = q.nextSibling)
        {
            if (q.nodeType != Quatrains.ELEMENT_NODE || q_id != q.getAttribute("qid"))
                continue;
            quatrain = q;
            break;
        }
        if (quatrain == null)
            return;
            
        var pane = dijit.byId(this._quatrainTextId);
        var html = "<h3>" + century.getAttribute("title") + " Quatrain " + quatrain.getAttribute("title") + "</h3>";
        html += "<p class='quatrain'>";
        for (var childNode = quatrain.firstChild; childNode != null; childNode = childNode.nextSibling)
        {
            if (childNode.nodeType == Quatrains.TEXT_NODE)
                html += childNode.data;
            else if (childNode.nodeType == Quatrains.ELEMENT_NODE && childNode.nodeName == "br")
                html += "<br/>";
        }
        html += "</p>";
        pane.setContent(html);
        
        this._updateDisplay();
    },
    
    _updateDisplay: function()
    {
        this._enableButton(this._previousButtonId, (this._q_current_idx > 0));
        this._enableButton(this._nextButtonId, (this._q_current_idx < this._q_index.length - 1));
    },
    
    _enableButton: function(id, enabled)
    {
        var btn = dijit.byId(id);
        btn.setDisabled(!enabled);
    },
    
    showQuickSearch: function(value)
    {
        var text = dijit.byId(this._searchButtonId);
        var results = dijit.byId(this._searchResultsId);
        results.setContent("<p>" + text.getValue() + "</p>");
    }
}
