/*
 * The MIT License
 *
 * Copyright (c) 2012 James Allardice
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

timely.define([],function(){function e(e,t,n){if(e.addEventListener)return e.addEventListener(t,n,!1);if(e.attachEvent)return e.attachEvent("on"+t,n)}function t(e,t){var n,r;for(n=0,r=e.length;n<r;n++)if(e[n]===t)return!0;return!1}function n(e,t){var n;e.createTextRange?(n=e.createTextRange(),n.move("character",t),n.select()):e.selectionStart&&(e.focus(),e.setSelectionRange(t,t))}function r(e,t){try{return e.type=t,!0}catch(n){return!1}}function P(e){var t;return e.value===e.getAttribute(h)&&e.getAttribute(p)==="true"?(e.setAttribute(p,"false"),e.value="",e.className=e.className.replace(f,""),t=e.getAttribute(d),t&&(e.type=t),!0):!1}function H(e){var t;return e.value===""?(e.setAttribute(p,"true"),e.value=e.getAttribute(h),e.className+=" "+a,t=e.getAttribute(d),t?e.type="text":e.type==="password"&&S.changeType(e,"text")&&e.setAttribute(d,"password"),!0):!1}function B(e,t){var n,r,i,s,o;if(e&&e.getAttribute(h))t(e);else{n=e?e.getElementsByTagName("input"):n,r=e?e.getElementsByTagName("textarea"):r;for(o=0,s=n.length+r.length;o<s;o++)i=o<n.length?n[o]:r[o-n.length],t(i)}}function j(e){B(e,P)}function F(e){B(e,H)}function I(e){return function(){x&&e.value===e.getAttribute(h)&&e.getAttribute(p)==="true"?S.moveCaret(e,0):P(e)}}function q(e){return function(){H(e)}}function R(e){return function(t){N=e.value;if(e.getAttribute(p)==="true")return N!==e.getAttribute(h)||!S.inArray(o,t.keyCode)}}function U(e){return function(){var t;e.getAttribute(p)==="true"&&e.value!==N&&(e.className=e.className.replace(f,""),e.value=e.value.replace(e.getAttribute(h),""),e.setAttribute(p,!1),t=e.getAttribute(d),t&&(e.type=t)),e.value===""&&(e.blur(),S.moveCaret(e,0))}}function z(e){return function(){e===document.activeElement&&e.value===e.getAttribute(h)&&e.getAttribute(p)==="true"&&S.moveCaret(e,0)}}function W(e){return function(){j(e)}}function X(e){e.form&&(O=e.form,O.getAttribute(v)||(S.addEventListener(O,"submit",W(O)),O.setAttribute(v,"true"))),S.addEventListener(e,"focus",I(e)),S.addEventListener(e,"blur",q(e)),x&&(S.addEventListener(e,"keydown",R(e)),S.addEventListener(e,"keyup",U(e)),S.addEventListener(e,"click",z(e))),e.setAttribute(m,"true"),e.setAttribute(h,L),H(e)}var i={Utils:{addEventListener:e,inArray:t,moveCaret:n,changeType:r}},s=["text","search","url","tel","email","password","number","textarea"],o=[27,33,34,35,36,37,38,39,40,8,46],u="#ccc",a="placeholdersjs",f=new RegExp("\\b"+a+"\\b"),l,c,h="data-placeholder-value",p="data-placeholder-active",d="data-placeholder-type",v="data-placeholder-submit",m="data-placeholder-bound",g="data-placeholder-focus",y="data-placeholder-live",b=document.createElement("input"),w=document.getElementsByTagName("head")[0],E=document.documentElement,S=i.Utils,x,T,N,C,k,L,A,O,M,_,D;if(b.placeholder===void 0){l=document.getElementsByTagName("input"),c=document.getElementsByTagName("textarea"),x=E.getAttribute(g)==="false",T=E.getAttribute(y)!=="false",C=document.createElement("style"),C.type="text/css",k=document.createTextNode("."+a+" { color:"+u+"; }"),C.styleSheet?C.styleSheet.cssText=k.nodeValue:C.appendChild(k),w.insertBefore(C,w.firstChild);for(D=0,_=l.length+c.length;D<_;D++)M=D<l.length?l[D]:c[D-l.length],L=M.getAttribute("placeholder"),L&&S.inArray(s,M.type)&&X(M);A=setInterval(function(){for(D=0,_=l.length+c.length;D<_;D++){M=D<l.length?l[D]:c[D-l.length],L=M.getAttribute("placeholder");if(L&&S.inArray(s,M.type)){M.getAttribute(m)||X(M);if(L!==M.getAttribute(h)||M.type==="password"&&!M.getAttribute(d))M.type==="password"&&!M.getAttribute(d)&&S.changeType(M,"text")&&M.setAttribute(d,"password"),M.value===M.getAttribute(h)&&(M.value=L),M.setAttribute(h,L)}}T||clearInterval(A)},100)}return i.disable=j,i.enable=F,i});