/**
 * @author Monty
 */

if (typeof SUU == "undefined") {
    var SUU = {};
}
SUU.search_loaded = true;
Ext.UpdateManager.defaults.loadScripts = true;

if (!SUU.search)
{
    // Add in the ability for Ext to use namespaces.

    //Ext.DomQuery.matchers.push( {
    //	re: /^(?:([\[\{])(?:@)?([\w-]+(?:\:[\w-]+))\s?(?:(=|.=)\s?['"]?(.*?)["']?)?[\]\}])/,
    //	select: 'n = byAttribute(n, "{2}", "{4}", "{3}", "{1}");'
    //});

    SUU.search = function() {

        return {
            selectDates : function(items) {
                if (items.limitCheckBox.getValue()) {
                    items.startDateField.enable();
                    items.endDateField.enable();
                } else {
                    items.startDateField.disable();
                    items.endDateField.disable();
                }
            },

            initSearchForm : function() {
                Ext.onReady(this.buildForm2x);
            },

            popup : function(url) {
                Ext.onReady(this.buildWindow(url, 700, 600, 'View Image'));
            },

            sched : function(url) {
                Ext.onReady(this.buildWindow(url, 800, 500, 'View Schedule'));
            },

            buildWindow : function(url, width, height, label) {
                Ext.QuickTips.init();
                Ext.DomHelper.insertFirst(document.body,
                        "<div id=\"img-win\" class=\"x-hidden\">" +
                        "<div class=\"x-window-header\">" + label + "</div>" +
                        "</div>");
                var win = new Ext.Window({
                    el:'img-win',
                    layout:'fit',
                    width:width,
                    height:height,
                    autoScroll: true,
                    waitMsg: 'Loading...',
                    autoLoad:{url:url, method:'GET', scripts:true},
                    closeAction:'close',
                    modal: true,
                    plain: false,
                    buttons: [{
                        text: 'Close',
                        handler: function(){
                            win.close();
                        }
                    }]
                });
                win.show();
            },

            buildForm2x : function() {
                Ext.QuickTips.init();
                Ext.form.Field.prototype.msgTarget = 'side';
                var simple = new Ext.form.FormPanel({
                    labelWidth: 85,
                    method: 'POST',
                    url: 'search',
                    baseParams: { start:0, limit:25 },
                    frame: true,
                    hideBorders: false,
                    title: 'Please enter your search term(s)',
                    bodyStyle: 'margin-left:5%;margin-right:5%',
                    onSubmit: Ext.emptyFn,
                    submit: function() {
                        var formDom = simple.form.getEl().dom;
                        formDom.action = this.url;
                        formDom.method = this.method;
                        formDom.submit();
                    },
                    buttons: [{
                        text: 'Search Now',
                        handler: function(b, e) {
                            simple.getForm().submit();
                        }
                    }]
                });
                var limitCheckBox = new Ext.form.Checkbox({
                    boxLabel:'Limit by Date',
                    labelSeparator:'',
                    name: 'dateLimit',
                    width: 'auto' });
                var startDateField = new Ext.form.DateField({
                    fieldLabel: 'Start',
                    name: 'limitStart',
                    width: 190,
                    allowBlank: false,
                    value: '01/01/99' });
                var endDateField = new Ext.form.DateField({
                    fieldLabel: 'End',
                    name: 'limitEnd',
                    width: 190,
                    allowBlank: false,
                    value: '12/31/03' });
                var boxParams = { limitCheckBox:limitCheckBox, startDateField:startDateField, endDateField:endDateField };
                limitCheckBox.on('check', function() {
                    SUU.search.selectDates(boxParams);
                });
                startDateField.disable();
                endDateField.disable();

                simple.add(
                        new Ext.form.TextField({
                            fieldLabel: 'Search Term',
                            name: 'q',
                            width: 300,
                            allowBlank: true
                        }),
                        new Ext.form.TextField({
                            labelSeparator:'',
                            name: 'c',
                            type: 'hidden',
                            hidden: true,
                            value: 'L'
                        }));
                simple.add(limitCheckBox);
                simple.add(startDateField);
                simple.add(endDateField);
                simple.add(new Ext.form.Checkbox({
                    boxLabel:'Include Schedule',
                    labelSeparator:'',
                    name: 'schedule',
                    checked: true,
                    width: 'auto' }));
                simple.add(new Ext.form.Checkbox({
                    boxLabel:'Include Executive Orders',
                    labelSeparator:'',
                    name: 'orders',
                    checked: true,
                    width: 'auto' }));
                simple.add(new Ext.form.Checkbox({
                    boxLabel:'Include News',
                    labelSeparator:'',
                    name: 'news',
                    checked: true,
                    width: 'auto' }));
                simple.add(new Ext.form.Checkbox({
                    boxLabel:'Include Policies',
                    labelSeparator:'',
                    name: 'policies',
                    checked: true,
                    width: 'auto' }));
                simple.add(new Ext.form.Checkbox({
                    boxLabel:'Include Speeches',
                    labelSeparator:'',
                    name: 'speeches',
                    checked: true,
                    width: 'auto' }));
                simple.add(new Ext.form.Checkbox({
                    boxLabel:'Include Legislation',
                    labelSeparator:'',
                    name: 'legislation',
                    checked: true,
                    width: 'auto' }));
                simple.add(new Ext.form.Checkbox({
                    boxLabel:'Include Photographs',
                    labelSeparator:'',
                    name: 'photos',
                    checked: true,
                    width: 'auto' }));
                simple.add(new Ext.form.Checkbox({
                    boxLabel:'Include Audio/Video',
                    labelSeparator:'',
                    name: 'av',
                    checked: true,
                    width: 'auto' }));
                simple.add(new Ext.form.Checkbox({
                    boxLabel:'Include Correspondence',
                    labelSeparator:'',
                    name: 'artifacts',
                    checked: true,
                    width: 'auto' }));
                simple.render('searchformdiv');
            }
        };
    }();
}