/**
 * fd_popup
 * Dit object start het popup object en kijkt of de nodige elementen aanwezig zijn.
 * 
 * @author communicatie bureau fourdesign_ Menno Tempelaar
 */
var fd_popup = function (fd, element) {

	var self				= this;
	
	var action, content;
	
	/**
	 * Basis functie. Deze wordt standaard uitgevoerd.
	 */
	var _construct = function () {
		if (_setup())
			_start();
	};
	
	
	/**
	 *	_setup
	 *	Checkt of de elementen aanwezig zijn en start de objecten van de elementen.
	 *	
	 *	@return boolean
	 */
	var _setup = function () {
		if ($(element).find('.fd_popup_action').length > 0)
			action = new fd_popup_action(self, $(element).find('.fd_popup_action'));
		
		if ($(element).find('.fd_popup_content').length > 0)
			content = new fd_popup_content(self, $(element).find('.fd_popup_content'));
		
		if (action !== undefined && content !== undefined)
			return true;
		
		return false;
	};


	/**
	 *	_start
	 *	
	 */
	var _start = function () {
		
	};
	
	
	/**
	 * toggle
	 * Deze functie start de show functie van het element fd_popup_content.
	 */
	this.toggle = function () {
		content.toggle();
	};
	
	
	_construct();
	return {
		
	};
};




/**
 *	fd_popup_action
 *	Dit is het element waarop de klik actie wordt geplaatst om de popup te laten tonen.
 *
 *	@author communicatie bureau fourdesign_ Menno Tempelaar
 */
var fd_popup_action = function (fd_popup, element) {
	
	var self			= this;
	
	var _construct = function () {
		_events();
	};
	
	
	/**
	 * _events
	 * Deze functie zorgt ervoor dat de onclick functie voor het element gezet wordt.
	 */
	var _events = function () {
		$(element).click(function () {
			fd_popup.toggle();
			return false;
		});
	};
	
	
	
	_construct();
	return{
		disable: function () {
			$(element).click(function () {
				return false;
			});
		}, 
		enable: function () {
			_events();
		}
	};
};	





var fd_popup_content = function (fd_popup, element) {
	
	var self			= this;
	var state			= false;
	var height, width, 
		windowHeight, windowWidth;
	
	var _construct = function () {
		_setup();
	};
	
	var _setup = function () {
		_setDimensions();
		
		$(element).css({
			height:		'0px',
			width:		'0px', 
			display:	'none', 
			position:	'absolute',
			left:		'0px', 
			top:		'0px'
		});
	};
	
	var _setDimensions = function () {
		height			= $(element).height();
		width			= $(element).width();
		windowHeight	= $(window).height();
		windowWidth		= $(window).width();
	};
	
	var _show = function () {
		$(element).css('display', 'block');
		$(element).animate({
			height:		height + 'px', 
			width:		width + 'px',
			left:		((windowWidth / 2) - (width / 2)) + 'px', 
			top:		(((windowHeight + $(document).scrollTop()) / 2) - (height / 2)) + 'px'
		}, {
			duration: 500, 
			complete: function () {
				state = true;
			}
		});
		return;
	};
	
	var _close = function () {
		$(element).fadeOut({
			complete: function () {
				state = false;
				_setup();
			}
		});
		return;
	};
	
	
	_construct();
	return {
		toggle: function () {
			if (!state) {
				_show();
			} else {
				_close();
			}
		}
	};
};
