/**
 * X-Team Custom Form Controls, v1.0
 *
 * http://x-team.com/
 * Copyright (c) 2010 X-Team
 */

if(typeof Xteam==='undefined'){Xteam={}}Xteam.keyCode=function(e){var code=e.keyCode?e.keyCode:e.which?e.which:false;return code};if(typeof Xteam.Ui==='undefined'){Xteam.Ui={}}if(typeof Xteam.Ui.Form==='undefined'){Xteam.Ui.Form={}}Xteam.Ui.Form.Checkbox=function Checkbox(element){element=$(element);if(element.data('customStyle')==true){return}if(this.constructor===window.constructor){return new arguments.callee(element)}if(typeof element!=='undefined'){this.basename='checkbox';this.elements=element;this.links=this.elements.map(this.styleElement.bind(this))}};Xteam.Ui.Form.Checkbox.prototype.styleElement=function styleElement(index,element){element=$(element);element.data('customStyle',true);var link=$('<a />').attr({href:'#',className:this.basename+(element.checked?' '+this.basename+'-checked':'')});link.insertBefore(element.hide());var clickedBound=this.clicked.bindAsEventListener(this);element.bind('change',clickedBound);element.closest('label').add($('label[for="'+element.attr('id')+'"]')).bind('click',function(){element.trigger('change')});link.bind('click keypress',clickedBound);return link.get(0)};Xteam.Ui.Form.Checkbox.prototype.check=function check(link,i){link=$(link);this.elements[i].checked=true;link.addClass(this.basename+'-checked')};Xteam.Ui.Form.Checkbox.prototype.uncheck=function uncheck(link,i){link=$(link);this.elements[i].checked=false;link.removeClass(this.basename+'-checked')};Xteam.Ui.Form.Checkbox.prototype.clicked=function clicked(e){if(e.type==='keypress'){if(Xteam.keyCode(e)!==32){return}e.preventDefault();e.stopPropagation()}var element=e.target,link=this.links[0];link.focus();if(element.nodeName.toLowerCase()==='a'){e.preventDefault();e.stopPropagation();if(this.elements[0].checked){this.uncheck(link,0)}else{this.check(link,0)}this.elements.eq(0).trigger('change')}else{if(this.elements[0].checked){this.check(link,0)}else{this.uncheck(link,0)}}};Xteam.Ui.Form.Radio=function Radio(element){element=$(element);if(element.data('customStyle')==true){return}if(this.constructor===window.constructor){return new arguments.callee(element)}this.inherits(Xteam.Ui.Form.Checkbox);this.basename='radio';this.elements=$('input[name="'+element.attr('name')+'"]');this.links=this.elements.map(this.styleElement.bind(this))};Xteam.Ui.Form.Radio.inherits(Xteam.Ui.Form.Checkbox);Xteam.Ui.Form.Radio.prototype.clicked=function clicked(e){if(e.type==='keypress'){if(Xteam.keyCode(e)!==32){return}e.preventDefault();e.stopPropagation()}var element=e.target,link=element.nodeName.toLowerCase()==='a'?element:this.links[this.elements.index(element)];if(element.nodeName.toLowerCase()==='a'&&e.type==='click'){e.preventDefault();e.stopPropagation()}link.focus();var self=this;this.links.each(function(index,element){self.uncheck(element,index)});this.check(link,this.links.index(link))};Xteam.Ui.Form.Placeholder=function(element){if(element&&Xteam.Ui.Form.Placeholder.nativeSupport(element)){return}if(this.constructor===window.constructor){return new arguments.callee(element)}this.element=$(element);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()}};Xteam.Ui.Form.Placeholder.nativeSupport=function nativeSupport(element){var type=$(element).get(0).nodeName.toLowerCase(),e=document.createElement(type);return'placeholder'in e};Xteam.Ui.Form.Placeholder.prototype.events=function events(){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))};Xteam.Ui.Form.Placeholder.prototype.fixPasswordField=function fixPasswordField(){this._element=this.element;this.otherField=$('<input />').attr('type','text').addClass(this.element.attr('class')).bind('focus',this.hide.bindAsEventListener(this));$('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};Xteam.Ui.Form.Placeholder.prototype.show=function show(){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','GrayText');this.active=true}};Xteam.Ui.Form.Placeholder.prototype.hide=function hide(){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.element.show().focus()}}this.element.val('');var toFocus=false;this.element.css('color','');this.active=false}};Xteam.Ui.Form.Placeholder.prototype.focus=function focus(){this.element.focus()};Xteam.Ui.Form.Placeholder.prototype.reset=function reset(){window.setTimeout(this.show.bind(this),20)};
