// start up quicktips
Ext.QuickTips.init();

Ext.onReady(function() {

    Ext.ns('Ext.ux');
    (function(){
        var lasts = ['Jones', 'space Smith', 'Lee Space', 'Wilson', 'Black', 'Williams', 'Lewis', 'Johnson', 'Foot', 'Little', 'Vee', 'Train', 'Hot', 'Mutt'];
        var firsts = ['Fred', 'Julie', 'Bill', 'Ted', 'Jack', 'John', 'Mark', 'Mike', 'Chris', 'Bob', 'Travis', 'Kelly', 'Sara'];
        var lastLen = lasts.length, firstLen = firsts.length;

        Ext.ux.getRandomInt = function(min, max){
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }

        Ext.ux.generateName = function(){
            var name = firsts[Ext.ux.getRandomInt(0, firstLen-1)] + ' ' + lasts[Ext.ux.getRandomInt(0, lastLen-1)];
            if(Ext.ux.generateName.usedNames[name]){
                return Ext.ux.generateName();
            }
            Ext.ux.generateName.usedNames[name] = true;
            return name;
        }
        Ext.ux.generateName.usedNames = {};

    })();

    Ext.ux.genData = function() {
        var data = [];
        var s = new Date(2007, 0, 1);
        var now = new Date(), i = -1;
        while(s.getTime() < now.getTime()){
            var ecount = Ext.ux.getRandomInt(0, 3);
            for(var i = 0; i < ecount; i++){
                var name = Ext.ux.generateName();
                data.push({
                    start : s.clearTime(true).add(Date.DAY, Ext.ux.getRandomInt(0, 27)),
                    name : name,
                    email: name.toLowerCase().replace(' ', '.') + '@exttest.com',
                    active: true,
                    salary: Math.floor(Ext.ux.getRandomInt(35000, 85000)/1000)*1000
                });
            }
            s = s.add(Date.MONTH, 1);
        }
        return data;
    }

    // execute examples
    Ext.each(Ext.query('.exec'), function() {
        eval(this.innerHTML);
    });

    // make code look pretty
    prettyPrint();

    // fetch external code
    Ext.each(Ext.query('.fetch-code'), function() {
        this.innerHTML = '<div class="loading">Loading...</div>';
        Ext.Ajax.request({
            url: this.getAttribute('rel'),
            disableCaching: false,
            scope: this,
            failure: function(response) {
                this.innerHTML = [
                    'Failed Request',
                    ' "', this.getAttribute('rel'), '" ',
                    response.statusText,
                    ' (', response.status, ')'
                ].join('');
            },
            success: function(response) {
                // find langExtension
                var langExtension = this.className.match(/\blang-(\w+)\b/);

                // emmbed code with link and it all prettied up
                this.innerHTML = [
                    '<div class="fig">',
                        'Download file: ',
                        '<a href="', this.getAttribute('rel'), '" target="_blank">', this.getAttribute('rel'), '</a>',
                    '</div>',
                    '<pre class="prettyprint">',
                        prettyPrintOne(response.responseText, langExtension),
                    '</div>'
                ].join('');
            }
        });
    });
});
