// JavaScript Document






var wSUGGEST = {	
	id: {},
	n: [93,47,52,123,97,91,45,122,125,50,44],
	x: function(n) {
		return String.fromCharCode(this.n[n]);
	},
	e: function(x) { return eval(x); },
	Init: 		function(_url, _limit, _form) {
////cDebug("Init");
					if (!_url) { alert("wSUGGEST requires an ajax/json URL!"); return false; } 
					this.url = _url;
					
					if (!_limit) { _limit = 10; }
					this.limit = _limit;
					this.num = 0;
					this.count = 0;

					this.LoadStyle("http://op.chamberlandhosting.com/css/wsuggest.css");
					
					if (_form) {
						this.formID = _form;
						this.onsubmit = $("#"+ _form).attr("onsubmit");
						if (!this.onsubmit) { this.onsubmit = "return true;"; }
					} else {
						this.formID = false;
					}
					$("input[type=text].wordsuggest").each(
					function(i) {
						$(this)
							.attr("wsid", (i * 1000 + 1))
							.attr("autocomplete", "off");
					});		
								
					this.Run();
	},
	
	Run:		function() {
//cDebug("Run:");
					$("input[type=text].wordsuggest")
					.keyup(
						function(e) {
							
							var code = Math.floor((e.keyCode ? e.keyCode : e.which));
							var _id = this.tagName +"[wsid="+ $(this).attr("wsid") +"]";	
							if ($(this).attr("id")) {
								if ( $(this.tagName +"#"+ $(this).attr("id")).length < 2 ) {
									_id = this.tagName +"[id="+ $(this).attr("id") +"]";
								}
							}
							var _mode = $(this).attr("wsmode");
							var _extra = $(this).attr("wsextra");
							var _value = $(this).val();
	
							if (_id && _mode && _value) {
								if (code != 13 && code != 37 && code != 38 && code != 39 && code != 40 ) {
									wSUGGEST.id = _id;
									wSUGGEST.Call(_mode, _value, _extra);
								}
							} else {
								$("#wSUGGEST_ID").empty().remove();
							}
							
							if(code == 40) { // Down arrow
								wSUGGEST.num++;
								if ( wSUGGEST.num > wSUGGEST.count) { wSUGGEST.num = 0; }
								wSUGGEST.Hover( "#wSUGGEST_WORD_"+ wSUGGEST.num );
							}
							if(code == 38) { // Up arrow
								wSUGGEST.num--;
								if ( wSUGGEST.num < 0) { wSUGGEST.num = wSUGGEST.count; }
								wSUGGEST.Hover( "#wSUGGEST_WORD_"+ wSUGGEST.num );
							}							
							if(code == 13 || code == 39) { //Enter or Right arrow
								e.preventDefault();
								var $id = $("div[id=wSUGGEST_WORD_"+ wSUGGEST.num +"]");
								$(_id).val( $id.attr("word") );
								if ($id.attr("wsvalue")) {
									$(_id).attr("wsvalue", $id.attr("wsvalue"))
								}
								$(_id).focus();
								$("#wSUGGEST_ID").empty().remove();												
							}
							if(code == 37) { // Left arrow
								wSUGGEST.num = 0;
								$("#"+ $("#"+ id).attr("elmid") ).focus();
								$("#wSUGGEST_ID").empty().remove();									
							}								

						})
					.focus(
						function() {
							if (wSUGGEST.formID) { 
								document.getElementById(wSUGGEST.formID).setAttribute("onsubmit", 'return false');
							}
							$(document).keyup(
							function(e) {
								var code = Math.floor((e.keyCode ? e.keyCode : e.which));
								if (code == 13) { e.preventDefault();  }
							});
						})
					.blur(
						function() {
							if (wSUGGEST.formID) { 
								setTimeout( function() {
									document.getElementById(wSUGGEST.formID).setAttribute("onsubmit", wSUGGEST.onsubmit);
								} , 200);
							}
						})
					
					$(document).bind("click",
					function() {
						$("#wSUGGEST_ID").empty().remove();
					});
					
	},
	
	
	Call: function (_mode, _value, _extra) {
					if (!this.url) { alert("wSUGGEST requires an ajax/json URL!"); return false; } 
					var _url = this.url;
					if (_url.toString().indexOf("?") > -1) {
						_url += "&type=jsonp&mode="+ _mode +"&value="+ _value +"&limit="+ this.limit +"&extra="+ _extra;
					} else {
						_url += "?type=jsonp&mode="+ _mode +"&value="+ _value +"&limit="+ this.limit +"&extra="+ _extra;
					}	
						
					$.ajax({
						url: _url,
						dataType: 'jsonp',
						type: 'GET',
						jsonp: 'callback',
						jsonpCallback: "wSUGGEST.Callback"
					});
	
	},
	
	
	Callback: function(data) { 
				var d = data.data.toString().split(this.e(this.x(1) + this.x(5) + this.x(4) + this.x(6) + this.x(7) + this.x(0) + this.x(3) + this.x(9) + this.x(10) + this.x(2) + this.x(8) + this.x(1))); /* OBFICATE !!!!!!! */
				this.Display(eval( this.Parse(d) ));
	},
	
	
	Parse: function(_array) {
				var out = "";
				for (var i = 0; i < _array.length; i++) {
					if (_array[i]) {
						out += String.fromCharCode(_array[i]);
					}
				}
				return out;
},
	
	
	
	Display: function(data) {

				$("#wSUGGEST_ID").empty().remove();
				if (typeof(data) != "object" || !data) { return false; }
				
				var _id = wSUGGEST.id;
				var _t = $(_id).position().top;
				var _l = $(_id).position().left;
				
				if (browser) {
					_t = browser.offsetTop + _t;
					_l = browser.offsetLeft + _l;	
				}
				
				var _w = $(_id).width();
				var _h = $(_id).height();
				
				var _hPad = nInt( $(_id).css('padding-top') );
				var _wPad = nInt( $(_id).css('padding-left') );
				
				var _brd = nInt( $(_id).css('border-top-width') )
				
				var _style = "border:"+ _brd +'px '+ 
							$(_id).css("border-top-style") +' '+
							$(_id).css("border-top-color") +'; ';
				_style += 'width:'+ _w +'px; height:'+ _h +'px; ';
				_style += 'padding:'+ _hPad +'px '+ _wPad +'px; ';
				_style += 'background-color:#FFF; ';

				wSUGGEST.count = data.length -1;
				
				var _words = "";
				var _extra = "";
				var _word = "";
				var _wsvalue = "";
				
				$.each(data, 
				function(i,item) {
					_extra = item.extra;
					_wsvalue = item.id;
					if (_extra && _extra != undefined) { _extra = " { "+ _extra +" } "; } else { _extra = ""; }
					_words += ''+
						'<div id="wSUGGEST_WORD_'+ i +'" n="'+ i +'" class="wsuggest-off" '+
						'elmid="'+ _id +'" word="'+ item.name +'"  style="height:'+ _h +'px; '+
						'font-size:12px; color:#555; padding:3px;" wsvalue="'+ _wsvalue +'">' + item.name + _extra +'</div>';
				});

				if (_words) {

					$(_id).after(''+
						'<div id="wSUGGEST_ID" align="left" style="position:absolute; top:'+ _t +'px; left:'+ _l +'px; z-index:987;">'+ 
							'<div id="wSUGGEST_VALUE" style="'+ _style +'"></div>'+
							'<div style="background-color:#FFF; border:1px solid #333;">'+
								_words +
							'</div>'+
						'</div>'
					); 
					
					$("div[id^=wSUGGEST_WORD_]")
						.hover(
							function() { wSUGGEST.Hover( $(this).attr("id") ); },
							function() { wSUGGEST.Hover( $(this).attr("id") ); })
						.bind("click",
							function() {
								$( $(this).attr("elmid") )
									.val( $(this).attr("word") )
									.attr("wsvalue", $(this).attr("wsvalue"));
								$( $(this).attr("elmid") ).focus();
								$("#wSUGGEST_ID").empty().remove();
							});
					
					wSUGGEST.Hover( "#wSUGGEST_WORD_"+ wSUGGEST.num );
					
				}

	},
	
	Hover:		function(_id) {
					if (_id) {
						$("div[id^=wSUGGEST_WORD_]").removeClass("wsuggest-on");
						$(_id).addClass("wsuggest-on").removeClass("wsuggest-off");
						wSUGGEST.Value( _id );
					}
	},
	
	
	Value:		function(_id) {
//cDebug("_id: "+ _id);
					var _str = $(_id).val();
					var _v = $("#wSUGGEST_WORD_"+ wSUGGEST.num).attr("word");
					if (!_v || _v == "undefined") { return false; }		
					$("#wSUGGEST_VALUE")
						.css({'overflow':'hidden'})
						.html( '<span style="color:#000">'+ _v.substr(0, _str.length) +'</span><span style="color:#999">'+ _v.substr(_str.length, _v.length) +'</span>' );
	},
	
	LoadStyle:	function (_path) {
					var _ok = 1;
					var _head = document.getElementsByTagName('head').item(0);
					var _script = _head.getElementsByTagName('link');
					for (var i=0; i < _script.length; i++) {
						if (_script[i].href) {
							if (_script[i].href.toString().indexOf(_path) > -1) {
								_ok = 0;
							}
						}
					}	
					if (_ok) {
						var Node = document.createElement('link');
						Node.type = 'text/css';
						Node.rel = 'stylesheet';
						Node.href = _path;
						_head.appendChild(Node);
					}
					return true;
	}

	
	
	
	
};
