(function($)
{
    $.extend(
    {
	      socialbitUI:
	      {
		        global: [],
		        guid: 1,
		        dataKey: "jQuery.socialbitUI",
		        classes:
		        {
		            'checkBox':               'socialbitUICheckBox',
		            'checkBoxChecked':        'checked',
		            'dropDown':               'socialbitUIDropDown',
		            'dropDownBottom':         'socialbitUIDropDownBottom',
		            'dropDownContent':        'socialbitUIDropDownContent',
		            'dropDownContentOpened':  'opened',
		            'dropDownDisabled':       'disabled',
		            'dropDownItem':           'socialbitUIDropDownItem',
		            'dropDownItemFirstChild': 'first',
		            'dropDownItemLastChild':  'last',
		            'dropDownItemSelected':   'selected',
		            'dropDownTop':            'socialbitUIDropDownTop',
		            'hidden':                 'socialbitUIHidden',
		            'radioBox':               'socialbitUIRadioBox',
		            'radioBoxChecked':        'checked'
		        },
		        openendDropDown: null,
		        selectors:
		        {
                'checkBox': 'input[type=checkbox]',
                'dropDown': 'select',
                'radioBox': 'input[type=radio]'
            },
            

		        registerCheckBoxes: function(element, parameters)
		        {				             
		            // Convert the element
		            element = $(element);

		          	// Remove the custom ui
		            $.socialbitUI.removeCustomUI(element, $.socialbitUI.classes.checkBox);

		            // Try to get the checkbox
		            var realCheckBox = element.find($.socialbitUI.selectors.checkBox);
		        
                // Check whether the element contains exactly one checkbox
                if (realCheckBox.length == 1)
                {
                    // Hide the checkbox
                    realCheckBox.hide();
                    
                    // Create the ui
                    var customCheckBox = $('<div></div>').addClass($.socialbitUI.classes.checkBox);
                    
                    // Check whether the real checkbox is checked already
                    if (realCheckBox.is(':checked'))
                    {
                        // Add the checked-class
                        customCheckBox.addClass($.socialbitUI.classes.checkBoxChecked);
                    }
                    
                    // Append the click event
                    element.click(function()
                    {
                        // Get the custom checkbox
                        var customCheckBox = $(this).find('.' + $.socialbitUI.classes.checkBox);
                    
                        // Get the real checkbox
                        var realCheckBox = customCheckBox.parent().find($.socialbitUI.selectors.checkBox);

                        // Check whether the real checkbox is checked
                        if (realCheckBox.is(':checked'))
                        {
                            // Uncheck the checkbox
                            realCheckBox.removeAttr('checked');
                            
                            // Remove the checked-class
                            customCheckBox.removeClass($.socialbitUI.classes.checkBoxChecked);
                            
                            // Check whether a callback was passed
                            if (parameters != null && parameters.unchecked)
                            {
                                // Call the callback function
                                parameters.unchecked();
                            }
                        }
                        // The real checkbox is not checked
                        else
                        {
                            // Check the checkbox
                            realCheckBox.attr('checked', 'checked');
                            
                            // Add the checked-class
                            customCheckBox.addClass($.socialbitUI.classes.checkBoxChecked);
                            
                            // Check whether a callback was passed
                            if (parameters != null && parameters.checked)
                            {
                                // Call the callback function
                                parameters.checked();
                            }
                        }      
                        
                        // Check whether a callback was passed
                        if (parameters != null && parameters.change)
                        {
                            // Call the callback function
                            parameters.change();
                        }               
                    });
                    
                    // Add the checkbox
                    element.prepend(customCheckBox);
                }
		        },
		        registerDropDowns: function(element, parameters)
		        {
		            // Check whether some parameters are given
		            if (!parameters)
		            {
		                parameters = null;
		            }
		            
                // Convert the element
		            element = $(element);
		            
		            // Remove the custom ui
		            $.socialbitUI.removeCustomUI(element, $.socialbitUI.classes.dropDown);

		            // Try to get the drop down menue
		            var realDropDown = element.find($.socialbitUI.selectors.dropDown);
		        
                // Check whether the element contains exactly one drop down menue
                if (realDropDown.length == 1)
                {
                    // Hide the drop down menue
                    realDropDown.addClass($.socialbitUI.classes.hidden);
                    
                    // Create the ui
                    var customDropDown        = $('<div></div>').addClass($.socialbitUI.classes.dropDown);
                    var customDropDownBottom  = $('<div></div>').addClass($.socialbitUI.classes.dropDownBottom);
                    var customDropDownContent = $('<ul></ul>').addClass($.socialbitUI.classes.dropDownContent);
                    var customDropDownTop     = $('<div></div>').addClass($.socialbitUI.classes.dropDownTop);

                    // Get all drop down itmes
                    element.find('option').each(function(index, value)
                    {
                        // Get the real entry
                        var realEntry = $(this);
                        
                        // Check whether a filter function is given
                        if (parameters == null || !parameters.filter || parameters.filter(realEntry.val(), realEntry.text()))
                        {
                            // Create the custom entry
                            var customEntry = $('<li></li>')
                                                  .addClass($.socialbitUI.classes.dropDownItem)
                                                  .append(
                                                      $('<span></span>')
                                                          .append(
                                                              $('<span></span>')
                                                                  .text(realEntry.text())
                                                          )
                                                  );
                                                  
                            // Append the click functionality
                            customEntry.click(function()
                            {                        
                                // Unselect all selected entries
                                element.find('option:selected').removeAttr('selected');
                                customDropDownContent.find('.' + $.socialbitUI.classes.dropDownItemSelected).removeClass($.socialbitUI.classes.dropDownItemSelected);
                              
                                // Select the new entry
                                realEntry.attr('selected', 'selected');
                                customEntry.addClass($.socialbitUI.classes.dropDownItemSelected);
                                
                                // Check whether a callback was passed
                                if (parameters != null && parameters.change)
                                {
                                    // Call the callback function
                                    parameters.change(customEntry);
                                }
                                if (customDropDown.hasClass('opened'))
                                {
                                    // Bring the control into the back
                                    customDropDown.css('z-index', 0);
                                
                                    // Call the change event
                                    realDropDown.trigger('change');                                
                                }
                            });
                                                  
                            // Check whether the real entry is selected
                            if (realEntry.is(':selected'))
                            {
                                // Add the selected-class
                                customEntry.addClass($.socialbitUI.classes.dropDownItemSelected);
                            }
                            
                            // Append the custom entry
                            customDropDownContent.append(customEntry);
                        }
                    });
                    
                    // Check whether no item was selected
                    if (!customDropDownContent.find(':selected:'))
                    {
                        // Select the first entry
                        customDropDownContent.find('li:first-child').addClass($.socialbitUI.classes.dropDownItemSelected);
                    }
                    
                    // Add some helper classes
                    customDropDownContent.find('li:first-child').addClass($.socialbitUI.classes.dropDownItemFirstChild);
                    customDropDownContent.find('li:last-child').addClass($.socialbitUI.classes.dropDownItemLastChild);
                    
                    // Append the rest
                    customDropDownBottom.append(customDropDownContent);
                    customDropDownTop.append(customDropDownBottom);
                    customDropDown.append(customDropDownTop);
                    
                    // Add the drop down menue
                    element.prepend(customDropDown);
                    
                    // Check whether the real control is disabled
                    if (realDropDown.is(':disabled'))
                    {
                        // Add the disabled class
                        customDropDown.addClass($.socialbitUI.classes.dropDownDisabled);
                    }
                    
                    // Append the open event
                    customDropDown.click(function()
                    {
                        // Check whether the drop down is not disabled
                        if (!customDropDown.hasClass('disabled'))
                        {                    
                            // Toggle the class
                            customDropDown.toggleClass($.socialbitUI.classes.dropDownContentOpened);
                            
                            // Check whether the drop down is closed
                            if (!customDropDown.hasClass($.socialbitUI.classes.dropDownContentOpened))
                            {
                                // Bring the control into the back
                                customDropDown.css('z-index', 0);
                            }
                            // The drop down is opened
                            else
                            {
                                // Bring the control into the front
                                customDropDown.css('z-index', 46954654);
                            }
                        }
                    });
                    
                    // Append the redraw event
                    realDropDown.bind('redraw', function()
                    {
                        // Check whether the real drop down is disabled
                        if (realDropDown.is(':disabled'))
                        {                    
                            customDropDown.addClass('disabled');
                        }
                        // The real drop down is disabled
                        else
                        {
                            customDropDown.removeClass('disabled');
                        }
                    });
                }
		        },
		        registerRadioBoxes: function(element, parameters)
		        {
                // Convert the element
		            element = $(element);
		        
		          	// Remove the custom ui
		            $.socialbitUI.removeCustomUI(element, $.socialbitUI.classes.radioBox);
		        
		            // Try to get the radiobox
		            var realRadioBox = element.find($.socialbitUI.selectors.radioBox);
		        
                // Check whether the element contains exactly one radiobox
                if (realRadioBox.length == 1)
                {
                    // Hide the radiobox
                    realRadioBox.hide();
		        
                    // Create the ui
                    var customRadioBox = $('<div></div>').addClass($.socialbitUI.classes.radioBox);
                    
                    // Check whether the real radiobox is checked already
                    if (realRadioBox.is(':checked'))
                    {
                        // Add the checked-class
                        customRadioBox.addClass($.socialbitUI.classes.radioBoxChecked);
                    }

                    // Append the click event
                    element.click(function()
                    {
                        // Get the custom radiobox
                        var realRadioBox = $(this).find('.' + $.socialbitUI.classes.radioBox);
                    
                        // Get the real radiobox
                        var realRadioBox = realRadioBox.parent().find($.socialbitUI.selectors.radioBox);
                        
                        // Check the radiobox
                        realRadioBox.attr('checked', $.socialbitUI.classes.radioBoxChecked);
                        
                        // Update all custom radioboxes
                        $.socialbitUI.updateRadioBoxes(parameters); 
                    });
                    
                    // Add the radiobox
                    element.prepend(customRadioBox);
		            }
		        },
		        removeCustomUI: function(element, className)
		        {
		            // Remove all events
		            element.unbind();
		            
		            // Remove the custom ui
		            element.find('.' + className).remove();
		        },
		        updateRadioBoxes: function (parameters)
		        {
		            // Get all custom radioboxes
                var customRadioBoxes = $('.' + $.socialbitUI.classes.radioBox);
          
                // Iterate all radio boxes
                customRadioBoxes.each(function(index, customRadioBox)
                {
                    // Get the jquery object
                    customRadioBox = $(customRadioBox);
                
                    // Get the container
                    var container = customRadioBox.parent();
                    
                    // Get the real radiobox
                    var realRadioBox = container.find($.socialbitUI.selectors.radioBox);
            
                    // Check whether the real radiobox is checked
                    if (realRadioBox.is(':checked'))
                    {
                        // Check the custom radiobox
                        customRadioBox.addClass($.socialbitUI.classes.radioBoxChecked);
                        
                        // Check whether a callback was passed
                        if (parameters != null && parameters.checked)
                        {
                            // Call the callback function
                            parameters.checked();
                        }
                    }
                    // The real radio box is not checked
                    else
                    {
                        // Uncheck the custom radiobox
                        customRadioBox.removeClass($.socialbitUI.classes.radioBoxChecked);
                        
                        // Check whether a callback was passed
                        if (parameters != null && parameters.unchecked)
                        {
                            // Call the callback function
                            parameters.unchecked();
                        }
                    }
                    
                    // Check whether a callback was passed
                    if (parameters != null && parameters.change)
                    {
                        // Call the callback function
                        parameters.change(customRadioBox);
                    }                 
                });
		        }
		    }
		});
		
		$.fn.extend(
		{
        socialbitUIRegisterCheckBoxes: function(parameters)
        {
            return this.each(function()
            {
                $.socialbitUI.registerCheckBoxes(this, parameters);
            });
        },
        socialbitUIRegisterDropDowns: function(parameters)
        {
            return this.each(function()
            {
                $.socialbitUI.registerDropDowns(this, parameters);
            });
        },
        socialbitUIRegisterRadioBoxes: function(parameters)
        {
            return this.each(function()
            {
                $.socialbitUI.registerRadioBoxes(this, parameters);
            });
        }
    });
})(jQuery);
