var fd_dropdown = function (fd, object) {
	
	var self			= this;
	
	var _construct = function () {
		_watch();
	};
	
	var _watch = function () {
		$(object).find('.closed').each(function () {
			new fd_dropdown_item(self, this, false);
		});
		
		$(object).find('.open').each(function () {
			new fd_dropdown_item(self, this, true);	
		});
	};
	
	_construct();
	return {
		
	};
};

var fd_dropdown_item = function (fd_dropdown, object, state) {

	var self				= this;
	var openHeight, closedHeight;
	var animating			= false;
	var fadeIns				= [];
	var noClicks			= [];
	
	var _construct = function () {
		_setup();
		
		_events();
	};
	
	var _setup = function () {
		if (state) {
			openHeight = $(object).height();
			$(object).removeClass('open').addClass('closed');
			closedHeight = $(object).height();
			$(object).removeClass('closed').addClass('open');
		} else {
			$(object).removeClass('closed').addClass('open');
			openHeight = $(object).height();
			$(object).removeClass('open').addClass('closed');
			closedHeight = $(object).height();
		}
		
		$(object).find('.fd_dropdown_fadeIn').each(function () {
			$(this).hide();
			fadeIns.push(this);
		});
		
		$(object).find('.fd_dropdown_false').each(function () {
			noClicks.push(this);
		})
	};
	
	var _events = function () {
		$(object).click(function () {
			if (state)
				_close();
			else
				_open();
		});
		
		if (noClicks.length > 0) {
			for ( var i = 0; i < noClicks.length; i++ ) {
				$(noClicks[i]).click(function () {
					event.preventDefault();
					return false;
				});
			}
		}
	};
	
	var _open = function () {
		if (!animating) {
			for ( var i = 0; i < fadeIns.length; i++ ) {
				$(fadeIns[i]).hide();
			}
			animating = true;
			$(object).css('height', closedHeight + 'px');
			$(object).removeClass('closed').addClass('open');
			$(object).animate({
				height: openHeight + 'px'
			}, {
				duration: 500, 
				complete: function () {
					animating = false;
					state = true;
				}
			});
		}
	};
	
	var _close = function () {
		if (!animating) {
			animating = true;
			$(object).animate({
				height: closedHeight + 'px'
			}, {
				duration: 500, 
				complete: function () {
					animating = false;
					state = false;
					
					for ( var i = 0; i < fadeIns.length; i++ ) {
						console.log('fade in');
						$(fadeIns[i]).fadeIn('fast');
					}
					
					$(object).removeClass('open').addClass('closed');
				}
			});
		}
	};
	
	_construct();
	return {
		
	};
};
