﻿$(function () {
    var url = "/services/search.aspx";
	var searchForm = $('#iSearch');

	searchForm.autocompletePlugin(url, {
                                    cacheLength: 0,
                                    width: 383,
                                    dataType: 'json',
                                    selectFirst: false,
                                    highlight: false,
                                    minChars: 0,
                                    matchContains: "word",
                                    autoFill: false,
		formatItem: function (row, i, max) {
                                        var info = '';
			info += (row.Link) ? '<div><a href="' + row.Link + '">' + row.Name + '</a>' : '<div>' + row.Name;
			info += (row.Title) ? ' <span>' + row.Title + '</span></div>' : '</div>';
			if (row.Phone || row.Fax || row.Email) {
                                            info += '<div>';
				info += (row.Phone) ? '<span><b>T: </b>' + row.Phone + ' </span>' : '';
				info += (row.Fax) ? '<span><b>F: </b>' + row.Fax + ' </span>' : '';
				info += (row.Email) ? '<a href="javascript:" class="emailDisclaimer"><span class="emailInfo">' + row.Email + '</span>Email</a>' : '';
				info += (row.Guid) ? ' | <a href="javascript:" class="emailDisclaimer"><span class="guidInfo" style="display: none;">' + row.Guid + '</span>V-Card</a>' : '';
                                            info += '</div>';
                                        }
                                        return info;
                                    },
		formatMatch: function (row, i, max) {
                                        return row.Name;
                                    },
		formatResult: function (row) {
                                        return row.Name;
                                    },
		formatCategory: function (row, term) {
			if (row.IsFirst) {
                                            var url, section;
				if (row.Category == 'PEOPLE') {
					url = '/People/Listing.aspx?key=' + term;
                                                section = 'People results';
                                            }
				else if (row.Category == 'PRACTICES') {
					url = '/Practices/A-to-Z.aspx';
                                                section = 'Practices';
                                            }
				else if (row.Category == 'OFFICES') {
					url = '/Offices.aspx';
                                                section = 'Offices';
                                            }
                                            var html = '<li class="ac_category"><h3>' + row.Category + '<a href="' + url + '">View All ' + section + ' +</a></h3></li>';
                                            return html;
                                        }
                                    },
		formatFooter: function (term) {
                                        return '<a href="/Search-Results.aspx?key=' + term + '">View all search results +</a>';
		}
	})
    .result(function (event, item) { location.href = item.Link; })
    .defaultResult(function () { location.href = "/search-results.aspx?key=" + $.URLEncode($(this).val()); });

    jQuery('input[type="text"],textarea').blur
    (
        function () {
            var txt = jQuery(this);
            txt.val(jQuery.trim(txt.val()));
        }
    );
});

//URL ENCODE PLUGIN
$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});
