/**
 * JSized HTML5 Placeholder for Legacy Browsers, v1.0
 *
 * @copyright Copyright (c) 2010 JSized, <a href="http://jsized.com/">JSized</a>
 * @author Stephen Reay
 * @version 1.0
 */

if(!Function.prototype.bind){Function.prototype.bind=function bind(context){var callback=this;var args=Array.prototype.slice.call(arguments,0);var object=args.shift();return function(){return callback.apply(object,args.concat(Array.prototype.slice.call(arguments,0)))}}}if(!Function.prototype.bindAsEventListener){Function.prototype.bindAsEventListener=function bindAsEventListener(context){var callback=this;var args=Array.prototype.slice.call(arguments,0);var object=args.shift();return function(event){var localArgs=Array.prototype.slice.call(arguments,0);localArgs.shift();return callback.apply(object,[event||window.event].concat(args,localArgs))}}}if(typeof JSized==='undefined'){var JSized={}}if(typeof JSized.Ui==='undefined'){JSized.Ui={}}if(typeof JSized.Ui.Form==='undefined'){JSized.Ui.Form={}}jQuery.fn.JSizedFormPlaceholder=function(options){return this.each(function(){new JSized.Ui.Form.Placeholder(this,options)})};JSized.Ui.Form.Placeholder=function(element,options){this.element=jQuery(element);if(!this.element.length||this.nativeSupport(this.element)){return}this.options=jQuery.extend(true,{},this.options,options||{});this.placeholderText=this.element.attr('placeholder');this.active=false;this.passwordMode=this.element.get(0).type==='password';this.passwordFieldFixed=false;if(this.placeholderText){this.events();this.show()}};JSized.Ui.Form.Placeholder.prototype.options={'activeClassName':'placeholder-active','color':'GrayText'};JSized.Ui.Form.Placeholder.prototype.nativeSupport=function(element){var type=element.get(0).nodeName.toLowerCase(),e=document.createElement(type);return'placeholder'in e};JSized.Ui.Form.Placeholder.prototype.events=function(){this.element.bind('focus',this.hide.bindAsEventListener(this));this.element.closest('form').bind('submit',this.hide.bindAsEventListener(this));this.element.closest('form').bind('reset',this.reset.bindAsEventListener(this));this.element.bind('blur',this.show.bindAsEventListener(this))};JSized.Ui.Form.Placeholder.prototype.fixPasswordField=function(){this._element=this.element;this.otherField=jQuery('<input />').attr('type','text').addClass(this.element.attr('class')).bind('focus',this.hide.bindAsEventListener(this));jQuery('label[for="'+this.element.attr('id')+'"]').bind('observe','click',this.focus.bind(this));this.element.parent().get(0).insertBefore(this.otherField.get(0),this.element.get(0));this.passwordFieldFixed=true};JSized.Ui.Form.Placeholder.prototype.show=function(){if(this.element.val().length===0){if(this.passwordMode){try{this.element.attr('type','text')}catch(e){if(!this.passwordFieldFixed){this.fixPasswordField()}this.element.hide();this.otherField.show();this.element=this.otherField}}this.element.val(this.placeholderText);this.element.css('color',this.options.color);this.element.addClass(this.options.activeClassName);this.active=true}};JSized.Ui.Form.Placeholder.prototype.hide=function(){if(this.active){if(this.passwordMode){try{this.element.attr('type','password')}catch(e){if(!this.passwordFieldFixed){this.fixPasswordField()}this.otherField.hide();this.element=this._element;this.active=false;this.element.show().focus()}}this.element.val('');var toFocus=false;this.element.css('color','');this.element.removeClass(this.options.activeClassName);this.active=false}};JSized.Ui.Form.Placeholder.prototype.focus=function(){this.element.focus()};JSized.Ui.Form.Placeholder.prototype.reset=function(){window.setTimeout(this.show.bind(this),20)};
