// require prototype.js
var GoogleMajority = Class.create();
GoogleMajority.prototype = {
    initialize: function(args){
        this.form = $(args.form);
        this.table = $(args.table);
        if ( args.working ) this.working = $(args.working);
        if ( args.message ) this.message = $(args.message);
        if ( args.oddClassName ) this.oddClassName = args.oddClassName;
        if ( args.evenClassName ) this.evenClassName = args.evenClassName;

        this.words = [];
        if ( args.uri ) this.uri = $(args.uri);
        if ( args.words && typeof args.words == 'object' ) this.words = args.words;

        Event.observe( this.form, 'submit', this.submit.bindAsEventListener(this) );
    },
    submit: function(event){
        if ( this.form.word.value == '' ) {
            if ( this.message ) this.message.innerHTML = 'no search words.';
        }
        else {
            Form.disable( this.form );
            if ( this.working ) Element.show( this.working );
            if ( this.message ) this.message.innerHTML = '';

            new Ajax.Request(
                this.form.action,
                {
                    method: 'post',
                    parameters: Form.serialize(this.form) + '&mode=json',
                    onSuccess: this.succeed.bind(this),
                    onFailure: this.failed.bind(this)
                }
            );
        }

        Event.stop(event);
    },
    succeed: function(transport){
        var json = eval(transport.responseText);
        this.appendResult(json);

        Form.enable( this.form );
        if ( this.working ) Element.hide( this.working );
        if ( this.uri ) this.queryUri(this.uri);
        
        this.form.word.value = '';
        this.form.word.focus();
    },
    failed: function(transport){
        if ( this.message ) {
            this.message.innerHTML = 'request failed';
        }

        Form.enable( this.form );
        if ( this.working ) Element.hide( this.working );
        this.form.word.focus();
    },
    appendResult: function(json){
        if ( json.results.length == 0 ) {
            this.message.innerHTML = 'no results';
            return 0;
        }
        for ( var i = 0; i < json.results.length; i++ ) {
            var block = json.results[i];
            var rowOption = {};
            if ( this.oddClassName && this.evenClassName ) {
                rowOption.className = ( this.table.getElementsByTagName('tr').length + 1 ) % 2 == 0
                    ? this.evenClassName
                    : this.oddClassName;
            }
            var anchor = this.createElement('a',{ href:this.googleUri(block.word) },block.word);
            var td_word = this.createElement('td',{ className:'cell-word' },anchor);
            var td_count = this.createElement('td',{ className:'cell-count' },block.count);
            var tr = this.createElement('tr',rowOption,td_word,td_count);
            this.table.appendChild(tr);

            this.words.push(block.word);
        }
        return this.words.length;
    },
    googleUri: function(word){
        return 'http://www.google.com/search?ie=UTF-8&oe=UTF-8&q='
            + encodeURIComponent(word);
    },
    queryUri: function(element){
        element.innerHTML = '';

        var uri = location.href.replace(/\?.*$/,'');

        if ( this.words.length > 0 ) {
            uri += '?';
            for( var i = 0; i < this.words.length; i++ ){
                if ( 0 < i ) uri += '&';
                uri += 'word=' + encodeURIComponent(this.words[i]);
            }
        }

        var anchor = this.createElement('a',{ href: uri },'this uri');
        element.appendChild(anchor);
    },
    createElement: function(tag,attrs){
        var element = document.createElement(tag);
        for( var key in attrs ) element[key] = attrs[key];
        for( var i = 2; i < arguments.length; i++ ){
            if( typeof arguments[i] == 'string' ){
                element.innerHTML += arguments[i];
            }
            else if( typeof arguments[i] == 'object' ){
                element.appendChild(arguments[i]);
            }
        }
        return element;
    }
};
