/*
 * jQuery Custom Drop-Down v1.1
 *
 * Customizable drop-down in jQuery and css
 *
 * Tested browsers : Firefox 3, Internet Explorer 7, Safari 3.1
 *
 * TERMS OF USE - jQuery Custom Drop-Down
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2009 Nicolas Blackburn
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/
(function($) {
	$.fn.CustomDropDown = function($options) {
		
		this.each(function() {
			var self = select = $(this), hover, fnWindowClick, fnSelfClick;
			var optGroup = self.find('.optgroup');
			var label = self.find('label.selected');
			var selected = optGroup.find(':radio:checked:first').parents('.option:first').addClass('selected');
			var selectedValue = selected.find(':radio').val();
			
			// Get the name attribute of the first radio button
			var postName = optGroup.find(':radio:first').attr('name');
			
			var values = optGroup.find(':radio').map(function() {
				return $(this).val();
			});
			
			var labels =  optGroup.find(':radio').map(function() {
				var label = $(this).parents('label:first');
				label.find(':radio').remove();
				return label.html();
			});
			
			
			// If no selection select first radio
			if (!selected) {
				selected = optGroup.find(':radio:first').parents('.option:first').addClass('selected');
			}
			
			// Create a hidden field to store the selected value and post variable
			var hidden = $(document.createElement('input')).attr('type', 'hidden');
			hidden.attr('name', postName);
			
			// Update component value and label
			hidden.attr('value', selectedValue);
			label.html(selected.find('label').html());
			
			self.append(hidden);
			
			fnDocumentClick = function(event) {
				optGroup.addClass('hidden');
				$(document).unbind('click', fnDocumentClick);
			};
			fnSelfClick = function(event) {
				event.stopPropagation();
				
				if (optGroup.hasClass('hidden')) {
					if (selected) {
						selected.addClass('selected');
					}
					hover = selected;
					optGroup.removeClass('hidden');
					$(document).click(fnDocumentClick);
				}
				
			};
			
			// Remove radio buttons
			optGroup.find(':radio').remove();
			
			self.find('.option').each(function(i) {
				var self = option = $(this), optLabel;
				
				option.hover(function() {
					if (hover) {
						hover.removeClass('selected');
					}
					hover = $(this);
					hover.addClass('selected');
				}, function() {
					if (hover) {
						hover.removeClass('selected');
					}
					hover = null;
				}).click(function(event) {
					event.stopPropagation();
					//event.preventDefault();
					label.html(labels[i]);
					hidden.attr('value', values[i]);
					selected = $(this);
					fnDocumentClick();
				});
				
			});
			self.addClass('custom').click(fnSelfClick);
			optGroup.addClass('hidden');
		});
	};
})(jQuery);