﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery, cc_resx_TR_Image, cc_resx_TR_Images, cc_resx_TR_Video, cc_resx_TR_Videos */

//  
//  Author: Kevin Burkitt
//  Date:   2010-03-08
//
//  Suggest Controller - provides suggest drop down to header search box
//
//  Uses jQuery AutoComplete Plugin (http://www.devbridge.com/projects/autocomplete/jquery/)
//
cc.suggest = function() {

    var initDone = false;
    var searchElId = '#q';

    return {

        init: function() {
            if (initDone === true) {
                return;
            }

            jQuery(searchElId).autocomplete({
                minChars: 2,
                delimiter: /(,|;)\s*/, // regex or character
                width: 201,
                zIndex: 9999,
                params: { country: 'Yes' }, //aditional parameters
                noCache: false, //default is false, set to true to disable caching
                serviceUrl: '/suggest/users/',
                params: { format: 'json' },
                fnFormatResult: function(value, data, currentValue) {
                    var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');
                    var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';

                    var html = '';
                    var space = '';
                    if (data.type === "user") {
                        html += '<dl><dt>';
                        html += '<a href="' + data.url + '" title="' + data.friendlyName + '">';
                        html += '<img width="50" height="50" src="' + data.avatarUrl50x50 + '" alt="' + data.friendlyName + '" />';
                        html += '</a></dt><dd>';
                        html += '<a href="' + data.url + '" title="' + data.friendlyName + '">';
                        html += data.friendlyName.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
                        html += '</a>';
                        html += '<div><p>';
                        if (data.cImages === 1) {
                            html += '' + data.cImages + ' ' + cc_resx_TR_Image + '&nbsp;';
                            space = '&nbsp;&nbsp;';
                        } 
                        if (data.cImages > 1) {
                            html += '' + data.cImages + ' ' + cc_resx_TR_Images + '&nbsp;';
                            space = '&nbsp;&nbsp;';
                        }
                        if (data.cVideos === 1) {
                            html += space + data.cVideos + ' ' + cc_resx_TR_Video + '</p></div>';
                        }
                        if (data.cVideos > 1) {
                            html += space + data.cVideos + ' ' + cc_resx_TR_Videos + '</p></div>';
                        }
                        html += '</p></div>';
                        html += '</dd></dl>';
                    }

                    return html;
                },
                onSelect: function(value, data) {
                    if (data.type === "user") {
                        location.href = data.url;
                    }
                }

            });

            initDone = true;
        }

    };

} ();
