/*
*  GrokSSJSONPCore
*  Association Ad Words search, Core Object
*/
if (typeof(Grok) == 'undefined') {
   var Grok = {};
}

Grok.SSCore = function(symbolName) {
    this.symbolName = symbolName;
};
Grok.SSCore.prototype = {
    coreVersion: 0.1,
    name: 'GrokSearchCore',
    grokSearchData: undefined,
    getType: function(){
        return '';
    },
    getPolicy: function(){
        return 'match';
    },
    getMax: function(){
        return 20;
    },
    targetQuery: '',
    getTargetQuery: function () {
        return '';
    },
    trace: false,
    setStatus: function(status) {
        this.status = status;
        if (this.trace) {
            if (typeof (console) != 'undefined') {
                console.log(this.status);
            } else {
                alert(this.status);
            }
        }
    },
    status: 'init',
    invoke: function() {
        this.setStatus('invoke');
        this.loadData();
    },
    loadData: function() {
        this.setStatus('loadData');
        var grokSQuery, script;
        this.targetQuery = encodeURIComponent(this.getTargetQuery());
        grokSQuery = [
            'query=' + this.targetQuery,
            'c=' + this.codec,
            'cb_func=' + this.symbolName + '.setGrokSearchData',
            'policy=' + this.getPolicy(),
            'max=' + this.getMax(),
            'type=' + this.getType()
        ];
        this.grokSearchSrc = 'http://' + this.grokHost + '/grokSSJSONP.py/?' + grokSQuery.join('&');
        script = document.createElement('script');
        script.type = 'text/javascript';
        script.charset = this.srcCodec;
        script.src = this.grokSearchSrc;
        this.setStatus('load...  ' + this.grokSearchSrc);
        document.body.appendChild(script);
    },
    setGrokSearchData: function(grokSearchData) {
        this.setStatus('setGrokSearchData');
        this.grokSearchData = grokSearchData || undefined;
        this.showWordList(this.grokSearchData);
    },
    showWordList: function(grokSearchData) {
        if (grokSearchData && grokSearchData.data.length > 0) {
            for (var i = 0, len = grokSearchData.data.length;i < len;i++) {
                console.log(grokSearchData.data[i]);
            }
        } else {
                this.noDataHandler();
        }
    },
    noDataHandler: function() {
        console.log('nodata');
    },
    // setting
    codec: 'utf8',
    grokHost: 'grok.kizasi.jp/api/'
};

Grok.SSUtil = {
    deligate: function(target, func) {
        return function() {
            func.apply(target, arguments);
        }
    },
    randomChoice: function(args) {
        return args[Math.floor(Math.random() * args.length)];
    },
    strip: function(str) {
        return String(str).replace(/^[ \s]*/gim, '').replace(/[ \s]*$/gim, '');
    },
    getElementsByClassName: function(className) {
        var children = document.getElementsByTagName('*') || document.all;
        var elements = [];
        for (var i = 0, len1 = children.length; i < len1; i++) {
            var classNames = children[i].className.split(' ');
            for (var j = 0, len2 = classNames.length; j < len2; j++) {
                if (classNames[j] == className) {
                    elements.push(children[i]);
                    break;
                }
            }
        }
        return elements;
    },
    map: function(args, func) {
        var results = [];
        for (var i = 0, len = args.length; i < len; i++) {
            results.push(func(args[i]));
        }
        return results;
    },
    addListener: (function() {
        if ( window.addEventListener ) {
            return function(elm, type, func) {
                elm.addEventListener(type, func, false);
            }
        } else if ( window.attachEvent ) {
            return function(elm, type, func) {
                var wrapperFunc = function() {
                    func.call(elm, window.event);
                };
                elm.attachEvent('on'+type, wrapperFunc);
            };
        } else {
            return function(elm, type, func) {
                elm['on'+type] = func;
            }
        }
    })()
};
