$(document).ready( function(){
	$.extend({
		CcFormHelper:{
			useLabelOnly: function( target ){
				target.useLabelOnly = true;
				$( target ).attr( 'style', 'opacity:0; filter: alpha(opacity=0); -moz-opacity:0; position:absolute; z-index:-99999;' );
				$( target ).click( function(){
					$.CcFormHelper._labelControl( this );
				} );
				$.CcFormHelper._labelControl( target );
			},
			
			useDefault: function( target ){
				target.useDefault = true;
				target.useDefaultValue = target.value;
				$( target ).focus( $.CcFormHelper._elmFocus );
			},
			
			useActive: function( target ){
				target.useActive = true;
				$( target ).focus( $.CcFormHelper._elmFocus );
				$( target ).blur( $.CcFormHelper._elmBlur );
			},
			
			useClear: function( target, user_func ){
				target.useClear = true;
				target.useClearUserFunc = user_func;
				$( target ).click( $.CcFormHelper._elmClick );
			},

			useButtonize: function( target ){
				var href = $( target ).attr( 'href' );
				var btn;
				
				btn = jQuery( '<button />' ).get( 0 );
				$( btn ).html( $( target ).html() ).attr( 'class', $( target ).attr( 'class' ) );
				btn.useButtonize = true;
				
				if( target.tagName == 'EM' ){
					$( btn ).addClass( 'form-button-current' );
					$( btn ).attr( 'disabled', 'disabled' );
					$( btn ).attr( 'readonly', 'readonly' );
				}
				else if( href ){
					$( btn ).addClass( 'form-button' );
					btn.useButtonizeHref = href;
					btn.useButtonizeTarget = $( target ).attr( 'target' );
					$( btn ).click( $.CcFormHelper._elmClick );
				}
				else{
					$( btn ).addClass( 'form-button-disabled' );
					$( btn ).attr( 'disabled', 'disabled' );
					$( btn ).attr( 'readonly', 'readonly' );
				}
				$( target ).replaceWith( $( btn ) );
			},
			
			_labelControl: function( target ){
				$( 'input[name=' + $( target ).attr( 'name' ) + ']' ).each( function(){
					if( this.useLabelOnly ){
						var type = $( this ).attr( 'type' );
						if( this.checked ){
							$('label[for=' + $( this ).attr( 'id' ) + ']' ).each( function(){
								$( this ).addClass( 'form-helper-' + type );
								$( this ).css( 'cursor', 'pointer' );
							});
						}
						else{
							$('label[for=' + $( this ).attr( 'id' ) + ']' ).each( function(){
								$( this ).removeClass( 'form-helper-' + type );
								$( this ).css( 'cursor', 'pointer' );
							});
						}
					}
				} );
			},
			
			_elmFocus: function( e ){
				if( this.useDefault ){
					if( $( this ).attr('class').match(/(^| )form-helper-example($| )/) ){
						$( this ).removeClass( 'form-helper-example' );
						$( this ).attr( 'value', '' );
					}
				}
				if ( this.useActive ) {
					$( this ).addClass( 'form-helper-active' );
				}
			},
			
			_elmBlur: function( e ){
				if ( this.useActive ) {
					$( this ).removeClass( 'form-helper-active' );
				}
			},
			
			_elmClick:function( e ){
				if( this.useClear ){
					e.preventDefault();
					
					$( 'input[type=text],input[type=password]' ).each( function(){
						this.value = '';
					} );
					$( 'textarea' ).each( function(){
						$( this ).text( "" );
					} );
					$( ':checkbox,:radio' ).each( function(){
						this.checked = false;
						if( this.useLabelOnly ){
							$.CcFormHelper._labelControl( this );
						}
					} );
					$( ':radio:first' ).each( function(){
						this.checked = true;
						if( this.useLabelOnly ){
							$.CcFormHelper._labelControl( this );
						}
					} );
					$( 'select' ).each( function(){
						this.selectedIndex = 0;
					} );
					
					if( this.useClearUserFunc ){
						this.useClearUserFunc();
					}
				}
				if( this.useButtonize ){
					e.preventDefault();
					if( this.useButtonizeTarget == '' ){
						window.open( this.useButtonizeHref, '_self' );
					}
					else{
						window.open( this.useButtonizeHref, this.useButtonizeTarget );
					}
				}
			}
		}
	});
} );
