﻿$(function() {
    _insightSearch = new InsightSearch();
    var _searchBase = new SearchBase();
    
    function InsightSearch () {       
        
        this.searchRadio = searchRadio;
        this.searchInsights = searchInsights;
        this.searchDateRadio = searchDateRadio;
        this.searchFormat = searchFormat;
        this.DoInsightSearch = DoInsightSearch;
        this.KeywordIDSearch = KeywordIDSearch;
        
        var SEARCHPAGE = "/InsightSearchResults?";
          
        var _keywordSearchType = 0;
        var _searchTerm = "";
        var _insightTypes = new Array();
        var _formatTypes = new Array();
        var _dateRange = 0;
        var _keywordID = 0;
        
        
        function DoInsightSearch()
        {
            _searchTerm = $('#iSearchBox').val();
            if (_searchTerm.length == 0)
            {
                return;            
            }
            else
            {
                if (_searchTerm == 'Search Health Care Insights')
                {
                    _searchTerm = '';
                }
                
                //build search string
                var topics = "";
                var i=0;
                while(i < _insightTypes.length)
                {
                    topics = topics + "&T=" + _insightTypes[i];
                    i++;
                }

                var formats = "";
                var i=0;
                while(i < _formatTypes.length)
                {
                    formats = formats + "&F=" + _formatTypes[i];
                    i++;
                }
                var qs = "KST=" + _keywordSearchType + "&KT=" + _searchTerm + topics + formats + "&D=" + _dateRange;
                window.location = SEARCHPAGE + qs;
            }
        }
        
        function searchRadio(ref){
            _searchBase.searchRadio(ref);
            _keywordSearchType = ref;            
        }
        
        function searchInsights(ref){
            var isOn = _searchBase.toggleInsight(ref);
            _searchBase.manageArray(_insightTypes, isOn, ref);
        }
        
        function searchDateRadio(ref){
            _searchBase.searchDateRadio(ref);
            
            if (ref == 0)
            {
                _dateRange = 0;
            }
            else
            {
                _dateRange = $('#myselectbox').val();
            }
        }
        
        function searchFormat(ref){
            var isOn = _searchBase.toggleFormat(ref);
            _searchBase.manageArray(_formatTypes, isOn, ref);
        }       
        
        function KeywordIDSearch(id, term)
        {
            window.location = SEARCHPAGE + "K=" + id + "&KWT=" + term;
        }
    };
});
