(function($){
	
	$.dropdown = (function(){
		var _retractTime = 1200,
			_over = function(dropdown)
			{
				var content = dropdown.find('.content');
				if (content.data('active'))
					_drop(dropdown);
				if (content.data('active'))
					_updateImage(-15, dropdown);
				else
					_updateImage(-5, dropdown);
			},
			_out = function(dropdown, retractTime)
			{
				var content = dropdown.find('.content');
				if (content.data('active'))
					_updateImage(-10, dropdown);
				else
					_updateImage(0, dropdown);
				if (!content.data('active'))
					return false;
				content.children().animate({"foo":"1"}, (retractTime != undefined ? retractTime : _retractTime), function(){
					$(this).slideUp(150, function(){
						content.data('active', false).hide();
						_updateImage(0, dropdown);
					});	
				});	
			},
			_drop = function(dropdown)
			{
				var content = dropdown.find('.content');
				content.data('active', true).show().children().stop().slideDown(150);
				_updateImage(-15, dropdown);
			},
			_updateImage = function(posY, dropdown)
			{
				dropdown.children('.selector').children('.img').css({"background-position":"0px "+posY+"px"});
			},
			_register = function()
				{
					$('.dropdown').each(function()
					{
						if ($(this).data('registered'))
							return false;
						$(this).find('.selector').unbind('click').bind({
							click : function(e)
							{
								e.preventDefault();
								_drop($(this).parents('.dropdown'));
							}
						});
						$(this).unbind('mouseenter','mouseleave').bind({
							mouseenter : function(e)
							{
								e.preventDefault();
								_over($(this));
							},
							mouseleave : function(e)
							{
								e.preventDefault();
								_out($(this));
							}
						});
						$(this).find('.content').each(function(){
							var _c = $(this).contents();
							var _shell = $(document.createElement('div'));
							$(this).empty();
							$(this).append(_shell.html(_c).hide());
						});
						$(this).css({"position":"relative","___z-index":"800"});
						$(this).find('.selector').css({"width":"100%", "position":"relative"});
						$(this).find('.selector .img').css({'position':'absolute', 'top':'50%', 'margin-top':'-2.5px', 'right':'5px', 'width':'5px', 'height':'5px', 'background':"url(http://media.rockstargames.com/rockstargames/img/global/buttons/dropdown.png) no-repeat"});
						$(this).find('.selector .filter').css({"margin":"0", "position":"relative"});
						$(this).find('.content').css({"display":"none", "top":"100%", "position":"absolute", "z-index":"1001", "width":"100%"});
						$(this).data('registered', true);
					});
					return true;
				};
		return {
			register : function()
			{
				return _register();
			},
			retract : function(dropdown)
			{
				return _out(dropdown, 50);
			}
		}
	})();

	$(document).ready(function() {
		$.dropdown.register();
	});
	
})(jQuery);
