GoogleSearch.SearchView = SC.View.extend({
   
    childViews: 'searchFieldLabel searchField searchButton resetButton filterBox'.w(),
    layout: { width: 670 },
    defaultResponder: 'GoogleSearch.TabHostView',
    
    searchFieldLabel: SC.LabelView.design({
        layout: { left: 0, right: 0, top: 0 },
        textAlign: SC.ALIGN_LEFT,
        tagName: 'p',
        value: 'Otsisõna'
    }),
    
    searchField: SC.TextFieldView.design({
        layout: { left: 0, width: 450, top: 22, height: 24 },
        errorLabel: "Sisesta otsisõna!",
        validator: SC.Validator.NotEmpty.create() 
    }),
    
    searchButton: SC.ButtonView.design({
        layout: { left: 460, width: 100, top: 22, height: 24 },
        buttonBehavior: SC.PUSH_BEHAVIOR,
        title: 'Otsi',
        defaultResponder: 'GoogleSearch.SearchView',
        action: 'searchButtonClick'
    }),
    
    resetButton: SC.ButtonView.design({
        layout: { left: 570, width: 100, top: 22, height: 24 },
        buttonBehavior: SC.PUSH_BEHAVIOR,
        title: 'Tühjenda',
        defaultResponder: 'GoogleSearch.SearchView',
        action: 'resetButtonClick'
    }),
    
    filterBox: SC.CheckboxView.design({
        layout: { left: 0, right: 0, top: 55 },
        buttonBehavior: SC.PUSH_BEHAVIOR,
        title: 'Filtreeri korduvad tulemused'
    }),
          
    resetButtonClick: function(evt)
    {
        if(this.get('filterBox').isSelected)
        {
            this.get('filterBox').set('isSelected', false);
        }
        this.get('searchField').setFieldValue('');
    },
    
    searchButtonClick: function(evt)
    {
        var keyword = this.get('searchField').getFieldValue();
        var filter = (this.get('filterBox').isSelected) ? 1 : 0;
        if(this.get('searchField').performValidate() === SC.VALIDATE_OK)
        {
            GoogleSearch.get('searchController').search(
                keyword, 1, 10, filter,
                function() { 
                    GoogleSearch.get('searchController').switchTabs();
                },
                function() {
                    SC.AlertPane.error({
                        message: "Tõrge!",
                        description: "Andmevahetusel tekkis viga! Korda otsingut!"
                    });
                }
            );  
        }
        else
        {
            SC.AlertPane.error({
                message: "Otsisõna sisestamata!",
                description: "Sisesta otsisõna ja vajuta otsi."
            });
        }
    }
    
});; if ((typeof SC !== 'undefined') && SC && SC.Module && SC.Module.scriptDidLoad) SC.Module.scriptDidLoad('google_search');