(function($){
	
	/*
	EXTENDING WIDGETS:

	$.widget( "sg.formradio", $.extend({}, $.sg.formcheckbox.prototype, {
		
	}));
	
	$.widget( "sg.formradio", $.sg.formcheckbox, {
	
	});
	*/
	
	var baseClass = "sg-form-widget tmp-ui-form ui-widget ui-helper-clearfix",
		labelClass = "sg-form-label",
		contentClass = "sg-form-content",
		inputClass = "sg-form-input ui-widget-content ui-corner-all",
		inputButtonClass = "sg-form-input sg-form-inputbutton ui-state-default ui-corner-all",
		inputInnerButtonClass = "sg-form-inputbutton ui-state-default ui-corner-right",
		inputInnerWrapperClass = "sg-form-inputwrapper ui-helper-clearfix",
		inputRowClass = "sg-form-inputrow ui-helper-clearfix";
	
	//##########################################################################################################
	//TEXTFIELD
	$.widget( "sg.formtext", {
		options: { 
			label: null,
			widgetClass: 'sg-form-textfield'
		},
	 
		_create: function() {
			var options = this.options;
			if(options.label === null){
				options.label = this.element.find('label').html();
			}
			
			var $inputfield = this.getInputField();
			
			//set widget wrapper ###########################################################################
			this.element.empty();
			this.element.addClass(options.widgetClass);
			this.element.addClass(baseClass);
			
			//set label ###########################################################################
			var $tag_label = $('<div class="'+labelClass+'">'+options.label+'</div>');
			this.element.append($tag_label);
			
			//set content wrapper ###########################################################################
			var $tag_content = $('<div class="'+contentClass+'"></div>');
			this.element.append($tag_content);
			
			//set input ###########################################################################
			var $tag_input = $('<div class="'+inputClass+'"></div>');
			$tag_content.append($tag_input);
			$tag_input.append($inputfield);
			this.editInputfield($inputfield);
			
			//mouse actions ###########################################################################
			$tag_input
				.bind( "click", function() {
					//alert( $(this).html() );
				})
				.bind( "mouseenter", function() {
					$(this).addClass( 'ui-state-hover' );
				})
				.bind( "mouseleave", function() {
					$(this).removeClass( 'ui-state-hover' );
				})
		},
		
		getInputField: function() {
			return this.element.find('input');
		},
		
		editInputfield: function($inputfield){
			
		}
	});
	
	//##########################################################################################################
	//TEXTAREA
	$.widget( "sg.formtextarea", $.sg.formtext, {
		options: { 
			widgetClass: 'sg-form-textarea',
			wysiwyg: true
		},
		
		getInputField: function() {
			return this.element.find('textarea');
		},
		
		editInputfield: function($inputfield){
			if(this.options.wysiwyg){
				/*
				$inputfield.wysiwyg({
					autoSave: true,
					brIE: true,
					removeHeadings: true,
					rmUnusedControls: true,
					css: 'templates/includes/admin_wysiwyg.css',
					rmUnwantedBr: true,
					initialContent: '',
					controls: {
						bold: { visible : true },
						italic: { visible : true },
						indent: { visible : true },
						outdent: { visible : true },
						subscript: { visible : true },
						superscript: { visible : true },
						undo: { visible : true },
						redo: { visible : true },
						createLink: { visible : true },
						insertOrderedList: { visible : true },
						insertUnorderedList: { visible : true },
						h1: { visible : true },
						h2: { visible : true },
						h3: { visible : true },
						cut: { visible : true },
						copy: { visible : true },
						paste: { visible : true },
						html: { visible : true },
						removeFormat: { visible : true },
					}
				});
				*/
				
				
				
				var editor = CKEDITOR.instances[$inputfield.attr('name')];
				if(editor) {
					CKEDITOR.remove(editor);
					editor.destroy(true);
					delete CKEDITOR.instances[$inputfield.attr('name')];
					editor = null;
				}
				/*CKEDITOR.replace(
					$inputfield.attr('name'),
					{
						toolbar : 'Basic'
					}
				);*/
				
				/*CKEDITOR.stylesSet.add('headline_styles', [
					{ name: 'h1 span', element: 'span', styles: {'class': 'h1'} },
					{ name: 'h2 span', element: 'span', attributes: {'class': 'h2'} },
					{ name: 'h3 span', element: 'span', attributes: {'class': 'h2'} }
				]);*/
				
				$inputfield.ckeditor(
					function() {
						
					},
					{
						toolbar: [
							[ 'Link','Unlink','SpecialChar','NumberedList','BulletedList','Source'],
							[ 'Format', 'Styles', 'Bold','Italic','RemoveFormat' ]
						],
						format_tags: 'h1;h2;h3',
						stylesSet: [
							{ name: 'h1 span', element: 'span', attributes: {'class': 'h1'} },
							{ name: 'h2 span', element: 'span', attributes: {'class': 'h2'} },
							{ name: 'h3 span', element: 'span', attributes: {'class': 'h3'} }
						],
						basicEntities: true, //Whether to escape basic HTML entities in the document
						entities: true, //Whether to use HTML entities in the output
						contentsCss: 'includes/CSS/wysiwyg_editor.css', //The CSS file(s) to be used to apply style to editor contents
						enterMode: CKEDITOR.ENTER_BR, //Sets the behavior of the Enter key
						forceEnterMode: true //Force the use of CKEDITOR.config.enterMode as line break regardless of the context
					}
				);
				
				/*
				var editor = CKEDITOR.instances['CONTENT_DATA[txt_shortdesc]'];
				if(editor) {
					//delete CKEDITOR.instances['CONTENT_DATA[txt_shortdesc]'];
					//CKEDITOR.remove(editor);
					editor.destroy(true);
					editor = null;
				}
				CKEDITOR.replace('CONTENT_DATA[txt_shortdesc]');
				*/


				
				/*$inputfield.ready(function(){
					
					alert('ready');
					$inputfield.ckeditor(function(){
						alert('ckeditor callback');
					});
				});*/
			}else{
				$inputfield.height('200px');
			}
			
			
			
			/*$inputfield.getWysiwyg().events.bind("getContent", function (orig) {
				e.preventDefault();
				var content = $.wysiwyg.utils.encodeEntities( $inputfield.wysiwyg('getContent') );
				alert(content);
			});*/
		}
	});
	
	//##########################################################################################################
	//BUTTON
	$.widget( "sg.formbutton", {
		options: { 
			label: null,
			widgetClass: 'sg-form-button',
			clickAction: function(){}
		},
	 
		_create: function() {
			var options = this.options;
			if(options.label === null){
				options.label = this.element.find('label').html();
			}
			
			var $inputfield = this.element.find('input');
			var $inputvalue = $inputfield.attr('value');
			var $ID = $inputfield.attr('ID');
			
			//set widget wrapper ###########################################################################
			this.element.empty();
			this.element.addClass(options.widgetClass);
			this.element.addClass(baseClass);
			
			//set label ###########################################################################
			var $tag_label = $('<div class="'+labelClass+'">'+options.label+'</div>');
			this.element.append($tag_label);
			
			//set content wrapper ###########################################################################
			var $tag_content = $('<div class="'+contentClass+'"></div>');
			this.element.append($tag_content);
			
			//set input ###########################################################################
			var $tag_input = $('<div ID="'+$ID+'" class="'+inputButtonClass+'"><span>'+$inputvalue+'</span></div>');
			$tag_content.append($tag_input);
			
			//mouse actions ###########################################################################
			$tag_input
				.bind( "click", function() {
					options.clickAction();
				})
				.bind( "mouseenter", function() {
					$(this).removeClass( 'ui-state-default' );
					$(this).addClass( 'ui-state-hover' );
				})
				.bind( "mouseleave", function() {
					$(this).removeClass( 'ui-state-hover' );
					$(this).addClass( 'ui-state-default' );
				})
		}
	});
	
	//##########################################################################################################
	//CHECKBOX
	$.widget( "sg.formcheckbox", {
		options: { 
			label: null,
			widgetClass: 'sg-form-checkbox'
		},
	 
		_create: function() {
			var self = this;
			var options = this.options;
			if(options.label === null){
				options.label = this.element.find('label').html();
			}
			
			var $inputfield = this.element.find('input');
			
			//set widget wrapper ###########################################################################
			this.element.empty();
			this.element.addClass(options.widgetClass);
			this.element.addClass(baseClass);
			
			//set label ###########################################################################
			var $tag_label = $('<div class="'+labelClass+'">'+options.label+'</div>');
			this.element.append($tag_label);
			
			//set content wrapper ###########################################################################
			var $tag_content = $('<div class="'+contentClass+'"></div>');
			this.element.append($tag_content);
			
			//set input ###########################################################################
			$tag_content.append($inputfield);
			$inputfield.each(function(){
				self.editInput( $(this) );
			});
		},
		
		editInput: function( $element ) { //$element is the input field
			$element.formcheckboxinput();
		}
	});
	$.widget( "sg.formcheckboxinput", {
		options: { 
			label: null,
			iconClass: 'ui-icon-check',
			iconCheckedClass: 'ui-icon-check checked',
			iconVisible: false
		},
	 
		_create: function() {
			var self = this;
			var options = this.options;
			if(options.label === null){
				options.label = this.element.attr('label');
			}
			
			var $inputfield = this.element;
			$inputfield.addClass('ui-helper-hidden');
			
			//set widget wrapper ###########################################################################
			var $tag_inputrow = $('<div class="'+inputRowClass+'"></div>');
			this.element.after( $tag_inputrow );
			
			//set html checkbox ###########################################################################
			var $tag_input = $('<div class="'+inputClass+'"></div>');
			$tag_inputrow.append( $tag_input );
			
			//set checkbox icon ###########################################################################
			var $tag_icon = $('<div class="ui-icon"></div>');
			$tag_icon.addClass(options.iconClass);
			if(options.iconVisible == false){
				$tag_icon.addClass('ui-helper-hidden');
			}
			$tag_input.append( $tag_icon );
			
			//set inputfield ###########################################################################
			$inputfield.detach();
			$tag_input.append( $inputfield );
			
			//set label ###########################################################################		
			var $tag_label = $('<div class="'+labelClass+'">'+options.label+'</div>');
			$tag_inputrow.append( $tag_label );
			
			//pre checked ###########################################################################		
			if( $inputfield.is(':checked') ){
				self.check($inputfield, $tag_input, $tag_icon);
			}
			
			//mouse action ###########################################################################
			$tag_input
				.bind( "click", function() {
					if( !$inputfield.is(':checked') ){
						self.check($inputfield, $(this), $tag_icon);
					}else{
						self.uncheck($inputfield, $(this), $tag_icon);
					}		
				})
				.bind( "mouseenter", function() {
					if(options.iconVisible == false){
						$(this).children('.ui-icon').removeClass( 'ui-helper-hidden' );
					}
					$(this).addClass( 'ui-state-hover' );
				})
				.bind( "mouseleave", function() {
					if(options.iconVisible == false){
						$(this).children('.ui-icon').addClass( 'ui-helper-hidden' );
					}
					$(this).removeClass( 'ui-state-hover' );
				})
		},
		
		check: function($inputfield, $tag_input, $tag_icon){
			var options = this.options;
			
			$tag_input.addClass('ui-state-default');
			$inputfield.attr('checked', true);
			$tag_icon.removeClass(options.iconClass);
			$tag_icon.addClass(options.iconCheckedClass);
		},
		
		uncheck: function($inputfield, $tag_input, $tag_icon){
			var options = this.options;
			
			$tag_input.removeClass('ui-state-default');
			$inputfield.removeAttr('checked');
			$tag_icon.removeClass(options.iconCheckedClass);
			$tag_icon.addClass(options.iconClass);
		}
	});
	
	//##########################################################################################################
	//RADIO
	$.widget( "sg.formradio", $.sg.formcheckbox, {
		editInput: function( $element ) { //$element is the input field
			$element.formradioinput();
		}
	});
	$.widget( "sg.formradioinput", $.sg.formcheckboxinput, {
		options: { 
			iconClass: 'ui-icon-radio-on',
			iconCheckedClass: 'ui-icon-bullet',
			iconVisible: true
		},
		
		check: function($inputfield, $tag_input, $tag_icon){
			var self = this;
			var options = this.options;
			var $inputfield_name = $inputfield.attr('name');
			
			$('input[name='+$inputfield_name+']').each(function()
			{
				var $tmp_inputfield = $(this);
				var $tmp_tag_input = $(this).parent();
				var $tmp_tag_icon = $(this).parent().find('.ui-icon');
				
				self.uncheck($tmp_inputfield, $tmp_tag_input, $tmp_tag_icon);
			});
			
			$tag_input.addClass('ui-state-default');
			$inputfield.attr('checked', true);
			$tag_icon.removeClass(options.iconClass);
			$tag_icon.addClass(options.iconCheckedClass);
		}
	});
	
	//##########################################################################################################
	//FILE
	$.widget( "sg.formfile", {
		options: { 
			label: null,
			widgetClass: 'sg-form-file'
		},
	 
		_create: function() {
			var options = this.options;
			if(options.label === null){
				options.label = this.element.find('label').html();
			}
			
			var $inputfield = this.element.find('input');
			$inputfield.addClass('ui-helper-hidden');
			
			this.element.empty();
			this.element.addClass(options.widgetClass);
			this.element.addClass(baseClass);
			
			var $tag_label = $('<div class="'+labelClass+'">'+options.label+'</div>');
			var $tag_content = $('<div class="'+contentClass+'"></div>');
			var $tag_input = $('<div class="'+inputClass+'"></div>');
			var $tag_inputbutton = $('<div class="'+inputInnerButtonClass+'"><span>File</span></div>');
			var $tag_inputwrapper = $('<div class="'+inputInnerWrapperClass+'"></div>');
			
			this.element.append($tag_label);
			this.element.append($tag_content);
			this.element.append($inputfield);
			$tag_content.append($tag_input);
			$tag_input.append($tag_inputbutton);
			$tag_input.append($tag_inputwrapper);
			
			$tag_input
				.bind( "click", function() {
					//alert( $(this).html() );
					//$inputfield
					$inputfield.click();
				})
				.bind( "mouseenter", function() {
					$(this).addClass( 'ui-state-hover' );
					$tag_inputbutton.addClass( 'ui-state-hover' );
				})
				.bind( "mouseleave", function() {
					$(this).removeClass( 'ui-state-hover' );
					$tag_inputbutton.removeClass( 'ui-state-hover' );
				});
			
			$inputfield.change(function()
			{
				//alert('changed '+$inputfield.val());
				$tag_inputwrapper.html($inputfield.val());
			});
		}
	});
	
	//##########################################################################################################
	//MULTIFILE
	$.widget( "sg.formmultifile", {
		options: { 
			label: null,
			upload_url: null,
			file_post_name: 'Filedata',
			post_params: {'PHPSESSID': 'not available'},
			debug: false,
			onComplete: function(){}
		},
	 
		_create: function() {
			var $options = this.options;
			
			this.element.addClass(baseClass);
			
			var swfu = new SWFUpload({
					flash_url : "includes/swfupload_v2.2.0.1/Flash/swfupload.swf",
					upload_url: $options.upload_url,
					file_post_name: $options.file_post_name,
					post_params: $options.post_params,
					file_size_limit : "100 MB",
					file_types : "*.*",
					file_types_description : "All Files",
					file_upload_limit : 999999,
					file_queue_limit : 999999,
					custom_settings : {
						progressTarget : "fsUploadProgress",
						cancelButtonId : "fsCancelBtn"
					},
					
					button_placeholder_id : "spanSWFUploadButton",
					button_width: "200",
					button_height: "22",
					button_text : '<span class="button">upload Tiles</span>',
					button_text_style : '.button { font-family: Verdana, Arial, sans-serif; font-size: 11pt; color:#000000 }',
					button_text_top_padding: 3,
					button_text_left_padding: 10,
					button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
					button_cursor: SWFUpload.CURSOR.HAND,
					debug: $options.debug,
					
					file_queued_handler: fileQueued,
					file_queue_error_handler: fileQueueError,
					file_dialog_complete_handler: fileDialogComplete,
					upload_start_handler: uploadStart,
					upload_progress_handler: uploadProgress,
					upload_error_handler: uploadError,
					upload_success_handler: uploadSuccess,
					upload_complete_handler: uploadComplete,
					queue_complete_handler: function(){
						queueComplete;
						$options.onComplete();
					}
				});
				
			if(this.options.debug == false){
				this.element.find('#SWFUpload_Console').remove();
			}
		},
		
		getInputField: function() {
			return this.element.find('input');
		}
	});
	
	//##########################################################################################################
	//SELECT
	$.widget( "sg.formselect", {
		options: { 
			label: null,
			selectaction: {},
			widgetClass: 'sg-form-select',
			iconClass: 'ui-icon-triangle-1-w',
			iconHoverClass: 'ui-icon-triangle-1-w'
		},
	 
		_create: function() {
			var self = this;
			var $wrapper = this.element;
			var options = this.options;
			if(options.label === null){
				options.label = this.element.find('label').html();
			}
			
			var $inputfield = this.element.find('select');
			$inputfield.addClass('ui-helper-hidden');
			
			//set widget wrapper ##############################################################################
			this.element.empty();
			this.element.addClass(options.widgetClass);
			this.element.addClass(baseClass);
			
			//set label ##############################################################################
			var $tag_label = $('<div class="'+labelClass+'">'+options.label+'</div>');
			this.element.append($tag_label);
			
			//set content wrapper ##############################################################################
			var $tag_content = $('<div class="'+contentClass+'"></div>');
			this.element.append($tag_content);
			
			//set input tag ##############################################################################
			var $tag_input = $('<div class="'+inputClass+'"></div>');
			$tag_content.append($tag_input);
			
				var $tag_inputbutton = $('<div class="'+inputInnerButtonClass+'"></div>');
				$tag_inputbutton.removeClass('ui-corner-right').addClass('ui-corner-all');
				$tag_input.append($tag_inputbutton);
				
					var $tag_inputicon = $('<div class="ui-icon '+options.iconClass+'"></div>');
					$tag_inputbutton.append($tag_inputicon);
					
				var $tag_inputwrapper = $('<div class="'+inputInnerWrapperClass+'"></div>');
				$tag_input.append($tag_inputwrapper);
			
			//set select list ##############################################################################
			$tag_content.append($inputfield);
			var $tag_selectlist = $('<div></div>');
			$inputfield.before( $tag_selectlist );
			
			$tag_selectlist.formselectlist({
				selectfield: $inputfield,
				selectaction: options.selectaction
			});
			
			$tag_selectlist
				.bind( "mouseleave", function() {
					//$tag_selectlist.addClass('ui-helper-hidden');
				})
			
			$inputfield
				.change(function(){
					$tag_inputwrapper.text( $(this).find('option:selected').text() );
				});
			$inputfield.trigger("change");
			
			//mouse functions ##############################################################################
			$tag_input
				.bind( "click", function(event) {
					event.stopPropagation();
					if( $tag_selectlist.is(':visible') ){
						$tag_selectlist.addClass('ui-helper-hidden');
						$('body').unbind("click");
					}else{
						$tag_selectlist.width($tag_inputwrapper.innerWidth());
						$tag_selectlist.removeClass('ui-helper-hidden');
						$('body').bind( "click", function() {
							if( $tag_selectlist.is(':visible') ){
								$tag_selectlist.addClass('ui-helper-hidden');
								$('body').unbind("click");
							}
						});
					}
				})
				.bind( "mouseenter", function() {
					$(this).addClass( 'ui-state-hover' );
					$tag_inputbutton.addClass( 'ui-state-hover' );
				})
				.bind( "mouseleave", function() {
					$(this).removeClass( 'ui-state-hover' );
					$tag_inputbutton.removeClass( 'ui-state-hover' );
				});
		}
	});
	$.widget( "sg.formselectlist", {
		options: { 
			selectfield: null,
			selectaction: {},
			baseClass: 'sg-form-selectlist ui-widget-content ui-state-hover ui-helper-hidden',
			activeClass: 'ui-state-default',
			topClass: 'ui-corner-tl top',
			btmClass: 'ui-corner-bottom btm'
		},
	 
		_create: function(){
			var options = this.options;
			var $element = this.element;
			var self = this;
			
			$element.addClass( options.baseClass );
			
			options.selectfield.find('option').each(function(){
				var $tag_selectoption = $('<div class="ui-widget-content mid"></div>');
				$tag_selectoption.text( $(this).text() );
				
				if( $(this).is(':first-child') ){
					$tag_selectoption.removeClass('mid');
					$tag_selectoption.addClass(options.topClass);
				}
				if( $(this).is(':last-child') ){
					$tag_selectoption.removeClass('mid');
					$tag_selectoption.addClass(options.btmClass);
				}
				if( $(this).attr('selected') ){
					$tag_selectoption.addClass(options.activeClass);
				}
				
				$element.append( $tag_selectoption );
				
				self.setMouseActions($tag_selectoption);
			});
		},
		
		setMouseActions: function($tag_selectoption)
		{
			var options = this.options;
			var self = this;
			
			//mouse functions
			$tag_selectoption
				.bind( "click", function(event) {
					var $i = $(this).index();
					var $this_option = options.selectfield.find('option:eq('+$i+')');
					var $selected_option = options.selectfield.find('option:selected');
					
					$selected_option.removeAttr('selected');
					$this_option.attr('selected', 'selected');
					
					options.selectfield.trigger("change");
					self.updateSelected();
				})
				.bind( "mouseenter", function() {
					if( !$(this).hasClass(options.activeClass) ){
						$(this).addClass( 'ui-state-hover' );
					}
				})
				.bind( "mouseleave", function() {
					$(this).removeClass( 'ui-state-hover' );
				});
		},
		
		updateSelected: function(){
			var options = this.options;
			var $tag_list = this.element;
			var $tag_option = $tag_list.children('div');
			var $inputfield = options.selectfield;
			var $option_selected = options.selectfield.find('option:selected');
			
			$tag_option.each(function(){
				$(this).removeClass('ui-state-default');
				if( $(this).index() == $option_selected.index() ){
					$(this).addClass('ui-state-default');
				}
			});
			
			$.each(options.selectaction, function(key, value)
			{ 
				//if($wrapper.find(key)){
					//$(this).find('option:selected').text();
				//}
				if($inputfield.parent().find(key).length>0){
					var e = {
						target: $option_selected
					};
					value(e);
				}
			});
		}
	});
	
	//##########################################################################################################
	//MULTI SELECT
	$.widget( "sg.formmultiselect", {
		options: { 
			label: null,
			widgetClass: 'sg-form-multiselect',
			iconClass: 'ui-icon-triangle-1-w',
			iconHoverClass: 'ui-icon-triangle-1-w'
		},
	 
		_create: function() {
			var self = this;
			var options = this.options;
			if(options.label === null){
				options.label = this.element.find('label').html();
			}
			
			var $inputfield = this.element.find('select');
			$inputfield.addClass('ui-helper-hidden');
			
			//set widget wrapper ##############################################################################
			this.element.empty();
			this.element.addClass(options.widgetClass);
			this.element.addClass(baseClass);
			
			//set label ##############################################################################
			var $tag_label = $('<div class="'+labelClass+'">'+options.label+'</div>');
			this.element.append($tag_label);
			
			//set content wrapper ##############################################################################
			var $tag_content = $('<div class="'+contentClass+'"></div>');
			this.element.append($tag_content);
			
			//set select list ##############################################################################
			$tag_content.append($inputfield);
			var $tag_selectlist = $('<div></div>');
			$tag_content.append( $tag_selectlist );
			
			$tag_selectlist.formmultiselectlist({
				selectfield: $inputfield
			});
		}
	});
	
	$.widget( "sg.formmultiselectlist", $.sg.formselectlist, {
		options: { 
			selectfield: null,
			baseClass: 'sg-form-selectlist ui-widget-content ui-state-hover',
			activeClass: 'ui-state-default',
			topClass: 'ui-corner-top top',
			btmClass: 'ui-corner-bottom btm'
		},
		
		setMouseActions: function($tag_selectoption)
		{
			var options = this.options;
			var self = this;
			
			//mouse functions
			$tag_selectoption
				.bind( "click", function(event) {
					var $i = $(this).index();
					var $this_option = options.selectfield.find('option:eq('+$i+')');
					var $tag_option = self.element.children('div:eq('+$i+')');
					
					//$selected_option.removeAttr('selected');
					if($this_option.attr('selected')){
						$this_option.removeAttr('selected');
						$tag_option.removeClass(options.activeClass);
					}else{
						$this_option.attr('selected', 'selected');
						$tag_option.addClass(options.activeClass);
					}
				})
				.bind( "mouseenter", function() {
					$(this).addClass( 'ui-state-hover' );
				})
				.bind( "mouseleave", function() {
					$(this).removeClass( 'ui-state-hover' );
				});
		}
	});
	
	//##########################################################################################################
	//##########################################################################################################
	//##########################################################################################################
	//FORM
	$.widget( "sg.sg_ui_form", {
		options: {
			buttons: {},
			buttons_setup: {},
			selectaction: {},
			ajax_post: function(){},
			ajax_complete: function(){},
			options_formmultifile: false,
			options_formmultiselect: false,
			options_formtextarea: false
		},		
	 
		_create: function() {
			var $widget = this;
			var $wrapper = this.element;
			var $options = this.options;
			
			$wrapper.addClass('sg-form tmp-ui-form');
			$wrapper.find('.sg-form-select').formselect({
				selectaction: $options.selectaction
			});
			$wrapper.find('.sg-form-multiselect').formmultiselect( $options.options_formmultiselect );
			$wrapper.find('.sg-form-textfield').formtext();
			$wrapper.find('.sg-form-textarea').formtextarea( $options.options_formtextarea );
			$wrapper.find('.sg-form-file').formfile();
			$wrapper.find('.sg-form-multifile').formmultifile( $options.options_formmultifile );
			$wrapper.find('.sg-form-checkbox').formcheckbox();
			$wrapper.find('.sg-form-radio').formradio();
			$wrapper.find('.sg-form-button').formbutton();
			
			$wrapper.sg_autobuttons({
				buttons: $options.buttons,
				buttons_setup: $options.buttons_setup
			});
		}
	});
	
}(jQuery));
