/*
 *  @name           Trapeze Style Form
 *
 *  @description    Wrap form elements with an HTML structure that allows easier and rich styles to be applied to the elements
 *
 *  @author         Marcos Abreu
 *
 *  @requires       Trapeze jQuery Distribution - based on jQuery 1.4.1
 *  @requires       Trapeze Core Utilities
 *
 */

$.namespace("trapeze.StyleForm");
trapeze.StyleForm=$.Class.extend({conf:null,forms:null,tpl_wrapper:'<%(wrap_tag) class="%(wrap_class)"></%(wrap_tag)>',tpl_corner:'<div class="%(tag_class)" %(add_attr)><!-- --\></div>',tpl_fake_select:'<div class="fake-select"><div class="%(select_fake_class)">%(selected_option)</div></div>',tpl_fake_checkradio:'<span class="%(fake_class)"><!-- --\></span>',renderCorner:function(a,b,c,d){if($.browser.msie&&parseInt($.browser.version)<=7)b=$.browser.version==6&&d?b-1:b+1;return trapeze.render_template(this.tpl_corner,
{tag_class:a,add_attr:'style="top:'+b+"px;left:"+c+'px;"'})},styleTextField:function(a){if($(a).is(":text")||$(a).is(":password"))var b=this.conf.textfield_corner_width,c=this.conf.classes.textfield_wrapper;else{b=this.conf.textarea_corner_width;c=this.conf.classes.textarea_wrapper}var d=c==this.conf.classes.textarea_wrapper;$(a).wrap(trapeze.render_template(this.tpl_wrapper,{wrap_tag:"div",wrap_class:c})).before(this.renderCorner(this.conf.classes.textfield_top_left,0,0,d)).before(this.renderCorner(this.conf.classes.textfield_top_right,
0,$(a).outerWidth()-b,d)).after(this.renderCorner(this.conf.classes.textfield_bottom_right,$(a).outerHeight()-b,$(a).outerWidth()-b,d)).after(this.renderCorner(this.conf.classes.textfield_bottom_left,$(a).outerHeight()-b,0,d))},styleCheckRadio:function(a){var b=$(a).attr("type")+"-"+this.conf.classes.checkradio_wrapper,c=this.conf.classes.checkradio_fake+"-"+$(a).attr("type"),d=$(a).is(":checkbox")?this.conf.checkbox_height:this.conf.radio_height;b=$(a).wrap(trapeze.render_template(this.tpl_wrapper,
{wrap_tag:"span",wrap_class:b})).before(trapeze.render_template(this.tpl_fake_checkradio,{fake_class:c})).hide().parent().find("."+c);a="-"+d*($(a).attr("checked")?2:0)+"px";b.bind("mousedown",$.proxy(this.checkRadioPush,this)).bind("mouseup",$.proxy(this.checkRadioPush,this));b.css("background-position-x")==""||b.css("background-position-x")===undefined?b.css("backgroundPosition",b.css("backgroundPosition").split(" ")[0]+" "+a):b.css("background-position-y",a)},checkRadioPush:function(a){var b=$(a.currentTarget),
c=b.next("input"),d=this.conf.classes.checkradio_fake+"-"+c.attr("type");if(a.type=="mouseup")if(c.is(":checkbox"))c.attr("checked",c.attr("checked")?false:true);else{c.parents("form").find('input:radio[name="'+c.attr("name")+'"]').each(function(){var e=$(this).attr("checked",false).parent().find("."+d);e.css("background-position-x")==""||e.css("background-position-x")===undefined?e.css("backgroundPosition",e.css("backgroundPosition").split(" ")[0]+" 0px"):e.css("background-position-y","0px")});c.attr("checked",
true)}var f=c.is(":checkbox")?this.conf.checkbox_height:this.conf.radio_height;f*=a.type=="mouseup"?c.attr("checked")?2:0:c.attr("checked")?3:1;f="-"+(f+1).toString()+"px";b.css("background-position-x")==""||b.css("background-position-x")===undefined?b.css("backgroundPosition",b.css("backgroundPosition").split(" ")[0]+" "+f):b.css("background-position-y",f);a.type=="mouseup"&&c.trigger("change")},styleSelect:function(a){var b=this.conf.classes.select_wrapper;if($(a).is("[class*='size-']"))b+=" "+
b+"-"+/^size-\d{1,2}/i.exec($(a).attr("class"))[0];$(a).wrap(trapeze.render_template(this.tpl_wrapper,{wrap_tag:"div",wrap_class:b})).before(trapeze.render_template(this.tpl_fake_select,{select_fake_class:this.conf.classes.select_fake,selected_option:$(a).find("option").eq(a.selectedIndex).text()})).css("height",$(a).parent().find("."+this.conf.classes.select_fake).outerHeight()+"px").fadeTo(0,0).bind("change",$.proxy(this.selectClick,this)).parent().css("height",$(a).height()+"px").find(".fake-select").prepend(trapeze.render_template(this.tpl_corner,
{tag_class:this.conf.classes.select_left,add_attr:""})).append(trapeze.render_template(this.tpl_corner,{tag_class:this.conf.classes.select_right,add_attr:""}));b=$(a).outerWidth()-this.conf.select_toggle_width;b=$(a).parent().find("."+this.conf.classes.select_fake).css("width",b+"px");b=$(a).parent().find("."+this.conf.classes.select_left).width()+$(a).parent().find("."+this.conf.classes.select_right).width()+b.outerWidth();$(a).css("width",b+"px")},selectClick:function(a){$(a.currentTarget).parent().find("."+
this.conf.classes.select_fake).text($(a.currentTarget).find("option").eq(a.currentTarget.selectedIndex).text())},styleForms:function(){this.conf.textfield&&this.forms.find("input:text").not(this.forms.find("."+this.conf.classes.text_wrapper+" input:text")).each($.proxy(function(a,b){this.styleTextField(b)},this));this.conf.password&&this.forms.find("input:password").not(this.forms.find("."+this.conf.classes.text_wrapper+" input:password")).each($.proxy(function(a,b){this.styleTextField(b)},this));
this.conf.textarea&&this.forms.find("textarea").not(this.forms.find("."+this.conf.classes.textarea_wrapper+" textarea")).each($.proxy(function(a,b){this.styleTextField(b)},this));this.conf.checkbox&&this.forms.find("input:checkbox").not(this.forms.find(".checkbox-"+this.conf.classes.checkradio_wrapper+" input:checkbox")).each($.proxy(function(a,b){this.styleCheckRadio(b)},this));this.conf.radio&&this.forms.find("input:radio").not(this.forms.find(".radio-"+this.conf.classes.checkradio_wrapper+" input:radio")).each($.proxy(function(a,
b){this.styleCheckRadio(b)},this));if(this.conf.select)if(!$.browser.msie||$.browser.msie&&parseInt($.browser.version)>6)this.forms.find("select").not(this.forms.find("."+this.conf.classes.select_wrapper+" select")).each($.proxy(function(a,b){this.styleSelect(b)},this));this.conf.onComplete()},init:function(a){this.conf=$.extend({},{classes:{textfield_top_left:"textfield-tl",textfield_top_right:"textfield-tr",textfield_bottom_left:"textfield-bl",textfield_bottom_right:"textfield-br",textfield_wrapper:"input-text-wrapper",
textarea_wrapper:"textarea-wrapper",select_wrapper:"select-wrapper",select_fake:"select-active-item",select_left:"fake-select-left",select_right:"fake-select-right",checkradio_wrapper:"wrapper",checkradio_fake:"fake"},textfield:true,password:true,textarea:true,select:true,checkbox:true,radio:true,image:false,submit:false,checkbox_height:19,radio_height:18,textfield_corner_width:6,textarea_corner_width:6,select_toggle_width:18,onComplete:function(){},selector:null},a);this.forms=$(this.conf.selector);
this.styleForms()}});

