(function($){
	$.fn.dropDown = function(options){
		var opts = $.extend({
		    callback : function(){ }
		}, options),

			dropdown = $(this);

        dropdown.live('click', function(){
			//current element
			var	self = $(this),
			targetInput = self.find('input[type=hidden]'),
				span = self.children("span").not(".l, .r, .arrows, .arrow");

			//hide/show dropdowns
			hide();
			self.addClass('active').children('.dropdown').show();


			//close on body click
			$('body').bind('click', function(){
				hide();
			});

			//cancel
			$('.cancel-button').unbind('click').bind('click', function(event){
				event.stopPropagation();
				hide();
				return false
			});


			//link click
			self.find('.dropdown li a').unbind('click').click(function(event){
				var value = $(this).attr('rel'),
                name = $(this).parent().parent().attr('id'),
                id = $(this).attr('id');

				if (targetInput.length) {
					targetInput.val(value);
				}

				if (span.length && ! dropdown.hasClass('keepValue')) {
					span.html($(this).html());
                    self.find('.dropdown li').show();
                    $(this).parent().hide();
				}

				self.trigger('change', [value, id, name]);
                
                if (! dropdown.hasClass('keepOpen')) {
                    hide();
                }
                
				$(this).unbind("click");
				
				return false;
			});

            dropdown.trigger("open");

			function hide() {
				$('body').unbind('click');
				$('.dropdown').hide().parent().removeClass('active');
			}

		});
            return dropdown;
	};
})(jQuery);
    
