var search_controller = new Class({
    Extends : controller,
    Implements: [Options, Events],
    options: {
        url:'as/search.php',
        use_fade:false
    },
    initialize : function(options){
        this.setOptions(options);
        this.parent(options);

        //Get all of the hardcoded elements
        this.search_results = $('quick_search_results');
        this.ajax_search_results = $('ajax_search_results');
        this.result_fade = new Fx.Tween(this.search_results);

        //Apply the event to the quick search close button
        if(this.search_close = $('quick_search_close')){
            this.search_close.addEvent('click',function(){
                this.hide_results();
            }.bind(this));
        }

        //Apply the event to the quick search input
        if(this.search_input = $('quick_search_input')){
            this.search_input.self = this;
            this.search_input.addEvent('keyup',function(e){
                if(e.target.value != '')
                    e.target.self.search({'keyword':e.target.value});
                else
                    e.target.self.search_close.fireEvent('click');
            });
        }
    },
    search : function(data){
        this.send(data);
    },
    default_response : function(data){
        alert('Hmm, could not see to find the correct response for your query');
    },
    populate_results : function(data){
        var record_open_string = '<div class="record"><div class="padding">';
        var record_close_string = '<div class="cb"></div><!-- cb --></div><!-- padding --></div><!-- record --><div class="line"></div><!-- line -->';
        if(data.length > 0){
            var inner_html = '';
            for(var i=0;i<data.length;i++){
                inner_html += record_open_string;
                inner_html += '<div class="name"><a href="project.php?id='+data[i].id+'">'+data[i].title+'</a></div><!-- name -->';
                inner_html += record_close_string;
            }
            this.ajax_search_results.setProperty('html',inner_html);
        }else{
            //populate the search result div with the data
            this.ajax_search_results.setProperty('html','<div class="padding">No results found. Try using advanced search <a href="search.php" style="text-decoration:underline;">here</a>, or click on the magnifying glass.</div><!-- padding -->');
            //make the result div visible
        }
        //this.search_results.setStyle('display','block');
        if(this.search_results.getStyle('display') != 'block')
            this.show_results();
    },
    show_results : function(){
        if(this.options.use_fade){
            this.search_results.setStyle('opacity','0');
            this.search_results.setStyle('display','block');
            this.result_fade.start('opacity',0,1);
        }else{
            this.search_results.setStyle('display','block');
        }
    },
    hide_results : function(){
        if(this.options.use_fade){
            this.result_fade.start('opacity',1,0).chain(function(){
                this.search_results.setStyle('display','none');
            }.bind(this));
        }else{
            this.search_results.setStyle('display','none');
        }
    }
});
