/*

Uniform v1.8.0+f
Copyright © 2009 Josh Pyles / Pixelmatrix Design LLC
http://pixelmatrixdesign.com

Requires jQuery 1.3 or newer

Much thanks to Thomas Reynolds and Buck Wilson for their help and advice on
this.

Disabling text selection is made possible by Mathias Bynens
<http://mathiasbynens.be/> and his noSelect plugin.
<http://github.com/mathiasbynens/noSelect-jQuery-Plugin>.

Also, thanks to David Kaneda and Eugene Bond for their contributions to the
plugin.

This version (+f) is from fidian's forked repository until the changes can
get merged upstream.  See https://github.com/fidian/uniform

License:
MIT License - http://www.opensource.org/licenses/mit-license.php

Enjoy!

*/
/*global jQuery, window, document*/
(function(e,t){"use strict";function n(t){if(!t){return""}return e("<span />").text(t).html()}function r(e,t){var n,r;for(n in t){if(t.hasOwnProperty(n)){r=n.replace(/ |$/g,".uniform");e.bind(n,t[n])}}}function i(e){var n;if(e[0].multiple){return true}n=e.attr("size");if(n===t||n<=1){return false}return true}function s(e,t,n){var r=e.val();if(r===""){r=n.fileDefaultHtml}else{r=r.split(/[\/\\]+/);r=r[r.length-1]}t.text(r)}function o(e,t){e.removeClass(t.hoverClass+" "+t.focusClass+" "+t.activeClass)}function u(e,t,n){if(n){e.addClass(t)}else{e.removeClass(t)}}function a(e,t,n){var r=t.is(":checked");if(t.prop){t.prop("checked",r)}else{if(r){t.attr("checked","checked")}else{t.removeAttr("checked")}}u(e,n.checkedClass,r)}function f(e,t,n){u(e,n.disabledClass,t.is(":disabled"))}function l(e,t,n){switch(n){case"after":e.after(t);return e.next();case"before":e.before(t);return e.prev();case"wrap":e.wrap(t);return e.parent()}return null}function c(t,n,r){var i,s;if(!r){r={}}r=e.extend({bind:{},css:null,divClass:null,divWrap:"wrap",spanClass:null,spanHtml:null,spanWrap:"wrap"},r);i=e("<div />");s=e("<span />");if(n.autoHide&&!t.is(":visible")){i.hide()}if(r.divClass){i.addClass(r.divClass)}if(r.spanClass){s.addClass(r.spanClass)}if(n.useID&&t.attr("id")){i.attr("id",n.idPrefix+"-"+t.attr("id"))}if(r.spanHtml){s.html(r.spanHtml)}i=l(t,i,r.divWrap);s=l(t,s,r.spanWrap);if(r.css){t.css(r.css)}f(i,t,n);return{div:i,span:s}}e.uniform={defaults:{activeClass:"active",autoHide:true,buttonClass:"button",checkboxClass:"checker",checkedClass:"checked",disabledClass:"disabled",fileButtonClass:"action",fileButtonHtml:"Choose File",fileClass:"uploader",fileDefaultHtml:"No file selected",filenameClass:"filename",focusClass:"focus",hoverClass:"hover",idPrefix:"uniform",radioClass:"radiod",resetDefaultHtml:"Reset",resetSelector:false,selectAutoWidth:false,selectClass:"selector",submitDefaultHtml:"Submit",useID:true},elements:[]};var h=true,p=[{match:function(e){return e.is("button, :submit, :reset, a, input[type='button']")},apply:function(t,i){var s,u,a;u=i.submitDefaultHtml;if(t.is(":reset")){u=i.resetDefaultHtml}if(t.is("a, button")){u=t.html()||u}else if(t.is(":submit, :reset, input[type=button]")){u=n(t.attr("value"))||u}a=c(t,i,{css:{display:"none"},divClass:i.buttonClass,spanHtml:u});s=a.div;r(s,{mouseenter:function(){s.addClass(i.hoverClass)},mouseleave:function(){s.removeClass(i.hoverClass);s.removeClass(i.activeClass)},"mousedown touchbegin":function(){s.addClass(i.activeClass)},"mouseup touchend":function(){s.removeClass(i.activeClass)},"click touchend":function(n){var r;if(e(n.target).is("span, div")){if(t[0].dispatchEvent){r=document.createEvent("MouseEvents");r.initEvent("click",true,true);t[0].dispatchEvent(r)}else{t.click()}}}});r(t,{focus:function(){s.addClass(i.focusClass)},blur:function(){s.removeClass(i.focusClass)}});e.uniform.noSelect(s);return{remove:function(){return t.unwrap().unwrap()},update:function(){o(s,i);f(s,t,i)}}}},{match:function(e){return e.is(":checkbox")},apply:function(e,t){var n,i,s;n=c(e,t,{css:{opacity:1},divClass:t.checkboxClass});i=n.div;s=n.span;r(e,{focus:function(){i.addClass(t.focusClass)},blur:function(){i.removeClass(t.focusClass)},"click touchend":function(){a(s,e,t)},"mousedown touchbegin":function(){i.addClass(t.activeClass)},"mouseup touchend":function(){i.removeClass(t.activeClass)},mouseenter:function(){i.addClass(t.hoverClass)},mouseleave:function(){i.removeClass(t.hoverClass);i.removeClass(t.activeClass)}});a(s,e,t);return{remove:function(){return e.unwrap().unwrap()},update:function(){o(i,t);s.removeClass(t.checkedClass);a(s,e,t);f(i,t)}}}},{match:function(e){return e.is(":file")},apply:function(t,n){function p(){s(t,a,n)}var i,u,a,h;i=c(t,n,{css:{opacity:0},divClass:n.fileClass,spanClass:n.fileButtonClass,spanHtml:n.fileButtonHtml,spanWrap:"after"});u=i.div;h=i.span;a=e("<span />").html(n.fileDefaultHtml);a.addClass(n.filenameClass);a=l(t,a,"after");if(!t.attr("size")){t.attr("size",u.width()/10)}p();r(t,{focus:function(){u.addClass(n.focusClass)},blur:function(){u.removeClass(n.focusClass)},mousedown:function(){if(!t.is(":disabled")){u.addClass(n.activeClass)}},mouseup:function(){u.removeClass(n.activeClass)},mouseenter:function(){u.addClass(n.hoverClass)},mouseleave:function(){u.removeClass(n.hoverClass);u.removeClass(n.activeClass)}});if(e.browser.msie){r(t,{click:function(){t.trigger("change");setTimeout(p,0)}})}else{r(t,{change:p})}e.uniform.noSelect(a);e.uniform.noSelect(h);return{remove:function(){t.siblings("span").remove();t.unwrap();return t},update:function(){o(u,n);s(t,a,n);f(u,t,n)}}}},{match:function(e){if(e.is("input")){var t=e.attr("type").toLowerCase(),n=" color date datetime datetime-local email month number password search tel text time url week ";return n.indexOf(" "+t+" ")>=0}return false},apply:function(e){var t=e.attr("type");e.addClass(t);return{remove:function(){e.removeClass(t)},update:function(){}}}},{match:function(e){return e.is(":radio")},apply:function(t,n){var i,s,u;i=c(t,n,{css:{opacity:0},divClass:n.radioClass});s=i.div;u=i.span;r(t,{focus:function(){s.addClass(n.focusClass)},blur:function(){s.removeClass(n.focusClass)},"click touchend":function(){var r=n.radioClass.split(" ")[0],i="."+r+" span."+n.checkedClass+":has([name='"+t.attr("name")+"'])";e(i).each(function(){var t=e(this),r=t.find(":radio");a(t,r,n)});a(u,t,n)},"mousedown touchend":function(){if(!t.is(":disabled")){s.addClass(n.activeClass)}},"mouseup touchbegin":function(){s.removeClass(n.activeClass)},"mouseenter touchend":function(){s.addClass(n.hoverClass)},mouseleave:function(){s.removeClass(n.hoverClass);s.removeClass(n.activeClass)}});a(u,t,n);return{remove:function(){return t.unwrap().unwrap()},update:function(){o(s,n);a(u,t,n);f(s,t,n)}}}},{match:function(e){var t;if(e.is("select")&&!i(e)){return true}return false},apply:function(t,n){var i,s,u,a,l;a=t.width();i=c(t,n,{css:{opacity:0,left:"2px",width:a+32+"px"},divClass:n.selectClass,spanHtml:(t.find(":selected:first")||t.find("option:first")).html(),spanWrap:"before"});s=i.div;u=i.span;if(n.selectAutoWidth){s.width(e("<div />").width()-e("<span />").width()+a+25);l=parseInt(s.css("paddingLeft"),10);u.width(a-l-15);t.width(a+l);t.css("min-width",a+l+"px");s.width(a+l)}else{l=t.width();s.width(l);u.width(l-25)}r(t,{change:function(){u.html(t.find(":selected").html());s.removeClass(n.activeClass)},focus:function(){s.addClass(n.focusClass)},blur:function(){s.removeClass(n.focusClass);s.removeClass(n.activeClass)},"mousedown touchbegin":function(){s.addClass(n.activeClass)},"mouseup touchend":function(){s.removeClass(n.activeClass)},"click touchend":function(){var e=t.find(":selected").html();if(u.html()!==e){t.trigger("change")}},mouseenter:function(){s.addClass(n.hoverClass)},mouseleave:function(){s.removeClass(n.hoverClass);s.removeClass(n.activeClass)},keyup:function(){u.html(t.find(":selected").html())}});e.uniform.noSelect(u);return{remove:function(){t.siblings("span").remove();t.unwrap();return t},update:function(){o(s,n);u.html(t.find(":selected").html());f(s,t,n)}}}},{match:function(e){var t;if(e.is("select")&&i(e)){return true}return false},apply:function(e){e.addClass("uniform-multiselect");return{remove:function(){e.removeClass("uniform-multiselect")},update:function(){}}}},{match:function(e){return e.is("textarea")},apply:function(e){e.addClass("uniform");return{remove:function(){e.removeClass("uniform")},update:function(){}}}}];if(e.browser.msie&&e.browser.version<7){h=false}e.fn.uniform=function(t){var n=this;t=e.extend({},e.uniform.defaults,t);if(t.resetSelector!==false){e(t.resetSelector).mouseup(function(){window.setTimeout(function(){e.uniform.update(n)},10)})}return this.each(function(){var n=e(this),r,i,s;if(n.data("uniformed")){e.uniform.update(n)}if(n.data("uniformed")||!h){return}for(r=0;r<p.length;r=r+1){i=p[r];if(i.match(n,t)){s=i.apply(n,t);n.data("uniformed",s);e.uniform.elements.push(n.get(0));return}}})};e.uniform.restore=function(n){if(n===t){n=e.uniform.elements}var r=e(n);r.each(function(){var t=e(this),n,r;r=t.data("uniformed");if(!r){return}r.remove();t.unbind(".uniform");n=e.inArray(this,e.uniform.elements);if(n>=0){e.uniform.elements.splice(n,1)}t.removeData("uniformed")})};e.uniform.noSelect=function(t){var n=function(){return false};e(t).each(function(){this.onselectstart=this.ondragstart=n;e(this).mousedown(n).css({MozUserSelect:"none"})})};e.uniform.update=function(n){if(n===t){n=e.uniform.elements}var r=e(n);r.each(function(){var t=e(this),n;n=t.data("uniformed");if(!n){return}n.update(t,n.options)})}})(jQuery)