/**
 * fd_header
 * desciption:
 *
 *
 * @author communicatie bureau fourdesign_ Menno Tempelaar
 */
var fd_header = function (object) {

	var self				= this;
	
	// Protected variabeles
	this.gallery			= null; 
	this.navigation			= null; 
	this.description		= null;

	var _construct = function () {
		_setup();
		
		_start();
	};

	var _setup = function () {
		if ($(object).find('.fd_header_gallery').length != 0)
			self.gallery = new fd_header_gallery(self, $(object).find('.fd_header_gallery'));
		
		if ($(object).find('.fd_header_navigation').length != 0)
			self.navigation = new fd_header_navigation(self, $(object).find('.fd_header_navigation'));
		
		if ($(object).find('.fd_header_description').length != 0)
			self.description	= new fd_header_description(self, $(object).find('.fd_header_description'));
			
	};
	
	var _start = function () {
		self.gallery.start();
	};

	_construct();
	return {
		type: {
			'name': 'fd_header', 
			'description': 'Een header object.'
		}
	};
};



/**
 * fd_header_gallery
 * desciption:
 *
 *
 * @author communicatie bureau fourdesign_ Menno Tempelaar
 */
var fd_header_gallery = function (fd_header, gallery) {
	
	var self				= this;
	
	// Private variabelen
	var items				= [];
	var active				= 0;
	var interval			= null;
	var pause				= false;
	
	var _construct = function () {
		_setItems();
	};
	
	var _setItems = function () {
		$(gallery).find('img').each(function () {
			items.push(new fd_header_gallery_item(self, this));
		});
	};	
	
	var _startInterval = function () {
		if (interval === null) {
			interval = window.setInterval(function () {
				
				var newActive = active;
				newActive++;
				if (newActive == items.length)
					newActive = 0;
				
				_set(newActive);
				
			}, 7000);
		}
	};
	
	var _set = function (_newActive) {
		if (fd_header.description !== null)
			fd_header.description.hide();

		items[active].down();
		items[active].hide();

		active = _newActive;
		items[active].up();
		items[active].show(function () {
			if (fd_header.navigation !== null) {
				fd_header.navigation.change({
					items: items, 
					active: active
				});
			}
			
			if (fd_header.description !== null) {
				fd_header.description.set(items[active].get, function () {
					var newActive = active;
					newActive++;
					if (newActive == items.length)
						newActive = 0;
					
					_set(newActive);
				});
			}
		});	
	};
	
	_construct();
	return {
		start: function () {
			if (items.length > 0) {
				items[active].ready(function () {
					
					items[active].show(function () {
						if (fd_header.navigation !== null) {
							fd_header.navigation.change({
								items: items, 
								active: active
							});
						}
						
						if (fd_header.description !== null) {
							fd_header.description.set(items[active].get, function () {
								var newActive = active;
								newActive++;
								if (newActive == items.length)
									newActive = 0;
								
								_set(newActive);
							});
						}
					});
					
					/* if (items.length > 1)
						_startInterval(); */
				});
			}
		}, 
		Break: function () {
			
		}, 
		next: function () {
			var newActive = active;
				newActive++;
			if (newActive == items.length)
				newActive = 0;
			
			if (pause == false) {
				pause = true;
				window.setTimeout(function () {
					pause = false;
				}, 10000);
			}

			_set(newActive);
		}, 
		previous: function () {
			var newActive = active;
			if (newActive == 0)
				newActive = (items.length - 1);
			else
				newActive--;
			
			_set(newActive);
		}, 
		get: {
			items: items, 
			active: active
		}
	};
}



/**
 * fd_header_gallery
 * desciption:
 *
 *
 * @author communicatie bureau fourdesign_ Menno Tempelaar
 */
var fd_header_gallery_item = function (fd_header_gallery, item) {

	var self			= this;

	var _construct = function () {
		_setup();
	};
	
	var _setup = function () {
		$(item)	.hide()
				.css('position', 'absolute');
	};
	
	_construct();
	return {
		show: function (cb) {
			$(item).fadeIn('slow', 'swing', function () {
				cb();
			});
			return;
		}, 
		hide: function () {
			$(item).fadeOut('slow', 'swing', function () {
				
			});
			return;
		}, 
		ready: function (callback) {
			$(item).ready(function () {
				callback();
			});
		}, 
		up: function () {
			$(item).css('zIndex', '1');
		}, 
		down: function () {
			$(item).css('zIndex', '0');
		}, 
		get: item
	};
};




/**
 * fd_header_navigation
 * desciption:
 *
 *
 * @author communicatie bureau fourdesign_ Menno Tempelaar
 */
var fd_header_navigation = function (fd_header, navigation) {
	
	var self			= this;
	
	var left;
	var right;
	
	var _construct = function () {
		_setup();
	};
	
	var _setup = function () {
		if ($(navigation).find('.fd_header_navigation_left').length != 0)
			left = new fd_header_navigation_button(self, $(navigation).find('.fd_header_navigation_left'), 'previous');
		
		if ($(navigation).find('.fd_header_navigation_right').length != 0)
			right = new fd_header_navigation_button(self, $(navigation).find('.fd_header_navigation_right'), 'next');
		
		$(navigation).hide();
	};
	
	var _change = function (data) {
		if (data.items.length > 1) {
			left.activate();
			right.activate();
		}
	};
	
	this.get = function () {
		return fd_header.gallery.get;
	};
	
	this.show = function () {
		$(navigation).fadeIn('fast');
	};
	
	this.next = function () {
		fd_header.gallery.next();
	};
	
	this.previous = function () {
		fd_header.gallery.previous();
	};
	
	_construct();
	return {
		change: function (data) {
			_change(data);
		}
	};
};




/**
 * fd_header_navigation_button
 * desciption:
 *
 *
 * @author communicatie bureau fourdesign_ Menno Tempelaar
 */
var fd_header_navigation_button = function (fd_header_navigation, button, type) {
	
	var self			= this;
	
	var _construct = function () {
		_setup();
	};
	
	var _setup = function () {
		$(button).hide();
		
		$(button).click(function () {
			eval('fd_header_navigation.' + type + '()');
		});
	};
	
	_construct();
	return {
		activate: function () {
			fd_header_navigation.show();
			$(button).fadeIn('fast');
		}, 
		deactivate: function () {
			$(button).fadeOut('fast');
		}
	};
};





/**
 * fd_header_description
 * desciption:
 *
 *
 * @author communicatie bureau fourdesign_ Menno Tempelaar
 */
var fd_header_description = function (fd_header, description) {
	
	var self			= this;
	
	var items			= [];
	
	var _construct = function () {
		_setup();
	};
	
	var _setup = function () {
		if ($(description).find('.fd_header_description_alt').length != 0)
			items.push(new fd_header_description_item(self, $(description).find('.fd_header_description_alt')));
	};
	
	this.get = function () {
		return fd_header.gallery.get;
	};
	
	_construct();
	return {
		set: function (item, callBack) {
			for ( var i = 0; i < items.length; i++ )
				items[i].set($(item).attr(items[i].attr), function () {
					callBack();
				});
		}, 
		hide: function () {
			for ( var i = 0; i < items.length; i++ )
				items[i].hide();
		}
	};
};


/**
 * fd_header_description_item
 * desciption:
 *
 *
 * @author communicatie bureau fourdesign_ Menno Tempelaar
 */
var fd_header_description_item = function (fd_header_description, item) {

	var self			= this;
	
	var _construct = function () {
		_setup();
	};
	
	var _setup = function () {
		$(item).html('');
		$(item).hide();
		
		return;
	};
	
	var _float = function (callBack) {
		$(item).animate({
			marginLeft: ((676 - $(item).width()) - 30) + 'px'
		}, {
			duration: 5500, 
			easing: 'linear',
			complete: function () {
				_hide(callBack);
			}
		});
	};
	
	var _hide = function (callBack) {
		$(item).animate({
			marginLeft:  '800px'
		}, {
			duration: 500,
			queue: false, 
			complete: function () {
				$(item)	.hide()
						.css('marginLeft', '-676px');
				
				if (callBack !== undefined && typeof callBack == 'function')
					callBack();
			}
		});
		$(item).fadeOut('fast');
	};
	
	_construct();
	return {
		set: function (_html, callBack) {
			var This = this;
			$(item)	.css('width', 'auto');
			$(item)	.html(_html)
					.css({
						marginLeft: '-676px', 
						width: $(item).width() + 'px'})
					.show()
					.animate({
						marginLeft: '70px'
					}, {
						duration: 500, 
						easing: 'swing',
						complete: function () {
							/* $(item).animate({
								marginLeft: '80px'
							}, {
								duration: 100, 
								complete: function () {
									$(item).animate({
										marginLeft: '70px'
									}, {
										duration: 200, 
										complete: function () {
											_float();
										}
									});
									
								}
								
							}); */
							if (fd_header_description.get().items.length == 1) {
								_float(function () {
									This.set(_html);
								});
							} else {
								_float(callBack);
							}
						}
						
					});
		},
		hide: function (callBack) { 
			_hide(callBack);
		}, 
		attr: 'alt'
	};
};

