// Created by Berk Ozer.

google.load("search", "1"); // loads the Google Search API


var app;

function kulfx_search_onload()
{
    app = new FXSearcher();
}

// Class FXSearcher ////////////////////////////////////////////////////////////

function FXSearcher()
{
    this.headerFrame         = document.getElementById( "header_frame" );
    this.logoImage           = document.getElementById( "logo_image" );
    this.resultsArea         = document.getElementById( "search_results" );
    this.resultsListArea     = document.getElementById( "search_results_list" );
    this.resultsCloseButton  = document.getElementById( "search_results_close_button" );

    // Create a search form
    document.getElementById("kulfx_search_area").style.visibility = "visible";
    var searchInput = document.getElementById( "kulfx_search_input" );
    searchInput.value = "";
    searchInput.onkeydown = this.newSearch;

    // Create a Google web searcher
    this.kulfxSearch = new google.search.WebSearch();
    this.kulfxSearch.setSiteRestriction( "kulfx.com" );
    this.kulfxSearch.setNoHtmlGeneration();
    this.kulfxSearch.setSearchCompleteCallback( this, FXSearcher.prototype.OnSearchComplete );

    this.resultsArea.style.visibility = "hidden";
}


FXSearcher.prototype.newSearch = function(event)
{
    if ( event.keyCode == 13 ) // the enter key
    {
        var searchInput = document.getElementById( "kulfx_search_input" );
        if ( searchInput.value )
        {
            app.kulfxSearch.execute( searchInput.value );
        }
        else
        {
            app.hideSearchArea();
        }
    }
    return true; /* necessary so the event can propagate to the text field */
}


FXSearcher.prototype.OnSearchComplete = function()
{
    this.resultsArea.style.visibility = "hidden";
    // Hide results from previous search.
    this.resultsArea.style.visibility = "hidden";
    while ( this.resultsListArea.hasChildNodes() )
    {
        this.resultsListArea.removeChild( this.resultsListArea.firstChild );
    }
    
    // Populate with new search results
    if ( this.kulfxSearch.results.length > 0 )
    {
        // Now manually generate the html that we disabled initially and display it.
        var list = document.createElement( "ul" ); // unordered list
        list.style.marginTop = 0;
        list.style.marginBottom = 10;
        for (var i = 0; i < this.kulfxSearch.results.length; i++)
        {
            var result = this.kulfxSearch.results[i];
            var listItemBox = this.createListItem( result.content, result.unescapedUrl );
            list.appendChild( listItemBox );
        }
        this.resultsListArea.appendChild( list );
    }
    else
    {
        var noResultsDiv = document.createElement( "div" );
        noResultsDiv.innerHTML = "<span style=\"margin-left:36px\">No Results</span>";
        this.resultsListArea.appendChild( noResultsDiv );
    }

    // Calculate the required shift in the height of the content area
    // in order to squeeze the search results area inbetween the header
    // and the content area.
    var diff = 0; // top margin + bottom margin
    for ( var j = 0; j < this.resultsArea.childNodes.length; j++ )
    {
        var node = this.resultsArea.childNodes[j];
        if ( node.nodeType == Node.ELEMENT_NODE )
        {
            diff = diff + node.offsetHeight;
        }
    }

    this.resultsArea.style.top = this.logoImage.clientHeight + 28 + "px";
    var newHeaderFrameHeight = diff + this.logoImage.clientHeight + 28 + "px";

    // JQuery animation
    $('#header_frame').animate( {height:newHeaderFrameHeight}, {duration:300, complete:this.headerFrameAnimationComplete} );

}


FXSearcher.prototype.headerFrameAnimationComplete = function()
{
    document.getElementById("search_results").style.visibility = "visible";
}


FXSearcher.prototype.hideSearchArea = function()
{    
    // Hide search area and remove search results
    this.resultsArea.style.visibility = "hidden";
    while ( this.resultsListArea.hasChildNodes() )
    {
        this.resultsListArea.removeChild( this.resultsListArea.firstChild );
    }

    this.resetHeaderFrameHeight();
    $('#header_frame').animate( {height:this.headerFrameNewHeight}, {duration:300} );
}


FXSearcher.prototype.createListItem = function( the_text, the_link )
{
    var listItem = document.createElement( "li" ); // list item
    var reference = document.createElement( "a" ); // anchor
    reference.href = the_link;
    reference.innerHTML = the_text;
    reference.style.color = '#204020';
    listItem.appendChild( reference );
    listItem.style.color = '#204020';
    listItem.className = "search_results";
    return listItem;
}

FXSearcher.prototype.resetHeaderFrameHeight = function()
{
    this.headerFrameNewHeight = this.logoImage.clientHeight + 10 + "px";
}

////////////////////////////////////////////////////////////////////////////////

//google.setOnLoadCallback(search_onload);

