(function($){$.fn.email=function(settings){var defaultSettings={emailTip:"",aEmail:["qq.com","gmail.com","126.com","163.com","hotmail.com","yahoo.com","yahoo.com.cn","live.com","sohu.com","sina.com"],wrapLayer:"body",className:"maillistbox",emailRemember:true,autoCursor:false,position:"bottom"};settings=settings?$.extend(defaultSettings,settings):defaultSettings;return this.each(function(){var elem=$(this),t=0,l=0,w=elem.outerWidth(),h=elem.outerHeight(),selectVal=sMail=inputVal="",arrayNum=0,isIndex=-1;switch(settings.position){case"bottom":t=elem.position().top;l=elem.position().left;break;case"left":t=elem.position().top-h;l=elem.position().left-w;break;case"right":t=elem.position().top-h;l=elem.position().left+w;break;default:t=elem.position().top;l=elem.position().left}var mailWrap=document.createElement("div");$(mailWrap).attr({"id":elem.attr("id"),"class":settings.className});$(settings.wrapLayer).append(mailWrap);if($.trim(elem.val())==""){elem.val(settings.emailTip)}elem.focus(function(){arrayNum=0;if($.trim(elem.val())==settings.emailTip){elem.val("")}if($.trim(elem.val())!=""){inputVal=$.trim(elem.val());isIndex=inputVal.indexOf("@");if(isIndex>=0){sMail=inputVal.substr(isIndex+1);inputVal=inputVal.substring(0,isIndex);if(sMail!=""){arrayNum=parseInt(!position(settings.aEmail,sMail)?0:position(settings.aEmail,sMail))}}if(settings.autoCursor){elem.val(inputVal);if($.browser.msie){setCaretAtEnd(elem.attr("id"))}}showList($(mailWrap),w,h,t,l);createMailList(mailWrap,inputVal,sMail,settings.aEmail,arrayNum)}}).blur(function(){if(elem.val()==""){elem.val(settings.emailTip);hideList($(mailWrap));return false}enterVal(mailWrap,elem);hideList($(mailWrap))});elem.keyup(function(e){var suffixArray=[],eKey=e&&(e.which||e.keyCode);switch(eKey){case 9:return;break;case 13:enterVal(mailWrap,elem);hideList($(mailWrap));break;case 38:showList($(mailWrap),w,h,t,l);cursorMove(mailWrap,-1);break;case 40:showList($(mailWrap),w,h,t,l);cursorMove(mailWrap,+1);break;default:inputVal=$.trim(elem.val());var keyIndex=inputVal.indexOf("@");var suffix="",suffixState=true;if(keyIndex>=0){suffix=inputVal.substr(keyIndex+1);inputVal=inputVal.substring(0,keyIndex);$("#t2").text("BBB"+inputVal);if(suffix!=""&&settings.emailRemember){for(var i=0;i<settings.aEmail.length;i++){if(settings.aEmail[i].indexOf(suffix)==0){suffixArray.push(settings.aEmail[i]);suffixState=false}}}if(suffix!=""&&!settings.emailRemember){for(var i=0;i<settings.aEmail.length;i++){if(settings.aEmail[i].indexOf(suffix)==0){arrayNum=i;suffixState=false;break}}}}suffixArray=suffixArray.length>0?suffixArray:settings.aEmail;if(inputVal==""&&suffix==""){hideList($(mailWrap));arrayNum=0;createMailList(mailWrap,inputVal,suffix,settings.aEmail,arrayNum)}else{showList($(mailWrap),w,h,t,l);createMailList(mailWrap,inputVal,suffix,suffixArray,arrayNum)}}});$(mailWrap).find("li:not('.first')").live("mouseover",function(){$(this).addClass("hover").siblings().removeClass("hover")});$(mailWrap).find("li:not('.first')").live("mousedown",function(){$(this).addClass("current").siblings().removeClass("current");enterVal(mailWrap,elem);hideList($(mailWrap))});$(mailWrap).bind("mouseout",function(){$(mailWrap).find("li:not('.first')").removeClass("hover")})})};function cursorMove(o,n){var cursorList=$(o).find("li:not('.first')"),k=new Number();for(i=0;i<cursorList.length;i++){if(cursorList[i].className=="current"){k=i+n;cursorList[i].className=""}}if(k<0){k=0}if(k>=cursorList.length-1){k=cursorList.length-1}cursorList[k].className="current"}function setCaretAtEnd(field){var b=document.getElementById(field);if(b.createTextRange){var r=b.createTextRange();r.moveStart("character",b.value.length);r.collapse();r.select()}}function position(array,value){for(var i in array){if(array[i]==value){return i;break}}}function enterVal(oWrap,elem){elem.val($(oWrap).find("li.current").text())}function showList(oElem,w,h,t,l){oElem.css({"display":"block","top":h+t,"left":l+2,"width":w-3})}function hideList(oElem){oElem.css({"display":"none"})}function createMailList(oWrap,sVal,suffix,aEail,nK){if(nK<0){nK=0}if(nK>aEail.length-1){nK=aEail.length-1}var mailList="<li class='first'><span>请选择邮箱类型</span></li>";var State=true;for(k=0;k<aEail.length;k++){if(suffix!=""&&aEail[k].indexOf(suffix)==0){State=false}}nK=parseInt(suffix!=""&&State&&!position(aEail,suffix)?0:nK);if(suffix!=""&&State){if(nK==0){mailList+='<li class="current"><span>'+sVal+"</span>@"+suffix+"</li>"}else{mailList+="<li><span>"+sVal+"</span>@"+suffix+"</li>"}}if($.isArray(aEail)){$.each(aEail,function(i){if(State&&suffix!=""){if(i==(nK-1)){mailList+='<li class="current"><span>'+sVal+"</span>@"+aEail[i]+"</li>"}else{mailList+="<li><span>"+sVal+"</span>@"+aEail[i]+"</li>"}}else{if(i==nK){mailList+='<li class="current"><span>'+sVal+"</span>@"+aEail[i]+"</li>"}else{mailList+="<li><span>"+sVal+"</span>@"+aEail[i]+"</li>"}}})}mailList="<ul>"+mailList+"</ul>";$(oWrap).html(mailList)}})(jQuery);