﻿namespace('nnet.sd');

nnet.sd.ContentToolBar = $j.klass({
    initialize: function() {
        try {
            this.ShowMapButtonOnInit = false;
            Ext.QuickTips.init();
            
            // following is not working for normal tooltips.
            Ext.apply(Ext.QuickTips.getQuickTip(), {
                maxWidth: 200,
                minWidth: 100,
                showDelay: 50,
                dismissDelay: 99999,
                shadow : true,
                trackMouse: true
            });

            // Must use this.id as this does not work.
            Ext.form.Field.prototype.afterRender = Ext.form.Field.prototype.afterRender.createSequence(function() {
                if (this.tooltip) {
                    new Ext.ToolTip({
                        target: this.id,
                        title: this.tooltipTitle,
                        html: this.tooltip,
                        showDelay: 20,
                        dismissDelay: 0,
                        shadow: true,
                        trackMouse: true
                    });
                }
            });



            this.SearchText = new nnet.sd.ContentToolBar.SearchField({
                id: 'ContentSearchField',
                width: 160
            });

            this.InsertButton = new Ext.Toolbar.Button({
                text: 'Insert',
                iconCls: 'insert-icon',
                tooltip: '<b>Insert Listing in Currently selected category</b><br/>Choose an appropriate (and lowest) category in the category tree on the left-hand side. The category tree can be expanded to view sub-categories by clicking the plus (+) button.',
                handler: this.onInsertButtonClick

            });
            this.MapButton = new Ext.Toolbar.Button({
                text: 'Map',
                iconCls: 'map-icon',
                tooltip: 'View Map of Listings',
                handler: this.onMapButtonClick,
                hidden: true

            });
            this.ListButton = new Ext.Toolbar.Button({
                text: 'List',
                iconCls: 'list-icon',
                tooltip: 'View Listings',
                handler: this.onListButtonClick,
                hidden: true
            });

            this.PrintButton = null;
            this.SearchButton = null;

            var da = new Ext.data.Store({
                proxy: new Ext.data.MemoryProxy(sVars.SD_SITEDROPDOWN_DATA),
                reader: new Ext.data.JsonReader({ totalProperty: 'itemCount', root: 'items', id: 'Id' }, [{ name: 'Id' }, { name: 'SiteName'}])
            });

            da.load();

            this.SiteDropdown = new Ext.form.ComboBox({
                id: 'SiteDropdown',
                store: da,
                valueField: 'Id',
                displayField: 'SiteName',
                mode: 'local',
                forceSelection: true,
                editable: false,
                typeAhead: true,
                triggerAction: 'all',
                width: 135,
                tooltipTitle: 'Current Site',
                tooltip: 'Select a Site or Group of Sites to search or browse',
                listeners: { select: this.onSiteChanged }
            });

            this.SiteDropdown.setValue(sVars.SD_SITEDROPDOWN_DATA.selectedItem);

            this.ToolBar = new Ext.Toolbar({
                renderTo: 'ContentToolbar',
                items: ['Search: ', ' ', this.SearchText, '-', this.SiteDropdown, '-', this.InsertButton, this.ListButton, this.MapButton]
            });

        } catch (e) { logE("sd.ContentToolBar.initialize", e); }
    },


    startup: function() {
        try {

        } catch (e) { logE("sd.ContentToolBar.startup", e); }
    },


    onInsertButtonClick: function(btn) {
        if (SD.NavigationTree.isLeaf()) new nnet.sd.Service().insert(SD.CurrentCategoryId);
        else alert('This category is divided into sub-categories. Please choose an appropriate (and lowest) sub-category.');
    },

    onListButtonClick: function(btn) {
        SD.show(sVars.LIST_CONTENT_VIEW_STATE);
        NNet.PageHistory.add();
    },

    onMapButtonClick: function(btn) {
        SD.show(sVars.MAP_CONTENT_VIEW_STATE);
        NNet.PageHistory.add();
    },

    onSiteChanged: function(combo, record, index) {
        try {
            ServiceContentLoadingImage.show();
            if (SD.SearchSource == sVars.TEXT_SEARCH_SOURCE) TextSearchServices(SD.ContentToolBar.SearchText, 0, record.data.Id);
            else if (SD.SearchSource == sVars.CATEGORY_SEARCH_SOURCE) CategorySearchServices(SD.CurrentCategoryType, SD.CurrentCategoryId, 0, record.data.Id);
        } catch (e) { logE("sd.ContentToolBar.siteChanged", e); }
    },

    showMapButton: function() {
        try {
            this.MapButton.show();
        } catch (e) { // if toolbar not init when call set to show on int
            this.ShowMapButtonOnInit = true;
        }
    },

    textSearch: function() {
        SD.ListingView.empty();
        SD.NavigationTree.Node = null;
        SD.CurrentCategoryId = 0;
        currentPage = 0;
        SD.SearchSource = sVars.TEXT_SEARCH_SOURCE;
        TextSearchServices(this.SearchText, currentPage, SD.CurrentSearchSite);
        if (SD.ContentViewState == sVars.DETAIL_CONTENT_VIEW_STATE) {
            SD.show(sVars.LIST_CONTENT_VIEW_STATE);
        }
    }


});


nnet.sd.ContentToolBar.SearchField = Ext.extend(Ext.form.TwinTriggerField, {
    initComponent: function() {
        nnet.sd.ContentToolBar.SearchField.superclass.initComponent.call(this);
        this.on('specialkey', function(f, e) {
            if (e.getKey() == e.ENTER) {
                this.onTrigger2Click();
                e.stopEvent();
                e.stopPropagation();
            }
        }, this);
    },

    validationEvent: false,
    validateOnBlur: false,
    trigger1Class: 'x-form-clear-trigger',
    trigger2Class: 'x-form-search-trigger',
    hideTrigger1: true,
    width: 180,
    hasSearch: false,
    paramName: 'query',
    tooltipTitle: 'Search Listings for text',
    tooltip: 'Press the return key or the seach button to start a search</br>' +
                'The following options are available for searching:<ul>' +
                '<li>- Words are wild carded by default</li>' +
                '<li>- Multiple words are and’d together by default</li>' +
                '<li>- Exact matches and phases in quotes e.g. “Corgi Registered”</li>' +
                '<li>- Words excluded using – e.g. search using: builder –corgi</li>' +
                '<li>- Fuzzy search using ~ e.g. cogi~</li>' +
                '<li>- To specify fields use :, e.g. name:bell will find Bell and Crown and Bellingers in the name field</li>' +
                '</ul>',
    emptyText: 'enter text to search',


    onTrigger1Click: function() {
        if (this.hasSearch) {
            this.el.dom.value = '';
            var o = { start: 0 };
            this.triggers[0].hide();
            this.hasSearch = false;
        }
    },

    onTrigger2Click: function() {
        var v = this.getRawValue();
        if (v.length < 1) {
            this.onTrigger1Click();
            return;
        }
        var o = { start: 0 };
        SD.ContentToolBar.SearchText = v;
        SD.ContentToolBar.textSearch();
        this.hasSearch = true;
        this.triggers[0].show();
    }
});
