/////////////////////////////////////////////////
///////////// SMART SUGGEST /////////////////////
$.ezSmartSuggest = function(container, selector, my_style_func, my_click_func, max, highlight, zebra) {
	var is_chrome = (navigator.userAgent.toString().match(/chrome/i)!=null) ? true : false;
	var suggestions = $(container).children(".ss_suggestions");
	
	max = (!max) ? -1 : max;
	highlight = (!highlight) ? 'highlight' : highlight;
	zebra = (!zebra) ? 'gray-bg' : zebra;
	$(document).click(function() {
		if(is_chrome) { $(".ss_suggestions").css("overflow-y", "hidden"); }
		$(suggestions).fadeOut("fast");
	});
	// Input keyup
	$(container).children(".ss_search_box").focus();
	$(container).children(".ss_search_box").keyup(function(e) {
		var links;
		var key_code = (e.keyCode) ? e.keyCode : e.which;
		if(key_code!=13 && key_code!=38 && key_code!=40) {
			// SEARCHING
			var val = $(this).val();
			val = val.toString().replace(/^\s+/, '').replace(/\s+$/, '');
			var pattern = new RegExp(val, "i");
			var html = ''; var count = 0;
			$(selector).each(function() {
				var r = my_style_func(pattern, $(this));
				if(r!==false) {
					html += r;
					count++;
				}
			});
			if(!count || (count>max && max!==-1)) {
				if(is_chrome) { $(suggestions).css("overflow-y", "hidden"); }
				$(suggestions).fadeOut("fast");
				return false;
			}
			
			// ENOUGH SEARCHES
			$(suggestions).html('<ul class="no_list_style tight">'+html+'</ul>');
			$(container).children(".ss_suggestions a:even").addClass(zebra);
			var width = $(this).outerWidth();
			var height = $(this).outerHeight();
			$(suggestions).css({
				"top":(height-1)+"px",
				"min-width":(width-2)+"px"
			});
			
			if(is_chrome) { $(suggestions).css("overflow-y", "hidden"); }
			$(suggestions).fadeIn("fast", function(){
				if(is_chrome) { $(this).css("overflow-y", "auto"); }
			});
			
			
			links = $(suggestions).find("a");
			$(links).eq(0).addClass(highlight);
			// EVENT LISTENERS
			$(links).click(function() {
				my_click_func($(this));
			});
			$(links).hover(function() {
				$(links).removeClass(highlight);
				$(this).addClass(highlight);
			});
			
			return;
		}
		
		links = $(suggestions).find("a");
		var acount = $(links).length;
		var rid = $(suggestions).find("a.highlight").parent().index();
		//if(key_code==37 || key_code==39) { return; }
		if(key_code==13) {
			// ENTER
			rid = (rid==-1) ? 0 : rid;
			$(links).eq(rid).trigger("click");
			return false;
		}
		
		// DOWN OR UP
		if(key_code==40) { rid = (rid < 0 || rid >= acount-1) ? 0 : rid+1; }
		else { rid = (rid <= 0 || rid > acount-1) ? acount-1 : rid-1; }
		$(links).removeClass(highlight);
		$(links).eq(rid).addClass(highlight);
	});
	$(container).children(".ss_search_box").click(function(){
		$(this).select();
	});
	$(container).children(".hint_me").click(function(){
		$(container).children(".ss_suggestions").hide();
		
		var i = $(this).parent().find(".ss_search_box");
		$(i).val('');
		$(i).focus();
		
		var e = $.Event("keyup");
		e.which = 1; // # Some key code value
		$(i).trigger(e);

		return false;
	});
}
