﻿(function($) {
    $.fn.AutoComplete = function(_objName) {
        return this.each(function() {
            $.fn.AutoComplete.init($(this), _objName, this);
        })
    }

    $.fn.AutoComplete.init = function(_this, _objName) {
        this.searchBox = _this;
        document.onclick = function() {
            $.fn.AutoComplete.close();
        }
        _this.keyup(
		    function(event) {
        if ($(this).val().length > 2 && (!(event.keyCode > 47 && event.keyCode < 58)) && (!(event.keyCode == 38 || event.keyCode==40))) {
					
		            $.fn.AutoComplete.statuControl(this);	
		        }
		        else {
		            if (!(event.keyCode == 38 || event.keyCode == 40))	$.fn.AutoComplete.close();
				}
		    }
		)
        _this.keydown(function(event) {
            return $.fn.AutoComplete.animationControl(event.keyCode);
        });

    }

    $.fn.AutoComplete.statuControl = function(_searchBox) {
        //$("#autoCompleteContainer").hide();
        this.loadData(_searchBox.value, MYROOT + "/autocomplete/default.aspx",
			function(json, _this) {
			    _this.myJson = json
			    if (_this.myJson != null) {
			        _this.myJsonCampaingnsLength = _this.myJson.campaignReturns.length;
			        _this.myJsonReturnsLength = _this.myJson.returns.length;
			        _this.myJsonCampaingns = _this.myJson.campaignReturns;
			        _this.myJsonReturns = _this.myJson.returns;
			        _this.getContent();
			    }
			}, this
		);
    }

    $.fn.AutoComplete.getContent = function() {
    	this.controlNumber = 0;
    	resultContainer = $("#autoCompleteContainer #results");
    	campaignContainer = $("#autoCompleteContainer #resultsCampaigns");
        if (this.myJsonReturnsLength > 0) {            
            _str = "";
            for (var i = 0; i < this.myJsonReturnsLength; i++) {
                _str += "<li><a href=\"#\">" + this.myJsonReturns[i] + "</a></li>";
            }
            resultContainer.html(_str);
            this.controlNumber+=1;
			this.resultsHeight = (parseInt($("#autoCompleteContainer #results li").css("height").replace("px","")) * this.myJsonReturnsLength)  + 10;
        }

        if (this.myJsonCampaingnsLength > 0) {            
            _str = "";
            for (var i = 0; i < this.myJsonCampaingnsLength; i++) {
                _str += "<li><a href=\"" + this.myJsonCampaingns[i][1] + "\">" + this.myJsonCampaingns[i][0] + "</a></li>";
            }
            campaignContainer.html(_str);
            this.controlNumber += 2;
			this.campaignHeight = (parseInt($("#autoCompleteContainer #resultsCampaigns li").css("height").replace("px","")) * this.myJsonCampaingnsLength)  + 10;
        }
        $("#results a").click(function(_this){
        	return function(){
        		_this.searchBox.val($(this).html().replace("<u>","").replace("</u>","").replace("<U>","").replace("</U>",""));   
    		}
        }(this))
        
        $("#autoCompleteContainer li a").mouseover(function(){
        	$.fn.AutoComplete.activeNode = $("#autoCompleteContainer li").index($(this).parent()) + 1;
        	$.fn.AutoComplete.animate();
        	
        })
        if (this.controlNumber == 1) { this.show(1); }
        else if (this.controlNumber == 2) { this.show(2); }
        else { this.show(3); }
    }

    $.fn.AutoComplete.animationControl = function(_key) {
        if (_key != null) {
            if (_key == 40) {
                if (this.activeNode < (this.myJsonCampaingnsLength + this.myJsonReturnsLength)) {
                    this.activeNode++;
                }
                this.animate();
            }
            else if (_key == 38) {
                if (this.activeNode > 1) {
                    this.activeNode--;
                }
                this.animate();
            }
            else if (_key == 13) {
            
            if (this.activeNode == 0) return true;

            if (this.myJsonReturnsLength > 0) {
            		if(this.activeNode>this.myJsonReturnsLength){
            		    window.location.href = $("#autoCompleteContainer li").eq(this.activeNode - 1).find("a").attr("href") 
            			return false;          			
            		} 
            		this.searchBox.val($("#autoCompleteContainer li").eq(this.activeNode -1).find("a").html().replace("<U>","").replace("</U>","").replace("<u>","").replace("</u>",""));     		
            	}
            	else{
            		window.location.href = $("#autoCompleteContainer li").eq(this.activeNode -1).find("a").attr("href");
            		return false;
            	}              
            }
            
            return true;
        }
    }

    $.fn.AutoComplete.animate = function() {
    	
		if(this.activeNode >this.myJsonReturnsLength ){	
			stepHeight = (this.campaignHeight - 10) / this.myJsonCampaingnsLength;	
			$("#autoCompleteContainer #results").css("background-position","0px "+this.resultsHeight+"px")
			$("#autoCompleteContainer #resultsCampaigns").stop().animate({ backgroundPosition: "(0px " + ((((this.activeNode - this.myJsonReturnsLength) - 1)  * stepHeight) + 5  ) + "px)" }, { duration: this.duration,complete:function(_this){
		return function(){
			$("#autoCompleteContainer #results").css("background-position","0px "+_this.resultsHeight+"px")	
		} 																																																										   }(this)})				
				
		}		
		else{
			stepHeight = (this.resultsHeight - 10) / this.myJsonReturnsLength;		
			$("#autoCompleteContainer #resultsCampaigns").css("background-position","0px -25px")
			$("#autoCompleteContainer #results").stop().animate({ backgroundPosition: "(0px " + (((this.activeNode - 1)  * stepHeight) + 5  ) + "px)" }, { duration: this.duration,complete:function(){
				$("#autoCompleteContainer #resultsCampaigns").css("background-position","0px -25px")
			}})

		}
    }

    $.fn.AutoComplete.loadData = function(_data, _path, _callback, _this) {
        $.ajax({
            type: "POST",
            url: _path,
            cache: false,
            data: "str=" + _data,
            dataType: "json",
            success: function(_json) {
                _callback(_json, _this);
            }
        });
    }

    $.fn.AutoComplete.close = function() {
        $("#autoCompleteContainer").hide();
        $("#autoCompleteContainer #results").hide();
        $("#autoCompleteContainer #results li").remove();
        $("#autoCompleteContainer #resultsCampaigns").hide();
        $("#autoCompleteContainer #resultsCampaigns li").remove();
        $("#autoCompleteContainer h3").hide();
    }

    $.fn.AutoComplete.show = function(_type) {
		this.activeNode = 0;
		$("#autoCompleteContainer #results").css("background-position","0px -30px");
		$("#autoCompleteContainer #resultsCampaigns").css("background-position","0px -30px")
        if (_type == 1) {
            $("#autoCompleteContainer #results").show();
            $("#autoCompleteContainer #resultsCampaigns").hide();
            $("#autoCompleteContainer h3").hide();
        }
        else if (_type == 2) {
        	$("#autoCompleteContainer #results").hide();
            $("#autoCompleteContainer #resultsCampaigns").show();
            $("#autoCompleteContainer h3").show();
        }
        else {
            $("#autoCompleteContainer #results").show();
            $("#autoCompleteContainer #resultsCampaigns").show();
            $("#autoCompleteContainer h3").show();
        }
        $("#autoCompleteContainer").show();
    }
    $.fn.AutoComplete.myJson = null;
    $.fn.AutoComplete.myJsonCampaingnsLength = 0;
    $.fn.AutoComplete.myJsonReturnsLength = 0;
    $.fn.AutoComplete.myJsonCampaingns = null;
    $.fn.AutoComplete.myJsonReturns = null;
    $.fn.AutoComplete.myObject = null;
    $.fn.AutoComplete.activeNode = 0;
    $.fn.AutoComplete.controlNumber = 0;
    $.fn.AutoComplete.searchBox = null;
	$.fn.AutoComplete.campaignHeight = 0;
	$.fn.AutoComplete.resultsHeight = 0;
	$.fn.AutoComplete.duration = 250;

})(jQuery);


