ExplorerApp = {};

Ext.onReady(function() {
   Ext.QuickTips.init();
   
   Ext.state.Manager.setProvider(new Ext.state.SessionProvider({state: Ext.appState}));

   var tpl = new Ext.Template(
        '<h2 class="title">{term}</h2>',
        '<p><b>Occurence</b>: {occur}',
        '<p><b>New</b>: {new}</p>',
        '<p><b>Type</b>: {type}</p>'
   );
   tpl.compile();

    ExplorerApp.getTemplate = function(){
        return tpl;
    }
   
    var el = Ext.get('tree');
    var version = el.dom.title; // "2.0.0";	// Default
    ExplorerApp.getVersion = function(){
        return version;
    }
   
    var tree = new ExplorerTree();
    ExplorerApp.getTree = function(){
        return tree;
    }

    var view = new ExplorerView();
    ExplorerApp.getView = function(){
        return view;
    }

    var viewport = new Ext.Viewport({
        layout:'border',
        items:[
            new Ext.BoxComponent({ // raw element
                region:'north',
                el: 'tree',
                height:75
            }),
            tree,
            view
         ]
    });    
    
});

// This is a custom event handler passed to preview panels so 
// links open in a new window
ExplorerApp.LinkInterceptor = {
    render: function(p){
        p.body.on({
            'mousedown': function(e, t){ // try to intercept the easy way
                var parts = t.href.split('?', 2);
                if(parts.length == 2) {
                   var args = Ext.urlDecode(parts[1]);
                   ExplorerApp.getView().openTab(args.term.replace(/\+/, ' '));
                }
            },
            'click': function(e, t){ // if they tab + enter a link, need to do it old fashioned way
                if(String(t.target).toLowerCase() != '_blank'){
                    e.stopEvent();
                    var args = Ext.urlDecode(t.href);
                    ExplorerApp.getView().openTab(args.term);
                }
            },
            delegate: 'a'
        });
    }
};
