﻿/**
* @author trygve
*/

Ext.ns('OXX.Search');



OXX.Search = {
    textEl: null,
    ajaxMode: false,
    colDest: [],
    colTemplate: [],
    resultCache: {},
    currentCacheKey: [],

    init: function() {
        this.ajaxMode = Ext.isObject(Ext.fly('searcharea'));

        if (this.ajaxMode) {
            Ext.Ajax.purgeListeners();
            Ext.Ajax.on('requestcomplete', this.resultCallback, this);
            Ext.Ajax.on('requestexception', this.resultCallback, this);

            this.colTemplate[0] = new Ext.Template('<div class="title-row row">{title}</div>');
            this.colTemplate[1] = new Ext.Template('<div class="date-row row">{date}</div>');
        }

        this.textEl = Ext.get('input_SEARCHTEXT');

        var txt = OXX.Util.paramvalue('text');
        if (Ext.isPrimitive(txt)) {
            this.textEl.dom.value = txt;
        }

        this.textEl.on('keyup', function(e, t) {
            var raw = e.getKey();
            var key = String.fromCharCode(raw);
            if ((this.ajaxMode) || (e.getKey() == 13)) {
                this.search(this.textEl.dom.value);
                //    alert(raw + ":" + key);
            }
        }, this);

    },

    cacheCheck: function(key) {
        return Ext.isObject(this.resultCache[key]);

    },

    cacheAdd: function(key, item) {        
        this.resultCache[key] = item;

    },
    
    cacheGet: function(key) {
        return  this.resultCache[key];
    },


    search: function(text) {

        if (this.ajaxMode) {
            this.cacheCheck(text) ? this.renderResult(this.cacheGet(text)) : this.ajaxSearch(text) ;
            
        } else {
            OXX.Util.redirect(6, 'default', null, 'text=' + this.textEl.dom.value);
        }
    },

    renderRow: function(obj) {
        for (var i = 0; i < 2; i++) {
            this.colTemplate[i].append(this.colDest[i], obj);
        }
    },

    renderResult: function(objs) {
        this.colDest[0] = Ext.DomHelper.overwrite(Ext.get('col1'), true);
        this.colDest[1] = Ext.DomHelper.overwrite(Ext.get('col2'), true);
        Ext.each(objs, this.renderRow, this);
    },

    resultCallback: function(o, t) {
        if (t.status == 200) {
            var resultObjs = Ext.decode(t.responseText);
            this.cacheAdd(this.currentCacheKey, resultObjs);
            this.renderResult(resultObjs);
        }
        else {
            alert('Det har skjedd en teknisk feil:' + t.responseText);
        }

    },

    ajaxSearch: function(text) {
        this.currentCacheKey = text;
        Ext.Ajax.request({
            url: 'DocumentSearch.aspx?text=' + text,
            method: 'POST',
            form: 'some-form',
            params: null,
            scope: this,

            failure: function(response, opts) {
                if (Ext.isGecko) console.log('pre:' + 'server-side failure with status code ' + response.status);
            }
        });
    },

    sort: function() {

    }
};






Ext.onReady(function() {
   
    OXX.Search.init();

});
    
    
