// Jitsu Documentation JavaScript

// a very simple string builder
var m_result = [];
function clear() { m_result = []; }
function pr(str) { m_result.push(str); }
function getout() { return m_result.join(''); }
function flush(elem) { 
    if (elem) elem.innerHTML = getout();
    else document.write(getout()); 
    m_result = [];
}

// Docs Class - methods for generating rounded corners and nav bars
//
function Docs() { 

    var m_corners = [];

    // public
    this.box = function(colorClass, type, outerClass) {
        m_corners.push(colorClass);
        m_corners.push(type);
        clear();
        var lines;
        if (type.indexOf('x-large') != -1) lines = [11,8,7,5,4,3,3,2,1,1,1];
        else if (type.indexOf('large') != -1) lines = [9,6,5,4,3,2,1,1,1];
        else lines = [5,3,2,1,1];
        drawLines(colorClass, type, lines, true);
        flush();
    }

    // public
    this.endBox = function() {
        var type = m_corners.pop();
        var colorClass = m_corners.pop();
        clear();
        var lines;        
        if (type.indexOf('x-large') != -1) lines = [1,1,1,2,3,3,4,5,7,8,11];
        else if (type.indexOf('large') != -1) lines = [1,1,1,2,3,4,5,6,9];
        else lines = [1,1,2,3,5];        
        drawLines(colorClass, type, lines, false);
        flush();
    }

    function drawLines(colorClass, type, widths, top) {
        var corners = '';
        if (type.indexOf(' ') != -1) {
            corners = type.substring(type.indexOf(' ') + 1);
        }
        if (corners.length == 0) {    
            for (var i = 0, j; j = widths[i]; i++) {
                pr('<div class="line '); 
                pr(colorClass); 
                pr('" style="margin: 0 '); 
                pr(j); 
                pr('px"></div>');
            }
        } else {
            var left = (top ? 'tl' : 'bl');
            var right = (top ? 'tr' : 'br');
            left = corners.indexOf(left) != -1;
            right = corners.indexOf(right) != -1;
            for (var i = 0, j; j = widths[i]; i++) {
                // top right bottom left
                pr('<div class="line '); 
                pr(colorClass); 
                pr('" style="margin: 0 '); 
                pr(right ? j : '0'); 
                pr('px 0 ');
                pr(left ? j : '0'); 
                pr('px; background-position:');
                pr(-j); pr('px '); pr(-i); pr('px;');
                pr('"></div>');
            }
        }
    }

    function menu(name, title, docName, pathToRoot, current) {
        pr('<li>');
        pr('<a href="'); pr(pathToRoot); pr(docName); pr('" '); 
        if (current==name) pr('class="navbar_current"');
        pr('>'); pr(title); pr('</a></li>');
    }
    function bmenu(name, title, docName, pathToRoot, current) {
        pr('<div class="');
        if (current==name) pr('bigmenu_current');
        pr(' bigmenu">');
        pr('<a href="'); pr(pathToRoot); pr(docName); pr('" '); 
        if (current==name) pr('class="navbar_current"');
        pr('>'); pr(title); pr('</a></div>');
    }
    
    this.mainNav = function(pathToRoot, current) {
        clear();
        pr('<div id="mainnav"><h1><a href="/jitsu/index.html">Jitsu</a></h1><ul id="navbar">');
        menu("home", "Home", "index.html", pathToRoot, current);
        menu("devindex", "Developers", "devindex.html", pathToRoot, current);
        menu("licensing", "Licensing", "licensing.html", pathToRoot, current);
        menu("contribute", "Contribute", "contribute.html", pathToRoot, current);
        menu("feedback", "Forums", "feedback.html", pathToRoot, current);
        menu("faq", "FAQ", "faq.html", pathToRoot, current);
        menu("download", "Download", "download.html", pathToRoot, current);
        pr('</ul>');
        pr('<div></div>');
        pr('</div>');
        flush();
    }

    this.devNav = function(current) {
        clear();
        pr('<div id="secondarynav">');
        flush();
        this.box('lightblue','small');
        clear();
        pr('<div class="padded lightblue">');
        this.devTabs("", "devindex", true, true);        
        this.devTabs("", "devindex", false, true);
        pr('</div>');        
        flush();
        this.endBox();
        clear();
        pr('</div>');
        flush();
    }

    this.guideNav = function(current) {
        clear();
        pr('<div id="secondarynav">');
        flush();
        this.box('lightblue','small');
        clear();
        pr('<div class="padded lightblue">');
        this.devTabs("../", "guide", true, current == "contents");
        pr('<ul>');
        // menu("contents", "Contents", "index.html", '', current);
        menu("overview", "1. Overview", "overview.html", '', current);
        menu("approach", "2. The Jitsu Approach", "approach.html", '', current);
        menu("projects", "3. Projects", "projects.html", '', current);
        menu("data", "4. Data Language", "data.html", '', current);
        menu("active_data", "5. Active Datasets", "activedata.html", '', current);
        menu("expressions", "6. Binding Expressions", "expressions.html", '', current);
        menu("extending", "7. Extending Controls", "extending.html", '', current);
        menu("directives", "8. JavaScript", "javascript.html", '', current);
        menu("optimizing", "9. Optimizing", "optimizing.html", '', current);
        menu("dragdrop", "10. Drag and Drop", "dragdrop.html", '', current);
        menu("optimizing", "11. The Back Button", "backbutton.html", '', current);
        menu("glossary", "Glossary", "glossary.html", '', current);
        pr('</ul>');
        this.devTabs("../", "guide", false, current == "contents");
        pr('</div>');
        
        flush();
        this.endBox();
        clear();
        pr('</div>');
        flush();
    }

    var devMenus = [
        ["devindex", "Developer Home", "devindex.html"],
        ["guide", "Programmers Guide", "guide/index.html"],
        ["quicktours", "Quick Tours", "quicktours/basic_helloworld.html"],
        ["reference", "API Reference", "reference/index.html"]
    ];

    this.devTabs = function(pathToRoot, current, isStart, showCurrentSelected) {        
        var seenCurrent = false;
        for (var i = 0; i < devMenus.length; i++) {
            var name = devMenus[i][0], title = devMenus[i][1], docName = devMenus[i][2];
            if (isStart) {
                showCurrentSelected = true;
                if (current == name) {
                    bmenu(name, title, docName, pathToRoot, 
                        (showCurrentSelected ? current : false));
                    break;
                } else {
                    bmenu(name, title, docName, pathToRoot, current);
                }
            } else {
                if (seenCurrent) bmenu(name, title, docName, pathToRoot, current);
                else if (current == name) seenCurrent = true;
            }
        }
    }

    this.technote = function(type) {
        document.write('<div class="technote">');
        if (type == 'early_access') color = 'lightgreen';
        else color = 'lightblue';
        this.box(color,'small');
        document.write('<div class="technote_' + type + '"><div class="' + color + ' padded">');
    }
    this.endTechnote = function() {
        document.write('</div></div>');
        this.endBox();
        document.write('</div>');
    }

    this.footer = function() {
        clear();
        pr('<div id="footer"><p><a href="http://attap.com/" title="Visit Attap.com" class="footerlogo">');
        pr('<img src="/jitsu/images/footer_logo.png" alt="attap"></a> &copy; 2006 <a href="http://attap.com/">ATTAP Technologies Inc&trade;</a>. All Rights Reserved. ');
        pr('<br /><a href="http://www.attap.com/about/">About-Press</a> / More ');
        pr('<a href="http://attap.com/" title="Visit Attap.com">attap</a> ideas: ');
        pr('<a href="http://www.riffs.com" title="Visit Riffs.com">Riffs</a>, ');
        pr('<a href="http://www.personaldna.com/" title="Visit Personal DNA">PersonalDNA</a>, ');
        pr('<a href="http://www.buzzvote.com" title="Visit Buzzvote">Buzzvote</a> and ');
        pr('<a href="http://lifeio.com/" title="LifeIO - Very Cool, Very Soon">LifeIO</a></p>');
        pr('</div>');
        pr('<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">');
        pr('</script>');
        pr('<script type="text/javascript">');
        pr('_uacct = "UA-211654-4";');
        pr('urchinTracker();');
        pr('</script>');
        flush();
    }
}

var docs = new Docs();

