/**
 * fd_imageSwitcher
 * 
 * @author communicatiebureau fourdesign_ Menno Tempelaar
 * 
 */

var fd_imageSwitcher = function (fd, object) {
	
	var self			= this;
	var main, gallery;
	
	var _construct = function () {
		_setup();
	};
	
	var _setup = function () {
		$(object).find('.main').each(function () {
			main = new fd_imageSwitcher_main(self, this);
		});
		
		$(object).find('.gallery').each(function () {
			gallery = new fd_imageSwitcher_gallery(self, this);
		});
	};
	
	this.switchMain = function (clone) {
		main.set(clone);
	};
	
	_construct();
	return {
		
	};
};





var fd_imageSwitcher_main = function (fd_imageSwitcher, object) {

	var self			= this;
	var clone, image;
	var action;

	var _construct = function () {
		_setup();
		_events();
	};
	
	var _setup = function () {
		image = $(object).find('img');
		//$(image).css('position', 'absolute');
		clone = $(image).clone();
		
		if ($(object).hasClass('fade'))
			action = 'fade';
	};
	
	var _events = function () {
		
	};
	
	var _switch = function () {
		$(image).fadeOut('medium', function () {
			$(image).remove();
			image = clone;
			$(image).css('zIndex',  '1');
			clone = $(image).clone();
		});
	};

	var _set = function (imageObject) {
		$(image).css('zIndex', '2');
		
		$(clone).load(function () {
				_switch();
			})
			.attr({
				src: $(imageObject).attr('src'), 
				alt: $(imageObject).attr('alt')
			})
			.appendTo($(object));
		return;
	};
	
	_construct();
	return {
		set: function (imageObject) {
			_set(imageObject);
		}
	};
};






var fd_imageSwitcher_gallery = function (fd_imageSwitcher, object) {
	
	var self			= this;
	var items			= [];
	
	var _construct = function () {
		_watch();
	};
	
	var _watch = function () {
		$(object).find('img').each(function () {
			items.push(new fd_imageSwitcher_gallery_item(self, this));
		});
	};
	
	this.set = function (clone) {
		fd_imageSwitcher.switchMain(clone);
		return;
	};
	
	_construct();
	return {
		
	};
};






var fd_imageSwitcher_gallery_item = function (fd_imageSwitcher_gallery, object) {
	
	var self			= this;
	var clone;
	
	var _construct = function () {
		_setup();
		_events();
	};
	
	var _setup = function () {
		clone = $(object).clone();
	};
	
	var _events = function () {
		$(object).click(function () {
			fd_imageSwitcher_gallery.set(clone);
		});
	};
	
	_construct();
	return {
		
	};
};
