﻿// -------------------------------------------------------------------------------------------------------
// Control which lets user enter a freetext criteria.
// -------------------------------------------------------------------------------------------------------
function ControlFreetext(name, headline, buttonText)
{
    this.name = name; 
    this.nameDiv = "Freetext_"+name+"Div";      // Name of div that surrounds the control.
    this.headline = headline; 
    this.buttonId = "Freetext_"+this.name+"_Button";
    this.textId =  "Freetext_"+this.name+"_Text";
    this.buttonText = buttonText;
    var self = this;
    
    
    // ------------------------------------------------------------------------------
    //  Render the control.
    // ------------------------------------------------------------------------------
    this.Render = function()
    {
        var temp="";  
        var criteria;
        
        // Outer div
        temp += "<div id='" + this.nameDiv + "' class='Freetext'>";   
            if(this.headline != null)
                temp += "<div id='"+this.nameDiv+"Headline' class='FreetextHeadline'>"+this.headline+"</div>";
            
//            temp += "<div id='Freetext_"+this.name+"_TextInputContainer' class='FreetextTextInputContainer'>";
                temp += "<input type='text' id='"+this.textId+"' />";
//            temp += "</div>";
                       
//            temp += "<div id='Freetext_"+this.name+"_ButtonContainer' class='FreetextButtonContainer'>";        
                temp += "<button type='button' id='"+this.buttonId+"'>"+ this.buttonText +"</button>";
//            temp += "</div>";                   
                    
        // End outer div.
        temp += "</div>";
        
        // Write HTML to page. 
        document.write(temp);   
        
        $(document).ready(function() 
        {         
            $("#"+self.buttonId).bind("click", self.OnInitiateSearch); 
            $("#"+self.textId).bind("keydown", self.OnKeyDown);   
        })              
    }
    this.Render();
    
    this.RegisterAsTrigger = function(searchController)
    {
        fc.RegisterTrigger(self.name, searchController);
    } 
	
    // ------------------------------------------------------------------------------
    //  Subscribe to specific event from searchcontroller.
    // ------------------------------------------------------------------------------ 
    this.SubscribeEvent = function(searchController, eventName, callback)
    {
        fc.SubscribeEvent(self, searchController, eventName, callback);
    }
    
    // ------------------------------------------------------------------------------
    //  Subscribe to all relevant events from searchcontroller.
    // ------------------------------------------------------------------------------ 
    this.SubscribeEvents = function(searchController)
    {
        self.SubscribeEvent(searchController, "SearchResult", self.OnSearchResult);
    }   
    
    
    this.OnKeyDown = function(event)
    {       
        if (event.which == '13') // enter
        {
            self.OnInitiateSearch(null);    
            return false; // don't refresh page.
        }       
    }

    this.OnInitiateSearch = function(event) 
    {
        var text = jQuery("#" + self.textId).val();

        var locationRegex = /\/search\.aspx/i;
        if (locationRegex.test(location.href)) {
            fc.ClearAllCriteria(self.name);
            fc.CriteriaChange_Freetext(self.name, text);
        }
        else {
            location.href = "/Search.aspx?search=" + encodeURI(text);
        }
    } 
    
	this.OnSearchResult = function(data, _this, persistedSearch)
	{	
		$jq("#"+self.textId).val(data.responseHeader.params.q);
	} 
}