/*!
 * @author Gproxy Design Inc.
 * @copyright (c) 2010, Gproxy Design Inc. All rights reserved.
 * @version 2.0
 *
 * Gproxy doesn't allow to copy or change this code without Gproxy authorization.
 * See http://www.gproxy.com/licenses/license01.pdf for the full license governing this code.
 */

/**
 * Create's the autosuggestions with the search results for a specific input field
 * @param {Object} el The input field
 * @param {Object} options 
 * 	autoSubmit 		- Automatically submit the parent form for the input field
 * 	minChar 		- Minimum quantity for begin searching
 *  maxHeight		- Maximum Height for the search results box
 *  deferRequestBy	- Delay the autosuggestion process in x milliseconds 
 *  width			- Width for the search results box
 *  highlight		- Highlight the results in the search results box
 *  params{}		- Array object with the parameters
 *  delimiter		- Results delimiter
 *  searchBkgStyle	- CSS Style for the input field when begin the autosuggestion
 *  searchCSSClass	- CSS Class for the search results box
 *  maxResults		- Maximum results to show in the search results box
 *  zIndex			- CSS zindex for the search results box 
 *  fnFormatResult	- Function to be executed on each result in order to give a special custom format - Parameters (Value, data, Current Value)
 *  fnViewMore		- Function to customize a especial behavior in order to display more results - Parameters (Query Value)
 *  fnParseResponse	- Function to customize the parse of the response for the ajax call - Parameters (Text Response, Query Value)
 *  fnOnSelect		- Function to be executed after the selection on the search results box - Parameters (Value, Data, Input Field) 
 */
(function($){
    function Autocomplete(el, options){
        this.el = $(el);
        this.el.attr('autocomplete', 'off');
        this.suggestions = [];
        this.data = [];
        this.badQueries = [];
        this.selectedIndex = -1;
        this.currentValue = this.el.val();
        this.intervalId = 0;
        this.cachedResponse = [];
        this.onChangeInterval = null;
        this.ignoreValueChange = false;
        this.serviceUrl = options.serviceUrl;
        this.isLocal = false;
        this.options = {
            autoSubmit: false,
            minChars: 1,
            maxHeight: 300,
            deferRequestBy: 0,
            width: 0,
            highlight: true,
            params: {},            
            delimiter: null,
            searchBkgStyle: '',
            searchCSSClass: 'autocomplete',
            maxResults: 0,
            zIndex: 9999
        };
        this.initialize();
        this.setOptions(options);
    }
    $.fn.autocomplete = function(options){
        return new Autocomplete(this.get(0) || $('<input />'), options);
    };
    Autocomplete.prototype = {
        killerFn: null,
        initialize: function(){
            var me, uid, autocompleteElId;
            me = this;
            uid = Math.floor(Math.random() * 0x100000).toString(16);
            autocompleteElId = 'Autocomplete_' + uid;
            this.killerFn = function(e){
                if ($(e.target).parents('.autocomplete').size() === 0) {
                    me.killSuggestions();
                    me.disableKillerFn();
                }
            };
            if (!this.options.width) {
                this.options.width = this.el.width();
            }
            this.mainContainerId = 'AutocompleteContainter_' + uid;
            $('<div id="' + this.mainContainerId + '" style="position:absolute;z-index:9999;"><div class="' + this.options.searchCSSClass + '-w1"><div class="' + this.options.searchCSSClass + '" id="' + autocompleteElId + '" style="display:none; width:300px;"></div></div></div>').appendTo('body');
            this.container = $('#' + autocompleteElId);
            this.fixPosition();
            if (window.opera) {
                this.el.keypress(function(e){
                    me.onKeyPress(e);
                });
            }
            else {
                this.el.keydown(function(e){
                    me.onKeyPress(e);
                });
            }
            this.el.keyup(function(e){
                me.onKeyUp(e);
            });
            this.el.blur(function(){
                me.enableKillerFn();
            });
            this.el.focus(function(){
                me.fixPosition();
            });
        },
        setOptions: function(options){
            var o = this.options;
            $.extend(o, options);
            if (o.lookup) {
                this.isLocal = true;
                if ($.isArray(o.lookup)) {
                    o.lookup = {
                        suggestions: o.lookup,
                        data: []
                    };
                }
            }
            $('#' + this.mainContainerId).css({
                zIndex: o.zIndex
            });
            this.container.css({
                maxHeight: o.maxHeight + 'px',
                width: o.width
            });
        },
        clearCache: function(){
            this.cachedResponse = [];
            this.badQueries = [];
        },
        disable: function(){
            this.disabled = true;
        },
        enable: function(){
            this.disabled = false;
        },
        fixPosition: function(){
            var offset = this.el.offset();
            $('#' + this.mainContainerId).css({
                top: (offset.top + this.el.innerHeight()) + 'px',
                left: offset.left + 'px'
            });
        },
        enableKillerFn: function(){
            var me = this;
            $(document).bind('click', me.killerFn);
        },
        disableKillerFn: function(){
            var me = this;
            $(document).unbind('click', me.killerFn);
        },
        killSuggestions: function(){
            var me = this;
            this.stopKillSuggestions();
            this.intervalId = window.setInterval(function(){
                me.hide();
                me.stopKillSuggestions();
            }, 300);
        },
        stopKillSuggestions: function(){
            window.clearInterval(this.intervalId);
        },
        onKeyPress: function(e){
            if (this.disabled || !this.enabled) {
                return;
            }
            switch (e.keyCode) {
                case 27:
                    this.el.val(this.currentValue);
                    this.hide();
                    break;
                case 9:
                case 13:
                    if (this.selectedIndex === -1) {
                        this.hide();
                        return;
                    }
                    this.select(this.selectedIndex);
                    if (e.keyCode === 9) {
                        return;
                    }
                    break;
                case 38:
                    this.moveUp();
                    break;
                case 40:
                    this.moveDown();
                    break;
                default:
                    return;            }
            e.stopImmediatePropagation();
            e.preventDefault();
        },
        onKeyUp: function(e){
            if (this.disabled) {
                return;
            }
            switch (e.keyCode) {
                case 38:
                case 40:
                    return;            }
            clearInterval(this.onChangeInterval);
            if (this.currentValue !== this.el.val()) {
                if (this.options.deferRequestBy > 0) {
                    var me = this;
                    this.onChangeInterval = setInterval(function(){
                        me.onValueChange();
                    }, this.options.deferRequestBy);
                }
                else {
                    this.onValueChange();
                }
            }
        },
        onValueChange: function(){
            clearInterval(this.onChangeInterval);
            this.currentValue = this.el.val();
            var q = this.getQuery(this.currentValue);
            this.selectedIndex = -1;
            if (this.ignoreValueChange) {
                this.ignoreValueChange = false;
                return;
            }
            if (q === '' || q.length < this.options.minChars) {
                this.hide();
            }
            else {
                this.getSuggestions(q);
            }
        },
        getQuery: function(val){
            var d, arr;
            d = this.options.delimiter;
            if (!d) {
                return $.trim(val);
            }
            arr = val.split(d);
            return $.trim(arr[arr.length - 1]);
        },
        getSuggestionsLocal: function(q){
            var ret, arr, len, val, i;
            arr = this.options.lookup;
            len = arr.suggestions.length;
            ret = {
                suggestions: [],
                data: []
            };
            q = q.toLowerCase();
            for (i = 0; i < len; i++) {
                val = arr.suggestions[i];
                if (val.toLowerCase().indexOf(q) === 0) {
                    ret.suggestions.push(val);
                    ret.data.push(arr.data[i]);
                }
            }
            return ret;
        },
        getSuggestions: function(q){
            var cr, me;
            cr = this.isLocal ? this.getSuggestionsLocal(q) : this.cachedResponse[q];
            if (cr && $.isArray(cr.suggestions)) {
                this.suggestions = cr.suggestions;
                this.data = cr.data;
                this.suggest();
            }
            else 
                if (!this.isBadQuery(q)) {
                    me = this;
                    me.options.params.search = q;
                    me.options.params.query = q;
                    this.el.css('background', me.options.searchBkgStyle);
                    $.get(this.serviceUrl, me.options.params, function(txt){
                        me.processResponse(txt);
                    }, 'text');
                }
        },
        isBadQuery: function(q){
            var i = this.badQueries.length;
            while (i--) {
                if (q.indexOf(this.badQueries[i]) === 0) {
                    return true;
                }
            }
            return false;
        },
        hide: function(){
            this.enabled = false;
            this.selectedIndex = -1;
            this.container.hide();
        },
        suggest: function(){
            var me, len, maxResults, viewMore, div, f, v, i, s, mOver, mClick;
            if (this.suggestions.length === 0) {
                this.hide();
                this.container.hide().empty();
                div = $('<div class="selected" title="No Results Found">No Results Found</div>');
                this.container.append(div);
                this.enabled = true;
                this.container.show();
                return;
            }
            me = this;
            len = this.suggestions.length;
            f = this.options.fnFormatResult;
            v = this.getQuery(this.currentValue);
            more = this.options.fnViewMore;
            maxResults = this.options.maxResults;			
            viewMore = false;
            mOver = function(xi){
                return function(){
                    me.activate(xi);
                };
            };
            mClick = function(xi){
                return function(){
                    me.select(xi);
                };
            };
            this.container.hide().empty();
            if (!maxResults) {
                maxResults = len;
            }
            else {
                if (len > maxResults) {
                    viewMore = true;
                }
            }
            for (i = 0; i < len && i < maxResults; i++) {
                s = this.suggestions[i];				
                div = $((me.selectedIndex === i ? '<div' : '<div') + ' class="item_result" title="' + s + '">' + ($.isFunction(f) ? f(s, this.data[i], v): s) + '</div>');
                div.mouseover(mOver(i));
                div.click(mClick(i));
                this.container.append(div);
            }
            if ($.isFunction(more) && viewMore) {
                this.container.append(more(this.options.params.query));
            }
            this.enabled = true;
            this.container.show();
        },
        processResponse: function(text){
            var response, f;
            try {
                f = this.options.fnParseResponse;
                if ($.isFunction(f)) {
                    response = f(text, this.options.params.query);
                }
                else {
                    response = eval('(' + text + ')');
                }
            } 
            catch (err) {
                return;
            }
            if (!$.isArray(response.data)) {
                response.data = [];
            }
            if (!this.options.noCache) {
                this.cachedResponse[response.query] = response;
                if (response.suggestions.length === 0) {
                    this.badQueries.push(response.query);
                }
            }
            if (response.query === this.getQuery(this.currentValue)) {
                this.suggestions = response.suggestions;
                this.data = response.data;
                this.suggest();
            }
            this.el.css('background', '');
        },
        activate: function(index){
            var divs, activeItem;
            divs = this.container.children();
            if (this.selectedIndex !== -1 && divs.length > this.selectedIndex) {
                $(divs.get(this.selectedIndex)).removeClass('selected');
            }
            this.selectedIndex = index;
            if (this.selectedIndex !== -1 && divs.length > this.selectedIndex) {
                activeItem = divs.get(this.selectedIndex);
                $(activeItem).addClass('selected');
            }
            return activeItem;
        },
        deactivate: function(div, index){
            div.className = '';
            if (this.selectedIndex === index) {
                this.selectedIndex = -1;
            }
        },
        select: function(i){
            var selectedValue, f;
            selectedValue = this.suggestions[i];
            if (selectedValue) {
                this.el.val(selectedValue);
                if (this.options.autoSubmit) {
                    f = this.el.parents('form');
                    if (f.length > 0) {
                        f.get(0).submit();
                    }
                }
                this.ignoreValueChange = true;
                this.hide();
                this.onSelect(i);
            }
        },
        moveUp: function(){
            if (this.selectedIndex === -1) {
                return;
            }
            if (this.selectedIndex === 0) {
                $j(this.container.children().get(0)).removeClass('selected');
                this.selectedIndex = -1;
                this.el.val(this.currentValue);
                return;
            }
            this.adjustScroll(this.selectedIndex - 1);
        },
        moveDown: function(){
            if (this.selectedIndex === (this.suggestions.length - 1)) {
                return;
            }
            this.adjustScroll(this.selectedIndex + 1);
        },
        adjustScroll: function(i){
            var activeItem, offsetTop, upperBound, lowerBound;
            activeItem = this.activate(i);
            offsetTop = activeItem.offsetTop;
            upperBound = this.container.scrollTop();
            lowerBound = upperBound + this.options.maxHeight - 25;
            if (offsetTop < upperBound) {
                this.container.scrollTop(offsetTop);
            }
            else 
                if (offsetTop > lowerBound) {
                    this.container.scrollTop(offsetTop - this.options.maxHeight + 25);
                }
            this.el.val(this.getValue(this.suggestions[i]));
        },
        onSelect: function(i){
            var me, fn, s, d;
            me = this;
            fn = me.options.fnOnSelect;
            s = me.suggestions[i];
            d = me.data[i];
            me.el.val(me.getValue(s));
            if ($.isFunction(fn)) {
                fn(s, d, me.el);
            }
        },
        getValue: function(value){
            var del, currVal, arr, me;
            me = this;
            del = me.options.delimiter;
            if (!del) {
                return value;
            }
            currVal = me.currentValue;
            arr = currVal.split(del);
            if (arr.length === 1) {
                return value;
            }
            return currVal.substr(0, currVal.length - arr[arr.length - 1].length) + value;
        }
    };
}(jQuery));

function formatResult(value, data, currentValue){
	var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');
	var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
	var strTitle = '';
	var strDesc = '';
	switch(data.type) {
		case 'first-cat':
			strTitle = '<div class="search-title">Categories</div>';
			break;
		case 'first-itm':
			strTitle = '<div class="search-title">Products</div>'
			break;
		default: 
			strTitle = ''
			break;
	}
	strDesc = unescape(data.desc);
	if(strDesc.length > 80){
		strDesc = strDesc.substr(0,80) + '...';
	}else{
		strDesc = unescape(data.desc);
	}
	return (strTitle + ((data.img!=null && data.img != '' && data.img != 'undefined') ? '<a href="'+data.url+'" title="'+value+'" class="thumb_search"><img src="'+ data.img +'" alt="'+value+'"></a>' : '') + '<span class="search-text"><a href="' + data.url + '" title="">' + value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>') + '</a></span>');
}

function parseResponse(text, query){
	var auxResponseCategories = $j(text).find(".cat_cell_search");          
	var auxResponseItems = $j(text).find(".product_cell_search");						
	var bolItems = true;
	var bolCategories = true
	
	if (auxResponseCategories.length === 0) {
		bolCategories = false							
	}
	if (auxResponseItems.length === 0){
		bolItems = false;
	}
	if(!bolItems && !bolCategories){
		response = eval("({query: '" + query + "',suggestions: [],data: []})");
		return response;
	}
	else {
		var strSuggestions = "";
		var strData = "";
		$j(auxResponseCategories).each(function(i){														
			var strCategoryName = $j(this).find('.cat_name_search a').text();
			var strCategoryUrl = $j(this).find('.cat_name_search a').attr('href');
			var strCategoryImg = $j(this).find('.cat_thumbnail_search img').attr('src');			
			strCategoryImg = ((strCategoryImg != '' && strCategoryImg != null && strCategoryImg != 'undefined') ? strCategoryImg.split(';') : '');
			var strCategoryDesc = $j(this).find('.cat_desc_search').html();
			
			if(strCategoryDesc != null && strCategoryDesc != undefined){
				strCategoryDesc = escape(strCategoryDesc.replace(/'/gi,'-'));
			}else{
				strCategoryDesc = '';
			}
			strSuggestions += "'"+ strCategoryName.replace(/'/gi,'-') + "',";
			if(i==0){
				strData +=  "{url: '" + strCategoryUrl + "',img: '" + strCategoryImg[0] + "', desc:'" + strCategoryDesc + "', type: 'first-cat'},";
			}else{
				strData +=  "{url: '" + strCategoryUrl + "',img: '" + strCategoryImg[0] + "', desc:'" + strCategoryDesc + "', type: 'cat'},";
			}
		});
		$j(auxResponseItems).each(function(i){													
			var strName = $j(this).find('.product_name_search a').text();
			var strItemUrl = $j(this).find('.product_name_search a').attr('href');
			var strItemImg = $j(this).find('.product_thumbnail_search img').attr('src');
			strItemImg = ((strItemImg != '' && strItemImg != null && strItemImg != 'undefined') ? strItemImg.split(';') : '');
			var strItemDesc = $j(this).find('.product_desc_search').html();
			if(strItemDesc != null && strItemDesc != undefined){
				strItemDesc = escape(strItemDesc.replace(/'/gi,'-'));
			}else{
				strItemDesc = '';
			}
			strSuggestions += "'"+ strName.replace(/'/gi,'-') + "',";
			if(i==0){									
				strData +=  "{url: '" + strItemUrl + "',img: '" + strItemImg[0] + "', desc:'" + strItemDesc + "', type: 'first-itm'},";
			}else{
				strData +=  "{url: '" + strItemUrl + "',img: '" + strItemImg[0] + "', desc:'" + strItemDesc + "', type: 'itm'},";
			}
		});
		strSuggestions = strSuggestions.substring(0, (strSuggestions.length - 1));			 
		strData = strData.substring(0, (strData.length - 1));
		response = eval("({query: '" + query + "',suggestions: [" + strSuggestions + "],data: [" + strData + "]})"); 
		return response;
	}
}

function viewMore(query){
	return ('<div class="view-more"><a href="/s.nl?search=' + query + '" title="">View all search results</a></div>')
}

function onSelect(value, data, obj){
	window.location = data.url;
}


