﻿/* Genuine Interactive 2009
 * Search.js
 * General Search JavaScript View Side
 * Author:  Joe Magner
 * Date:    7/20/09
 */
 
/*  Search()
    *   Major search functionality
    *   Returns ~
    *       success(bool)   - did we get our object and set all handlers?
    *   */
function Search(Base){
    this.Base = Base;
    this.Initialize();
    this.interval = 1000;
    this.lastKeypress = null;
    this.results = null;
}
/*  Search.interceptKeypress()
    *   The search request
    *   Returns ~
    *   */
Search.prototype.interceptKeypress = function(){
    $('.search-loader').fadeIn("fast");
    this.lastKeypress = new Date().getTime();
    var self = this;
    setTimeout(function() {
        this.currentTime = new Date().getTime();
        if(this.currentTime - self.lastKeypress > self.interval) {
            self.sendRequest();
        }
    }, self.interval + 10);
}
/*  Search.Initialize()
    *   Set up search event handlers
    *   Returns ~
    *       success(bool)   - did we get our object and set all handlers?
    *   */
Search.prototype.Initialize = function(){
    var self = this;
    $('.search-query,.results-container').click(function(e){
        //prevent the body.click event from firing when the search button is clicked
        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation(); 
    });
    $('.search-query').keyup(function(e){
        self.interceptKeypress();
    });
    $('.search-button').click(function(e){
        $('.search-loader').fadeIn("fast");
        //prevent the body.click event from firing when the search button is clicked
        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation(); 
        self.sendRequest();
    });
    $('.view-results').click(function(e){
        //prevent the body.click event from firing when the search button is clicked
        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
        $(this).attr('src','/App_Images/Search/results-on.gif');
        $('.past-results').slideDown();
    });
    $('.spyglass').click(function(e){
        $('.query').hide();
        $('.past').show();
        $('.slide-effect').animate({
            width: "0px"
        },100, function(){
        $('.past-search-query').css('width','170px');
        });
    });
    $('.closed-spyglass').click(function(e){
        $('.past-search-query').css('width','0px');
        $('.slide-effect').animate({
            width: "170px"
        },100,function(){
            $('.past').hide();
            $('.query').show();
        });
    });
    $('body').click(function(){
        self.hideResults();
        $('.view-results').attr('src','/App_Images/Search/results-off.gif');
        $('.past-results').slideUp();
    });	
}
/*  Search.showResults()
    *   Show the results div if we have results
    *   Returns ~
    *   */
Search.prototype.showResults = function(){
    $('.search-loader').fadeOut("fast");
    if(!this.resultsShowing()){
        $('.search-button').attr('src','/App_Images/Search/results-on.gif');
        $('.current-results').slideDown();
    }
}
/*  Search.hideResults()
    *   Hide the results div
    *   Returns ~
    *   */
Search.prototype.hideResults = function(){
    if(this.resultsShowing()){
        $('.search-button').attr('src','/App_Images/Search/results-off.gif');
        $('.current-results').slideUp();
    }
}
/*  Search.resultsShowing()
    *   Returns true if the results div is currently open
    *   Returns ~
    *   */
Search.prototype.resultsShowing = function(){
    if($('.current-results').css("display") == "block") return true;
    else return false;
}
/*  Search.sendRequest()
    *   The search request
    *   Returns ~
    *   */
Search.prototype.sendRequest = function() {
    var self = this;
    if ($('.search-query').val() != "") {
        this.Base.callService({
            name    : 'Search',
            post    : {
                'param' : $('.search-query').val()
            },
            success : function ( response ) {
                var results = response.d;
                if(results.length > 0)
                {
                    $('.current-space ul').html(results);
                    $('#view-all-current-results').click(function(e){
                        window.location = "/" + $('.search-query').val()+ "/Search.aspx"; 
                    });
                    self.showResults();
                } else { 
                    $('.current-space ul').html("<li><p>Search did not find anything.</p></li>");
                    self.showResults();
                }
            },
            error   : 100
        });
    } else {
        $('.current-space ul').html("<li><p>You must enter a search query.</p></li>");
        self.showResults();
    }
}
